I'm using a Google Apps script to get Google Calendar data. It works fine if I don't use a parameter in the URL. But I want a bit more flexibility and have a variable time span for which the calendar should be checked. I added a parameter to the entry function and manually set it to a fixed value like this: function doGet(timeSpan=600000) { do stuff }. When I use the "Execute" function in the script editor, I get the expected result. When I use the URL which is produced when I implement the script, I always get an error reponse that the end date must be later than the start date, with or without the parameter in the URL.
I added debug code to check the dates which are used to get the calendar entries using the API function cal.getEvents(start, end). When I run the script in the script editor I get response = {"status":"debug","data":["2024-03-23T16:09:15.946Z","2024-03-30T14:49:15.946Z"]}, which is what I expect. When I use the URL in a browser to call the script, I get {"status":"debug","data":["2024-03-23T16:10:00.158Z",null]}. The end date is always null.
This works:
// Get upcoming events for the next x days from the 'Recycle' calendar
function doGet() {
const SECOND = 1;
const MINUTE = 60 * SECOND;
const HOUR = 60 * MINUTE;
const DAY = 24 * HOUR;
const WEEK = 7 * DAY;
var _calendarName = 'Recycle';
//var _timeSpan = timeSpan;
var _timeSpan = 600000;
return ContentService.createTextOutput(getOutput(_calendarName, _timeSpan));
}
function getOutput(calendarName, timeSpan) {
// for debugging only: show the result which will be sent to the caller
// Logger.log('response = ' + JSON.stringify(getResponse(calendarName, timeSpan)));
// create a JSON formatted string from the result object
return JSON.stringify(getResponse(calendarName, timeSpan));
}
// Query the calendar using calendarName and timeSpan
function getResponse(calendarName, timeSpan) {
// use the first (and only) calendar with the specified name
var cal = CalendarApp.getCalendarsByName(calendarName)[0];
if (!cal) {
return {status: 'error', message: 'Error, calendar "' + calendarName + '" does not exist.'};
}
var now = new Date(),
then = new Date();
then.setSeconds(now.getSeconds() + timeSpan);
arr = [];
arr.push(now);
arr.push(then);
arr.push(timeSpan);
return {status: 'debug', data: arr};
}
This does not work:
// Get upcoming events for the next x days from the 'Recycle' calendar
function doGet(timeSpan) {
const SECOND = 1;
const MINUTE = 60 * SECOND;
const HOUR = 60 * MINUTE;
const DAY = 24 * HOUR;
const WEEK = 7 * DAY;
var _calendarName = 'Recycle';
var _timeSpan = timeSpan;
//var _timeSpan = 600000;
return ContentService.createTextOutput(getOutput(_calendarName, _timeSpan));
}
function getOutput(calendarName, timeSpan) {
// for debugging only: show the result which will be sent to the caller
// Logger.log('response = ' + JSON.stringify(getResponse(calendarName, timeSpan)));
// create a JSON formatted string from the result object
return JSON.stringify(getResponse(calendarName, timeSpan));
}
// Query the calendar using calendarName and timeSpan
function getResponse(calendarName, timeSpan) {
// use the first (and only) calendar with the specified name
var cal = CalendarApp.getCalendarsByName(calendarName)[0];
if (!cal) {
return {status: 'error', message: 'Error, calendar "' + calendarName + '" does not exist.'};
}
var now = new Date(),
then = new Date();
then.setSeconds(now.getSeconds() + timeSpan);
arr = [];
arr.push(now);
arr.push(then);
arr.push(timeSpan);
return {status: 'debug', data: arr};
}
Note: the "debug" version exits before I actually get the calendar data. The problem lies in the value of "then" which is null as a result of adding "timeSpan" to "now".
Getting Calendar Events for a start date and duration
Try something like this. It uses a start date and a duration in milliseconds; You will have to add your own calendar id's