在管理目录中查找和读取资源日历

时间:2018-08-22 14:34:11

标签: google-apps-script google-admin-sdk

上下文
在POST形式的酒店预订表格中,我首先要检查一下我们公寓中是否有免费房间。我通过GSuite管理面板创建了资源日历(包括建筑物等)。

现在,我正在使用在此站点上找到的一些代码进行测试。它找到资源日历,然后我仅按名称过滤它们(以摆脱所有会议室)。但是现在我想“阅读”日历本身。但是看来resourceId不是相应资源日历的ID。

如何查看资源日历中的可用性?

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); 

1 个答案:

答案 0 :(得分:0)

我尝试了此代码,它可以工作。

请注意,我使用电子邮件资源作为CalendarApp

的ID
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); 
}