Skip to content

Instantly share code, notes, and snippets.

@tizmagik
Last active September 10, 2024 17:22
Show Gist options
  • Save tizmagik/51d2bd0dd61ebb6fb2238d52e0ca304d to your computer and use it in GitHub Desktop.
Save tizmagik/51d2bd0dd61ebb6fb2238d52e0ca304d to your computer and use it in GitHub Desktop.
syncPersonalToWorkCalendar() Google Apps Script

syncPersonalToWorkCalendar()

Use this to block off time in your Work calendar for events in your Personal Calendar

Installation

  1. Go to Google Apps Script and create a new project
  2. Paste in this code
  3. Update the personalCalendarId and workCalendarId emails
  4. Select setupTrigger() and hit Run in the Apps Script editor. This will setup a time-based recurring trigger.
  5. Done!

If you ever need to, you can delete all synced Personal Events by running the deletePersonalEvents() function.

const personalCalendarId = "<yourpersonalemail>@gmail.com";
const workCalendarId = "<yourworkemail>@yourjob.com";
const personalCalendar = CalendarApp.getCalendarById(personalCalendarId);
const workCalendar = CalendarApp.getCalendarById(workCalendarId);
const DAYS_TO_SYNC = 30;
const RUN_EVERY_MINUTES = 1;
const now = new Date();
const future = new Date();
future.setDate(future.getDate() + DAYS_TO_SYNC); // Sync events for the next N days
/**
* Syncs personal events to the work calendar
*/
function syncPersonalToWorkCalendar() {
const getRecurrenceId = (event) => {
return event.isRecurringEvent() ? event.getStartTime().toISOString() : "";
};
const updateWorkEvent = (workEvent, personalEvent) => {
if (
workEvent.getStartTime().getTime() !==
personalEvent.getStartTime().getTime() ||
workEvent.getEndTime().getTime() !== personalEvent.getEndTime().getTime()
) {
workEvent.setTime(
personalEvent.getStartTime(),
personalEvent.getEndTime()
);
}
// Update other properties if needed
};
const createWorkEvent = (personalEvent) => {
const personalEventId = personalEvent.getId();
const recurrenceId = getRecurrenceId(personalEvent);
workCalendar
.createEvent(
"Personal Event",
personalEvent.getStartTime(),
personalEvent.getEndTime(),
{
description: `This time is blocked for a personal event. (ID: ${personalEventId}${recurrenceId})`,
}
)
.setVisibility(CalendarApp.Visibility.PRIVATE);
};
const personalEvents = personalCalendar.getEvents(now, future);
const workEvents = workCalendar.getEvents(now, future, {
search: "Personal Event",
});
const workEventsMap = {};
// Create a map of existing work events
workEvents.forEach((workEvent) => {
const description = workEvent.getDescription();
const match = description.match(/\(ID: (.*?)\)/);
if (match) {
const personalEventId = match[1];
workEventsMap[personalEventId] = workEvent;
}
});
// Sync personal events to work calendar
personalEvents.forEach((personalEvent) => {
const personalEventId = personalEvent.getId();
const recurrenceId = getRecurrenceId(personalEvent);
const workEvent = workEventsMap[personalEventId + recurrenceId];
if (workEvent) {
// Update existing work event
updateWorkEvent(workEvent, personalEvent);
delete workEventsMap[personalEventId + recurrenceId];
} else {
// Create new work event
createWorkEvent(personalEvent);
}
});
// Delete work events that no longer have corresponding personal events
Object.values(workEventsMap).forEach((workEvent) => workEvent.deleteEvent());
}
/**
* Deletes personal events from the work calendar
*/
function deletePersonalEvents() {
const workEvents = workCalendar.getEvents(now, future, {
search: "Personal Event",
});
// Remove existing personal events from work calendar
workEvents.forEach((workEvent) => {
workEvent.deleteEvent();
});
}
/**
* Sets up a trigger to run the syncPersonalToWorkCalendar function every N minutes
*/
function setupTrigger() {
// Delete existing triggers
ScriptApp.getProjectTriggers().forEach((trigger) => {
if (trigger.getHandlerFunction() === "syncPersonalToWorkCalendar") {
ScriptApp.deleteTrigger(trigger);
}
});
// Create new trigger to run every N minutes
ScriptApp.newTrigger("syncPersonalToWorkCalendar")
.timeBased()
.everyMinutes(RUN_EVERY_MINUTES)
.create();
}
/**
* Sets up a trigger to run the syncPersonalToWorkCalendar function every N minutes
*/
function onDeployment() {
setupTrigger();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment