First Google Sheets™, now Google Docs™. The menus are changing!! Let’s be honest, it is really annoying when the different product’s menus do not match. As of this week, Google Docs is going the way of Google Sheets to have an “Extensions” menu instead of an “Add-ons” menu. Yay! Speaking of Add-ons, I have over a dozen Add-ons I have personally coded. So the Extensions menu is where you can find “Doc to Slides” and “Document Outline.”
Things Have Moved
With the new Extensions menu, the Add-ons menu is now just an item under the Extensions. Items that were under tools have moved as well. Script editor, which I use a LOT, has moved from the Tools menu to the Extensions menu and is now labeled as “Apps Script.”
Tools
The new Tools menu is honestly things I only use occasionally. It is good to become familiar with what is available in the Tools menu for when you need it. Word Count, Translate Document, and Voice Typing are some highlights of the Tools menu.
What is Apps Script?
You can CODE Google Docs™! That is what my Add-ons do. Provide extra functionality to Google Docs (or Sheets, Slides, Forms, etc…). To get started with Coding Google Docs you want to use that Extensions Menu.
Script Editor – IDE
Clicking on “Apps Script” in the Extensions menu takes you to the coding environment. Use this IDE to make your own custom Add-on. Click on where it says “Untitled project” to name your Add-on. This script is what is called “bounded.” It is bounded to this particular Google Doc. What this will allow you to do is to share the Google Doc with the code. Anyone who copies the document, also copies your Add-on.
Make a Menu
An Add-on needs a menu to allow users to access the functionality of the coded script without having to look at the code.
onOpen
It is important that the name of your function for the menu is called onOpen(). This signals to the Google Doc that upon opening the Google Doc that the menu should automatically load. When starting your menu you want to identify what you are coding a menu for. DocumentApp or SpreadsheetApp or SlidesApp. Press period to show a list of options. What you are looking for is .getUi(). This is the user interface. The user interface is buttons, menus, sidebars, pop up alerts, etc…
A Second Function
The function onOpen() only creates the Add-on menu. You will need a second function that will perform an action within your Google Doc.
Each function is encapsulated by a set of {curly braces.} After the ending brace, press enter several times and type the word function all lower case. Then a space, followed by your function name. You can name it whatever you want (mostly) so long as it is one word (no spaces, no funky characters).
You want to start each function, usually, with what Google Workspace App you are using. In this case we are still using DocumentApp. Press period to find the option of “.getActiveDocument().” You will need to end the line of code with a semicolon.
Variables
Variables are a super important part of coding. You define a variable, something you want to store, by using one of 3 things.
- const doc = DocumentApp.getActiveDocument();
- let doc = DocumentApp.getActiveDocument();
- var doc = DocumentApp.getActiveDocument();
const, let, and var are different options that essentially do the same thing. Personally, I am trying to get used to using let more often since I frequently choose var. To get started it will not matter which of the 3 you use. As you improve your JavaScript and Apps Script skills you will want to choose which of these 3 options makes the most sense. I am introducing all 3 options to you to do as being interchangeable so that if you look at sample code and someone defines their variable with const instead of var you will know it does the same thing.
Document Body
There are 3 parts to a Google Doc. The Header, the Body, and the Footer. You need to be specific as to which part you are wanting to focus on. Define another variable for the document body. Choose any of the 3 options for calling a variable and define the body by using the previous variable and pressing period to find “getBody().” Note, this is case sensitive.
Append Paragraph
I use append paragraph a lot when coding Google Docs. It adds a paragraph to the bottom. It will seem like a silly thing to do in this context since it would legitimately be faster to type the paragraphs into the Google Doc. An example of how I used append paragraph recently was I was helping out with the “Adulting day” event at my school. Each student is assigned 9 thirty minute sessions. To share the schedule with students I wanted to grab off the spreadsheet the first session and append it to the student Google Doc. Then I wanted to append the 2nd session, etc… And I needed to do this over 400 times. You can see how code is much nicer than copying and pasting 9 sessions over 400 times!
The variable I defined for body I will need to use to appendParagraph. On the next line type the variable for the Document body and press the period to select “appendParagraph().”
Test String is in Single Quotes
The word string is very important for coding. It means a string of characters… or in other words, some text! You need to make sure your text is in single quotations. Notice the text turns red when you surround it by quotes. Within the parenthesis for the appendParagraph method type in a phrase you want to append to the document.
Add Your Function to the Menu
Once your function is complete you will want to modify the onOpen menu to include your function. The dot addItem is a text string (single quotes) of what the user will see in the menu. Then a comma outside of the quotations. Then, also in single quotes, the name of the function that is being called.
Save and Run
Click the save icon (Control S) and press the Run icon (a triangle). You will be prompted to authorize the Add-on. You are only authorizing yourself access to your own documents. After saving, running, and authorizing the onOpen menu notice that it is now part of the Extensions menu. Also, notice the icon is slightly different than your other installed icons. This is letting you know it is a bounded script to the document and not an approved Add-on by Google.
Select the menu item and see that it will add your paragraph each time you select it!
Get Started
This is far from a complete tutorial on how to use the Apps Script option in the Google Docs Extensions menu. However, I hope this helps get you started with knowing that it is possible to code and customize Google Docs. Do a tutorial on JavaScript and check out the tutorials on Google Apps Script.