Update the Copies Explainer

Aim of the puzzle: Use Apps Script to complete the final step 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 last step of 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 3.

Inside the for…of loop that’s inside the main() function declaration, create a variable called copy that stores the document accessed by DocumentApp.openById(copyId). Then, right below copy, create a variable called edits that stores the nested array:

[
  [ 'CLIENT', row[0] ],
  [ 'FEE', row[1] ],
  [ 'SERVICE', row[2] ]
]

Finally, below edits, call the updateDoc() function. Set the 1st argument to copy and the 2nd argument to edits.

JavaScript concepts: Variables, Strings, Nested Arrays, Array Indexing, Arguments, Function Declarations, Function Calls
Apps Script concepts: DocumentApp, .openById()

2 Likes

i am not understanding this last nested array on the last assignment. i have played with it for 3 days now. HELP

Hey there, you’re almost there. Try changing the edits variable to this:

let edits = [
      ['CLIENT', row[0]],
      ['FEE', row[1]],
      ['SERVICE', row[2]]
    ];

Hope this helps. Let me know if you have any questions!
Ben

1 Like

Is that telling you which row it’s in? I can’t see it on my ph - will have to check it once I’m home

  • V

HA! DONE! (would have never done that! :crazy_face:)

Finished Grasshopper. I will be around utilizing the practice codes.

Thanks!

1 Like

can you post screenshot of your code pls

i cannot understand it @Grasshopper_Ben

Hey there, try sharing a screenshot of where you’re stuck, and I can give you some pointers from there.

From the starting of the code
I cannot understand the concept of the last puzzle pls help me understand

From First!!!

Hey there, it’s a tough puzzle! I’ll walk you through it:

First, add a new line inside the for loop after the line let copyId = makeCopy()

Then, create a variable named copy. Give it the value that’s written in the instructions.
Next, create a variable named edits. It’s value should be the nested array from the instructions (you can copy/paste it)

Next, call the updateDoc() function like this: updateDoc(copy, edits)

All of these lines should be added inside the for loop, after the line starting let copyId

Hope this helps!
Ben

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();

for (let row of values) {
let copyId = makeCopy(contractId, row[0] + ’ Contract’);
let copy = Documentapp.openById(copyId);
var edits = [
[ ‘CLIENT’, row[0] ],
[ ‘FEE’, row[1] ],
[ ‘SERVICE’, row[2] ]
];
updateDoc = (copy, edits);
}
}

main();

Thanks @Grasshopper_Ben for your help!! And @hunain_yousuf see here:
image
Here in DocumentApp line you have missed italic capital in ‘‘App’’ pls change that as soon as possible as we cannot run the code because of that.

And next @Grasshopper_Ben?

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();

for (let row of values) {
let copyId = makeCopy(contractId, row[0] + ’ Contract’);
var copy = DocumentApp.openById(copyId);
var edits = [
[‘CLIENT’, row[0] ],
[ ‘FEE’ , row[1] ],
[ ‘SERVICE’ , row[2] ]
]
updateDoc(copy, edits)
}
}

main();

This is the very right code.

Took a minute to figure out what that was…:flushed:. I figured it out (had to literally put in the array set).

Thanks though!

  • V

Hey there. If you copy and paste the code that is not code-formatted, then your code will get messed up.

This is formatted:

let spreadsheetId = '114hEvWFEbgwmk9TIF54m6bRM6Nb-anMXF4G4qN1cufY';

This is not formatted:
let spreadsheet = ‘114hEvWFEbgwmk9TIF54m6bRM6Nb-anMXF4G4qN1cufY’;

You can take a look at my topic that teach everyone on formatting code.

Link to the code-formatting topic!

Thanks!
P :slightly_smiling_face:

HURRAY I HAVE FINISHED IT!!


Thanks All for your help!!

1 Like

image
Can anybody please tell me where I’ve went wrong?