🚫 📅 Block Busy Time in Work Google Calendar from Personal Calendar

2022-02-18

I use Google Calendar for my Personal calendar. At work we also use Google Workplace. Rather than manually booking time within my Work calendar for personal events, this script will update the Work calendar as events are added/removed from my Personal calendar. I also share my Personal calendar with my Work calendar so that I can see the personal event listed side-by-side the "blocked" work event.

function sync() {

  var id="[email protected]"; // CHANGE ME - id of the secondary calendar you want 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 look up to and therefore 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];

    // This block will check if the primary calendar already has an event at the same time as the secondary calendar and ignore the event from the secondary calendar
    for (existingEvents in primaryEvents)
      {
        if ((primaryEvents[existingEvents].getStartTime().getTime()==evi.getStartTime().getTime()) && (primaryEvents[existingEvents].getEndTime().getTime()==evi.getEndTime().getTime()))
        {
           primaryEvents[existingEvents].setColor('3')
           stat=0;
           break;
        }
      }

    if (stat==0) continue;

    var d = evi.getStartTime();
    var n = d.getDay();

    if (evi.isAllDayEvent()) continue;

    if (n >= 1 && n <= 5) // skip weekends. Delete/adjust the conditional as needed to include/exclude other days of the week (JS getDay so sunday is 0, saturday is 6)
    {
      var newEvent = primaryCal.createEvent('🚫 Busy Personal Time',evi.getStartTime(),evi.getEndTime()); // Text to display for blocked event in primary calendar
      // 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.setColor('3')
      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
    }

  }
  for (existingEvents in primaryEvents) {
    var primaryEvent = primaryEvents[existingEvents]
    if (primaryEvent.getTitle() == 'Busy Personal Time') {
      var found = false
      for (ev in secondaryEvents) {
        evi = secondaryEvents[ev]
        if (primaryEvent.getStartTime().getTime() == evi.getStartTime().getTime() && (primaryEvent.getEndTime().getTime() == evi.getEndTime().getTime())) {
          found = true
          break
        }
      }
      if (!found) {
        primaryEvent.deleteEvent()
      }
    }
  }
}

To use the script,

  1. Go to https://script.google.com (from your Work account)
  2. Select "New Project"
  3. Rename the project to "Sync Personal Calendar"
  4. Paste the code, replacing variables as appropriate for your own calendar
  5. Run the function once to grant the necessary permissions
  6. Selet the "alarm" clock icon (triggers)
  7. In the bottom right select "add trigger"
    • Function to run: sync
    • Deployment to run: Head
    • Event-source: Time driven
    • Time-based trigger: Hour
    • Hour interval: every hour
    • Failure notifications: Notify me daily

Documentation of available Event Colors - https://developers.google.com/apps-script/reference/calendar/event-color