Last active
April 17, 2023 00:19
-
-
Save ttsukagoshi/81cfd06b0210431d1a9eb0281494096b to your computer and use it in GitHub Desktop.
Remind myself of the events scheduled tomorrow in my Google Calendar. The notification will come to my Gmail.
This file contains hidden or 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
const DRY_RUN = false; | |
function getEventsForTomorrow() { | |
const timeZone = Session.getScriptTimeZone(); | |
let tomorrow = new Date(); | |
tomorrow.setDate(tomorrow.getDate() + 1); // get date for tomorrow | |
const tomorrowStr = Utilities.formatDate(tomorrow, timeZone, 'yyyy-MM-dd'); | |
// Get the events | |
const calendars = CalendarApp.getAllCalendars(); // get all calendars that the user owns or is subscribed to | |
const tomorrowEvents = []; | |
calendars.forEach((calendar) => { | |
let events = calendar.getEventsForDay(tomorrow); // get all events for tomorrow | |
events.forEach((event) => { | |
const eventStartDateTime = Utilities.formatDate(event.getStartTime(), timeZone, 'yyyy-MM-dd HH:mm'); | |
const eventEndDateTime = Utilities.formatDate(event.getEndTime(), timeZone, 'yyyy-MM-dd HH:mm'); | |
const eventStartTime = eventStartDateTime.slice(0, 10) === tomorrowStr | |
? eventStartDateTime.slice(11) | |
: eventStartDateTime; | |
const eventEndTime = eventEndDateTime.slice(0, 10) === tomorrowStr | |
? eventEndDateTime.slice(11) | |
: eventEndDateTime; | |
const eventObj = { | |
isAllDay: event.isAllDayEvent(), | |
start: eventStartTime, | |
end: eventEndTime, | |
title: event.getTitle() | |
} | |
// console.log(JSON.stringify(eventObj)); // log events | |
tomorrowEvents.push(eventObj); | |
}); | |
}); | |
// Sort the events | |
// console.log(JSON.stringify(tomorrowEvents)); // log | |
const tomorrowEventsSorted = tomorrowEvents.sort((a, b) => { | |
if (a.start > b.start) { | |
// Sort by start date & time ascending | |
return 1; | |
} else if (a.start < b.start) { | |
return -1; | |
} else { | |
// If the start time is the same, sort by end time ascending | |
if (a.end > b.end) { | |
return 1; | |
} else if (a.end < b.end) { | |
return -1; | |
} else { | |
return 0; | |
} | |
} | |
}); | |
// console.log(JSON.stringify(tomorrowEventsSorted)); // log | |
// Send email to myself to remind the events | |
const mailTo = Session.getActiveUser().getEmail(); | |
const mailSubject = 'Tomorrow\'s Events'; | |
const mailBody = `Your schedule for tomorrow:\n\n${tomorrowEventsSorted.map((eventObj) => `${eventObj.start} - ${eventObj.end}: ${eventObj.title}`).join('\n')}\n\n---\nThis reminder is sent automatically by the following script file:\nhttps://script.google.com/home/projects/${ScriptApp.getScriptId()}/edit`; | |
if (!DRY_RUN) { | |
MailApp.sendEmail( | |
mailTo, | |
mailSubject, | |
mailBody | |
) | |
} else { | |
console.log(mailTo, mailSubject, mailBody); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment