如何使用OneNote Javascript API阅读OneNote缩进段落?
我有一个笔记本,里面有这些如何使用OneNote Javascript API阅读OneNote缩进段落?,javascript,office-js,onenote-api,Javascript,Office Js,Onenote Api,我有一个笔记本,里面有这些 OneNote API的文档位于此处(已选择段落类) 如果我运行此代码: export async function run() { try { await OneNote.run( async context => { var page = context.application.getActivePage(); var pageContents = page.contents; var firstPageC
OneNote API的文档位于此处(已选择段落类) 如果我运行此代码:
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);
}
}
代码中的问题是您没有等待项目承诺,因此,它抛出
PropertyNotLoaded
错误。发生这种情况的原因是您没有等待上下文同步,例如,在队列中:
context.sync()//这是一个承诺,因此需要.then()方法
console.log(段落.richText.text)
调试器;
以下代码适用于多个缩进行,并使用await/async
方法等待承诺:
导出异步函数run(){
试一试{
等待OneNote。运行(异步(上下文)=>{
var page=context.application.getActivePage();
var pageContents=page.contents;
var firstPageContent=pageContents.getItemAt(0);
var段落=firstPageContent.outline.Pages;
段落加载('richText/text');
//运行排队命令,并返回指示任务完成的承诺。
wait context.sync();
//每个级别0段落
对于(让段落中的段落。项目){
//读这段话
等待阅读段落(上下文,段落,0);
}
});
}捕获(错误){
日志(“错误:+错误”);
}
}
//读取段落数据和子数据
异步函数READPARATION(上下文、段落、级别){
试一试{
段落.装载(“项目”);
wait context.sync();
log('Level'+Level+'>Data:',段落.richText.text);
让LevelPages=段落;
level段落加载('richText/text');
wait context.sync();
对于(Left p of Level.items){
等待阅读段落(上下文,p,级别+1);
}
}捕获(错误){
console.log('级别错误'+级别错误);
}
}
我用它来做测试,得到了结果
我希望它能帮助你 谢谢大家!!我会在今天晚些时候或明天看一看,我会确认它对我有效,我真的很感谢你的帮助,谢谢你,一周前我开始使用Javascript,我已经接近最终解决方案了。我仍在琢磨这些承诺的运作方式
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.