Issue with getOoxml() and insertOoxml() for a ContentControl with in a Table

258 Views Asked by At

I have multiple contentConrols in a table and trying to implement hide/display feature.
While hide - taking getOoxml() of the ContentControl inside the table and deleting it.
While Display - InsertOoxml(xml,'Replace') method in the same table to create again that ContentControl.
But Iam facing below two issues.

  1. If I do hide/display more than one time (second attempt) unable to insert as ContentControl with that Ooxml
    and showing as corrupted.
  2. After using Insertooxml(xml,'Replace') method getting extra paragraph every time.

    If there is no ContentControl inside a table in the document, it is working fine.

    Could someone help me how to resolve these issues?
2

There are 2 best solutions below

0
Jinghui-MSFT On

Thanks for reporting this issue. It has been put on our backlog<Bug#6255624> for internal track. We unfortunately have no timelines to share at this point.

0
Viral Shah On

Instead of deleting the content control from the table, can you try updating Ooxml and add the <w:vanish/> tag for content controls that you wish to hide? The vanish tag will hide the content control and not delete it. If you wish to display it again, just remove the vanish tag.

const tableCells = tblRowXml[i]?.getElementsByTagName('w:tc');
for (let j = 0; j < tableCells.length; j++) {
    let newCellPropertyWpPr = document.createElement('w:pPr');
    let newCellPropertyWrPr = document.createElement('w:rPr');
    let vanishProperty = '<w:vanish/>';
    newCellPropertyWrPr.innerHTML = vanishProperty;
    newCellPropertyWpPr.innerHTML = newCellPropertyWrPr.outerHTML;

    /* Add vanish nodes to table cell pPr */
    const pElement = tableCells[j]?.getElementsByTagName('w:p');
    if (pElement[0]) {
        pElement[0]?.insertBefore(newCellPropertyWpPr, pElement[0]?.childNodes[0]);
    }


    /* Add vanish nodes to table cell rPr */
    const rElement = tableCells[j]?.getElementsByTagName('w:r');
    if (rElement[0]) {
        rElement[0]?.insertBefore(newCellPropertyWrPr, rElement[0]?.childNodes[0]);
    }

We have done similar for our word add-in as well, where we are hiding entire table row if it contains all zero values.