Google日历Events.import报错Invalid iCalUID value的解决咨询
解决Google Calendar Events.import的Invalid iCalUID value错误
根据你提供的信息,问题核心在于同一iCalUID对应了不同的事件ID:成功导入的事件ID是abcd1234,失败的事件ID是abcd1234_20230213T161500Z,但两者的iCalUID均为abcd1234@google.com。Google Calendar要求同一日历内的事件iCalUID必须唯一,当尝试导入iCalUID已存在但事件ID不同的事件时,就会触发这个错误。
具体解决办法:
检查并复用现有事件
在导入前先查询目标日历中是否存在该iCalUID的事件,若存在则改用更新操作而非重复导入:// 查询目标日历中是否有对应iCalUID的事件 const existingEvents = Calendar.Events.list(calendarId, { iCalUID: event.iCalUID }); if (existingEvents.items.length > 0) { // 更新已存在的事件 Calendar.Events.update(event, calendarId, existingEvents.items[0].id); } else { // 导入新事件 Calendar.Events.import(event, calendarId); }为重复事件实例生成唯一iCalUID
从失败事件的ID后缀判断,这应该是重复事件的单个实例。这种情况下需要给每个实例生成唯一的iCalUID,比如在原iCalUID后拼接实例时间戳:// 处理重复事件实例的iCalUID if (event.id.includes('_')) { const instanceSuffix = event.id.split('_')[1]; event.iCalUID = `${event.iCalUID.split('@')[0]}_${instanceSuffix}@google.com`; } // 执行导入 Calendar.Events.import(event, calendarId);移除自定义事件ID,让Google自动生成
如果不需要保留原事件ID,可以在导入前删除事件对象中的id字段,由Google Calendar自动分配唯一ID(需确保iCalUID本身在目标日历中唯一):delete event.id; Calendar.Events.import(event, calendarId);
内容的提问来源于stack exchange,提问作者luisa
相关产品推荐
相关产品推荐

