Forum


Replies: 1   Views: 85
How to remove specific table cell? or table row?

Posted by fairshareitservices  · 08-04-2024 - 11:01

We are using below code to remove a specific table on condition. But we want to remove only the cell that contain 'Regular' text. Instead of removing complete table from document need to delete specific row or cell. 

if($placeHolderName == 'tracking_error_regular')
        {
        $templateDocx->removeTableRowByTemplateVariable($placeHolderName);
        $referenceNode = array(
            'type' => 'table',
            'contains' => 'TRACKING ERROR',
        );
        $templateDocx->removeWordContent($referenceNode);
        mLog("Removed table for tracking_error_regular of $schemeCode");
        return $templateDocx;
        }

Posted by admin  · 08-04-2024 - 15:30

Hello,

To remove a row or a cell, you can use table-row and table-cell types or use a custom XPath query.
In the following sample included in the package: DocxPath/removeWordContent/sample_9.php you can find a sample removing a row using a customQuery.

Using types, please check the following samples that use the tables.docx file included in the package.

Remove the row that contains "14":

$docx = new CreateDocxFromTemplate('../../files/docxpath/tables.docx');

// get the reference node to be removed
$referenceNode = array(
    'type' => 'table-row',
    'parent' => '/',
    'contains' => 14,
);

$docx->removeWordContent($referenceNode);

Remove the cell that contains "14":

$docx = new CreateDocxFromTemplate('../../files/docxpath/tables.docx');

// get the reference node to be removed
$referenceNode = array(
    'type' => 'table-cell',
    'parent' => '/',
    'contains' => 14,
);

$docx->removeWordContent($referenceNode);

If instead of the whole cell you want to remove only the contents, you should use a custom XPath query to select only the elements in the cell.

Regards.