Searching list items in word document but the list items not be found inside of tables

99 views Asked by At

I have the next source code to search list in a word document and create a treeview only with the list items of docs:

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

I have the next word document: I have the next word document:

As you can see all chapters are in the same list. The result in my tree view is the next: tree model

As you can see the Chapter 3 has been skipped. The log messages are the next: log messages

I have not idea why the list not cover inside of table. Anyone have any idea about that?

Thank you very much for your time!

0

There are 0 answers