Hugo Fierro
Google Apps Script Team
May 2009
Goal
This tutorial shows how to use Spreadsheet data to send emails to different people.
Time to complete
Approximately 10 minutes
Prerequisites
Before you begin this tutorial, you must:
- Feel comfortable using the Script Editor and have experience using the most basic Spreadsheet functions.
Section 1: Running a simple example
- Create a new empty Spreadsheet.
Add a few rows of data. Every row should contain an email address in column A and the email message to be sent to that person in column B. For testing purposes, you may want to use your own email address in column A. Here's an example:
Open the Script Editor by clicking on the Tools menu, then select Script editor.
Copy and paste the following script:
Save the Script.
Select the function sendEmails in the function dropdown list and click Run.
Check out your email inbox. Messages are usually immediately delivered, but sometimes it takes a few seconds.
You might want to have a look at the documentation for the following methods used in the script above:
Sheet.getRange()
(note that there are four versions of this method)Range.getValues()
MailApp.sendEmail()
(note that there are four versions of this method)
Section 2: Improvements
You might want to mark a cell in each row every time an email is sent. This way, if your script stops running (for instance, if there is a bug in your code or you reach the maximum number of emails you can send every minute or day) you will be able to re-run the script later on and avoid sending email duplicates.
Here's a simple extension of the code that sets the cells in column C to 'EMAIL_SENT' for each row after sendEmail is called.
Summary
Congratulations, you've completed this tutorial. You should now be able to easily make your scripts send emails from a spreadsheet.