如何使用 OneNote Javascript API 阅读 OneNote 缩进段落?
How do I read OneNote indented paragraphs using OneNote Javascript APIs?
我有一个包含这些的笔记本
OneNote API 的文档在这里(第 class 段已被选中)
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.
您的代码中的问题是您没有等待 Item Promises,因此,它抛出 PropertyNotLoaded
错误。发生这种情况是因为您没有等待 context sync 例如。在行中:
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 for testing and these结果。
希望对你有帮助!
我有一个包含这些的笔记本
OneNote API 的文档在这里(第 class 段已被选中) 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.
您的代码中的问题是您没有等待 Item Promises,因此,它抛出 PropertyNotLoaded
错误。发生这种情况是因为您没有等待 context sync 例如。在行中:
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 for testing and these结果。
希望对你有帮助!