【问题标题】:How do I read OneNote indented paragraphs using OneNote Javascript APIs?如何使用 OneNote Javascript API 阅读 OneNote 缩进段落?
【发布时间】:2020-09-28 00:52:16
【问题描述】:

我有一个包含这些的笔记本

OneNote API 的文档在这里(已选择段落类) https://docs.microsoft.com/en-us/javascript/api/onenote/onenote.paragraph?view=onenote-js-1.1

如果我运行这段代码:

export async function run() {
  try {
    await OneNote.run( async context => {
      var page = context.application.getActivePage();
      var pageContents = page.contents;
      var firstPageContent = pageContents.getItemAt(0);
      var paragraphs=firstPageContent.outline.paragraphs
      //firstPageContent.delete()
      //var out_line=firstPageContent.outline
      paragraphs.load('richText/text');




      // Run the queued commands, and return a promise to indicate task completion.
      return context.sync()
        .then(function () {
              //debugger;
              console.log("Items",paragraphs.items);
              for (var i=0; i < paragraphs.items.length; i++)
                {
                  var paragraph= paragraphs.items[i] 
                  paragraph.load('items')
                  context.sync()   
                  console.log(paragraph.richText.text)
                  show_next_level(paragraph,i)
                }

          });


    });
} catch (error) {
    console.log("Error: " + error);
}
}

export async function show_next_level(paragraph,i) {
  try {
    await OneNote.run( async context => {
      //debugger;
      //var paragraphs=par.paragraphs
      var paragraphs=paragraph.paragraphs
      paragraphs.load('richText/text');
      //console.log("Items",paragraphs.items);


      // Run the queued commands, and return a promise to indicate task completion.
      return context.sync()
        .then(function () {

          console.log("Items",paragraphs.items);
          for (var i=0; i < paragraphs.items.length; i++)
            {
              var paragraph= paragraphs.items[i] 
              paragraph.load()

              context.sync()  
              console.log(paragraph.richText.text)
              debugger;
              //paragraph.richText.text=paragraph.richText.text+'►'
              show_next_level(paragraph,i)
            }

          });


    });
} catch (error) {
    console.log("Error: " + error);
}
}

经过多次迭代,我设法读取了下一级缩进,但仍然出现错误。上面的输出现在是

Items (4) [h, h, h, h]
One line 1 
One line 2
One line 3
One line 4
Items [h]
Two line 0
Items (3) [h, h, h]
Two line 1
Two line 2
Two line 3
Items []
5taskpane.js:192 Error: PropertyNotLoaded: The property 'items' is not available. Before reading the property's value, call the load method on the containing object and call "context.sync()" on the associated request context.

【问题讨论】:

    标签: javascript office-js onenote-api


    【解决方案1】:

    您的代码中的问题是您没有等待 Item Promises,因此,它会抛出 PropertyNotLoaded 错误。发生这种情况是因为您没有等待 上下文同步,例如。在行中:

    context.sync() // It's a promise, so it requires .then() method
    console.log(paragraph.richText.text)
    debugger;
    

    以下代码适用于多行缩进并使用await/async 方法等待承诺:

    export async function run() {
      try {
        await OneNote.run(async (context) => {
          var page = context.application.getActivePage();
          var pageContents = page.contents;
          var firstPageContent = pageContents.getItemAt(0);
          var paragraphs = firstPageContent.outline.paragraphs;
    
          paragraphs.load('richText/text');
    
          // Run the queued commands, and return a promise to indicate task completion.
          await context.sync();
    
          // Foreach level 0 paragraph
          for (let paragraph of paragraphs.items) {
            // Read the paragraph
            await readParagraph(context, paragraph, 0);
          }
        });
      } catch (error) {
          console.log("Error: " + error);
      }
    }
    
    // Read Paragraph data and Child data
    async function readParagraph(context, paragraph, level) {
      try {
        paragraph.load('items');
    
        await context.sync();
        console.log('Level ' + level + ' > Data:', paragraph.richText.text);
    
        let levelParagraphs = paragraph.paragraphs;
        levelParagraphs.load('richText/text');
    
        await context.sync();
    
        for (let p of levelParagraphs.items) {
          await readParagraph(context, p, level + 1);
        }
      } catch (error) {
        console.log('Error in Level ' + level, error);
      }
    }
    

    我使用these data 进行测试,these 是结果。

    希望对你有帮助!

    【讨论】:

    • 谢谢!我会在今天晚些时候或明天看看,我会确认它对我有用,非常感谢您的帮助
    • 工作了,谢谢,对于一个像一周前开始使用 Javascript 的人来说,我非常接近最终的解决方案。我仍在咀嚼 Promise 的工作方式
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-07
    相关资源
    最近更新 更多