Through experimenting with Apps Script, I managed to recreate the issue you mentioned. Since Apps Script also utilizes Javascript, it confirmed that your observations were accurate. When utilizing template literals, the request parameters timeMin
and timeMax
end up being rendered as strings instead of in a datetime
format. My solution involved concatenating the strings using the (+) operator, which proved to be successful.
Example Code:
var calendarId = "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ddaebcb0adb1b89dbaafb2a8adf3bebcb1b8b3b9bcaff3bab2b2bab1b8f3beb2b0">[email protected]</a>";
var date = {
value: "2021-04-03"
}
var startTime = {
value: "01:00"
}
var endTime = {
value: "05:00"
}
// Incorrect 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);
// Correct 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);
Result:
6:38:55 AM Notice Execution started
6:38:56 AM Info {items=[{<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e9808dd49a888499858ca98e9b869c99c78a88858c878d889bc78e86868e858cc78a8684">[email protected]</a>}], timeMax="2021-04-03T05:00:00.000Z", timeMin="2021-04-03T01:00:00.000Z", timeZone=GMT+08:00}
6:38:56 AM Info {items=[{<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c7aea3fab4a6aab7aba287a0b5a8b2b7e9a4a6aba2a9a3a6b5e9a0a8a8a0aba2e9a4a8aa">[email protected]</a>}], 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={<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="eb988a869b878eab8c99849e9bc5888a878e858f8a99c58c84848c878ec5888486">[email protected]</a>={busy=[]}}, kind=calendar#freeBusy, timeMin=2021-04-03T01:00:00.000Z}
6:38:57 AM Notice Execution completed
- It's noticeable that
timeMin
and timeMax
are displayed as strings in the resource1
variable, while in resource2
, they are in a non-string format.