Make Multiple Copies Explainer

Aim of the puzzle: Use Apps Script to complete the 2nd of 3 steps in writing a function to automate contract creation.

Walk through of the solution: For the last topic of this course, you’ll use Apps Script to create a function that automates the process of creating contracts.

In this puzzle, you’ll complete the 2nd of 3 steps in the main function.

The main function has been divided into 3 steps:

  1. Open a spreadsheet and get a range of values
  2. Loop through every row in the range and call makeCopy() to create and name a new copy of the contract
  3. Open each copy and use updateDoc() to replace its placeholder text with text from the spreadsheet

To complete the puzzle, complete step 2.

Add a for…of loop inside the main() function declaration that iterates through every row of values. Then, inside the for…of loop, create a variable called copyId that stores the id returned by makeCopy(contractId, row[0] + ‘ Contract’). Finally, still inside the for...of loop and just below the copyIdvariable declaration, printcopyId` to the console.

JavaScript concepts: Variables, Strings, For…of Loops, Nested Arrays, Array Indexing

2 Likes

What am I doing wrong here?

Hey there, try fixing the formatting of your code. It will make it easier to read and find bugs.

You’ve written the for loop and its code on one line at the very top of the function. It’s trying to loop through values before values has been declared. When the computer tries to run this for loop, it won’t know what to loop through.

Try resetting the puzzle, then add the for loop inside the function below the line where values is declared, like this:

function main() {
    let sheet = SpreadsheetApp.openById(spreadsheetId);
    let values = sheet.getRange("A2:C5").getValues();
// add your for loop here:
    for (let row of values) {
        // ...
    }
}

Hope this helps!
Ben

I’m doing the format like this

function main() {
    let sheet = SpreadsheetApp.openById(spreadsheetId);
    let values = sheet.getRange("A2:C5").getValues();
// add your for loop here:
    for (let row of values) {
        // ...
    }
}

then I added the makeContract() thing inside the {} and outside the for of i did console log, maybe the console.log thing is wrong but what is exactly wrong. The code looks like this.

function main() {
  let sheet = SpreadsheetApp.openById(spreadsheetId);
  let values = sheet.getRange("A2:C5").getValues();
  
  for (let row of values){
      makeCopy(contractId, row[0] + 'Contract')
  }
}
console.log(copyId)
main();

Hey there, you’re on the right track! You’re printing a variable named copyId, but that variable hasn’t been declared anywhere, so the computer doesn’t know what it is.

If you look at the makeCopy() function, it creates a copy of a file, and then returns the id of that copy. Right now, you aren’t storing that returned value anywhere. Let’s try saving that id to a variable, like this:

function main() {
  let sheet = SpreadsheetApp.openById(spreadsheetId);
  let values = sheet.getRange("A2:C5").getValues();
  
  for (let row of values) {
// the value returned by makeCopy() is now stored in a variable named copyId
      let copyId = makeCopy(contractId, row[0] + 'Contract')
// this variable is then printed
      console.log(copyId)
  }
}
main();

You’re almost there! You’ll just have to declare that copyId variable inside the for loop, and also move the console log where you print copyId so that it’s inside that for loop.

Hope this helps!
Ben

Thanks for the tip here

I am trying to do the second step in the last problem. Below is my coding. If I hit run code, I either get the message below or that my copyId is not defined and needs to be defined inside the for…of loop. Can you provide any assistance, please? Also, I no longer get the input values from the spreadsheet displayed, even if I hit “reset code.”

If I run it with “var” instead of “let”, I am getting the following:

So I took out the semicolon after the for … of loop and before the { and the code ran, but the output on the console looked like gibberish. It said I passed that part. Is it okay?

1 Like

Yup! That semicolon was the issue. Glad you got past it.

Let me know if you have any other issues!
Ben

1 Like

Thank you so much for all of your help.

1 Like

et spreadsheetId = ‘114hEvWFEbgwmk9TIF54m6bRM6Nb-anMXF4G4qN1cufY’;
let contractId = ‘226uAXY0YRUj-HMW5LQZ2YeeJgyDqmJTpnzRkGmFLch0’;

function makeCopy(id, name) {
let file = DriveApp.getFileById(id);
let copy = file.makeCopy(name);
return copy.getId();
}

function updateDoc(doc, edits) {
for (let edit of edits) {
doc.replaceText(edit[0], edit[1]);
}
}

function main() {
let sheet = SpreadsheetApp.openById(spreadsheetId);
let values = sheet.getRange(“A2:C5”).getValues();
for (let row of values){
var copyId = makeCopy(contractId,row[0]+‘Contract’)
console.log(copyId);
}
}

main();


pls answer what is wrong

i’am tanmay pls answer to me

It helps me a lot… Thanks

can someone help me please

Is something wrong?

let spreadsheetId = ‘114hEvWFEbgwmk9TIF54m6bRM6Nb-anMXF4G4qN1cufY’;
let contractId = ‘226uAXY0YRUj-HMW5LQZ2YeeJgyDqmJTpnzRkGmFLch0’;

function makeCopy(id, name) {
let file = DriveApp.getFileById(id);
let copy = file.makeCopy(name);
return copy.getId();
}

function updateDoc(doc, edits) {
for (let edit of edits) {
doc.replaceText(edit[0], edit[1]);
}
}

function main() {
let sheet = SpreadsheetApp.openById(spreadsheetId);
let values = sheet.getRange(“A2:C5”).getValues();
console.log(values[0][0])
}

main();