我正在使用 API 1.4 编写 Outlook 加载项,并尝试将br
标签附加到 Outlook 2016 作曲家正文。
但是,一旦开始键入 Outlook 就会重新格式化文档并删除元素。
下面是实现,它以 1 秒的轮询间隔调用——据我所知,没有更好的方法来观察 DOM。
Office.context.mailbox.item.body.getAsync(
Office.CoercionType.Html, {
asyncContext: "Body"
},
function(asyncResult) {
// DOMParser
var doc = document.implementation.createHTMLDocument("document");
doc.documentElement.innerHtml = asyncResult.value;
// Append <br> if not found
var isModified = false;
var $html = $(doc.documentElement);
var $body = $html.find("body");
if (!!$body.find("br[id*='unique-identifier']").length) {
$body.append("<br id='unique-identifier'>");
isModified = true;
}
// Set if <br> was not found
if (isModified) {
Office.context.mailbox.item.body.setAsync(
$body.html(), {
coercionType: Office.CoercionType.Html
}
function(asyncResult) {}
)
}
}
);
随后的 getAsync 会显示br
标签,直到您开始输入,然后它会被删除并再次调用 setAsync。这是一个烦恼,因为 setAsync 会导致光标跳到作曲家的开头并干扰用户流畅地输入。br
由于标签的性质,在这种情况下,我无法使用 prepend 或 setSelectedDataAsync 。
我假设重新格式化是通过从纯 HTML 转换为基于 asyncResult 值的 Word Doc 发生的,但我不知道如何解释这一点。
注意:这也与 OWA 不同,OWA 不会在 Word Doc 和 HTML 之间进行转换,但会为 theid
和其他属性添加前缀,例如class
withx_
并导致光标四处跳动。