Categories > OpenTBS with DOCX >

Dynamic table incorrectly displayed when number of columns > 8 and "AutoFit to Contents" activated

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: Delgan
Date: 2015-07-20
Time: 13:35

Dynamic table incorrectly displayed when number of columns > 8 and "AutoFit to Contents" activated

Hello.

First of all, I would like to thank you very much for your work. OpenTBS is an amazing, very useful and very powerful tool.
Thank you for all the energy you have invested in this project, thank you for sharing it for free, and thank you to be still present on this forum and helping users.

I wanted to evoke a bug I encountered using OpenTBS to generate Word 2010 documents including tables filled dynamically with a non predefined number of columns.
Moreover, it happens when the "AutoFit to Contents" option from table menu is enabled. This is annoying as this option is very useful for dynamic tables.

When there are not too many columns it works well, the width fits the content. However, if there is too much, the size does not fit in, it keeps the original one when the cell contained the OpenTBS's block and this causes a distortion of the table which exceeds the size of the Word sheet chaotically.

My DOCX Template is a 2 rows and 1 column simple table:

[head.title;block=tbs:cell;parallel=tbs:table]
[body.[head.title];block=tbs:row]

My PHP script looks like this (I added custom parameters used to see when the bug shows up):

$head = array();
$body = array();

$nb_columns = 8;
$nb_rows = 2;
$cell_length = 7;

for ($i = 0; $i < $nb_columns; $i++) {
    $title = substr(md5(rand()), 0, $cell_length);
    $head[] = array("title" => $title);
}

for ($i = 0; $i < $nb_rows; $i++) {
    $row = array();
    for ($j = 0; $j < $nb_columns; $j++) {
        $title = $head[$j]["title"];
        $value = substr(md5(rand()), 0, $cell_length);
        $row[$title] = $value;
    }
    $body[] = $row;
}

$TBS->LoadTemplate('template.docx');

$TBS->MergeBlock("head", $head);
$TBS->MergeBlock("body", $body);

$TBS->Show(OPENTBS_FILE, "report.docx");

From the few tests I conducted, I noticed:
- The number of rows does not seem to have any influence
- The size of contents does not seem to have any influence
- The margin page does not seem to have any influence

Nevertheless, the cells left and right margins (accessed from options of table properties) have an influence: the less it is, the more the number of columns can be without bug.
Examples:
   - 0.05 cm (default value) -> Max columns = 8
   - 0.00 cm -> Max columns = 9
   - 1.00 cm -> Max columns = 7

For now, I have no other way but to set the width of the cells to a small value, and activate the autofit by hand after generating the document. This is not very convenient because I use a lot of dynamic tables.
I wanted to know if this bug was known, and if a fix or workaround was possible?

Thank you in advance!
By: Skrol29
Date: 2015-07-20
Time: 15:24

Re: Dynamic table incorrectly displayed when number of columns > 8 and "AutoFit to Contents" activated

Hi Delgan,

Thanks for your comments about TBS.

Thanks also for your clear exploitations, it helped me to reproduce the behavior with Ms Word 2013.


The problem seems to come from the width of each cols which is actually automatically saved by Ms Word in the column definition. That is in the in the XML element <w:tblGrid>. But those values are ignored when option « AutoFit to Contents » is set to on. Those values are probably saved by Ms Word in order to display the last columns definition when you change this option back to false.

The bug disappears when you delete those superfluous XML elements.
This can be done in two ways :

Your can delete quickly all column size of all table at the PHP side :
(faster if coded before any MergeBlock())
$TBS->PlugIn(OPENTBS_DELETE_ELEMENTS, array('w:tblGrid'));
This field will be move in the proper XML entity, and then delete it.


Or you can delete it for a single table, by adding anywhere in the table the following TBS field:
[onload;att=w:tblGrid#any;magnet=w:tblGrid]

I will have look to see if a fix can be supported in native by OpenTBS.
By: Delgan
Date: 2015-07-21
Time: 07:19

Re: Dynamic table incorrectly displayed when number of columns > 8 and "AutoFit to Contents" activated

You are awesome, this works perfectly!

I can not tell you how much I am grateful, thank you so much!