-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBirthdayReminder.json
1 lines (1 loc) · 5.66 KB
/
BirthdayReminder.json
1
{"files":[{"id":"1ccf15c6-d064-4118-a407-44d2b315cb6a","name":"birthdayReminder","type":"server_js","source":"function doGet() {\n return HtmlService.createHtmlOutputFromFile(\u0027index.html\u0027)\n .setSandboxMode(HtmlService.SandboxMode.IFRAME);\n}\n\nfunction sendReminderEmails() {\n var contacts \u003d ContactsApp.getContacts();\n var now \u003d new Date();\n \n for(var i \u003d 0; i \u003c contacts.length; i++) {\n var contact \u003d contacts[i];\n var birthday \u003d contact.getDates(ContactsApp.Field.BIRTHDAY)[0];\n \n if(birthday) {\n var birthdayDate \u003d new Date(now.getYear(), getMonthNumber(birthday.getMonth()), birthday.getDay(), now.getHours(), now.getMinutes(), now.getSeconds(), now.getMilliseconds()); \n \n var message \u003d \u0027\u0027\n var shouldSendMail \u003d false;\n \n if(isSameDay(birthdayDate, now)) {\n shouldSendMail \u003d true;\n message \u003d getBirthdayMessage(birthday, contact, 0);\n } else if(hasBirthdayInDays(birthdayDate, getDaysInAdvance())) {\n shouldSendMail \u003d true;\n message \u003d getBirthdayMessage(birthday, contact, getDaysInAdvance());\n }\n \n if(shouldSendMail) {\n GmailApp.sendEmail(Session.getActiveUser().getEmail(), message, \"\");\n } \n }\n }\n}\n\nfunction getBirthdayMessage(birthday, contact, inDays) {\n var birthYear \u003d birthday.getYear();\n inDays \u003d parseFloat(inDays); // don\u0027t show number with .0\n var message \u003d contact.getFullName() + \u0027\\\u0027s \u0027;\n // some contacts don\u0027t have year entry\n if(birthYear) {\n var age \u003d parseFloat(new Date().getYear() - birthYear);\n message +\u003d age + \u0027th \u0027; \n }\n message +\u003d \u0027birthday is \u0027;\n \n if(inDays \u003d\u003d\u003d 0) {\n message +\u003d \u0027today\u0027; \n } else {\n message +\u003d \u0027in \u0027 + inDays + \u0027 days\u0027;\n }\n return message;\n}\n\nfunction isSameDay(birthdayDate, date) {\n return birthdayDate.getMonth() \u003d\u003d\u003d date.getMonth() \u0026\u0026 birthdayDate.getDate() \u003d\u003d\u003d date.getDate();\n}\n\nfunction hasBirthdayInDays(birthdayDate, days) {\n var newDate \u003d new Date(Date.now() + days * 60 * 60 * 24 * 1000);\n return isSameDay(birthdayDate, newDate); \n}\n\n// number of days the user wants to get the first reminder mail before the birthday\nvar DAYS_IN_ADVANCE_KEY \u003d \"DAYS_IN_ADVANCE_KEY\";\n\nfunction setDaysInAdvance(numb) {\n Logger.log(\u0027set days in advance to \u0027 + numb);\n PropertiesService.getScriptProperties().setProperty(DAYS_IN_ADVANCE_KEY, numb);\n}\n\nfunction getDaysInAdvance() {\n return PropertiesService.getScriptProperties().getProperty(DAYS_IN_ADVANCE_KEY);\n}\n\n// google has month enum and date uses 0-11 for months\nfunction getMonthNumber(name) {\n return monthNameToNumber[name];\n}\n\nvar monthNameToNumber \u003d {\n\"JANUARY\": 0,\n\"FEBRUARY\": 1,\n\"MARCH\": 2,\n\"APRIL\": 3,\n\"MAY\": 4,\n\"JUNE\": 5,\n\"JULY\": 6,\n\"AUGUST\": 7,\n\"SEPTEMBER\": 8,\n\"OCTOBER\": 9,\n\"NOVEMBER\": 10,\n\"DECEMBER\": 11,\n}\n\n"},{"id":"f45f6751-1fcd-432a-b8cd-18539f08c192","name":"index","type":"html","source":"\u003c!DOCTYPE html\u003e\n\u003chtml\u003e\n \u003chead\u003e\n \u003cbase target\u003d\"_top\"\u003e\n \u003c/head\u003e\n \u003cbody\u003e\n \u003cdiv\u003e\n Send birthday reminder E-Mails \u003cinput value\u003d\"7\" style\u003d\"width: 20px; margin: 0px 5px 0px 5px\"id\u003d\"days-advance\"\u003edays in advance and on birthday\n \u003c/div\u003e\n \u003cdiv\u003e\u003cbutton onclick\u003d\"setTrigger()\" style\u003d\"padding: 10px 20px 10px 20px; margin: 15px 0px 0px 15px\"\u003e\u0026nbsp\u0026nbspSubscribe\u0026nbsp\u0026nbsp\u003c/button\u003e\n \u003c/div\u003e\n \u003cdiv\u003e\u003cbutton onclick\u003d\"clearTrigger()\" style\u003d\"padding: 10px 20px 10px 20px; margin: 15px 0px 0px 15px\"\u003eUnsubscribe\u003c/button\u003e\n \u003c/div\u003e\n \u003c/body\u003e\n \u003cscript\u003e\n function setTrigger() {\n var daysInAdvance \u003d document.getElementById(\u0027days-advance\u0027).value;\n alert(\u0027Subscription succesful.\\nYou will be notified \u0027 + daysInAdvance + \u0027 days in advance and on birthday.\u0027);\n google.script.run.setDaysInAdvance(daysInAdvance);\n google.script.run.setTrigger();\n }\n \n function clearTrigger() {\n alert(\u0027You will no longer receive birthday reminder mails\u0027);\n google.script.run.clearTrigger();\n \n }\n \u003c/script\u003e\n\u003c/html\u003e\n\n\n"},{"id":"0a89c67b-d9f2-4308-8e2e-65f2979dbcf6","name":"triggers","type":"server_js","source":"function getTomorrowMidnight() {\n var tomorrow \u003d new Date();\n tomorrow.setDate(new Date().getDate() + 1);\n tomorrow.setHours(0);\n return tomorrow;\n}\n\nfunction setTrigger() {\n clearTrigger();\n var tomorrowMidnight \u003d getTomorrowMidnight();\n // delay setting the daily trigger until tomorrow and send mails right after subscribing, so user gets instant feedback and sees that the script is working\n ScriptApp.newTrigger(\u0027startDailyTrigger\u0027)\n .timeBased()\n .at(tomorrowMidnight)\n .create();\n \n Logger.log(\u0027will start daily triggers at \u0027 + tomorrowMidnight.toLocaleString());\n sendReminderEmails();\n}\n\nfunction startDailyTrigger() {\n Logger.log(\u0027started daily trigger\u0027);\n ScriptApp.newTrigger(\u0027sendReminderEmails\u0027)\n .timeBased()\n .atHour(6)\n .everyDays(1)\n .create(); \n}\n\nfunction clearTrigger() {\n Logger.log(\u0027cleared all triggers\u0027);\n ScriptApp.getProjectTriggers().forEach(function(el) { ScriptApp.deleteTrigger(el)}); \n}"}]}