You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google Apps Script获取日历数据URL传参致日期异常问题

问题:Google Apps Script部署后URL传参导致结束日期为null的错误

我用Google Apps Script获取Google Calendar数据,无URL参数时运行正常。为实现可变时间范围查询,给入口函数doGet添加了参数(比如function doGet(timeSpan=600000)),在脚本编辑器中执行可得到预期结果,但通过部署生成的URL访问时,无论是否传参,均返回“结束日期必须晚于开始日期”的错误。

添加调试代码后发现:

  • 在脚本编辑器中运行,cal.getEvents使用的日期为["2024-03-23T16:09:15.946Z","2024-03-30T14:49:15.946Z"],符合预期;
  • 通过浏览器URL调用时,得到的日期为["2024-03-23T16:10:00.158Z",null],结束日期始终为null。

注:调试版本在获取日历数据前就返回结果,问题源于将timeSpan与now相加后,then的值为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) {
  // 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) {
  // 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};
}

问题原因与解决方案

问题原因

Google Apps Script的doGet函数不直接接收URL查询参数作为函数参数,而是接收一个包含所有请求信息的e对象。直接给doGet定义timeSpan参数,在脚本编辑器运行时是手动传参,但通过URL访问时,传入的是请求对象而非数值,导致timeSpan为undefined,进而计算then时出错变为null。

修复代码

修改doGet函数通过e.parameter获取URL参数,并添加参数有效性校验:

function doGet(e) {
  const SECOND = 1;
  const MINUTE = 60 * SECOND;
  const HOUR = 60 * MINUTE;
  const DAY = 24 * HOUR;
  const WEEK = 7 * DAY;
  
  var _calendarName = 'Recycle';
  // 获取URL参数并转为数值,无参数时用默认值600000
  var _timeSpan = e?.parameter?.timeSpan ? parseInt(e.parameter.timeSpan) : 600000;

  return ContentService.createTextOutput(getOutput(_calendarName, _timeSpan));
}

function getResponse(calendarName, timeSpan) {
  var cal = CalendarApp.getCalendarsByName(calendarName)[0];
  
  if (!cal) {
    return {status: 'error', message: 'Error, calendar "' + calendarName + '" does not exist.'};
  }
  
  // 确保timeSpan是有效数值,避免非数值导致日期计算错误
  const validTimeSpan = typeof timeSpan === 'number' && !isNaN(timeSpan) ? timeSpan : 600000;
  
  var now = new Date(),
      then = new Date();
  
  then.setSeconds(now.getSeconds() + validTimeSpan);

  arr = [];
  arr.push(now.toISOString());
  arr.push(then.toISOString());
  arr.push(validTimeSpan);
  return {status: 'debug', data: arr};
}

说明

  1. doGet接收e对象,e.parameter.timeSpan获取URL中的查询参数,需转为数值类型使用;
  2. 添加默认值逻辑,无参数或参数无效时使用600000;
  3. 在getResponse中二次校验timeSpan有效性,避免非数值输入导致日期计算异常。

内容的提问来源于stack exchange,提问作者Gilbert

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 05:12:52