Skip to content

Instantly share code, notes, and snippets.

@bdunnette
Created October 8, 2014 20:36
Show Gist options
  • Save bdunnette/6b581edc2bee755c31ad to your computer and use it in GitHub Desktop.
Save bdunnette/6b581edc2bee755c31ad to your computer and use it in GitHub Desktop.
function remindToFloss() {
var ss = SpreadsheetApp.openById("INSERT SHEET ID HERE");
var responses = ss.getSheets()[0].getDataRange();
var participants = ss.getSheets()[1].getDataRange();
var needsNotifying = [];
// Compile a list of email addresses that may need notifications
for (var i = 0; i <= participants.getNumRows() - 1; i++) {
var email = participants.getValues()[i][0];
needsNotifying.push(email);
}
var numRows = responses.getNumRows();
var values = responses.getValues();
// Loop through rows in the spreadsheet - starting at the LAST row, since it's likely to be most recent, to speed up the search; only keep searching as long as there are users in the "needs notifying" list
for (var i = numRows - 1; i >= 1 && needsNotifying.length > 0; i--) {
var row = values[i];
var flossDate = row[0];
var email = row[1];
var today = new Date();
var userIndex = needsNotifying.indexOf(email);
// If the user has an entry matching today's date, remove ('pop') them from the list of users to notify today
if (userIndex >= 0 && today.toDateString() == flossDate.toDateString()) {
needsNotifying.pop(userIndex);
}
}
for (var i in needsNotifying) {
var email = needsNotifying[i];
MailApp.sendEmail(email, "Did You Floss Today?", "It looks like you forgot to floss today!\n\nRemember to log your flossing here: https://docs.google.com/a/umn.edu/forms/d/1BtLGon001MVjrD6N8_JTLjYsWF3D3r3ro9bs0z-WOyc/viewform")
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment