我有一个笔记本,里面有这些

OneNote API的文档在这里(已经选择了段落类) https://learn.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.发布于 2020-06-11 08:00:29
代码中的问题是,您没有等待项目承诺,因此,它会引发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);
}
}我希望它能帮到你!
https://stackoverflow.com/questions/62272535
复制相似问题