有没有办法让html从此函数返回给调用者?

时间:2019-04-01 19:34:03

标签: javascript node.js office-js

我正在使用OneNote插件设置Office.js,并使用以下代码从页面获取HTML。我正在调用该函数,它会打印出HTML,但不会返回。

我已经更新了代码以反映更普通的office.js调用。

function getHTMLcontent() {
try {
    OneNote.run(function (context) {

        var outline = context.application.getActiveOutlineOrNull();

        outline.load('id, type, paragraphs/id, paragraphs/type');

        return context.sync().then(function () {
            if (!outline.isNull) {
                var richTextParagraphs = [];
                var htmls = [];
                console.log("outline id: " + outline.id);
                for(var i = 0;  i < outline.paragraphs.items.length; i++){
                    var paragraph = outline.paragraphs.items[i];
                    console.log("paragraph type " + paragraph.type);
                    if (paragraph.type == "RichText"){
                        richTextParagraphs.push(paragraph);
                        var html = paragraph.richText.getHtml();
                        htmls.push(html);
                        paragraph.load("richtext/id, richtext/languageid")
                    }
                }

                return context.sync().then(function(){
                    for(var i = 0; i < richTextParagraphs.length; i++){
                        var richTextParagraph = richTextParagraphs[i];
                        console.log("Rich text paragraph id: " + richTextParagraph.richText.Id + " and " + richTextParagraph.richText.languageId)
                    }
                    for(var i = 0; i < htmls.length; i++){
                        var html = htmls[i];
                        console.log("Rich text paragraph html: " + html.value)
                    }
                });
            }
        });
    })
    .catch(function(error) {
        console.log("Error: " + error);
        if (error instanceof OfficeExtension.Error) {
            console.log("Debug info: " + JSON.stringify(error.debugInfo));
        }
    });
} catch (error) {
    OfficeHelpers.UI.notify(error);
    OfficeHelpers.Utilities.log(error);
}
}

0 个答案:

没有答案