code google apps script

You Can Code Google Apps Script

I literally learned to code Google Apps Script in one hour. I went from knowing zero about Google Apps Script (other than knowing it is based on JavaScript) to creating my first template (alicekeeler.com/rostertab) and blogging it in under an hour. In a nutshell, Google Apps Script lets you code Google Apps. All those times you think “Why doesn’t Google Apps do ____” It probably can… if you code it yourself!

Very Readable

Without knowing how to code you can look at Google Apps Script and tell what it does. Read the following two lines of code carefully, can you tell what they do?

DocumentApp.getActiveDocument();

SpreadsheetApp.getActiveSpreadsheet.insertSheet();

Analyze not Memorize

I put no effort into memorizing code. First of all, Google Apps Script is multiple choice so that is nice. But mostly what I do is copy and paste code from the Internet (or you can steal it from any of my projects at alicekeeler.com/scripts). Since Google Apps Script is so readable you can usually just edit the code and make it work without fully knowing all the ins and outs of the code.

Very Intro to Coding with Google Apps Script Online Workshop

If you want to get started with coding consider taking my online workshop for Google Apps Script. It is 6 weeks and each week is asynchronous. That means you do not do the whole workshop on your own time, but each week is on your own time. Each week you get a lesson intended to take about an hour (this does not include debugging time!) to try out some code and to practice writing some code. We interact together each week (you and me and classmates) because I subscribe to Social Learning Theory by Bandura.

alicekeeler.com/events

Do It Again

The best reason for teachers to learn to code is that they have 30 students in front of them every day that causes them to do repetative tasks. The third time in a row that you do the same thing you have to be asking yourself:

“there has to be a faster way to do this?!”

Don’t Memorize the Loop

You’ll learn it soon enough and won’t need to Google Search it or copy and paste it anymore. You can look up: for loop javascript and get the code easily.
for loop javascript

Copy Paste the For Loop

for(var i=0; i<10; i++){
code goes here
}

What this says is start counting at zero and keep counting until you hit 10 and then stop. The i++ says to increment the value of i (which starts at zero) one each time. The for loop is the best friend of a teacher. It does repetative tasks for you.

Try This

Create a new Google Doc and name it. Make it into some sort of template that you would like to make copies of. So type on it and add a picture maybe. Personally, I would put a Bitmoji on it.

Using the Tools menu choose “Script editor.” This is where you write your code.
Script Editor

What App Are You Using?

When I code Google Apps Script my first line of code is usually answering the question “What App am I using?” In this case we are using:

DocumentApp

Notice the capitalization! Type this in between the curly braces after myFunction().
DocumentApp in Apps Script

And Now What….

Type a period onto the DocumentApp to bring up a multiple choice menu that lets you know what you can do with that app.

getActiveDocument();

From the multiple choice drop down choose getActiveDocument(). Again, notice the capitalization.
Use the period to get multiple choice going

End With a Semi-Colon

For each line of code, end it with a semi-colon.

DocumentApp.getActiveDocument();

Name Your Code

It is super helpful to give your line of code a name so you can reuse it. Use variables to remember your code so you do not have to retype that exact line again within the same project.

var

Use var to indicate you are creating a variable. Then you can use any variable name you want but I highly recommend you use something meaningful.

var doc = DocumentApp.getActiveDocument();

Next Line…. Start With Your Variable

Each line of code does not remember the previous line of code. So if you want to do things with the active document you have to keep reminding the code you’re using the active document (doc).

Start your next line with the variable you used, doc.

Then use a period to get the multiple choice menu going.

Choose to get the ID of the document. Find getId() and end with a semi-colon. Make this line of code into a variable also. Start the line of code with var and give it a meaningful name (I used docId) and an equals sign.
get the ID of the document

DriveApp

DriveApp will allow you to pull up documents from Google Drive and make copies. So let’s make a copy of this document. Type DriveApp and a period to bring up the multiple choice list. Find getFileById() and click on it. The ID is stored in the variable docId so replace the default text of id with docId (it should turn blue saying you’re using a variable). Add another period to pull up the multiple choice list. Find makeCopy(). Place a semi-colon at the end.
DriveApp dot make copy

Note: DriveApp is blocked on some school domains. If this does not work use your personal Gmail accounts. 

Save and Run

Save and run the code. If you did it right you should be able to find the copy of the document in your Google Drive. Click the save icon in the toolbar and use the Run icon (looks like a play button).
save and run

You will have to name the code and authorize it. But then you can push the Run button a whole bunch of times to make a whole bunch of copies.

Seriously Alice That Was Not Faster

Nope. That is where the for loop comes in! What do you want to do a whole bunch of times? Which line of code?
Make copies should repeat

That one line of code needs to be in between the curly braces of the for loop. Copy and paste that code for the for loop right above the DriveApp line of code.
For loop on DriveApp

NOTICE THE EXTRA SET OF CURLY BRACES!!! One set for the for loop and one set for the function.

Save and Run Again

Now when you run the code it will make 10 copies. Go look in your Google Drive you should have a bunch of copies of your document.

OR if you ANALYZE the code and notice the i<10 says how many copies to make, you can change the 10 to a different number 🙂

Digiprove sealCopyright secured by Digiprove © 2017