Teacher Tech blog with Alice Keeler

Paperless Is Not a Pedagogy

Alice Keeler

New! Extensions Menu in Google Docs

Check out the new Extensions and Tools menu in Google Docs. New! Extensions Menu in Google Docs will allow you to code Google Docs with the Apps Script option.
New! Extensions Menu in Google Docs

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.”

Extensions menu in Google Docs showing add-ons and Apps-Script

New! Extensions Menu in Google Docs

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.

Tools menu with spelling and grammar, word count, review suggested edits, citations, etc...

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…

function onOpen() {
  DocumentApp.getUi()
  .createAddonMenu()
  .addItem()
  .addToUi();
  
}

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.

function addAParagraph(){
let doc = DocumentApp.getActiveDocument();


}

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.

However you call your variable you will want to use that variable to reference the current active document and then press period.

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.

function addAParagraph(){
let doc = DocumentApp.getActiveDocument();
const body = doc.getBody();

}

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.

//This is a sample function for how to create an Add-on menu in your Google Doc.

function onOpen() {
  DocumentApp.getUi()
  .createAddonMenu()
  .addItem('Add a Paragraph','addAParagraph')
  .addToUi();
  
}



//This is the second function that will run from the menu

function addAParagraph(){
let doc = DocumentApp.getActiveDocument();
const body = doc.getBody();
body.appendParagraph('Hello World');

}

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.

extensions menu in the Google Doc. Notice the unofficial Add-on for Coding Tutorial by Alice Keeler

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.

© 2024 All Rights Reserved.

💥 FREE OTIS WORKSHOP

Join Alice Keeler, Thursday Mar 28th or register to gain access to the recording.
Create a free OTIS account.

Join Alice Keeler for this session for a way to create dynamic and interactive digital lessons. The Desmos platform is completely free and allows for any topic to be created or customized.

Exit this pop up by pressing escape or clicking anywhere off the pop up.