问题
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.
回答1:
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);
}
来源:https://stackoverflow.com/questions/25734575/why-does-the-free-busy-time-for-google-calendar-api-come-back-undefined