Skip to content

Instantly share code, notes, and snippets.

@cjwinchester
Created December 13, 2017 05:04
Show Gist options
  • Save cjwinchester/0d79fd98c9453a40fb9047eddf199166 to your computer and use it in GitHub Desktop.
Save cjwinchester/0d79fd98c9453a40fb9047eddf199166 to your computer and use it in GitHub Desktop.
a google apps script that runs against a spreadsheet every week and emails me if peeps got a bday coming up
/*
a script that runs every sunday to let me know of birthdays coming up that week
most of the math happens in the spreadsheet
--> example: https://docs.google.com/spreadsheets/d/1iXcg0THKuQwosKiD5syktAmGA390Gu8OSFs5iKM1EwQ/edit?usp=sharing
*/
function birthdayNotifier() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getDataRange();
var rows = selection.getNumRows();
var days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
var recipients = ['[email protected]']
var bdays_this_week = [];
var str = '';
for (var row = 2; row <= rows; row++) {
var is_this_week = range.getCell(row, 8).getValue();
var days_till = range.getCell(row, 7).getValue();
if (is_this_week) {
var age = range.getCell(row, 4).getValue() + 1;
if (days_till === 0) {
age = range.getCell(row, 4).getValue();
}
bdays_this_week.push({
'name': range.getCell(row, 1).getValue() + ' ' + range.getCell(row, 2).getValue(),
'age_on_bday': age,
'day_of_week': days[range.getCell(row, 5).getValue().getDay()]
})
}
}
if (bdays_this_week.length > 0) {
var subject = 'Birthdays this week (' + bdays_this_week.length + ')';
if (bdays_this_week.length === 1) {
subject = 'Birthday this week';
}
str += '<h4>Birthdays this week (' + bdays_this_week.length + ')</h4><ul>';
for (var i = 0; i < bdays_this_week.length; i++) {
str += '<li>' + bdays_this_week[i].name + ' (' + bdays_this_week[i].age_on_bday + '): ' + bdays_this_week[i].day_of_week + '</li>';
}
str += '</ul>';
MailApp.sendEmail({
to: recipients.join(','),
subject: subject,
htmlBody: str
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment