Update a Document Explainer

Aim of the puzzle: Use Apps Script to write a function that replaces text in a document from Docs.

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 write the 2nd of two helper functions that will be used later in the main function. The function should replace multiple words in a document from Docs.

In the code that you start off with, the updateDoc helper function takes a doc and some edits. The doc parameter represents a file that’s a copy of a contract, for example:

This agreement certifies that CLIENT will pay $FEE for SERVICE.

The edits parameter represents a nested array, where the 1st word in each inner array is a placeholder to be replaced and the 2nd is the word to put in its place. For example:

let edits = [
  ["CLIENT", "Saanvi",],
  ["FEE", "275",], 
  ["SERVICE", "Tree Pruning"]
];

To complete the puzzle, add a for…of loop inside updateDoc() that iterates through every edit of edits. Next, inside the for…of loop, add doc.replaceText(). Set the 1st argument to edit[0] and the 2nd argument to edit[1].

JavaScript concepts: Variables, Strings, Parameters, Arguments, Function Declarations, Function Calls, Import Statements, Nested Arrays, Array Indexing

Apps Script concepts: .replaceText()

Additional Code (hidden code that runs before the puzzle’s code):

let textEdits = [
  ["CLIENT", "Saanvi",],
  ["FEE", "275",],
  ["SERVICE", "Tree Pruning"]
];

I’m having trouble with this exercise. I think I did most of what I have to but I need some help on what else I’m doing wrong.

add
doc=doc.replaceText(edit[0],edit[1]);

Hi, I keep getting this wrong. How can I fix it? I’ve tried many ways.

I added it, it didn’t work.

1 Like

I did this and it worked.

I tried this and I keep getting a doc duplication error.

In the “for…of loop”, change the word element to edit and in line 14, add a semicolon at the end.

I added it. Still didn’t fix the duplication error.

Most mistakes here are because you guys did not iterate “edit”. Instead, most of you guys are using elements as your initialization.

Within your for loops.

So it’s for(let edit of edits){...}

1 Like

Hey there, the updateDoc() function has 2 parameters, one named doc and another named edits. When the function is called with arguments, like this:

updateDoc(copyOfContract, textEdits)

copyOfContract will be stored in the doc parameter and textEdits gets stored in edits.

Here’s a simpler example:

function double(num) { // num is the parameter
    return num * 2;
}
double(5) // 5 is the argument
// 5 gets stored in num when the function is called
// the function then returns 5 * 2, which is 10

In your code, try deleting the 2 lines before your for loop, where you declare new variables named doc and edits.

You’ve also named the looping variable element, but inside the for loop you’re using a variable named edit, which hasn’t been declared anywhere. You can either change element to edit, or edit to element. Either will work.

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

1 Like

Hey there, try deleting the 2 lines before your for loop, where you declare the doc and edits variables. Your code has technically solved the puzzle because your for loop is correct, but your function isn’t actually working. See my post here for an explanation.

1 Like

Hey there am having difficulty with my code.Please help.

Hey there, try using square brackets instead of curly brackets, like this:

doc.replaceText(edit[0], edit[1])

Hope this helps!
Ben

Thanks Grasshopper Ben.It worked!!!

*It didn’t work .I think it’s a bug problem.

Help me I’m stuck.I can’t do it.

@Justin_Kiboi Hey there, remove the ; that’s between the for loop’s setup () and code block {}

@HARISH_Kabadwal Remove that ; that’s after the for loop’s setup (). Also, remove the first two lines of the function.

Thanks ben but still one issue in output pls help.