The packages for MITERS makerships are being routed through me (to force people to go to MITERS, heh). I wrote a quick and dirty script to automate notifying people by email when the packages arrive at MITERS. My first try at google apps scripting.
Google's documentation is excellent: http://code.google.com/googleapps/appsscript/service_spreadsheet.html
The code is like so:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//When run, (a) creates a custom menu -- script is not run automatically, you have to hit the button | |
//(b) checks the spreadsheet to see who if package has arrived and email notification has not yet been sent. | |
//This is not smart enough to combine multiple packages into one email, so fairly spamtastic. | |
//loads our special menu each time the spreadsheet is opened | |
function onOpen() { | |
loadMenu(); | |
} | |
function sendEmails() { | |
var ss = SpreadsheetApp.getActiveSpreadsheet(); | |
var packageSheet = ss.getSheets()[0]; | |
var range = packageSheet.getDataRange(); | |
var data = range.getValues(); | |
//iterate over all the rows in this sheet | |
for(var i = 1; i < range.getNumRows(); ++i){ | |
var row = data[i]; | |
var recipient = row[2]; | |
var packageName = row[3]; | |
var dateArrived = row[9]; | |
var haveSentEmail = row[10]; | |
//check if the package has arrived and that we haven't sent a notification email yet | |
if (dateArrived !== "" && haveSentEmail == ""){ | |
dateArrived = Utilities.formatDate(dateArrived, "EST", "MM-dd-yyyy"); | |
//Send a notification email. I'm cheating and not doing a lookup of name:to email address, instead asking people to use their MIT username and all I have to do is append @mit.edu | |
MailApp.sendEmail(recipient+"@mit.edu", | |
"makerships package at MITERS: " + packageName, | |
"Hello " + recipient + "," + | |
"\nYour package, " + packageName + ", was ordered with your makership and arrived at MITERS on " + dateArrived + | |
". You can find it in the backroom." + | |
"\nFor more information, see the ordering form https://docs.google.com/spreadsheet/ccc?key=0Atr0HZeoMbmgdF9SNU5fTi1Zbk0xNnQyVTFTRkxFamc&hl=en_US#gid=0" + | |
"\n\nThis is an automated message; if this method is too spamm-y, let me know." + | |
"\n\nThank you, \n--Nancy (on behalf of MITERS)"); | |
//check off that we sent the email so we don't spam people THAT much | |
packageSheet.getRange(i+1,11,1,1).setValue("yes"); | |
} | |
} | |
} | |
//When you first run this script, google will prompt you to confirm that you want to run this script. After you say yes, you're set. | |
function loadMenu() { | |
var ss = SpreadsheetApp.getActiveSpreadsheet(); | |
ss.addMenu("don't touch!", [{name:"Send package arrival emails", functionName:"sendEmails"}]); | |
} | |
//Pops up a message box in the spreadsheet. Useful for debugging: | |
//Browser.msgBox("sending email, " + "i: " + i); |
Which corresponds to a spreadsheet is set up like so.
So, the full steps:
You start out with the raw spreadsheet. Then, to add a script to it, got to Tools > Script Editor... which will take you to a blank version of the following. To check your script for errors, you hit the play button here.
Now, go to Tools > Script Manager > Run to run your script and create the custom menu (which is just for convenience for running your script, but invaluable when you're rerunning all the time to debug). The first time Google will double-check with you if you want to run this script, since it's doing Dangerous Things like emailing people under your name. After that you're set.
The emails, interestingly enough, will NOT thread/group in gmail. So this is still a rather spamtastic script. _shrug_ I don't care enough. You can see my testing emails below:
I didn't choose to use an HTML template for me email. See http://googleappsdeveloper.blogspot.com/2011/10/4-ways-to-do-mail-merge-using-google.html if you want to implement that.
No comments:
Post a Comment