如何修改代码获取OneNote页面NoteTags属性并清除已勾选的ToDo标签
代码修改方案
一、获取NoteTags的Id、Status、Type属性
原代码仅加载了段落的基础属性,需要补充加载noteTags集合及其子属性,修改逻辑如下:
- 调整
paragraphs.load的入参,显式声明需要加载的noteTags属性 - 遍历段落时新增遍历
noteTags集合,输出对应属性值
二、清除已勾选的ToDo标签
遍历所有NoteTag,筛选出类型为待办(ToDo)且状态为已完成(已勾选)的标签,调用remove方法移除,最后提交同步即可。
完整修改后代码
function getAndClearCompletedToDoTags() { OneNote.run(function (context) { var pageContents = context.application.getActivePage().contents; var pageContent = pageContents.getItemAt(0); var paragraphs = pageContent.outline.paragraphs; // 追加加载noteTags的相关属性 paragraphs.load("id, type, richText, noteTags/id, noteTags/status, noteTags/type"); return context.sync() .then(function () { $.each(paragraphs.items, function(index, paragraph) { // 输出段落基础属性 console.log( "Paragraph Id: " + paragraph.id + "; Type: " + paragraph.type + "; Text: " + paragraph.richText.text); // 遍历当前段落的所有NoteTag $.each(paragraph.noteTags.items, function(tagIdx, noteTag) { // 输出NoteTag属性 console.log( " NoteTag Id: " + noteTag.id + "; Status: " + noteTag.status + // 0=未完成/1=已完成/2=禁用 "; Type: " + noteTag.type // 枚举值对应不同标签类型,ToDo为0 ); // 筛选已勾选的ToDo标签并移除 if (noteTag.type === OneNote.NoteTagType.toDo && noteTag.status === OneNote.NoteTagStatus.completed) { noteTag.remove(); } }) }); // 提交标签移除的修改 return context.sync(); }); }) .catch(function(error) { onError(error); }); }
补充说明
- NoteTag的状态枚举:
OneNote.NoteTagStatus.normal对应未勾选,OneNote.NoteTagStatus.completed对应已勾选 - 若仅需修改勾选状态而非移除标签,可直接给
noteTag.status赋值后同步即可,无需调用remove方法
内容的提问来源于stack exchange,提问作者mjRola
相关产品推荐
相关产品推荐

