Wednesday, November 21, 2012

Using MailApp and a Form Submit Trigger

So I had grand plans of posting regularly as I learned GAS. Unfortunately I haven't done a good job with the blogging. My excuses are that I had a new son in March, worked tirelessly getting my house ready to sell, did job search, and moved 700 miles back to the Chicago area.  But I am going to try and post again more regularly and document my experiences with GAS.  I have written a bunch of simple scripts over the past year, and I do plan to go back and post about them.

But to get me going again I will start with a simple example. A teacher came in to my office today and asked if there was a way for students to check into the media center, then have their teacher get an email notification. I figured this could be accomplished fairly easily via a Google Form with some Google Apps Script to send the email.

I started by creating a form.

Now I want to create a script that will use the MailApp Class to send an email to the teacher of the student who is checking in or out. There are probably many ways to accomplish this, but my solution was to create another sheet and in column A put the teacher names. These values must match the multiple choice options from the teacher question on the form. Column B is filled with each respective teacher's email address. I will use Google Apps Script to read this data into a JavaScript Object. It was important for me to keep the list of teachers out of the Google Apps Script because I want this to be maintainable by the end user. If there is a change in the list of teachers a user could make the change in the form and the spreadsheet, without having to touch the code. You can see the spreadsheet here.

On to the code.  I start by opening spreadsheet. I chose to open it by id; using getActiveSpreadsheet should also work. Then I open the sheet with the emails by name. Then I get the data and write the values into a 2d array.

Before I talk about the next part, it is important to understand how this script will run. The GAS documentation has an excellent article about execution methods. We want this script to run every someone submits a form. When that form is submitted a parameter (e) is passed to the function. Read about this here. (Form submit events are at the bottom of the page) You can actually access the values by the name of the form field, but I prefer to access them via their array index. For example, e.values[0] is column A, which is always the time stamp in Google Forms.

So I grab the teacher name (e.values[5]), then I loop through the emails array looking for a match to the submitted teacher name. When the loop finds a match, it sets the email variable.

Next I get the rest of the values from the form submit event and send the email.  Sending messages is pretty simple, with 3 parameters, the email address, the subject line and the body. It is possible to create an HTML body, but in this case it isn't necessary.  Another nice feature is that you can set the emails to display with a no reply address.  Without setting this, it will look like the emails will come from you.

 Check out the code below. Please let me know if you have any questions, suggestions or if you catch a mistake.



1 comment: