Google Apps Script

Here is a very quick project you can do to make your first program. Then you can brag to your students that you can code. Do not be embarrassed that you do not know very much, the fact that you coded at all is braggable. If we want to encourage students to think about learning to code, it starts with us.

Task

Write a code to send an email through Gmail.

The Code

function sendEmail() {
GmailApp.sendEmail(recipient, subject, body);
}

Steps

Google Apps Script

Google Apps Script is based on JavaScript. But even if you know any JavaScript do not let that intimidate you. You can use Google Apps Script. To get started go to the Google Apps Script page

http://script.google.com

You may need to click on a blue start button to take you to the project page. A splash screen pops up, click on the blue “Close” button.
Just click okay

Function

A function tells the computer what to do. It always follows the same format. Luckily Google Apps Script gives you a default function, so you do not need (at this time) to learn how to make one.
Function

The “myFunction” is the name of your function. You can change that to whatever you want so long as it is only one word.

For fun, change the function name from “myFunction” to “sendEmail.”
Change Function Name

Click Here to see an explanation

You may notice that I capitalized the E in email but did not capitalize the s in send. You could have if you wanted to. When looking at JavaScript commands they tend to capitalize the first letter of words that are smashed together but start with a lowercase. This is style, not a rule.

Note: Capitalization matters.

When calling your function later if you put SendEmail instead of sendEmail that does not match. So however you write the name of your function, make sure you write it the exact same way when you need to use it.

Code

Your code goes between the curly braces.
put code between curly braces

Choose Your App

Google Apps Script allows you to manipulate Google Apps. You will type one of these to start with:

  • GmailApp
  • DocumentApp
  • SpreadsheetApp
  • DriveApp
  • CalendarApp
  • ContactsApp
  • FormApp
  • GroupsApp
  • LanguageApp
  • Maps
  • SitesApp

For this project, we want to send an email so type

GmailApp

Period

With Google Apps Script (and JavaScript) you stack your methods together to make something happen. You separate your methods with a period.

After typing a period after the GmailApp, a menu should (in theory) pop up to show you what choices you have! (This is where it gets fun.)

Look through this list to see what you might want to do with Gmail!!

sendEmail

Choose the “sendEmail()” method from the list of choices. Remember that capitalization matters, so it is better to choose the method from the list rather than typing it by hand.

GmailApp.sendEmail(recipient, subject, body)

SemiColon

When coding JavaScript/Google Apps Script, you need to end each line of code with a semicolon. I recommend you do this now, so you do not forget.

When your code does not run correctly, most of the time you forgot a semicolon.

GmailApp.sendEmail(recipient, subject, body);

Quotes

You need quotations on all of your text strings. Use single quotations (although double quotations will work too).

Replace “recipient” with the email address of who you want to send this email to ‘joesmith@yourschoolemail.edu’

Replace “subject” with what the subject line of the email is. ‘I am sending this email from Google Apps Script.’

Replace “body” with what you want the email to say. ‘I am not a coding ninja yet, but this is my first step on the journey! I can now say I can code. ‘

YOU DID IT!

You wrote your first script!! You can code! You’re a programmer!

Run the Code

Save the code by clicking on the disk icon in the toolbar. You will be prompted to name your code. This is the name of your code; I chose Email.

From the Run menu, choose the name of the function you wish to run. In this case, we only have one choice. Choose “sendEmail.”
Run menu

This should send out that email!!

Getting Sassy

If you would like to be sassy and see how you can send out TWENTY emails at once just to annoy someone with your mad coding skills, the directions are below.

Click Here to kick the project up a notch

For the most part I do not memorize code, I copy and paste it. I go to the Google Apps Script page, and I find sample code and copy and paste it into my scripts.google.com file.

for Loop

A for loop repeats the same line of code over and over again. Here is the template for you to copy and paste.

for(var i = 0; i < 20 ; i++){
}

Explain

The for loop means for as long as the statement in the middle is true it will run the code.
var allows you to set a variable. We are using the variable i and setting it at a value of zero.
semicolon
The i<20 means so long as i is less than 20 it will run the code over and over and over.
semicolon
The i++ is shortcode for i = i + 1. Meaning each time the code is run the i is replaced with the value of i + 1. So it starts at zero. Runs the code. Now i = 1. Runs the code. Now i = 2, etc…

Add the Code

The line of code you ran above you will want to run multiple times. Copy and paste this above your line of code: for(var i = 0; i < 20 ; i++){
for loop

Below your line of code you need a closed curly brace } to end the for loop. This gives you TWO closed curly braces on your code.

Add an i

Just for fun, you have this variable i you can use. Everywhere you put the i the value of the i will be displayed. In the subject line, you can add the i by putting +i outside of the quotations. You can also place the i in the body.

Example

function sendEmail() {
for(var i=0; i<20; i++){
GmailApp.sendEmail(‘joesmith@yourschoolemail.edu’, ‘I am sending this email from Google Apps Script. Email number ‘ + i, ‘I am not a coding ninja yet, but this is my first step on the journey! I can now say I can code. ‘ + i);
}
}

Code

Here is a link to the code I wrote in scripts.google.com.

Other Tutorials

Project 1: Change the name of a Google Doc

Project 2: Create a bunch of new Google Doc’s

Project 3: Create copies of a file in your Google Drive