我正在使用 Google Apps 脚本来获取 Google 日历数据。如果我不在 URL 中使用参数,它就可以正常工作。但我想要更多的灵活性,并且有一个可变的时间跨度来检查日历。我向入口函数添加了一个参数,并手动将其设置为固定值,如下所示: function doGet(timeSpan=600000) { do stuff }。当我在脚本编辑器中使用“执行”功能时,我得到了预期的结果。当我使用实现脚本时生成的 URL 时,我总是收到错误响应,指出结束日期必须晚于开始日期,无论 URL 中是否包含参数。
我添加了调试代码来检查用于使用 API 函数 cal.getEvents(start, end) 获取日历条目的日期。当我在脚本编辑器中运行脚本时,我得到 response = {"status":"debug","data":["2024-03-23T16:09:15.946Z","2024-03-30T14:49:15.946Z"]},这是我所期望的。 当我在浏览器中使用 URL 调用脚本时,我得到 {“状态”:“调试”,“数据”:[“2024-03-23T16:10:00.158Z”,null]}。结束日期始终为空。
这有效:
// 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};
}
这不起作用:
// 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};
}
注意:“调试”版本在我实际获取日历数据之前退出。问题在于“then”的值为空,因为“now”加上“timeSpan”。
尝试这样的事情。它使用开始日期和持续时间(以毫秒为单位)
function getEvents(obj) {
const year = obj.startyear;
const month = obj.startmonth;
const day = obj.startday
const dur = obj.timeSpan;
const calids = obj.calids
let evs = [["Title","StartDate","Description"]]
calids.forEach((id,i) => {
let cal = CalendarApp.getCalendarById(id);
let start = new Date(obj.startyear,obj.startmonth -1,obj.startday);
let end = new Date(start.valueOf() + obj.timeSpan);
let events = cal.getEvents(start,end);
events.forEach(ev => {
evs.push([ev.getTitle(),ev.getStartTime(),ev.getDescription()])
})
});
if(evs.length > 1) {
Logger.log(JSON.stringify(evs)).replace(/\],/g,'],\n')
}
}
function testabovefunction() {
const DAY = 86400000;
let myobj = {calids:[],startyear:"2024",startmonth:3,startday:1,timeSpan:10 * DAY};
getEvents(myobj);
}