Created
August 24, 2016 00:02
-
-
Save Spencer-Easton/a798e70554fee0689b09e767fc55ac72 to your computer and use it in GitHub Desktop.
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
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