Home » Google » Coding Google Apps – Get Started

Coding Google Apps – Get Started

In my opinion, one of the easiest ways to get started coding something that will SAVE YOU TIME is Google Apps Script. It is based on JavaScript and allows you to code Google Docs, Sheets, Slides, Forms, etc… (Note: Currently you can not code Jams or Sites).

Script Editor

Get started by using the Tools menu in Google Docs and choosing Script editor.

Functions

A function is a set of instructions. You might have more than one function for different sets of instructions. For example you can create an Add-on menu to allow users to avoid looking at the code and just use your application.

Menu Function

function onOpen() {
DocumentApp.getUi()
.createAddonMenu()
.addItem(‘Insert Rubric’,’rubric’)
.addToUi();
}

Other Function

One set of instructions tells the code to make a menu, a different set of instructions tells the code to …. do something else. You can see in this function I have the set of directions starting to set up inserting a rubric onto the Google Doc.

function rubric(){
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
body.appendParagraph();
}

Create the Function

Start by typing the word function. This is all lower case. Space. Then you can name your function whatever you want so long as it is a single word with no spaces or symbols. Typically you would smash words together by capitalizing the 2nd word. For example for http:/alicekeeler.com/slidesbreak I named one of my functions breakSlides where the b is lowercase and the S is upper case. This is not required to use the capitalization like this, but it’s fairly typical.

Your function name needs a set of parenthesis (). Most of the time the parenthesis are blank. However, you can accept an input into the function.

Curly Braces

In JavaScript the set of directions for a function are between curly braces.

function name(){
//code here
}

What App Are You Using?

More often than not my first line of code is what Google App I am trying to code.

DocumentApp
SlidesApp
FormApp
SpreadsheetApp

Notice the capitalization!!!

Press Period

After typing, for example, DocumentApp press period. This will bring up a multiple choice list of what you can do with the document.

Methods

Methods are actions that can be performed. Note that methods MUST have parenthesis. Sometimes method have an input that goes into the parenthesis, sometimes not.

var doc = DocumentApp.getActiveDocument();

After DocumentApp I pressed period and chose to get the current Google Doc I was using. Since getActiveDocument is a method (pay attention to the pop up hint, it will tell you) you will need a set of parenthesis.

Semicolon

End each line of code with a semicolon. This signals that you are ready to move to another line of directions.

Variables

Variables are an important part of coding. They allow you to essentially make a short code to reuse lines of code without retyping it.

Shorten variable to var

What app am I using? DocumentApp. In front of this I want to put the variable I want to use to indicate to the code repeatedly that I am talking about that document.

var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();

In the first line I used the variable doc to indicate I am talking about the current Google Doc. In the 2nd line I used the variable doc to remind the code “I’m talking about the current document.” Pressing period, I was given options for what I can do with the document.

On the second line of code I also used a variable of body. A Google Doc has a header, a body, and a footer. It is important that after I identify that I want to use this Google Doc, what part of the document am I focusing on.

I will then use the body variable on another line and press period to add to the body of the Google Doc.

function rubric(){
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
body.appendParagraph(‘Rubric Goes Here’);
}

Text Strings

Your text (things that are not code) will need to go in single quotes. Notice in the code sample above that I am adding a paragraph to the bottom of the Google Doc. Inside the parenthesis is the text that I want to add to the bottom of the document. ‘Rubric Goes Here’ is in single quotes. This sentence will be added to my Google Doc.

This is a very simple example of coding a Google Doc, but it will feel amazing when the sentence shows up in your document. Usually this is more helpful if you have some text somewhere, you grab the text and add it to either a Google Doc or onto each students Google Docs. But this is a place to start.

Save and Run

Use the toolbar to click the Save icon. Also be sure you renamed your script in the upper left corner.

In this example we had 2 functions. The onOpen function and the rubric function. Pay attention in the toolbar as to which function you are running. Running onOpen will allow the menu to appear in the Add-on menu. Running rubric will add the text to the paragraph.

Open Any Google Doc

Try this out with any Google Doc you own. Open from Google Drive and use the Tools menu to find the Script editor.

Learn More

Are you ready to learn more? Join my coding workshops, as part of the premium membership, for more tutorials and support.

Want More Help with This? Become a Premium Member

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.