|
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(); |
|
} |