1

I have a table in a word document that would have merged cells and I am trying to extract data via the word addin using office.js

Example:

  • Table (with merged columns)

I want to gather data in json i.e. for each headers an array pointing out the values present for them i.e.

  • Table expected result
{
   "Header 0,0": [['Cell 1.1', 'Cell 2.1']],
   "Header 3": [['Cell 3']],
   "Header 4": [['Cell 4']],
   "Header 5,6": [['Cell 5.1', 'Cell 6.1']],
}

Currently I have tried the approach to fetch the values from the table via office.js Table class object and directly iterating over the values but I am getting the following 2D array from it,

[
   ['Header 0,0', 'Header 3', 'Header 4', 'Header 5,6'],
   ['Cell 1.1', 'Cell 2.1', 'Cell 3', 'Cell 4', 'Cell 5.1', 'Cell 6.1']
]

From the above value we would not be able to form a relation of header to its data cells.

1 Answer 1

1

There are a few approaches I'd take to this problem.

The first is by far the simplest, but would be the least robust as the code would need re-working if headers changed.

Basically, if the headers don't change, just build a mapping object like:

{
   "Header 0,0": 2,
   "Header 3": 1,
   "Header 4": 1,
   "Header 5,6": 2,
}

Then you can take your array you've shown above, iterate over it and build your desired output.

Second would be to unmerge the header cells before getting the values, build your desired output and re-merge cells

Third would be to get a object defining the range of the merged headers and iterating over your values to get your desired output.

I took a look through the API briefly and I can't find anything for approach 2 or 3 unfortunately, both are possible in Excel via the Office-JS API though.

Reading this answer --> How to split a TableCell using Javascript for a Microsoft Word Add-in?

Makes me thing either option one is your easiest route, but you can also use pure web technologies like OpenXML to read the table and gather the information.

Here are some links that should help you in the correct direction -->

https://learn.microsoft.com/en-us/office/dev/add-ins/word/create-better-add-ins-for-word-with-office-open-xml

https://github.com/OfficeDev/Office-Add-in-samples/tree/main/Samples/word-add-in-load-and-write-open-xml

https://learn.microsoft.com/en-us/office/open-xml/word/how-to-insert-a-table-into-a-word-processing-document?tabs=cs-0%2Ccs-1%2Ccs-2%2Ccs-3%2Ccs-4%2Ccs#structure-of-a-table

https://stackoverflow.com/a/50347584/5079799

https://learn.microsoft.com/en-us/samples/officedev/pnp-officeaddins/word-get-set-edit-ooxml/

https://jiteshkumawat.github.io/oxml.js/

As I said before, its possible w/ Excel Office-JS API, but not w/ word to my understanding. As you haven't provided any code, there is no code in my answer, if you have trouble implementing a solution, you can start a new post though w/ code.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer, I am also thinking to handle this case OOXML would be a much better approach as we don't have any merge cell object present on the word. Will update once I get a proper solution or approach.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.