Simple notification script for Google Sheets
UPDATED: May 19, 2017
I changed the script to be more user-friendly and the ability to include HTML in the email body. I also found that Google moved a few things in the menus, so I modified the instructions as appropriate.
Google Sheets has a nice feature to notify yourself if something has changed, but every person who wants to be notified must add a notification for themselves. With a simple script, you can send notifications to people of your choosing.
In this example, I've written a script to send an email when 1 or more rows are added.
Get started.
- Go to the sheet
- Find a cell in the sheet that wont ever be over-written.
- Enter the value of the current total number of rows in the sheet.
- Note the cell for use in your script. I used G2.
- Go to Tools > Script editor...
Write your script.
Add this script in the script editor. Change as required to meet your needs.
// /////////////// //////////////// ////////////////
// /////////////// Cell to track last row //////////////// //
// /////////////// //////////////// ////////////////
var theLastRow = "G2";
// Remember to put the number of rows, i.e. last row, in your spreadsheet into the cell you've referenced here.
// /////////////// //////////////// ////////////////
// /////////// End cell to track last row /////////////// //
// /////////////// //////////////// ////////////////
// get the spreadsheet object
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
// set the first sheet as active
SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[0]);
// fetch this sheet
var sheet = spreadsheet.getActiveSheet();
// fetch sheet name
var sheetName = spreadsheet.getName();
// figure out what the last row is
var lastRow = sheet.getLastRow();
// cell used to track last row we checked. See the Get started section in this post.
var lastRowChecked = sheet.getRange(theLastRow).getValue();
// The number of new lines on the sheet.
var newLines = lastRow - lastRowChecked;
// /////////////// //////////////// ////////////////
// /////////////// Customized variables //////////////// //
// /////////////// //////////////// ////////////////
// These variables that can be used in your subject or message:
// 1. newLines : the number of new rows added to the sheet
// 2. theSheetURL : the URL to the Google sheet
// 3. sheetName : the name of the Google sheet
// Update with appropriate email address(es). Multiple addresses should be comma delimited.
var theEmailDistro = "my-email-address@foo.com";
// Set email subject
var theSubject = "My email subject";
// The URL specific to your Google Sheet. This variable can be used in the email body.
var theSheetURL = "https://docs.google.com/spreadsheets/my-super-sheet";
// The email message. The message must be quoted. If using HTML, be sure to only use single quotes in your HTML.
var theEmailHtmlBody = "There is " + newLines + " or more new entries in the " + sheetName + " Google sheet."
// /////////////// //////////////// ////////////////
// ///////////// End customized variables /////////////// //
// /////////////// //////////////// ////////////////
if (newLines > 0) {
// This is the message that will be emailed.
// 'newLines is
MailApp.sendEmail({
to: theEmailDistro ,
subject: theSubject ,
htmlBody: theEmailHtmlBody
});
// set new 'last row'
sheet.getRange(theLastRow).setValue(lastRow);
}
}
Go to the menu and select File > Save.
Test your script.
Testing is easy.
- Make sure your email address is one of the notification email addresses.
- Either change the value in the row tracking cell to lower than the number of rows OR add a new row.
- Run the script.
- Click the 'play' button -- find it left of the 'bug' button.
OR - Click 'Run' from the menu and select the name of your function.
- Wait for your email.
Deploying.
- Clean up from your test.
- Make sure your last row number (in G2 in my example) is correct.
- Make sure the notification emails are correct.
- Save the script again if you changed anything, such as the email address(es).
- Select Edit > Current project triggers from the menu.
- Select from the trigger options. For example you can have it check on the hour or once a day.
Need more help? Search Google of course.
Good luck!
Comments
These are so useful. Thank you for helping !