5. Google Script

5.1 JavaScript Review


Brush up on your JavaScript skills at Codecademy. If you feel comfortable with JavaScript, you can move on to 5.2.

5.2 Getting Started on Google Script


  1. Sign up for a Google account if you need to.
  2. Log into Google
  3. Open this in one tab if you want an overview of Google Scripts.
  4. Open this in another tab if you want a reference page with all the Google things.
  5. Open this in another tab if you want a reference page with all the JavaScript things.
  6. Open the Script editor in another tab.
  7. Play around with it. Try some example code.

5.3 Make a flyer and email it to yourself

  1. If you haven’t, open the Script editor in a new tab.
  2. For more information, open the tutorial in another tab.
  3. Copy this into the Script editor:
    function createAndSendFlyer() {
     // Create a new Google Doc named 'Coders for Liberty flyer'
     var doc = DocumentApp.create('Coders for Liberty flyer');
     // Access the body of the document, then add paragraphs.
     doc.getBody().setFontFamily('Roboto Mono');
     doc.getBody().appendParagraph('Join us at https://code4liberty.wordpress.com');
     doc.getBody().appendParagraph('Have you ever wanted to write a program');
     doc.getBody().appendParagraph('but you thought it was too difficult?');
     doc.getBody().appendParagraph('Programming is not out of your reach.')
     doc.getBody().appendParagraph('Programming is like any other skill.');
     doc.getBody().appendParagraph('It takes practice, persistence,'); 
     doc.getBody().appendParagraph('and the will to succeed.');
     doc.getBody().appendParagraph('The question is not if you can write a program');
     doc.getBody().appendParagraph('but what you want your program to do.');
     doc.getBody().appendParagraph('The key to programming is creativity
     doc.getBody().appendParagraph('and imagination.');
     doc.getBody().appendParagraph('And the persistence to learn');
     doc.getBody().appendParagraph('the syntax of the languages.');
     doc.getBody().appendParagraph('Free yourself from the shackles of your own doubts.');
     // Get the URL of the document.
     var url = doc.getUrl();
     // Get the email address of the active user - that's you.
     var email = Session.getActiveUser().getEmail();
     // Get the name of the document to use as an email subject line.
     var subject = doc.getName();
     // Append a new string to the "url" variable to use as an email body.
     var body = 'Link to your doc: ' + url;
     // Send yourself an email with a link to the document.
     GmailApp.sendEmail(email, subject, body);
  4.  Save the file
  5. Select “createAn SendFlyer” from the drop down menu.
  6. Click the triangle (Run).
  7. Make edits to make it better.
  8. If you have an upcoming event, add information for your event using the CalendarApp.

5.3 Form Letter Email

While the above example may seem silly. There are occasions where it may make sense to write a program that generates a form letter, flyer, or email. While the following program is obviously a joke, it demonstrates how you can use variables to generate a form letter email. Just please don’t be spamming people. That’s not nice and reduces your credibility. Only send emails to people you genuinely believe are interested in what you have to say. Otherwise, you’re just wasting your time.

function majorParty() {
    // candidate that I support
    var iSupport = "Kudos";
    // candidate that I oppose
    var iOppose = "Kang";
    // body of email

    // third party candidates
    var thirdParty = ["A third party candidate", "Another third party candidate"];
    // names of email recipients
    var name = ["Cody Liberty", "Cody"]
    // email addresses of email recipients
    var email = ["code4liberty@gmail.com", "code4liberty@gmail.com"]
    // loop through email addresses
    for (j = 0; j < name.length; j++) {
    var message = "";
    message = message.concat("Dear ", name[j], ":\n\n")
    // cylce through third party candidates
    for (i = 0; i < thirdParty.length; i++) 
      message = message.concat("It does not matter that ", thirdParty[i]);
      message = message.concat(" is not ", iOppose);
      message = message.concat("."," ");
      } // close 3rd party loop
    message = message.concat("What matters is that ", iSupport);
    message = message.concat(" is not ", iOppose);
    message = message.concat("! Therefore you must support ", iSupport);
    message = message.concat("! ", iSupport);
    message = message.concat(" is the only one that is not ");
    message = message.concat(iOppose, " that can win because people like me support ");
    message = message.concat(iSupport, "! It does not matter how ");
    message = message.concat(iSupport, " stands on the issues. Let's jump off this cliff together! ");
    message = message.concat("Who cares what your mom told you when you were a kid! That does not matter now! We cannot allow ", iOppose);
    message = message.concat(" to win!");
    message = message.concat("\n\nSincerely,\n", iSupport, " against ", iOppose);
    message = message.concat("\n\nThis is a joke. No one paid me to do this.");
    message = message.concat(" I am just practicing my coding skills on stupid");
    message = message.concat(" political nonsense. Don't vote for anyone! ");
    message = message.concat("They're all bat excrement crazy!");

  // Get the email address of the active user - that's you.
  //var email = Session.getActiveUser().getEmail();

  // Subject line
  var subject = "We cannot allow ".concat(iOppose, " to win! You must vote for ");
  subject = subject.concat(iSupport, "!!!");

  // Send yourself an email with a link to the document.
  GmailApp.sendEmail(email[j], subject, message);
    } // close loop
} // close function

5.4 Sending Emails from an Email List on a Spreadsheet

For the life of me, I could not figure out how to do this the way Google suggests in their tutorial. If you can get that to work, let me know. Anyway, my solution is as follows.

function sendEmails() {
  // these top four variable are what you may need to change.
  // string from url and share link
  targetId = '1fgBuVkM1rk9oZ8tjbwi1UrNFDfElpG2zw2qfj_VJpm8'; 
  var count = 2; // number of emails;
  var ecol = 'A'; // column with emails
  var ncol = 'B'; // column with names
  var yourName = "Punkbass"; // name of person sending email.
  // should not need to change these
  var sheet = SpreadsheetApp.openById(targetId);
  var emails = new Array(count); // emails
  var names = new Array(count); 
  var ecell = '';  // email cell
  var ncell = ''; // name cell
  // cycle through cells to get emails and names
  for (i = 0; i < (count); i++) {
    ecell = ecol.concat(i+2);
    ncell = ncol.concat(i+2);
    emails[i] = sheet.getRange(ecell).getValue();
    names[i] = sheet.getRange(ncell).getValue();

  // send the emails
  for (i=0; i < names.length; i++) {
    var subject = "Coders for Liberty";
    // salutations
    var message = "Dear ".concat(names[i], ":\n\n");
    // body
    var message = message.concat("Thank you for joining coders for liberty.");
    // signature
    var message = message.concat("\n\nSincerely,\n", yourName);
    MailApp.sendEmail(emails[i], subject, message);

You will want to make a new Google Sheet to test this out on. Something like this:

email address name
you@gmail.com Cody
you@gmail.com Sam

When you first test it out, put in your own email address in both spots. If you have more than one email address, I guess you could use both of em if you want to check them both. If you have more than two email addresses, you’ll want to change the count variable to the number of email addresses. Honestly, it might actually be better to use a while loop. That could be your homework, rewrite this with a while loop.

While loop pop quiz:

Go to the top of the page in the address bar and copy the long string of characters and paste it in the script where it says:

  targetId = '1fgBuVkM1rk9oZ8tjbwi1UrNFDfElpG2zw2qfj_VJpm8';

And change yourName to your name. And you might want to change the body of the message to something with more substance. And if you’re using an existing spreadsheet with the email and name in different columns, just change the ecol and ncol variables to the columns in your spreadsheet. That might be easier to do than changing the spreadsheet itself.

Note: Normally you would not want to create an array using the new command. At least that is what w3School says. Normally, you would use brackets like a Python list:

var theArrayName = [element1, element2, element3];

If you do this:

var theArrayName = new Array(2);

You make an array with 2 elements. But that’s the way I wanted to make my array. I wanted an array with count elements. That’s the only reason I used new. If you decide to use the while loop, I would recommend using the [ ] method and the .push() method.

5.5 Newsletter

Your capstone project is to combine what you have learned to create a newsletter and send it out to a mailing list in your spreadsheet. You should personalize this newsletter with information that you pull from your spreadsheet. You can also use dates from the CalendarApp even though we didn’t cover that because you’re awesome like that.

Congratulations you have completed the level 5 Coders for Liberty training!

Onward to level 6!

One thought on “5. Google Script

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s