【问题标题】:Why does the 'Free/Busy' time for Google Calendar API come back 'Undefined'?为什么 Google Calendar API 的“空闲/忙碌”时间返回“未定义”?
【发布时间】:2014-09-08 23:25:48
【问题描述】:

我是 Javascript 和 Google 脚本的新手,并且正在学习 JavaScript 和 Google 脚本,我正在尝试编写一个脚本来检查日历是否在某个时间忙碌。根据https://developers.google.com/google-apps/calendar/v3/reference/freebusy/query,这应该以以下格式返回:

{
  "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);
 };
}

它为我记录了所有日历属性,但由于某种原因,当我尝试进入嵌套的“忙碌”对象时,除了“[对象对象]”或“未定义”之外不会返回任何内容。

日历在这段时间故意忙碌(2014-09-09T18:00:00Z 有一个事件持续一个小时),当我记录整个函数时你可以看到这一点(Logger.log(' Response: '+ response); 显示日历 ID、'busy' 以及开始和结束时间),所以我的代码显然出错了。

任何帮助都会很棒。

【问题讨论】:

  • 感谢您揭示这个我不知道的功能 :-) 实际上非常有趣!

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


【解决方案1】:

在记录器中“深入”查看我想出了这段代码,它似乎返回了你想要的:

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

【讨论】:

  • 谢谢,这给了我需要弄清楚的东西!出于某种原因,它会在不存在事件时抛出“开始未定义”,并且仅在事件存在时才有效。不知道为什么它不只是返回一个空白值....我已将您的建议标记为“已接受”。
猜你喜欢
  • 1970-01-01
  • 2012-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-09
  • 1970-01-01
相关资源
最近更新 更多