0

Context
During a form POST for booking a hotel, I first want to check if we have free rooms in our apartments. I created Resource calendars (with buildings, etc.) via the GSuite admin panel.

Now I am testing a bit with some code I found on this site. It finds the resource calenders and I simply filter them by name (to get rid of all the meeting rooms). But now I would like to "read" the calendar itself. But it seems the resourceId is not the id of the respective resource calendar.

How can I check the availability in the resource calendar?

do {
  var arguments = {
    maxResults: 200,
    pageToken: pageToken
  }; 
  calendars = AdminDirectory.Resources.Calendars.list("my_customer", arguments);
  if (calendars.items && calendars.items.length > 0) {
    for (var i = 0; i < calendars.items.length; i++) {
      var calendar = calendars.items[i];
      if (calendar.buildingId.substr(0, 9).toUpperCase() === "APARTMENT" ) {
        Logger.log('YES %s (ID: %s) (%s),', calendar.resourceId, calendar.resourceName, calendar.buildingId);  

        // 
        // The code below doesn't work (null reference). But I would to do something like this.
        //
        var resourceCalendar = CalendarApp.getCalendarById(calendar.resourceId);
        Logger.log("I found the following calendar %s ", resourceCalendar.getName() );

      }
    }
  } else {
    Logger.log('No calendars found.');
  }
  pageToken2 = calendars.nextPageToken;
} while (pageToken); 
tehhowch
  • 9,645
  • 4
  • 24
  • 42
Jeroen
  • 3
  • 2
  • Did you mean to set `pageToken` to be `calendars.nextPageToken`? or `pageToken2`, which is otherwise never used (in this code)? – tehhowch Aug 22 '18 at 15:29
  • I think you shouldn't mix the Calendar API and the CalendarApp feature, they don't use the same ID structure . And also IMHO you don't need it since the API is capable of everything. – Serge insas Aug 22 '18 at 16:56

1 Answers1

0

I tried this code and it works.

Note that I used the email ressource as ID for CalendarApp

function myFunction() {
  var pageToken;
  do {
    var arguments = {
      maxResults: 200,
      pageToken: pageToken
    }; 
    calendars = AdminDirectory.Resources.Calendars.list("my_customer", arguments);
    if (calendars.items && calendars.items.length > 0) {
      for (var i = 0; i < calendars.items.length; i++) {
        var calendar = calendars.items[i];
        Logger.log('ressource name : '+calendar.resourceName);  

        //Logger.log('ressource data : '+JSON.stringify(calendar)+'\n');// this is useful to see what we get  

        var resourceCalendar = CalendarApp.getCalendarById(calendar.resourceEmail);
        if(resourceCalendar){
          Logger.log("I found the following calendar %s \n", resourceCalendar.getName() );
        }else{
          Logger.log('ressource skipped- undefined ----------------');
        }
      }
    }
  } while (pageToken); 
}
Serge insas
  • 45,904
  • 7
  • 105
  • 131
  • Thanks. This worked. Good tip. And I found another reason. The resources were new and no events were created yet. After I created an event, the resourceCalendar was returned. – Jeroen Aug 22 '18 at 19:22