Google Apps Script执行成功但Google Calendar未创建事件求助
Google Apps Script创建日历事件无报错但日历不显示问题排查
我使用Google Apps Script在Google Calendar中创建事件,代码执行无报错且日志显示成功,但日历中始终看不到任何事件。一个月前用相同逻辑的代码(仅事件内容不同)还能正常创建事件,现在多次执行问题依旧。已经核对过数据正确性、Calendar ID以及日历设置。
代码示例
function createEvents() { // Replace "CALENDAR_ID" with the ID of your Google Calendar var calendar = CalendarApp.getCalendarById("0150f92d506def1e40d37891011d987b3cb017e1048b796d658b8f574a700d4d@group.calendar.google.com"); // Define start times and descriptions, the format must be "mm/dd/yyyy hh:mm" var startTimes = [ new Date(" 03/07/2023 11:00 "), new Date(" 03/08/2023 09:30 "), new Date(" 03/08/2023 11:00 "), new Date(" 03/08/2023 11:00 "), new Date(" 03/10/2023 09:30 "), new Date(" 03/10/2023 09:30 "), new Date(" 03/14/2023 08:30 "), new Date(" 03/14/2023 08:30 "), new Date(" 03/15/2023 08:30 "), new Date(" 03/15/2023 08:30 "), new Date(" 03/16/2023 08:30 "), new Date(" 03/17/2023 10:00 "), new Date(" 03/22/2023 14:00 "), new Date(" 03/22/2023 14:00 "), new Date(" 03/22/2023 14:30 "), new Date(" 03/24/2023 08:30 "), new Date(" 03/31/2023 08:30 "), new Date(" 03/31/2023 08:30 "), new Date(" 03/31/2023 08:30 "), new Date(" 04/03/2023 10:00 "), new Date(" 04/04/2023 10:00 "), new Date(" 04/05/2023 08:30 "), new Date(" 04/05/2023 10:00 "), new Date(" 04/07/2023 08:30 "), new Date(" 04/07/2023 08:30 "), new Date(" 04/12/2023 08:30 "), new Date(" 04/12/2023 08:30 "), new Date(" 04/12/2023 14:00 "), new Date(" 04/13/2023 08:30 "), new Date(" 04/14/2023 08:30 "), new Date(" 04/14/2023 10:00 "), new Date(" 04/18/2023 08:30 ") ]; var descriptions = [ " 🇺🇸 US Fed Chair Powell Testimony Previous Consensus Forecast ", " 🇺🇸 US Balance of Trade JAN Previous $-67.4B Consensus $-68.9B Forecast $-69.0B ", " 🇺🇸 US JOLTs Job Openings JAN Previous 11.012M Consensus 10.5M Forecast 10.6M ", " 🇺🇸 US Fed Chair Powell Testimony Previous Consensus Forecast ", " 🇺🇸 US Unemployment Rate FEB Previous 0.034 Consensus 0.034 Forecast 0.034 ", " 🇺🇸 US Non Farm Payrolls FEB Previous 517K Consensus 200K Forecast 210.0K ", " 🇺🇸 US Core Inflation Rate YoY FEB Previous 0.056 Consensus Forecast ", " 🇺🇸 US Inflation Rate YoY FEB Previous 0.064 Consensus Forecast 0.062 ", " 🇺🇸 US PPI MoM FEB Previous 0.007 Consensus 0.003 Forecast ", " 🇺🇸 US Retail Sales MoM FEB Previous 0.03 Consensus 0.002 Forecast ", " 🇺🇸 US Building Permits Prel FEB Previous 1.339M Consensus Forecast ", " 🇺🇸 US Michigan Consumer Sentiment Prel MAR Previous 67 Consensus 67.5 Forecast ", " 🇺🇸 US FOMC Economic Projections Previous Consensus Forecast ", " 🇺🇸 US Fed Interest Rate Decision Previous 0.0475 Consensus Forecast 0.05 ", " 🇺🇸 US Fed Press Conference Previous Consensus Forecast ", " 🇺🇸 US Durable Goods Orders MoM FEB Previous -0.045 Consensus Forecast ", " 🇺🇸 US Core PCE Price Index MoM FEB Previous 0.006 Consensus Forecast ", " 🇺🇸 US Personal Income MoM FEB Previous 0.006 Consensus Forecast 0.005 ", " 🇺🇸 US Personal Spending MoM FEB Previous Consensus Forecast ", " 🇺🇸 US ISM Manufacturing PMI MAR Previous 47.7 Consensus Forecast 49 ", " 🇺🇸 US JOLTs Job Openings FEB Previous Consensus Forecast ", " 🇺🇸 US Balance of Trade FEB Previous Consensus Forecast ", " 🇺🇸 US ISM Non-Manufacturing PMI MAR Previous 55.1 Consensus Forecast 48 ", " 🇺🇸 US Unemployment Rate MAR Previous Consensus Forecast 0.037 ", " 🇺🇸 US Non Farm Payrolls MAR Previous Consensus Forecast 125K ", " 🇺🇸 US Core Inflation Rate YoY MAR Previous Consensus Forecast 0.05 ", " 🇺🇸 US Inflation Rate YoY MAR Previous Consensus Forecast 0.06 ", " 🇺🇸 US FOMC Minutes Previous Consensus Forecast ", " 🇺🇸 US PPI MoM MAR Previous Consensus Forecast -0.002 ", " 🇺🇸 US Retail Sales MoM MAR Previous Consensus Forecast -0.004 ", " 🇺🇸 US Michigan Consumer Sentiment Prel APR Previous Consensus Forecast ", " 🇺🇸 US Building Permits Prel MAR Previous Consensus Forecast 1.45M ]; // Loop through the rows of data for (var i = 0; i < startTimes.length; i++) { // Get the end time by adding one hour to the start time var endTime = new Date(startTimes[i].getTime() + (60 * 60 * 1000)); // Create a new event on the calendar with the start and end times, and description var event = calendar.createEvent(descriptions[i], startTimes[i], endTime); // Add the reminders to the event event.addPopupReminder(1440); // 1 day before event.addPopupReminder(2880); // 2 days before event.addPopupReminder(10080); // 1 week before event.addPopupReminder(60); // 1 hour before } }
排查方向
- 修复语法错误:descriptions数组最后一个元素缺少闭合双引号,补全后才能让代码正确执行:
" 🇺🇸 US Building Permits Prel MAR Previous Consensus Forecast 1.45M " - 清理日期字符串空格:startTimes中的日期字符串前后存在大量空格,建议用
trim()处理后再转换为Date对象,避免解析异常:new Date("03/07/2023 11:00".trim()) - 验证数组长度匹配:确认startTimes和descriptions的元素数量完全一致,避免循环中出现索引越界
- 添加调试日志:在循环内添加日志,打印每个事件的ID、开始时间,确认事件是否真的被创建:
之后可以通过事件ID在日历中搜索,确认事件是否存在console.log(`Created event ${i+1}: ID=${event.getId()}, Start=${startTimes[i]}`); - 检查时区一致性:对比脚本项目时区(文件→项目属性→信息)和目标日历的时区设置,避免事件被创建到非预期时区的时间点
- 重新确认权限:在脚本编辑器中重新运行授权流程(运行→授权),确保脚本拥有Calendar的编辑权限;同时检查目标日历是否仍对当前账号开放编辑权限
- 简化测试代码:暂时注释掉添加提醒的代码,只保留创建事件的逻辑,测试是否能正常显示事件,排除提醒设置的影响
内容的提问来源于stack exchange,提问作者Christian HEREDIA
相关产品推荐
相关产品推荐

