Forum


Replies: 12   Views: 382
Column removal
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by fairshareitservices  · 20-05-2024 - 09:55

Hi,

I am generating a template document using the createDocx() method of CreateDocxFromTemplate and CreateDocx classes. This template has a table that has columns. Depending on certain values I need to remove the column of the table. If the value of the first row of any column is 'N.A.' then need to remove that column. The number of columns that need to be removed from the table is any no.

Posted by admin  · 20-05-2024 - 11:23

Hello,

You can use DOCXPath methods for this task. With getWordContents you can get the text contents of the chosen element (such as specific cell); then using removeWordContent you can remove specific elements.

To remove columns in a table you need to remove the cells (w:tc tag) of the rows (w:tr tag) in the specific position (1, 2...). In this case you need to use a custom XPath query. In the examples/DocxPath folder available in Advanced and Premium packages you can find many samples using all DOCXPath methods.

For example, the following sample removes the second column using the tables.docx file incuded in the package:

$docx = new CreateDocxFromTemplate('tables.docx');

$referenceNode = array(
    'customQuery' => '//w:tbl[1]/w:tr/w:tc[2]',
);
$docx->removeWordContent($referenceNode);

Regards.

Posted by fairshareitservices  · 22-05-2024 - 05:36

Thank you for your response. Can you please elaborate on how to use custom query 'To remove columns in a table you need to remove the cells (w:tc tag) of the rows (w:tr tag) in the specific position (1, 2...). In this case you need to use a custom XPath query. Can we remove the column after the generation of the table I know the Column Headings for which I need to remove the columns if the whole column contains a certain value like 'NA'.

Posted by admin  · 22-05-2024 - 07:23

Hello,

You can use getDocxPathQueryInfo or getWordContents to query if an element (such as a table, column or cell) contains a specific text content. With this information you can use removeWordContent to remove the needed elements.

We recommend you read the API pages of these methods and check and run the samples included in the package, that illustrate how to use all options.

Regards.

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

Thank you for getting back to me. It would be a great help if you could explain the custom query
'customQuery' => '//w:tbl[1]/w:tr/w:tc[2]' from the code that you specified

Posted by admin  · 04-06-2024 - 08:32

Hello,

The customQuery option available in DOCXPath and DOCXCustomizer methods uses XPath (XML Path Language) to query contents. XPath is an XML standard language.

To understand how XPath works, we recommend you read the documentation available on https://www.w3schools.com/xml/xpath_intro.asp.

The customQuery sample from our previous reply:

'customQuery' => '//w:tbl[1]/w:tr/w:tc[2]',

selects the second cells (w:tc tag) of all rows (w:tr tag) in the first table (w:tbl tag) using XPath.

Regards.

Posted by fairshareitservices  · 04-06-2024 - 09:14

Okay if we know the table then can we remove //w:tbl[1]

'customQuery' => '//w:tbl[1]/w:tr/w:tc[2]',

selects second cells (w:tc tag) of all rows (w:tr tag) in the first table (w:tbl tag) using XPath.

Posted by fairshareitservices  · 04-06-2024 - 11:18

With respect to 'customQuery' => '//w:tr/w:tc[2]' how to get column number from the table of the generated documnet which has specific text with it

Posted by admin  · 04-06-2024 - 12:33

Hello,

Please note that we replied to this same question in a previous message:

You can use getDocxPathQueryInfo or getWordContents to query if an element (such as a table, column or cell) contains a specific text content. With this information you can use removeWordContent to remove the needed elements.

With getDocxPathQueryInfo and getWordContents methods you can query elements to get positions, contents and other information.

Regards.

Posted by fairshareitservices  · 05-06-2024 - 12:23

Hi for below code table-cell is not getting deleted

$referenceNode = array(

                       'type' => 'table-cell',

                        'customQuery' => '//w:tr/w:tc[contains(., "${sipcol_removal}")]',      

                   );                  

                    $templateDocx->removeWordContent($referenceNode);

if the cell contains ${sipcol_removal} then this table-cell should get removed

Posted by admin  · 05-06-2024 - 13:21

Hello,

The following sample code uses tables.docx (file available in the package: examples/files/docxpath folder) to remove the cell that contains "22":

$docx = new CreateDocxFromTemplate('tables.docx');

$referenceNode = array(
    'customQuery' => '//w:tr/w:tc[contains(., "22")]',
);
$docx->removeWordContent($referenceNode);

$docx->createDocx('output_table');

If you run this code using the included file, you can check the cell is removed from the DOCX output.

If we replace in this same file "22" with "${sipcol_removal}" and run:

$docx = new CreateDocxFromTemplate('tables.docx');

$referenceNode = array(
    'customQuery' => '//w:tr/w:tc[contains(., "${sipcol_removal}")]',
);
$docx->removeWordContent($referenceNode);

$docx->createDocx('output_table');

The cell is removed correctly too. We recommend you check and run these samples using the file included in the package. Also note that XPath is case sensitive by default (https://www.phpdocx.com/documentation/snippets/case-insensitive-xpath).

For further support, we'd need to check the DOCX file you are using. Please open a support ticket (https://www.phpdocx.com/support) and attach the DOCX document you are using, we'll generate a sample using it.

Regards.

Posted by fairshareitservices  · 17-06-2024 - 10:23

I have the below code to remove cell contents in php. 

 $referenceNodeSIP = array(

        'customQuery' => '//w:tr/w:tc[contains(., "${remove_sipcol}")]',    

    );                  

    $docx->removeWordContent($referenceNodeSIP);

 

    //below code is to remove cells of header of second portfolio table that don't have values

  $referenceNodePortfolio = array(

        'customQuery' => '//w:tr/w:tc[contains(., "${remove_portfolio_tbl}")]',    

    );                  

    $docx->removeWordContent($referenceNodePortfolio); 

When i run this code it runs and generates an output file. But when I try to open output file it doesn't get open. Got an error 'Word experienced an error trying to open the file. Try these suggestions
1. Check the file permission

2. Check the sufficient free memory

And values of the cell are set before calling the function where above code resides

Posted by admin  · 17-06-2024 - 14:06

Hello,

Please note that, as explained in previous replies, we'd need to check your DOCX. We recommend you open a support ticket with the DOCX document you are using and we'll generate a custom sample using it.

Also, note that a row must include at least one cell. If you need to remove all cells in a row, the whole row must be removed.

Regards.