是否可以在请求主体中使用模板文字对Google Calendar API进行忙闲查询



var freeRequest = gapi.client.calendar.freebusy.query({
"items": [
{"id": calendarid}
],
"timeMin": `"${date.value}T${startTime.value}:00.000Z"`,
"timeMax": `"${date.value}T${endTime.value}:00.000Z"`,
"timeZone": "GMT+01:00",
});

我想通过忙/闲查询检查日历是否繁忙

我知道请求只适用于日期格式:YYYY-MM-DDTH:MM:SS.MMMZ。我的计划是将从html输入中获得的值粘贴到字符串文字中,然后相应地格式化它。当我控制台日志:

`"${date.value}T${endTime.value}:00.000Z"`

控制台以正确的格式(例如"2021-03-31T18:29:00.000Z"(给我日期。但当在我的应用程序中发送请求时,它会给我一个400错误的请求错误。我想问题出在字符串文字上,因为timeMin和timeMax值应该只有引号,而不是

``
我还尝试将日期保存在一个变量中,但这也不起作用。有办法解决这个问题吗?

我能够使用Apps Script复制您的问题,因为它也使用Javascript。您的发现是正确的,当您使用模板文字时,请求参数timeMintimeMax将变为字符串而不是datetime格式。我所做的是使用(+(运算符将字符串连接起来,它就可以工作了。

示例代码:

var calendarId = "sample@group.calendar.google.com";
var date = {
value: "2021-04-03"
}
var startTime = {
value: "01:00"
}
var endTime = {
value: "05:00"
}

// Bad request
var resource1 = {
timeMin: `"${date.value}T${startTime.value}:00.000Z"`,
timeMax: `"${date.value}T${endTime.value}:00.000Z"`,
items: [
{
id: calendarId
}
],
timeZone: "GMT+08:00"
};
Logger.log(resource1);
// Successful request
var resource2 = {
timeMin:  date.value+"T"+startTime.value+":00.000Z",
timeMax: date.value+"T"+endTime.value+":00.000Z",
items: [
{
id: calendarId
}
],
timeZone: "GMT+08:00"
};
Logger.log(resource2);
var request = Calendar.Freebusy.query(resource2);
Logger.log(request);

输出:

6:38:55 AM  Notice  Execution started
6:38:56 AM  Info    {items=[{id=sample@group.calendar.google.com}], timeMax="2021-04-03T05:00:00.000Z", timeMin="2021-04-03T01:00:00.000Z", timeZone=GMT+08:00}
6:38:56 AM  Info    {items=[{id=sample@group.calendar.google.com}], timeMax=2021-04-03T05:00:00.000Z, timeMin=2021-04-03T01:00:00.000Z, timeZone=GMT+08:00}
6:38:56 AM  Info    {timeMax=2021-04-03T05:00:00.000Z, calendars={sample@group.calendar.google.com={busy=[]}}, kind=calendar#freeBusy, timeMin=2021-04-03T01:00:00.000Z}
6:38:57 AM  Notice  Execution completed
  • 请注意,timeMintimeMaxresource1变量中变为字符串,而在resource2中它们不是字符串格式

最新更新