为什么Google Calendar API的“忙/闲”时间会回来“未定义”?

时间:2014-09-08 23:25:48

标签: javascript google-apps-script google-api

我刚接触并学习Javascript和Google Script,我试图编写一个脚本来检查日历是否在某个时间正忙。根据{{​​3}},这应该以以下格式返回:

{
  "kind": "calendar#freeBusy",
  "timeMin": datetime,
  "timeMax": datetime,
  "calendars": {
    "busy": [{"start": datetime,"end": datetime}]
  }
}

但是,我使用以下代码(简化以重现问题):

function checkResource() {
 var calendarId = [The Calendars ID removed for privacy];
 var check = {
    items: [{id: calendarId, busy: 'Active'}],
    timeMax: "2014-09-09T21:00:31-00:00",
    timeMin: "2014-09-09T17:00:31-00:00"
 };  

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

 Logger.log('Response: '+ response);
 Logger.log('Response Calendars: '+ response.calendars.calendarId);

 for (var property in response){
    Logger.log('Property: '+ property);
    Logger.log('Property Value: '+ response[property]);
 }

 for (var calendarValue in response){
   Logger.log('Calendar Value: '+ response.calendars.calendarValue);
 };
}

哪些日历属性记录了我,但出于某种原因,除了' [object Object]'之外还没有返回任何内容。或者' undefined'当我试图进入嵌套的“忙碌”时对象。Screenshot of logging output

在此期间,日历是故意忙碌的(2014-09-09T18:00:00Z的事件持续一小时),当我记录整个功能时你可以看到这个(Logger) .log('响应:' +响应);显示日历ID,“忙碌”和“开始和结束时间”,因此我的代码显然出现了问题。

任何帮助都会很棒。

1 个答案:

答案 0 :(得分:3)

采取"深度"看一下记录器,我发现这个代码似乎可以返回你想要的东西:

function checkResource() {
  var calendarId = ["_________________o9mvpo5r5cvb5nb4cg@group.calendar.google.com"];
  var check = {
 items: [{id: calendarId, busy: 'Active'}],
 timeMax: "2014-09-09T21:00:31-00:00",
 timeMin: "2014-09-09T17:00:31-00:00"
};  

  var response = Calendar.Freebusy.query(check);
  Logger.log(response.kind)
  Logger.log('Time MIN = '+response.timeMin)
  Logger.log('Time MAX = '+response.timeMax)
  Logger.log('busy start = '+response.calendars[calendarId].busy[0].start)
  Logger.log('busy end = '+response.calendars[calendarId].busy[0].end)
} 

结果日志:

[14-09-10 15:50:51:724 CEST] calendar#freeBusy
[14-09-10 15:50:51:724 CEST] Time MIN = 2014-09-09T17:00:31.000Z
[14-09-10 15:50:51:724 CEST] Time MAX = 2014-09-09T21:00:31.000Z
[14-09-10 15:50:51:725 CEST] busy start = 2014-09-09T17:00:31Z
[14-09-10 15:50:51:725 CEST] busy end = 2014-09-09T18:00:00Z

记录基本值:

[14-09-10 15:47:37:676 CEST] Response: {"kind":"calendar#freeBusy","timeMin":"2014-09-09T17:00:31.000Z","calendars":{"_____________9mvpo5r5cvb5nb4cg@group.calendar.google.com":{"busy":[{"start":"2014-09-09T17:00:31Z","end":"2014-09-09T18:00:00Z"}]}},"timeMax":"2014-09-09T21:00:31.000Z"}

源自您的完整脚本:

function checkResource() {
  var calendarId = ["________________vb5nb4cg@group.calendar.google.com"];
  var check = {
 items: [{id: calendarId, busy: 'Active'}],
 timeMax: "2014-09-09T21:00:31-00:00",
 timeMin: "2014-09-09T17:00:31-00:00"
};  

  var response = Calendar.Freebusy.query(check);
  Logger.log(response.kind)
  Logger.log('Time MIN = '+response.timeMin)
  Logger.log('Time MAX = '+response.timeMax)
  Logger.log('busy start = '+response.calendars[calendarId].busy[0].start)
  Logger.log('busy end = '+response.calendars[calendarId].busy[0].end)

  Logger.log('Response: '+ response);
}