Search code examples
opentbstinybutstrong

Generate table with OpenTBS - each row in separate cell


I try to create an ODT file (or docx) with table inside. I read and search all SO and support forum but have no idea how to make something like this:

cell1.val1  |  cell2.val1  |  cell3.val1
cell1.val2  |  cell2.val2  |  cell3.val2
cell1.val3  |  cell2.val3  |  cell3.val3
------------|--------------|------------
cell4.val1  |  cell5.val1  |  cell6.val1
cell4.val2  |  cell5.val2  |  cell6.val2
cell4.val3  |  cell5.val3  |  cell6.val3
----------------------------------------`

and so on, and on... I don't know how to build the array and how to create a template for it. If someone has a similar solution and could help with it I will be very appreciate.


Solution

  • There is two merges in your table: merging the different cells in the table and merging items in a cell.

    For merging the different cells in the table you can merge a block with option serial . See example and doc.

    For merging the items in a cell, you can use a sub-block. The simplest are « automatic sub-blocks ». See example and doc.

    So if your data are structured like this :

    $data = array(
       array(
        'cell_id' => 1,
        'items' => array(
            array('value' => 1),
            array('value' => 2),
            array('value' => 3),
            // ...
        ),
       ),
       array(
        'cell_id' => 2,
        'items' => array(
            array('value' => 1),
            // ...
        ),
       ),
       ...
    );
    

    then your template can be like this :

    -------------------------------------------------------------------------------------------------------------------------------
    | Column 1                                | Column 2                                | Column 3                                |
    -------------------------------------------------------------------------------------------------------------------------------
    | [b;block=tbs:row;serial]                |                                         |                                         |
    | [b_1.cell_id;block=tbs:cell;sub1=items] | [b_2.cell_id;block=tbs:cell;sub1=items] | [b_3.cell_id;block=tbs:cell;sub1=items] |
    | [b_1_sub1.value;block=tbs:p]            | [b_2_sub1.value;block=tbs:p]            | [b_3_sub1.value;block=tbs:p]            |
    -------------------------------------------------------------------------------------------------------------------------------
    

    In this example, items in a cell are merged on a paragraph (block=tbs:p) assuming that the TBS fields is placed in its own paragraph. You can also use a nested table, then you have to specify block=tbs:row.