I keep a notebook that contains the following entries:
https://i.stack.imgur.com/MLdO0.png
For information on OneNote APIs, you can refer to this link (paragraph class selected already) https://learn.microsoft.com/en-us/javascript/api/onenote/onenote.paragraph?view=onenote-js-1.1
When executing the following code:
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 () {
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 => {
var paragraphs = paragraph.paragraphs;
paragraphs.load('richText/text');
// 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 subParagraph = paragraphs.items[i];
subParagraph.load();
context.sync();
console.log(subParagraph.richText.text);
debugger;
show_next_level(subParagraph, i);
}
});
});
} catch (error) {
console.log("Error: " + error);
}
}
After several iterations, I successfully managed to read the next level of indentation but encountered an error in the process. The current output is as follows:
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.