Skip to content

Instantly share code, notes, and snippets.

@evandavis
Created April 1, 2020 16:03
Show Gist options
  • Save evandavis/fa768978b1a5f2436a8df2098fb29f5e to your computer and use it in GitHub Desktop.
Save evandavis/fa768978b1a5f2436a8df2098fb29f5e to your computer and use it in GitHub Desktop.
Calendar Blocking GScript
function sync() {
// gcal IDs: email address if using default calendar
const SOURCE_ID = "XXXXXX";
const DEST_ID = "YYYYYY";
// SETTINGS
// how many days out to clone events
const ADVANCE_DAYS = 5; // integer
// title of cloned events
const TITLE = "Busy 📵";
const sourceCal = CalendarApp.getCalendarById(SOURCE_ID);
const destCal = CalendarApp.getCalendarById(DEST_ID);
const now = new Date();
const enddate = new Date(now.getTime() + ADVANCE_DAYS * 24 * 60 * 60 * 1000);
// events from source calendar, with all-day events removed
const sourceEvents = sourceCal
.getEvents(now, enddate)
.filter(ev => !ev.isAllDayEvent());
// event blocks already on work calendar
const destEvents = dest.getEvents(now, enddate, { search: TITLE });
// add blocks when one does not yet exist
sourceEvents.forEach(event => {
if (
!destEvents.some(
block =>
event.getStartTime().getTime() === block.getStartTime().getTime() &&
event.getStartTime().getTime() === block.getStartTime().getTime()
)
) {
destCal
.createEvent(TITLE, event.getStartTime(), event.getEndTime())
.removeAllReminders();
}
});
// delete blocks when original event is removed
destEvents.forEach(block => {
if (
!sourceEvents.some(
event =>
event.getStartTime().getTime() === block.getStartTime().getTime() &&
event.getStartTime().getTime() === block.getStartTime().getTime()
)
) {
block.deleteEvent();
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment