Skip to content

Instantly share code, notes, and snippets.

@FrozenDroid
Created February 4, 2020 10:34
Show Gist options
  • Save FrozenDroid/08d4bef9669b74d690ee4c46d478e466 to your computer and use it in GitHub Desktop.
Save FrozenDroid/08d4bef9669b74d690ee4c46d478e466 to your computer and use it in GitHub Desktop.
function sync() {
var id="[email protected]"; // 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 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];
for (existingEvents in primaryEvents) // if the secondary event has already been blocked in the primary calendar, ignore it
{
if ((primaryEvents[existingEvents].getStartTime().getTime()==evi.getStartTime().getTime()) && (primaryEvents[existingEvents].getEndTime().getTime()==evi.getEndTime().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('Booked',evi.getStartTime(),evi.getEndTime()); // 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
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment