Google Calendar API的“忙/闲”时间为什么会返回“未定义”? [英] Why does the 'Free/Busy' time for Google Calendar API come back 'Undefined'?

查看:155
本文介绍了Google Calendar API的“忙/闲”时间为什么会返回“未定义”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手,并且学习了Javascript和Google脚本,并且正在尝试编写一个脚本来检查日历是否在特定时间忙碌。根据 https://developers.google.com/google- apps / calendar / v3 / reference / freebusy / query ,这应该返回格式: b $ bkind:calendar#freeBusy,
timeMin:datetime,
timeMax:datetime,
日历:{
busy: [{start:datetime,end:datetime}]
}
}

然而,我使用下面的代码(简化来重现问题):

  function checkResource(){ 
var calendarId = [日历中的ID已移除以获得隐私];
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);

(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'不同的东西。



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



任何协助都会很好。

解决方案

在记录器中进行深入的观察,我找到了这段代码,它似乎返回了您想要的内容:

  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]日历#freeBusy 
[14-09-10 15:50:51:724 CEST]时间MIN = 2014-09-09T17:00:31.000Z
[14-09-10 15:50:51:724 CEST]时间MAX = 2014-09-09T21:00:31.000Z $ b $ [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]回应:{kind:calendar#freeBusy,timeMin: 2014-09-09T17:00:31.000Z, 日历:{ _____________ 9mvpo5r5cvb5nb4cg@group.calendar.google.com:{ 忙:[{ 开始: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);
}


I'm new to and learning both Javascript and Google Script, and I'm trying to write a script that checks if a calendar is busy at a certain time. According to https://developers.google.com/google-apps/calendar/v3/reference/freebusy/query, this should be returned in the format:

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

However, I'm using the following code (Simplified to reproduce the issue):

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

Which logs for me all of the calendar properties, but for some reason won't return anything apart from '[object Object]' or 'undefined' when I try to get into the nested 'busy' object.

The calendar is intentionally busy during this time (there's an event on at 2014-09-09T18:00:00Z which lasts for an hour), and you can see this when I log the entire function (Logger.log('Response: '+ response); shows the calendar ID, 'busy' and the start and end time), so I've clearly gone wrong with my code.

Any assistance would be great.

解决方案

Taking a "deep" look in the logger I came up to this code that seems to return what you wanted :

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

Result log :

[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

Log from the basic values :

[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"}

Full script derived from yours :

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

这篇关于Google Calendar API的“忙/闲”时间为什么会返回“未定义”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆