Oct 2023
dev
I use Google Calendar as my primary calendar app. I have multiple Google accounts, hence multiple Google Calendars. Let's say I have calendars A, B, and C. I needed events on A and B to show up as events on C.
I know there are apps that help you sync and manage multiple calendars, but I just decided I was going to use this as my journey into scripting and automation. Zapier is a no-code solution that helps you build automation between two different apps, or sometimes within apps. I tried using Zapier to automate one calendar from another and it worked with some minor inconveniences.
I used the New or Updated Event in Google Calendar to trigger a Create Detailed Event in Google Calendar
Let's just say this Zap still helps me manage Calendar A to C to date.
Full disclosure. Projects like these are quick and dirty. I don't have a ton of time to make it super clean or super efficient. I took some time late in the post to explain some drawbacks to both approaches I took and what I would do to improve it if I really wanted to.
I wrote very little Javascript within Google Apps Script which is a cloud-based scripting language for light-weight application development in the Google Workspace platform
The script mainly consists of two main functions. I wrote updateCalendar
to help check Calendar B for events and create those that don't exist as an event on Calendar C titled Ini Busy. And cleanClendar
to clean up the events no longer needed.
updateCalendar(bEvents, cEvents, cCalendar);
cleanCalendar(cEvents, bEvents, aEvents, cCalendar);
The script
It accesses three separate Google Calendars: bCalendar
, aCalendar
, and cCalendar
(which is the default calendar for the user).
It defines a date range from the current date (now) to 4 Fridays away.
It fetches all events within this date range for the three calendars.
Calls updateCalendar
to ensure that cCalendar
has all events from bCalendar
for that range.
Calls cleanCalendar
to remove any 'Ini Busy' events from cCalendar that are not in bCalendar or aCalendar.
updateCalendar():
Iterates through each event from bCalendar.
Checks if an equivalent event exists in cCalendar using checkCFor. If not, it creates one in cCalendar with the title 'Ini Busy'.
cleanCalendar():
Iterates through each 'Ini Busy' event from cCalendar.
Checks if an equivalent event exists in bCalendar or aCalendar. If not, the event is deleted from cCalendar.
Logic and Functionality:
The logic is mostly straightforward and serves its intended purpose: synchronizing events from bCalendar to cCalendar and cleaning up events that no longer have a counterpart.
Efficiency:
The loops used in the checkBFor and checkAFor functions are not efficient. For large numbers of events, this could slow down the script, as it has a time complexity of O(n^2) for both the update and clean processes.
Readability:
The function names are descriptive, which makes the code relatively easy to understand.
Some variable names, like bCheck, aCheck, and cEvent, are clear in their intentions.
Potential Improvements:
It would be more efficient to use a data structure like a set or a map to store start and end times for events. This would allow for constant-time lookups instead of linear-time lookups.
The code to calculate fouthFriday
is slightly confusing, the logic is not immediately clear.
Consistent and more detailed comments would improve readability.
The nested functions like clean, checkBFor
, and checkAFor
could be moved to the top level for clarity. Nesting functions are useful in some scenarios, but in this case, it might add unnecessary complexity.
Overall, the code achieves its objective, but there's room for optimization and clarity improvements.
Thanks for the feedback Chat. Overall, I'm pretty happy with my automation. Here are some of my drawbacks
this logic currently doesn't work when I have overlapping B events. Am I going to fix that someday? Only if it bugs me enough
I still have Zapier taking care of Calendar A to C. Am I going to add Calendar A updates to my custom automation? Only if it bugs me enough
I currently have it running every 2 hours, thanks to the executors in Google Apps Script, am I somehow wasting any resources? I don't know. It's all free right now, except Google starts complaining about running a short script every 2hours for me. I doubt they will
Here is a satisfying gif of what it looks like in action
» More posts
saxo
dev
dev
dev
dev
saxo
saxo
dev
dev
saxo
You can offer to hire me, collaborate with me, check up on me