Skip to content

Instantly share code, notes, and snippets.

@cassus
Last active March 12, 2024 08:27
Show Gist options
  • Save cassus/3f7e38b9c8115b9432aee42c083de59e to your computer and use it in GitHub Desktop.
Save cassus/3f7e38b9c8115b9432aee42c083de59e to your computer and use it in GitHub Desktop.
Automatically add/remove guests on Google Calendar Events

Automatically add/remove guests on Google Calendar Events

This Google Apps Script automatically manages guests for events in a specified Google Calendar. I use it to sync all events I'm invited to with my company calendar to my personal calendar, so my free-busy availability is up to date there. It also attempts (I might not have permission) to remove my company calendar from the guest list, so I'm not duplicated on the events.

Features

  • Automatically adds a predefined guest to all events within a specified time frame if they're not already included.
  • Removes a specific guest from all events within the same time frame if they're currently included.
  • Handles recurring events efficiently by processing the entire series rather than individual occurrences, preventing duplicate processing.

How to Use

  1. Set up your Google Apps Script project: Copy this script into a new Google Apps Script project linked to your Google Calendar.
  2. Customize your calendar and guest settings: Replace hello@example.com with the ID of the Google Calendar you wish to manage. Update the guest email addresses in the script (guest.to.add@example.com for adding, guest.to.remove@example.com for removing) to your desired guests.
  3. Schedule execution (optional): For automated management, set up a calendar chage trigger in the Apps Script editor to run the onCalendarChange function.

Notes

  • Test the script with a few events before scheduling regular runs to ensure it behaves as expected.
// Create a trigger on this
function onCalendarChange(triggerEvent) {
Logger.log("triggerEvent:", triggerEvent);
Main()
}
function Main() {
var today = new Date();
var endDate = new Date();
endDate.setDate(endDate.getDate() + 30);
Logger.log(today + " " + endDate);
var calendar = CalendarApp.getCalendarById("hello@example.com")
const processedEventIds = new Set()
var events = calendar.getEvents(today, endDate);
for (let event of events) {
if(event.isRecurringEvent()) {
event = event.getEventSeries()
}
// Skip processing the same recurring event multiple times
if(processedEventIds.has(event.getId())) {
Logger.log("Skipped: " + event.getTitle());
continue
}
processedEventIds.add(event.getId())
if(!event.getGuestByEmail("guest.to.add@example.com")) {
Logger.log(`adding guest.to.add@example.com to "${event.getTitle()}"`);
event.addGuest("guest.to.add@example.com");
}
if(event.getGuestByEmail("guest.to.remove@example.com")) {
Logger.log(`remove guest.to.remove@example.com to "${event.getTitle()}"`);
event.removeGuest("guest.to.remove@example.com");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment