Created
April 1, 2020 16:03
-
-
Save evandavis/fa768978b1a5f2436a8df2098fb29f5e to your computer and use it in GitHub Desktop.
Calendar Blocking GScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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