google-apps-scriptcalendargoogle-calendar-api

How to return if Google Calendar Time or Calendar Event is Busy?


I am trying to determine if a calendar has availability during a specific time. Specifically, when a calendar event is manually created, there's a checkbox for busy or free:

Image of Setting

I would like to test if the calendar is free or not. I assumed this would have been a property in the calendar itself, but I do not see one in the google calendar classes. (I was looking for some property along the lines of 'isBusy').

My next step would be to test if there are events at the requested time, and if there are, I again don't see a method to test if an event's setting is busy or not. Does anyone know how to do this?

Here's what I have:

function getAvailability(startTime,EndTime){
  var myCalendar = CalendarApp.getDefaultCalendar();

  //sample date to test availability 
  var aDate = new Date(2020,3,19);
  var dayEvents = myCalendar.getEventsForDay(aDate);

  //specific event captured:
  var myEvent = dayEvents[0];
  Logger.log(myEvent.getDescription());
 //here is where I don't know what to do. 
 //How can I return if during this event's time if calendar is free?

}

Solution

  • You need to use the FreeBusy object to check this detail.

    This can be done by creating a query similar to:

        var check = {
     items: [{id: calendarId, busy: 'Active'}],
     timeMax: "2014-09-09T21:00:31-00:00",
     timeMin: "2014-09-09T17:00:31-00:00"
    };
    

    and using the query:

    var response = Calendar.Freebusy.query(check);
    

    to get the details you have asked to check.

    API Documentation for FreeBusy can be found here: https://developers.google.com/calendar/v3/reference/freebusy