2016-11-23 13 views
2

exampleには、リッチテキストを取得するためのコードが用意されています。 を平文のページのテキストコンテンツにすることはできますが、のHTMLのページコンテンツを返すようにはできません。例えばOneNoteアドイン: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'型)です。 Per 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

ジョージ、迅速な返答をいただきありがとうございます。上記のコードは、Chromeで構文エラーの警告を表示しています。私はそれがコピー&ペーストの問題かもしれないと思ったので、手で書きましたが、エラーが出ました: 'Error:ValueNotLoaded:結果オブジェクトの値がまだロードされていません。 valueプロパティを読み込む前に、関連するリクエストコンテキストで "context.sync()"を呼び出します。 –

+0

コードを更新してテストしました。今はうまくいくはずです。 –

関連する問題