Outlook网页版Office.js报错:item.body非Body实例,prependAsync不存在
问题描述
开发适配Outlook网页版、Windows及Mac版Outlook 365的插件时,调用item.body.prependAsync方法报错:item.body.prependAsync is not a function。控制台打印item.body结果如下:
{type: undefined, getAsync: ƒ} getAsync: ƒ wt(e) type: undefined [[Prototype]]: Object
HTML代码
<!DOCTYPE html> <html> <head> <title>Addin test</title> <script src="https://appsforoffice.microsoft.com/lib/1/hosted/office.js"></script> <script> let item; Office.onReady((info) => { if (info.host === Office.HostType.Outlook) { item = Office.context.mailbox.item; console.log(item.body); item.body.prependAsync("text", function (asyncResult) { if (asyncResult.status === Office.AsyncResultStatus.Failed) { console.log("Action failed with error: " + asyncResult.error.message); return; } console.log(`"${'text'}" prepended to the body.`); }); } }); </script> </head> </html>
报错日志
Uncaught TypeError: item.body.prependAsync is not a function at URLtoHTML/html?et=:14:19 at appsforoffice.microsoft.com/lib/1/hosted/office.js:76:23268 at t (appsforoffice.microsoft.com/lib/1/hosted/office.js:76:22669) at appsforoffice.microsoft.com/lib/1/hosted/office.js:76:29316 at c (appsforoffice.microsoft.com/lib/1/hosted/office.js:76:4168) at f.waitForFunction (appsforoffice.microsoft.com/lib/1/hosted/office.js:76:4257) at e.j [as appReadyCallback] (appsforoffice.microsoft.com/lib/1/hosted/office.js:76:28650) at appsforoffice.microsoft.com/lib/1/hosted/outlook-web-16.01.js:20:339988
精简后的manifest.xml
<?xml version="1.0" encoding="UTF-8"?> <OfficeApp xmlns="http://schemas.microsoft.com/office/appforoffice/1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:bt="http://schemas.microsoft.com/office/officeappbasictypes/1.0" xmlns:mailappor="http://schemas.microsoft.com/office/mailappversionoverrides/1.0" xsi:type="MailApp"> <Id>7218ed19-90f5-4ad6-99e8-8d17ee2556</Id> <Version>1.0.0.0</Version> <Hosts> <Host Name="Mailbox" /> </Hosts> <Requirements> <Sets> <Set Name="Mailbox" MinVersion="1.1" /> </Sets> </Requirements> <FormSettings> <Form xsi:type="ItemRead"> <DesktopSettings> <SourceLocation DefaultValue="URL to HTML" /> <RequestedHeight>250</RequestedHeight> </DesktopSettings> </Form> </FormSettings> <Permissions>ReadWriteItem</Permissions> <Rule xsi:type="RuleCollection" Mode="Or"> <Rule xsi:type="ItemIs" ItemType="Message" FormType="Read" /> </Rule> <DisableEntityHighlighting>false</DisableEntityHighlighting> <VersionOverrides xmlns="http://schemas.microsoft.com/office/mailappversionoverrides" xsi:type="VersionOverridesV1_0"> <Requirements> <bt:Sets DefaultMinVersion="1.1"> <bt:Set Name="Mailbox" /> </bt:Sets> </Requirements> <Hosts> <Host xsi:type="MailHost"> <DesktopFormFactor> <FunctionFile resid="Commands.Url" /> <ExtensionPoint xsi:type="MessageReadCommandSurface"> <OfficeTab id="TabDefault"> <Group id="msgReadGroup"> <Label resid="GroupLabel" /> <Control xsi:type="Button" id="msgReadOpenPaneButton"> <Label resid="TaskpaneButton.Label" /> <Supertip> <Title resid="TaskpaneButton.Label" /> <Description resid="TaskpaneButton.Tooltip" /> </Supertip> <Icon> <bt:Image size="16" resid="Icon.16x16" /> <bt:Image size="32" resid="Icon.32x32" /> <bt:Image size="80" resid="Icon.80x80" /> </Icon> </Control> </Group> </OfficeTab> </ExtensionPoint> </DesktopFormFactor> </Host> </Hosts> <Resources> <bt:Urls> <bt:Url id="Commands.Url" DefaultValue="URL to HTML" /> </bt:Urls> <bt:ShortStrings> <bt:String id="GroupLabel" DefaultValue="Name" /> <bt:String id="TaskpaneButton.Label" DefaultValue="Action" /> </bt:ShortStrings> </Resources> </VersionOverrides> </OfficeApp>
测试环境为https://outlook.office365.com/网页版,疑问:为何item.body不是预期的Body对象?
问题原因及解决方案
核心原因
- 运行模式不匹配:
prependAsync是仅支持撰写模式(新建/回复/转发邮件时)的修改类API,而当前插件配置为阅读模式(查看已发送/收件箱邮件时)。阅读模式下的item.body是只读对象,仅提供getAsync方法用于读取内容,不支持任何修改操作。 - API版本不足:
prependAsync要求Mailbox API最低版本为1.3,而你的manifest中声明的最低版本是1.1,版本不满足会导致API无法正常暴露。
具体修改步骤
1. 更新manifest配置,切换到撰写模式
- 修改
<FormSettings>部分,将阅读模式改为撰写模式:
<FormSettings> <Form xsi:type="ItemEdit"> <DesktopSettings> <SourceLocation DefaultValue="URL to HTML" /> <RequestedHeight>250</RequestedHeight> </DesktopSettings> </Form> </FormSettings>
- 修改规则部分,匹配撰写模式的邮件:
<Rule xsi:type="RuleCollection" Mode="Or"> <Rule xsi:type="ItemIs" ItemType="Message" FormType="Edit" /> </Rule>
- 更新ExtensionPoint为撰写模式的命令栏:
<ExtensionPoint xsi:type="MessageComposeCommandSurface"> <OfficeTab id="TabDefault"> <Group id="msgComposeGroup"> <Label resid="GroupLabel" /> <Control xsi:type="Button" id="msgComposeOpenPaneButton"> <Label resid="TaskpaneButton.Label" /> <Supertip> <Title resid="TaskpaneButton.Label" /> <Description resid="TaskpaneButton.Tooltip" /> </Supertip> <Icon> <bt:Image size="16" resid="Icon.16x16" /> <bt:Image size="32" resid="Icon.32x32" /> <bt:Image size="80" resid="Icon.80x80" /> </Icon> </Control> </Group> </OfficeTab> </ExtensionPoint>
2. 升级Mailbox API版本要求
- 更新主配置中的版本:
<Requirements> <Sets> <Set Name="Mailbox" MinVersion="1.3" /> </Sets> </Requirements>
- 更新VersionOverrides中的版本:
<Requirements> <bt:Sets DefaultMinVersion="1.3"> <bt:Set Name="Mailbox" /> </bt:Sets> </Requirements>
3. 代码中增加模式判断(可选)
为了避免模式错误导致的异常,可以在代码中先判断当前是否为撰写模式:
Office.onReady((info) => { if (info.host === Office.HostType.Outlook) { item = Office.context.mailbox.item; // 验证是否为撰写模式的邮件 if (item.isInWriteMode && item.itemType === Office.MailboxEnums.ItemType.Message) { item.body.prependAsync("text", function (asyncResult) { if (asyncResult.status === Office.AsyncResultStatus.Failed) { console.log("Action failed with error: " + asyncResult.error.message); return; } console.log(`"text" prepended to the body.`); }); } else { console.log("该操作仅支持在撰写邮件时使用"); } } });
内容的提问来源于stack exchange,提问作者Jinairu
相关产品推荐
相关产品推荐

