如何解决在Word文档中搜索列表项,但在表内部找不到列表项
我有下一个源代码来搜索Word文档中的列表,并仅使用文档的列表项创建树形视图:
export async function getChaptersAndSteps() {
await removeBookmarks()
await window.Word.run(async (context) => {
console.log('getChaptersAndSteps')
const tree = new TreeModel()
const root = tree.parse({
id: uuid(),stepIndex: '',name: 'Root',children: []
})
let orderId = 0
const arrayList = []
const arrLevelCount = Array(9).fill(0)
try {
const myListCollectionParagraphs = context.document.body.lists
myListCollectionParagraphs.load([
'id','levelExistences','levelTypes','paragraphs','paragraphs/text','paragraphs/listItemOrNullObject','paragraphs/listItemOrNullObject/listString','paragraphs/listItemOrNullObject/level'
])
await context.sync()
for (
let index = 0;
index < myListCollectionParagraphs.items.length;
index++
) {
console.log('---- NEW LIST ----',index)
console.log('id:',myListCollectionParagraphs.items[index].id)
const itemList = myListCollectionParagraphs.items[index]
const isCorrectList = true
for (let inx = 0; inx < itemList.levelExistences.length; inx++) {
if (itemList.levelExistences[inx]) {
if (itemList.levelTypes[inx].toUpperCase() !== 'NUMBER') {
isCorrectList = false
}
}
}
if (isCorrectList) {
const listParagraphs =
myListCollectionParagraphs.items[index].paragraphs.items
console.log('Nº paragraphs:',listParagraphs.length)
for (let cont = 0; cont < listParagraphs.length; cont++) {
const myListParagraph = listParagraphs[cont]
console.log('paragraph Nº',cont)
console.log('text',myListParagraph.text)
console.log(
'listString',myListParagraph.listItemOrNullObject.listString
)
console.log('level',myListParagraph.listItemOrNullObject.level)
const listItemParagraph = myListParagraph.listItemOrNullObject
await context.sync()
if (!listItemParagraph.isNullObject) {
const currentLevel = listItemParagraph.level
arrLevelCount[currentLevel] = arrLevelCount[currentLevel] + 1
arrLevelCount.fill(0,currentLevel + 1) // reset the above levels
const myRange = myListParagraph.getRange('Start')
const idBookMark = BKM + arrLevelCount.join('_')
console.log('idBookMark',idBookMark)
myRange.insertBookmark(idBookMark)
arrayList[listItemParagraph.level] = idBookMark
const parentChapter = listItemParagraph.getAncestorOrNullObject()
parentChapter.load([
'text','listItemOrNullObject','listItemOrNullObject/listString','listItemOrNullObject/level'
])
await context.sync()
console.log('parentChapter',parentChapter)
const nodeToAdd = tree.parse({
id: idBookMark,stepIndex: listItemParagraph.listString.trim(),name: myListParagraph.text.slice(0,60),typeStep: STEP,orderId: orderId++,children: []
})
if (parentChapter.isNullObject) {
root.addChild(nodeToAdd)
} else {
const searchID =
arrayList[parentChapter.listItemOrNullObject.level]
const foundNode = root.first(function (node) {
return node.model.id === searchID
})
if (foundNode) {
foundNode.addChild(nodeToAdd)
}
}
}
}
}
// }
}
} catch (e) {
console.log('Error in getChaptersAndSteps',e)
}
console.log('getChaptersAndSteps END')
})
}
您可以看到所有章节都在同一列表中。
我的树状视图中的结果是下一个:
我不知道为什么列表不包含在表中。 有人对此有任何想法吗?
非常感谢您的光临!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。