Skip to content

Instantly share code, notes, and snippets.

@wroman
Last active July 31, 2023 21:04
Show Gist options
  • Save wroman/388d4b575db03e917111666885637c7a to your computer and use it in GitHub Desktop.
Save wroman/388d4b575db03e917111666885637c7a 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
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
}
}
}
@jegirard
Copy link

jegirard commented May 8, 2018

Thanks so much for doing this!

When I bump the range up to 30 days, I'm getting what looks like a rate error (too many changes to calendar in short period of time) even if I put a utilities.sleep(2500) in the update loop.

When dropping it back to 7 days, it runs fine, but just noticed this summary written to the log file, even if I run the script twice in rapid succession:

[18-05-07 20:41:48:119 PDT] Primary events previously created: 27
[18-05-07 20:41:48:120 PDT] Primary events updated: 27
[18-05-07 20:41:48:121 PDT] Primary events deleted: 0
[18-05-07 20:41:48:121 PDT] Primary events created: 0

I had assumed that the script would skip previously created primary events that didn't need to be updated (events with no changes), but this appear to be updating all previous events, whether they've changed or not. I'm wondering if that's the intention, and if it's what might be responsible for the rate issue when I bump up the date range?

Curious to know your thoughts, and whether there is an elegant way to restrict updates to only events that have changed (my first guess is that there isn't unless there's a lastUpdate property or something that could be used).

@wroman
Copy link
Author

wroman commented Aug 24, 2018

@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)

@harrisonbagdan
Copy link

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment