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
相关产品推荐
相关产品推荐

