【问题标题】:OneNote Add in: Getting HTML contentOneNote 插件:获取 HTML 内容
【发布时间】:2016-11-23 23:55:41
【问题描述】:

example 中提供了获取 RichText 的代码。它能够获取页面的 plain 文本内容,但我似乎无法让它返回页面的 HTML 格式化内容。

例如:

标题:

  • 一个
  • B

应该是:

<p>Header:</p>
<ul>
  <li>A</li>
  <li>B</li>
</ul>

但是,示例代码使用richText/text 并且只返回Header:。是否可以执行类似 richText/HTML 之类的操作并获得上面显示的 HTML? (注意:我只想使用 插件,而不是 OneNote REST API。)

谢谢!

文档中的代码 sn-p:

OneNote.run(function (context) {

// Get the collection of pageContent items from the page.
var pageContents = context.application.getActivePage().contents;

// Get the first PageContent on the page, and then get its outline's paragraphs.
var outlinePageContents = [];
var paragraphs = [];
var richTextParagraphs = [];
// Queue a command to load the id and type of each page content in the outline.
pageContents.load("id,type");

// Run the queued commands, and return a promise to indicate task completion.
return context.sync()
    .then(function () {
        // Load all page contents of type Outline
        $.each(pageContents.items, function(index, pageContent) {
            if(pageContent.type == 'Outline')
            {
                pageContent.load('outline,outline/paragraphs,outline/paragraphs/type');
                outlinePageContents.push(pageContent);
            }
        });
        return context.sync();
    })
    .then(function () {
        // Load all rich text paragraphs across outlines
        $.each(outlinePageContents, function(index, outlinePageContent) {
            var outline = outlinePageContent.outline;
            paragraphs = paragraphs.concat(outline.paragraphs.items);
        });
        $.each(paragraphs, function(index, paragraph) {
            if(paragraph.type == 'RichText')
            {
                richTextParagraphs.push(paragraph);
                paragraph.load("id,richText/text");
            }
        });
        return context.sync();
    })
    .then(function () {
        // Display all rich text paragraphs to the console
        $.each(richTextParagraphs, function(index, richTextParagraph) {
            var richText = richTextParagraph.richText;
            console.log("Paragraph found with richtext content : " + richText.text + " and richtext id : " + richText.id);
        });
        return context.sync();
    });
})
.catch(function(error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
}); 

【问题讨论】:

标签: office-js onenote onenote-api


【解决方案1】:

我们还没有记录它(很快就会添加),但是在richText 对象上有一个“getHtml()”方法。这是一个示例 sn-p。

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));
    }
});

【讨论】:

  • 请注意,这不是整页 HTML,仅在富文本级别。上面的函数也没有考虑到所有富文本对象,页面中可能还有更多(表格内,子段落中......)
  • 豪尔赫,感谢您的快速回复。上面的代码在 Chrome 中引发了我的语法错误警告。我想这可能是复制和粘贴的问题,所以我手写了它,但后来我得到了错误:Error: ValueNotLoaded: The value of the result object has not been loaded yet. Before reading the value property, call "context.sync()" on the associated request context.你能澄清一下吗?
  • 我已经更新了代码并进行了测试。它现在应该可以工作了。
  • 是否可以从表格对象中获取 HTML,还是必须遍历表格的每个单元格才能获取 HTML?
  • 不幸的是,没有。你必须迭代。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-18
  • 2013-10-29
  • 2022-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多