Last active
November 1, 2018 14:28
-
-
Save alexanderchuranov/a29a4bd430951989346adb0508ceab2a to your computer and use it in GitHub Desktop.
Sync your work Google Calendar with your personal Google Calendar by blocking time in Personal Calendar whenever there is an event in Work Calendar.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // This script solves the problem of bidirectional syncing of | |
| // work and personal Google Calendars. | |
| // | |
| // Google Calendar is amazing software, but we often want to book some | |
| // time for personal use (e.g. appointment to a doctor), invite friends | |
| // and relatives (e.g. visiting with a spouse) and have this time booked | |
| // at work calendar too (so that no-one schedules a 1:1 meeting for that | |
| // interval). This is often done by inviting your work account to the | |
| // personal event. | |
| // | |
| // However, this does not solve a problem of booking time in your personal | |
| // calendar (so that your spouse does schedule a fun event for that time) | |
| // whenever you have a meeting at work. Inviting your personal account to | |
| // work events is impractical (there are too many) and is often against | |
| // the company policy (risk of leaking data). | |
| // | |
| // This script relies on your work Google Calendar to be shared with your | |
| // personal account in the "see free/busy" mode. You paste the code as | |
| // Google Apps Script (script.google.com), change the SOURCE_CALENDAR_ID, | |
| // TITLE_TO_EXCLUDE, and GENERATED_EVENT_TITLE, then schedule the sync3days() | |
| // and sync30days() functions to run periodically, blocking | |
| // the time in your personal calendar as needed. | |
| // | |
| // Obviously, the script can be used for bi-directional syncing provided you | |
| // set TITLE_TO_EXCLUDE and GENERATED_EVENT_TITLE so that what one account uses | |
| // as EXCLUDE is GENERATED for another. | |
| // | |
| // Copyright 2018 Alexander Churanov | |
| // | |
| // Permission is hereby granted, free of charge, to any person obtaining | |
| // a copy of this software and associated documentation files (the "Software"), | |
| // to deal in the Software without restriction, including without limitation | |
| // the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
| // and/or sell copies of the Software, and to permit persons to whom | |
| // the Software is furnished to do so, subject to the following conditions: | |
| // | |
| // The above copyright notice and this permission notice shall be included | |
| // in all copies or substantial portions of the Software. | |
| // | |
| // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
| // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | |
| // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
| // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | |
| // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT | |
| // OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE | |
| // OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
| // PARAMETERS | |
| // | |
| // The Google Calendar to pull events from (the "source calendar"). | |
| var SOURCE_CALENDAR_ID = "user@gmail.com"; | |
| // Do not import events from the source calendar if they have this title. | |
| // Useful for bi-directional sync of work/personal calendars. | |
| var TITLE_TO_EXCLUDE = "Not at Work"; | |
| // Use this title when blocking time in the destination calendar. | |
| var GENERATED_EVENT_TITLE = "Focused at Work"; | |
| // CODE | |
| // | |
| // Returns a text string represesting the 'date' in %Y-%m-%d format. | |
| function formatDate(date) { | |
| return date.getFullYear() + '-' + (date.getMonth() + 1) + '-'+ date.getDate(); | |
| } | |
| // Returns a text string representing the 'time' in %H:%M format. | |
| function formatTime(time) { | |
| text = time.getHours() + ':' + time.getMinutes(); | |
| if (text[text.length - 1] === '0' && text[text.length - 2] === ':') { | |
| text = text + '0'; | |
| } | |
| return text; | |
| } | |
| // Returns true if the two objects represent the same point in time, | |
| // false otherwise. | |
| function equalTimes(lhs, rhs) { | |
| return lhs.getTime() === rhs.getTime(); | |
| } | |
| // Removes events that have exact title of GENERATED_EVENT_TITLE and | |
| // happen on day 'day from the 'calendar. | |
| // Types of arguments: | |
| // calendar - Calendar | |
| // day - Date | |
| function removeGeneratedEvents(calendar, day) { | |
| Logger.log('Removing generated events from calendar "' + | |
| calendar.getName() + '".'); | |
| var events = calendar.getEventsForDay(day); | |
| for (i in events) { | |
| var event = events[i]; | |
| if (event.getTitle() === GENERATED_EVENT_TITLE) { | |
| var start_str = formatTime(event.getStartTime()); | |
| var end_str = formatTime(event.getEndTime()); | |
| Logger.log("Removing event: " + start_str + " – " + end_str); | |
| event.deleteEvent(); | |
| } | |
| } | |
| } | |
| // Types of arguments: | |
| // dest_calendar - Calendar | |
| // start_time - Date | |
| // end_time - Date | |
| function bookTime(dest_calendar, start_time, end_time) { | |
| var start_str = formatTime(start_time); | |
| var end_str = formatTime(end_time); | |
| Logger.log('Booking time from ' + start_str + ' to ' + end_str); | |
| dest_calendar | |
| .createEvent(GENERATED_EVENT_TITLE, start_time, end_time) | |
| .removeAllReminders(); | |
| } | |
| // Returns true if the event is deemed worthy of importing based on: | |
| // 1. The properties of the 'event' (all-day or marker title of the event). | |
| // 2. Whether the user rejected the invitation or not. | |
| function isEligibleForImport(event, dest_events) { | |
| if (event.isAllDayEvent()) { | |
| return false; | |
| } | |
| if (event.getTitle() == TITLE_TO_EXCLUDE) { | |
| return false; | |
| } | |
| if (event.getMyStatus() == CalendarApp.GuestStatus.NO) { | |
| return false; | |
| } | |
| return true; | |
| } | |
| // Searches for events in the 'source' calendar within the | |
| // [today, today + window_days) window and books the corresponding time | |
| // in the 'dest' calendar for eligible events from the source calendar. | |
| // Types of arguments: | |
| // source_calendar - Calendar | |
| // dest_calendar - Calendar | |
| // window_days - Number | |
| function syncCalendars(source_calendar, dest_calendar, window_days) { | |
| var start_day = new Date(); | |
| Logger.log('Using window of ' + window_days + | |
| ' days starting from ' + formatDate(start_day)); | |
| for (var day_number = 0; day_number < window_days; ++day_number) { | |
| var day = new Date(); | |
| day.setDate(start_day.getDate() + day_number); | |
| Logger.log('Processing day: ' + formatDate(day)); | |
| removeGeneratedEvents(dest_calendar, day); | |
| var source_events = source_calendar.getEventsForDay(day); | |
| var dest_events = dest_calendar.getEventsForDay(day); | |
| for (i in source_events) { | |
| var event = source_events[i]; | |
| if (isEligibleForImport(event, dest_events)) { | |
| bookTime(dest_calendar, event.getStartTime(), event.getEndTime()); | |
| } | |
| } | |
| } | |
| } | |
| function syncNDays(num_days_to_sync) { | |
| var source_calendar = CalendarApp.getCalendarById(SOURCE_CALENDAR_ID); | |
| if (source_calendar === null) { | |
| Logger.log("ERROR: Can not access source calendar '" + | |
| SOURCE_CALENDAR_ID + "'."); | |
| return; | |
| } | |
| var dest_calendar = CalendarApp.getDefaultCalendar(); | |
| syncCalendars(source_calendar, dest_calendar, num_days_to_sync); | |
| } | |
| // The functions to schedule in "Current project's triggers". | |
| function sync3days() { | |
| syncNDays(3); | |
| } | |
| function sync30days() { | |
| syncNDays(30); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment