如何在Apps Script中调用Google Calendar API判断事件是否来自预约时段?
判断Google日历事件是否来自预约时段的方法
嘿,这个问题我刚好有实操经验!在Apps Script里,你可以借助Google Calendar API的events.get或events.list方法,通过检查事件对象的私有扩展属性来判断——这个属性就是Google日历用来标记“来自预约时段”的关键。
具体来说,当事件是由Appointment Slots创建时,事件对象的extendedProperties.private.createdFromSlot值会是"true",这就对应你在日历详情里看到的那句提示:"This event was created from an appointment slot"。
给你一个简单的示例代码,直接在Apps Script里就能用:
function isEventFromAppointmentSlot(eventId, calendarId = 'primary') { try { // 获取目标事件详情 const event = Calendar.Events.get(calendarId, eventId); // 检查私有扩展属性中的标记 const isFromSlot = event.extendedProperties?.private?.createdFromSlot === 'true'; if (isFromSlot) { console.log(`事件 ${eventId} 是由预约时段创建的`); } else { console.log(`事件 ${eventId} 不是由预约时段创建的`); } return isFromSlot; } catch (err) { console.error('获取事件信息失败:', err.message); return false; } }
注意事项
- 确保你的Apps Script项目已经启用了Google Calendar API:在脚本编辑器顶部点击「扩展」→「Apps Script」,然后在左侧菜单选择「服务」,找到Google Calendar API并启用它。
- 这个
createdFromSlot是Google日历的私有属性,虽然目前稳定可用,但官方文档不会公开标注,不过实际使用中一直很可靠。
内容的提问来源于stack exchange,提问作者Arie Fisher
相关产品推荐
相关产品推荐

