Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Spencer-Easton/a798e70554fee0689b09e767fc55ac72 to your computer and use it in GitHub Desktop.
Save Spencer-Easton/a798e70554fee0689b09e767fc55ac72 to your computer and use it in GitHub Desktop.
function emailReminder() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var leases = ss.getSheetByName("Leases").getDataRange().getValues();
var emails = ss.getSheetByName("Emails").getDataRange().getValues();
var notificationWindows = {}
notificationWindows[addMonths(new Date(),6)] = makeNotifications(emails, "Six Months");
notificationWindows[addMonths(new Date(),3)] = makeNotifications(emails, "Three Months");
notificationWindows[addMonths(new Date(),1)] = makeNotifications(emails, "One Month");
notificationWindows[addWeeks(new Date(),2)] = makeNotifications(emails, "Two Weeks");
notificationWindows[addWeeks(new Date(),1)] = makeNotifications(emails, "One Week");
for(var i in leases){
if(leases[i][1] in notificationWindows){
notificationWindows[leases[i][1]](leases[i][0]);
}
}
}
function addMonths(date, months) {
date.setMonth(date.getMonth() + months);
date.setHours(0,0,0,0);
return date;
}
function addWeeks(date, weeks){
date.setDate(date.getDate() + (weeks * 7));
date.setHours(0,0,0,0);
return date;
}
function makeNotifications(emails, timeToExpire){
return function(property){
for(var i in emails){
var notification = "The lease for " + property + " is set to expire in " + timeToExpire;
MailApp.sendEmail(emails[i][0], notification, notification);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment