-
-
Save wroman/388d4b575db03e917111666885637c7a to your computer and use it in GitHub Desktop.
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 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 | |
} | |
} | |
} |
@wroman this code has literally been a game changer for my business. i have a sales company and we manage multiple gsuites for appointments at once. i have been using for 2 years now. today, for the first time i have run into an error code coming up. it looks like google apps scripts changed their platform abit, but i have never had an errror, i have 3 calendars successfully running on it now. however, i cannot successfully make new calendar connections. the error says "TypeError: Cannot call method "getEvents" of null. (line 9, file "Code") any way youd be willing to help? im a sales guy not a tech guy and i got soooo lucky finding your original article to save my life but now i am stuck and clueless and not sure what to do outside of hiring a coder for this one thing. any help is greatly appreciated. thank you
@jegirard This line should skip existing events; I'm very open to suggestions for improvements if it is not working as intended.
for (existingEvents in primaryEvents)