2016-11-23 126 views
2

example中,提供了用于获取RichText的代码。它能够获得页面的文本内容,但我似乎无法得到它返回页面格式化内容的HTMLOneNote加入:获取HTML内容

例如:

Header:

  • A
  • B

应该是:

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

然而,示例代码使用richText/text和只返回Header:。是否有可能做类似richText/HTML并获得上面显示的HTML? (注意:我只想使用加载项而不是OneNote REST API。)

谢谢!从文档

代码片段:

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

richTextParagraph.richText对象有哪些属性?其中有一个html吗?如果是这样,它的返回类型是什么? –

+0

属性只有'id'和'text'(都是'String'类型)。根据https://dev.office.com/reference/add-ins/onenote/richtext –

回答

2

我们还没有记录它,但(这将很快加入),但富文本对象上的“getHtml()”方法。这里是一个示例代码片段。

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

注意,这不是整页HTML,只能在富文本级别。上面的函数也没有考虑到所有富文本对象,页面中可能会有更多(表格内部,子段落内......) –

+0

Jorge,感谢您的快速回复。上面的代码在Chrome中引发了一个语法错误警告。我想这可能是一个复制和粘贴问题,所以我写了出来,但我得到了错误:'错误:ValueNotLoaded:结果对象的值尚未加载。在读取value属性之前,请在关联的请求上下文中调用“context.sync()”。请您澄清一下吗? –

+0

我更新了代码并进行了测试。它现在应该工作。 –