Skip to content

Instantly share code, notes, and snippets.

@ttsukagoshi
Last active January 28, 2025 15:26
Show Gist options
  • Save ttsukagoshi/54568742b8b3c3d4e7c0f5e0a88ba18f to your computer and use it in GitHub Desktop.
Save ttsukagoshi/54568742b8b3c3d4e7c0f5e0a88ba18f to your computer and use it in GitHub Desktop.
A Google Apps Script to remind myself of the events scheduled tomorrow in my Google Calendar. The notification will be sent via a Google Chat Space.
// Prerequisites:
// * You must have a Business or Enterprise Google Workspace account with access to Google Chat.
// * Your Google Workspace organization must let users add and use incoming webhooks.
// See: https://developers.google.com/workspace/chat/quickstart/webhooks
//
// Steps:
// 1. Create a standalone Google Apps Script Project.
// 2. Create a webhook URL in the Google Chat Space by following Google's official documentation above.
// 3. Save the webhook URL to as a Script Property and name it `chatWebHookUrl`
// 4. Copy & Paste the following script to your Apps Script project.
// 5. Set the Apps Script's time-based trigger to suit your needs.
const DRY_RUN = true; // Switch this to false when you're ready to use it.
/**
* The main function to be executed on time triggers.
*/
function getEventsForTomorrow() {
// Get date for tomorrow
const timeZone = Session.getScriptTimeZone();
let tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
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) => {
// For each event, create a simplified object...
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()
}
// ... and append it to `tomorrowEvents`
tomorrowEvents.push(eventObj);
});
});
// Sort the events
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;
}
}
});
// The chat message body
const messageBody = `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) {
// Send Google Chat message to myself to remind the events
UrlFetchApp.fetch(
PropertiesService.getScriptProperties().getProperty('chatWebHookUrl'),
{
method: 'POST',
contentType: 'application/json; charset=UTF-8',
payload: JSON.stringify({
text: messageBody,
}),
}
);
console.info(`Chat message sent: ${messageBody}`)
} else {
console.info('Running on dry-run mode')
console.info(messageBody)
}
}
/**
* MIT License
Copyright (c) 2025 TSUKAGOSHI Taro
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment