Teacher Tech blog with Alice Keeler

Paperless Is Not a Pedagogy

Alice Keeler

Google Apps Script: Getting Started Part 2

Google Apps Script: Getting Started Part 2

Google Apps Script

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.

Getting Started with Google Apps Script

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.

Script.google.com

Go to http://script.google.com to create a blank project.

Delete out the default text (Control A to select all. Delete)

Write Your Function

Your script needs a function. I am calling my function makeMeADoc. Don’t forget the opening and closing curly braces. Type:

function makeMeADoc(){
}

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++){
}

Screen Shot 2015-03-14 at 4.52.07 PM

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

Screen Shot 2015-03-14 at 5.06.04 PM
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’);

Screen Shot 2015-03-14 at 5.12.30 PM

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();

Screen Shot 2015-03-14 at 5.16.59 PM

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();

Screen Shot 2015-03-14 at 5.19.34 PM

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.

2015-03-14_17-25-06

 

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.
2015-03-14_17-26-47

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.

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