-
-
Save kevincoleman/7169b6008064dab26bb3812cc5102b7c to your computer and use it in GitHub Desktop.
Auto Block Time on Primary Google Calendar from Events on Secondary Calendar - Instructions: https://medium.com/@willroman/auto-block-time-on-your-work-google-calendar-for-your-personal-events-2a752ae91dab
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() { | |
var id="XXXXXXXX"; // CHANGE - id of the secondary calendar to pull events from | |
var secondaryCal=CalendarApp.getCalendarById(id); | |
var today=new Date(); | |
var enddate=new Date(); | |
enddate.setDate(today.getDate()+30); // how many days in advance to monitor and block off time | |
var easement = 30; // set easement, in case you need travel time, or any other buffer | |
var secondaryEvents=secondaryCal.getEvents(today,enddate); | |
var primaryCal=CalendarApp.getDefaultCalendar(); | |
var primaryEvents=primaryCal.getEvents(today,enddate); | |
var stat=1; | |
var evi, existingEvents; | |
for (ev in secondaryEvents) | |
{ | |
stat=1; | |
evi=secondaryEvents[ev]; | |
var blockoutStart = new Date(evi.getStartTime()); | |
blockoutStart.setMinutes(blockoutStart.getMinutes() - easement); | |
var blockoutEnd = new Date(evi.getEndTime()); | |
blockoutEnd.setMinutes(blockoutEnd.getMinutes() + easement); | |
for (existingEvents in primaryEvents) // if the secondary event has already been blocked in the primary calendar, ignore it | |
{ | |
if ((primaryEvents[existingEvents].getStartTime().getTime()==blockoutStart.getTime()) && (primaryEvents[existingEvents].getEndTime().getTime()==blockoutEnd.getTime())) | |
{ | |
stat=0; | |
break; | |
} | |
} | |
if (stat==0) continue; | |
var d = evi.getStartTime(); | |
var n = d.getDay(); | |
if (evi.isAllDayEvent()) continue; | |
if (n==1 || n==2 || n==3 || n==4 || n==5) // skip weekends. Delete this if you want to include weekends | |
{ | |
var newEvent = primaryCal.createEvent('Unavailable',blockoutStart,blockoutEnd); // change the Booked text to whatever you would like your merged event titles to be | |
// alternative version below that copies the exact secondary event information into the primary calendar event | |
// var newEvent = primaryCal.createEvent(evi.getTitle(),evi.getStartTime(),evi.getEndTime(), {location: evi.getLocation(), description: evi.getDescription()}); | |
newEvent.removeAllReminders(); // so you don't get double notifications. Delete this if you want to keep the default reminders for your newly created primary calendar events | |
} | |
} | |
} | |
function myFunction() {sync()} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment