In this video, you will build code in Apps Script that finds the word “very” in the starter project document.
“Very” is commonly overused in writing, and it usually can be deleted or replaced.
Once you create a program that finds one overused word in a document, you can build on it to identify several different words or phrases.
By the end of this video, you will have completed the first step in your plan for this activity: finding a single overused word in the document.
The starter project you opened at the end of the last video contains sample text for your Auto Editor to check.
It does not contain code-- that part is up to you!
To start, go to the script editor.
Rename the project “Auto Editor Code.”
Your code will start with a function.
A function is a block of code that carries out a specific series of instructions.
The function you will use for your Auto Editor is called “find Text.”
Call the “document app” function to get the open, or “active,” document.
As you type code, an autofill menu appears with suggestions.
Select the function from the menu as you type.
Type the code exactly as you see here.
Then, you’ll retrieve the text from the body of the document.
Type a period, then “getBody.”
Finally, “call” the function “Find Text.”
Next, add a parameter, which is a value for the function to use.
In this case, the parameter is the word in the text you want the program to find.
Type the word “very” inside the parentheses.
Be sure to put double quotation marks around the word “very.”
This tells the computer that this is a parameter, rather than another instruction.
You will add more parameters later in this unit, but start with this example.
Run the code.
You may see a message asking you to authorize the code.
This is just a security measure.
Authorize the code to run.
No error message appears!
If you click back to the tab with the text, though, it is hard to tell whether the code worked.
The piece of writing looks the same.
This function has a “return type,” which means it “returns,” or responds to, a value.
To store a value for the program to use, create a variable.
Create the variable above the Document App function but inside the curly braces.
Type “VAR,” space, “search result” to create the variable.
Then, define the search result.
Type “search result,” space, “equals,” space, to set the variable equal to “find text” function result.
To see what the “find text” function returns, add the logger function.
The logger function is a common piece of code used to test what is happening in the computer program.
Use it frequently as you write code to make sure you haven’t made any mistakes.
Run the code and check the log to test.
The “find text function” should return “Range Element” if the word “very” is found in the text.
The log shows that it does.
If you get an error message, check your code carefully.
One little mistake, and your code will not work.
Check to be sure you’ve typed the code exactly as you saw on the screen.
To test again, change the parameter.
Add a few R’s to the word “very.”
Run the code, and look at the log again.
This time, the log says “null,” which is another word for “nothing.”
That shows that the new parameter was not found in the document.
The test worked!
Remove the extra R’s from your parameter.
As a program gets longer and more complex, it is hard to remember what each part does.
To help yourself and others understand it, leave comments.
Comments in a program do not affect how the code works.
They simply give information and reminders to people who look at the program.
On a separate, blank line in your program, add two forward slashes.
Then, type a note to yourself about the next line of code.
Comments appear in a different color from the rest of the code.
This shows that the text is not used by the computer and is not part of the coding instructions.
If your comments do not turn a different color, make sure they begin with two forward slashes.
The comments you see here are just examples.
Type in comments that you find helpful.
You have completed step one and gotten your Auto Editor off to a good start!
Now, it’s your turn: Open the script editor in an example document, and rename it “Auto Editor Code.”
Use a function, a parameter, and a variable to find the word “very” in the document.
Then, use the log to test the code and make sure “range element” is returned.
Add comments to describe what the code does.
By this point, your code should look like this.