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
doc parameter represents a file that’s a copy of a contract, for example:
This agreement certifies that CLIENT will pay $FEE for SERVICE.
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
edits. Next, inside the for…of loop, add
doc.replaceText(). Set the 1st argument to
edit and the 2nd argument to
Apps Script concepts:
Additional Code (hidden code that runs before the puzzle’s code):
let textEdits = [ ["CLIENT", "Saanvi",], ["FEE", "275",], ["SERVICE", "Tree Pruning"] ];