如何通过Outlook Add-in区分日历事件是新建还是编辑状态?
判断Outlook日历事件是新建还是编辑状态
可以通过Outlook Add-in的Office.js API直接区分两种状态,核心依赖以下两种方式:
Office.context.mailbox.item.isNew:官方推荐的直接判断属性,返回布尔值。true对应新建未保存的事件,false对应编辑已保存的旧事件。Office.context.mailbox.item.itemId:新建事件的itemId为空或undefined,已有事件的itemId会携带具体的持久化标识符,可作为旧版本API的兼容方案。
代码示例
async function judgeEventStatus() { await Office.onReady(); const currentItem = Office.context.mailbox.item; // 方法1:使用isNew属性(优先推荐) const isNewEvent = currentItem.isNew; if (isNewEvent) { // 执行新建事件的响应逻辑 console.log("当前为新建日历预订"); } else { // 执行编辑旧事件的响应逻辑 console.log("当前为编辑已有日历预订"); } // 方法2:通过itemId判断(兼容旧版API) const hasItemId = !!currentItem.itemId; if (!hasItemId) { console.log("当前为新建日历预订"); } else { console.log("当前为编辑已有日历预订"); } }
注意事项
- 必须在
Office.onReady()完成后再访问item对象,避免API未初始化引发的错误。 isNew属性支持Outlook 2016及以上客户端、网页版和移动端Add-in,是最可靠的判断方式。
内容的提问来源于stack exchange,提问作者Sebastian
相关产品推荐
相关产品推荐

