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

Calendar API更新后,修改fromGmail类型事件颜色报错求助

解决Google Calendar API修改fromGmail事件颜色报错的问题

问题原因

你当前使用Calendar.Events.update()方法时,会将整个事件对象提交给API,但自2024年5月30日的调整后,fromGmail类型的事件仅允许更新指定属性(reminders、colorId、visibility、status、extendedProperties)。即使你只修改了colorId,update方法仍会携带事件的所有其他属性,而这些属性是不允许被修改的,因此触发了"Bad Request"错误。

解决方案

改用Calendar.Events.patch()方法,该方法属于增量更新,仅提交需要修改的属性,而非整个事件对象。这样就能避开API对fromGmail事件的属性限制。

修改后的代码如下:

var eventsLocal = Calendar.Events.list(calendarId, optionalArgs)["items"];
for (var i = 0; i < eventsLocal.length; i++) {
  var eventLocal = eventsLocal[i];
  try {
    if (eventLocal.eventType === "fromGmail") {
      // 仅提交需要修改的colorId属性
      Calendar.Events.patch(
        { colorId: "10" },
        calendarId,
        eventLocal.id
      );
    } else {
      // 非fromGmail事件可继续用update,或也改用patch
      eventLocal.colorId = "10";
      eventLocal = Calendar.Events.update(
        eventLocal,
        calendarId,
        eventLocal.id
      );
    }
  } catch (e) {
    Logger.log('update/patch threw an exception: ' + e);
  }
}

补充说明

  • patch方法只发送你指定的字段,完全符合Google官方对fromGmail事件的更新限制
  • 通过eventLocal.eventType字段判断事件类型,针对性处理能提升脚本执行效率

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 17:52:35