-
-
Save ttrahan/a88febc0538315b05346f4e3b35997f2 to your computer and use it in GitHub Desktop.
function sync() { | |
var id="XXXXXXXXXX"; // CHANGE - id of the secondary calendar to pull events from | |
var today=new Date(); | |
var enddate=new Date(); | |
enddate.setDate(today.getDate()+7); // how many days in advance to monitor and block off time | |
var secondaryCal=CalendarApp.getCalendarById(id); | |
var secondaryEvents=secondaryCal.getEvents(today,enddate); | |
var primaryCal=CalendarApp.getDefaultCalendar(); | |
var primaryEvents=primaryCal.getEvents(today,enddate); // all primary calendar events | |
var primaryEventTitle="Personal appt"; // update this to the text you'd like to appear in the new events created in primary calendar | |
var stat=1; | |
var evi, existingEvent; | |
var primaryEventsFiltered = []; // to contain primary calendar events that were previously created from secondary calendar | |
var primaryEventsUpdated = []; // to contain primary calendar events that were updated from secondary calendar | |
var primaryEventsCreated = []; // to contain primary calendar events that were created from secondary calendar | |
var primaryEventsDeleted = []; // to contain primary calendar events previously created that have been deleted from secondary calendar | |
Logger.log('Number of primaryEvents: ' + primaryEvents.length); | |
Logger.log('Number of secondaryEvents: ' + secondaryEvents.length); | |
// create filtered list of existing primary calendar events that were previously created from the secondary calendar | |
for (pev in primaryEvents) | |
{ | |
var pEvent = primaryEvents[pev]; | |
if (pEvent.getTitle() == primaryEventTitle) | |
{ primaryEventsFiltered.push(pEvent); } | |
} | |
// process all events in secondary calendar | |
for (sev in secondaryEvents) | |
{ | |
stat=1; | |
evi=secondaryEvents[sev]; | |
// if the secondary event has already been blocked in the primary calendar, update it | |
for (existingEvent in primaryEventsFiltered) | |
{ | |
var pEvent = primaryEventsFiltered[existingEvent]; | |
var secondaryTitle = evi.getTitle(); | |
var secondaryDesc = evi.getDescription(); | |
if ((pEvent.getStartTime().getTime()==evi.getStartTime().getTime()) && (pEvent.getEndTime().getTime()==evi.getEndTime().getTime())) | |
{ | |
stat=0; | |
pEvent.setTitle(primaryEventTitle); | |
pEvent.setDescription(secondaryTitle + '\n\n' + secondaryDesc); | |
// event.setDescription(evi.getTitle() + '\n\n' + evi.getDescription()); | |
pEvent.setVisibility(CalendarApp.Visibility.PRIVATE); // set blocked time as private appointments in work calendar | |
primaryEventsUpdated.push(pEvent.getId()); | |
Logger.log('PRIMARY EVENT UPDATED' | |
+ '\nprimaryId: ' + pEvent.getId() + ' \nprimaryTitle: ' + pEvent.getTitle() + ' \nprimaryDesc: ' + pEvent.getDescription()); | |
} | |
} | |
if (stat==0) continue; | |
var d = evi.getStartTime(); | |
var n = d.getDay(); | |
if (evi.isAllDayEvent()) | |
{ | |
continue; // Do nothing if the event is an all-day or multi-day event. This script only syncs hour-based events | |
} | |
else if (n==1 || n==2 || n==3 || n==4 || n==5) // skip weekends. Delete this if you want to include weekends | |
// if the secondary event does not exist in the primary calendar, create it | |
{ | |
var newEvent = primaryCal.createEvent(primaryEventTitle,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.setDescription(evi.getTitle() + '\n\n' + evi.getDescription()); | |
newEvent.setVisibility(CalendarApp.Visibility.PRIVATE); // set blocked time as private appointments in work calendar | |
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 | |
primaryEventsCreated.push(newEvent.getId()); | |
Logger.log('PRIMARY EVENT CREATED' | |
+ '\nprimaryId: ' + newEvent.getId() + '\nprimaryTitle: ' + newEvent.getTitle() + '\nprimaryDesc ' + newEvent.getDescription() + '\n'); | |
} | |
} | |
// if a primary event previously created no longer exists in the secondary calendar, delete it | |
for (pev in primaryEventsFiltered) | |
{ | |
var pevIsUpdatedIndex = primaryEventsUpdated.indexOf(primaryEventsFiltered[pev].getId()); | |
if (pevIsUpdatedIndex == -1) | |
{ | |
var pevIdToDelete = primaryEventsFiltered[pev].getId(); | |
Logger.log(pevIdToDelete + ' deleted'); | |
primaryEventsDeleted.push(pevIdToDelete); | |
primaryEventsFiltered[pev].deleteEvent(); | |
} | |
} | |
Logger.log('Primary events previously created: ' + primaryEventsFiltered.length); | |
Logger.log('Primary events updated: ' + primaryEventsUpdated.length); | |
Logger.log('Primary events deleted: ' + primaryEventsDeleted.length); | |
Logger.log('Primary events created: ' + primaryEventsCreated.length); | |
} |
How can I sync all-day or multi-day events?
Utilities.sleep(sec1000); // added to line 39 to prevent "too many updates" error on a busy calendar. I'm no coder, probably a better method.
@elabrant
Close. It should be this which worked great for me when inserted to line 40:
Utilities.sleep(1000); // added to prevent "too many updates" error on a busy calendar
@jboudreau77 I figured out why this is happening. On line 28 when the primaryEvents array is being built, it's looking for events with the primaryEventTitle. If you're like me, and changed the code on lines 72 and 74, then there will be nothing in the primaryEvents array, because each event has a different name.
@ttrahan got a quick fix? I'm not the best coder, but I can give it a shot.
@jboudreau77 I think I got a fix working here: https://github.com/ReaganHelms/calendar-sync/tree/main
Hello! I did a tiny patch to add support for multiple secondary calendars. Feel free to ignore it. https://gist.github.com/vaurora/a6deb299574b3c23652d776915025b9e
I have updated this script to also make it much more efficient (helping the planet just a little), and configurable.
Is there a way to alter this to include multi-day events? (for example, personal holidays)
Additionally is there a way to omit the personal events title and notes from being synced to the "busy" event?
Hi there. Thanks for this. a few questions:
1 - when i set the trigger to "sync", it creates double events. when i set it to hourly update it does not. is there any way to get it working wtih sync trigger without the double events?
2 - i'd like it to only create primary calendar events that occur after 9am on weekdays. is there any way to adjust it for this?
3 - I'd like it to be able to create primary calendar events of all day events. is there a way to enable all day events? thanks so much!
Utilities.sleep(sec1000); // added to line 39 to prevent "too many updates" error on a busy calendar. I'm no coder, probably a better method.