如何访问所选Outlook Web加载项JavaScript的字体?

时间:2018-02-26 13:40:52

标签: javascript office-js outlook-web-addins

在Word Web加载项中,我可以访问所选context.document.getSelection().font的字体,但我在Outlook Web Add-in中找不到它(在搜索之后),我只能获取由Office.context.mailbox.item.getSelectedDataAsync选择Office.CoercionType.Text参数,我该如何获取字体?

1 个答案:

答案 0 :(得分:3)

Outlook中的文本格式是用HTML完成的(假设格式不是纯文本)。您可以使用Office.CoercionType.Html返回基础HTML:

Office.initialize = function () {
    Office.context.mailbox.item
        .getSelectedDataAsync(Office.CoercionType.Html, {},
            function (asyncResult) {
                var htmlData = asyncResult.value.data;
                // do stuff
            });
}

由于HTML格式可能已设置在您选择的范围之外,您可能还想抓住整个正文。然后,您可以使用getSelectedDataAsync结果查找完整HTML正文中的当前选择:

function myFunction() {

    // Get the selected text
    Office.context.mailbox.item
        .getSelectedDataAsync('html', {}, function (asyncResult) {

            // Get the full body and pass through the selectedData
            // in the asyncContext. 
            Office.context.mailbox.item.body.getAsync("html", {
                    asyncContext: asyncResult.value.data
                },
                function callback(asyncResult) {
                    // Get the body from the result
                    let bodyDaya = asyncResult.value.data;

                    // Get the selectedData we passed in
                    let selectedData = asyncResult.asyncContext;

                    // Do stuff
                });

        });
}