在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')
  })
}

我有下一个word文档:

I have the next word document:

您可以看到所有章节都在同一列表中。 我的树状视图中的结果是下一个:

tree model

您可以看到第3章已被跳过。 日志消息是下一条:

log messages

我不知道为什么列表不包含在表中。 有人对此有任何想法吗?

非常感谢您的光临!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...