JS开发Outlook插件如何监听日历事件编辑页保存按钮点击事件
Outlook日历插件保存触发同步方案
你需要的功能可以通过Office JS提供的ItemSend事件实现,该事件仅在用户点击日历事件的保存/发送按钮时触发,不会因为用户中途编辑字段、丢弃修改而执行,完全匹配你的需求。
实现步骤
1. 插件清单注册事件
首先在你的manifest.xml中注册ItemSend事件,指定触发规则和处理函数名:
<!-- 仅展示核心配置节点,其他原有配置保留即可 --> <VersionOverrides xmlns="http://schemas.microsoft.com/office/mailappversionoverrides" xsi:type="VersionOverridesV1_0"> <Extensions> <Extension xsi:type="LaunchEvent"> <LaunchEvents> <!-- 注册ItemSend事件,指定处理函数为onCalendarSave,SendMode根据需求选择 --> <LaunchEvent Type="ItemSend" FunctionName="onCalendarSave" SendMode="AutoContinue" /> </LaunchEvents> <Runtimes> <Runtime resid="Taskpane.Url" lifetime="short" /> </Runtimes> </Extension> </Extensions> </VersionOverrides>
SendMode可选值说明:
AutoContinue:同步逻辑不影响保存操作,无论同步是否成功都允许用户保存事件PromptUser:同步失败时弹出提示,让用户选择是否继续保存SoftBlock:同步失败时直接阻止保存,显示错误提示
2. JavaScript实现处理逻辑
在你的插件代码中实现对应处理函数,读取最新的日历事件数据后同步到Salesforce:
// 注册事件处理函数,必须和manifest中配置的FunctionName一致 function onCalendarSave(event) { const calendarItem = Office.context.mailbox.item; // 异步读取所有需要同步的字段 Promise.all([ new Promise(resolve => calendarItem.subject.getAsync((result) => resolve(result.value))), new Promise(resolve => calendarItem.start.getAsync((result) => resolve(result.value))), new Promise(resolve => calendarItem.end.getAsync((result) => resolve(result.value))), new Promise(resolve => calendarItem.requiredAttendees.getAsync((result) => resolve(result.value))), new Promise(resolve => calendarItem.optionalAttendees.getAsync((result) => resolve(result.value))), new Promise(resolve => calendarItem.location.getAsync((result) => resolve(result.value))), new Promise(resolve => calendarItem.body.getAsync(Office.CoercionType.Text, (result) => resolve(result.value))) ]).then(([subject, start, end, requiredAttendees, optionalAttendees, location, body]) => { // 组装需要同步到Salesforce的事件数据 const syncData = { subject, start, end, requiredAttendees, optionalAttendees, location, body, outlookEventId: calendarItem.itemId }; // 调用你的Salesforce同步接口 return fetch("你的Salesforce同步接口地址", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(syncData) }); }).then(() => { // 处理完成,允许保存操作继续 event.completed({ allowEvent: true }); }).catch((err) => { // 同步失败,根据SendMode配置处理 event.completed({ allowEvent: false, errorMessage: "同步到Salesforce失败,请稍后重试,错误详情:" + err.message }); }); }
注意事项
- 该事件同时适配新建日历事件、编辑现有日历事件的保存场景,不需要额外监听参会人、时间等字段的变更事件
- 若需要兼容全平台Outlook(网页端、桌面端、移动端),请确保清单中声明的
Mailbox最低要求版本为1.10及以上 - Salesforce的接口域名需要提前添加到插件清单的
AppDomains节点中,避免跨域请求被拦截
内容的提问来源于stack exchange,提问作者Tamilarasan Rajamanickam
相关产品推荐
相关产品推荐

