Google Apps Script: Getting Started Part 2

In the first Google Apps Script tutorial you learned a little JavaScript to allow you to change the title of a Google Doc. In this tutorial you will create a document and make multiple copies of the document.

JavaScript Recap

Function: A chunk of code that is used perform a particular task. The function is called and then executes the chunk of code. function myFunction(){ }

Semi-Colon: The semicolon goes at the end of a line of code to signify the end of the line.

Variable: Define variables to reference back to values to allow for reusability. var name = value

JavaScript Tutorial

Loop

A loop allows you to run the same code repeatedly until certain criteria is met. The for loop contains 3 parts: starting value, ending value and the incremental value. Each of these are separated by a semi-colon.

for(i = 0; i < # ; i++){

}

Typically for a loop you start counting at zero, however you can start at any value you want. Choose a variable (usually i) and set the starting value. The next value is an expression that tells the loop when to stop looping. The third value usually increments the variable by one until the middle expression is false. You can write i = i + 1 or you can use the shortcut of i++ which automatically increases the value of i by 1.

Project

For this project you will create a document and create copies of the document.

Write Your Function

For Loop

You want to create a document repeatedly. For this you will need a loop. I am going to make 5 copies of the document, but you can make however many copies you want. Write a for loop between the curly braces of the function. You will create a copy of the document for as long as i is less than 5.

for(i=0; i<5; i++){}

Document App

This script utilizes the DocumentApp. Look over Google Developers page on the DocumentApp: Click Here

Define a Variable

Within the loop we will want to call the DocumentApp and create a document. Define a variable (I am using drive). Call the DocumentApp and use the method of create to create a new document. Remember that you append your code instructions with a period.

var drive = DocumentApp.create(

Inside the parenthesis of the create method is the name of the document. Remember that your string of text needs to be in quotations. If you want you can add the value of i to the title. This lets you know which copy you are looking at. Outside of the quotations the plus sign tells the code to add the value of i to the document title.

Suggestion to always put two slashes after your code to leave a comment to describe what you are doing. Notice the semi-colon before the slashes to signal the end of that line of code.

var drive = DocumentApp.create(‘Ha, this is my new document’ + i); //This creates a new document with the ith version

Add Editor

You can tell the script who is an editor on the document. If you want multiple editors you can repeat the line of code but with different email addresses or use the addEditors() method.

You want to use the method addEditor(). You need to tell the script what document to add the editor to. This is where the variable comes in handy. The variable drive is what we are using for the document we are creating. Append drive. in front of the addEditor. Inside of the addEditor parenthesis you will put the email address of the person you share the document with. Notice the semi-colon at the end of the line.

drive.addEditor(‘joesmith@email.com’);

Add Content

You do not want to create 5 blank documents. You can add default text to the body of the document. Using different methods you can get a lot fancier but we are just going to do something simple. Create a variable for the body of the document where the text will go. Call the getBody() method and remember to append the variable drive in front of the method. Again notice the semi-colon at the end of the line.

var body = drive.getBody();

Create Paragraph

Use the appendParagraph() method to add text to the document. You can write anything you want inside of the parenthesis. Make sure the text is in quotations. Notice the end of the line has a semi-colon.

body.appendParagraph(‘This is the directions for your assignment. Make sure that you read all of the directions and turn this in by the due date. ‘);

Create a page break if you wish by using the method appendPageBreak() body.appendPageBreak();

Two curly braces should finish out your code. They should already be there. One to close off the for loop and the other to close off the function.

Code

Your complete code should look like:

function makeMeADoc() { for(i=0; i<5; i++){ var drive = DocumentApp.create(‘Ha, this is my new document’ + i);//This creates a new document with the ith version drive.addEditor(‘joesmith@email.com’); var body = drive.getBody(); // Append a paragraph and a page break to the document body section directly.body.appendParagraph(‘This is the directions for your assignment. Make sure that you read all of the directions and turn this in by the due date. ‘);body.appendPageBreak(); }

}

Run Code

Click on the save icon in the toolbar. You will be prompted to name the script. I named mine makeMeADoc as I tend to name the scripts after the functions I use but this is not required. From the Run menu, run the makeMeADoc function that you created. You will need to authorize the script for it to run. Click Here for my sample code.

Google Drive

Go to your Google Drive. You should see 5 documents with your document title. Notice the icon next to the document titles showing that the documents are shared.

You can get a lot fancier with this script, this is just to help you get started with using Google Apps script. Share your ideas in the comments for how you could modify this project for something useful.

My Referral Links

50% Off February Special

Online Workshops for Google Sheets, Classroom, Slides and more are 50% off for February. Use the discount code february at checkout. Also annual subscriptions for access to all of the Go Slow online workshops are 50% off!