Categories > OpenTBS with DOCX >

How to merge table cells?

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: ryan
Date: 2013-03-19
Time: 09:01

How to merge table cells?


I want to merge cells with the same value in a table column. So I sort the data source by the column to make sure all the same values are in conjunction.

I found that in document.xml

begining cell with the code:
<w:vMerge w:val="restart"/>

continuing cell with the code:
<w:vMerge />

Is there any method to finish my job, like ope=mergeCell ???Or should I write my own mergeCell method??

thanks.
By: Skrol29
Date: 2013-03-19
Time: 12:56

Re: How to merge table cells?

Hi Ryan,

For merging cells vertically in a Ms Word table, there must be an element
<w:vMerge w:val="restart"/> in cells that are starting a group
and
<w:vMerge w:val="continue"/> or <w:vMerge/> for other cells

For doing this, you can use parameter "att" to change an attributes in the underlying XML.
But you have to give a value on each row in order to indicate what type of cell it is (restart or continue).

So first, you have to add a column in your data. It can be a real column 'merge' that you calculate yourself using SQL or PHP,
or it can be a virtual colmun added thanks to parameter "ondata" and a custom function.

Example l:
DOCX:
[b;block=w:tr;ondata=f_add_column]

PHP:
function f_add_column($BlockName,&$CurrRec,$RecNum) {
  static $prev_val='';
  if ($prev_val==$CurrRec['category']) {
    $CurrRec['merge'] = 'continue';
  } else {
    $CurrRec['merge'] = 'restart';
    $prev_val = $CurrRec['category'];
  }
}

Now for modifying the attribute: you have to put this somewhere in the row (inside or after the column to merge) :
[b.merge;att=w:vMerge#w:val]

BUT that is not all. You also have to be sure that your row actually has the element <w:vMerge>. Otherwise it won't be possible to localize the attribute "w:val".
Unfortunately you can have this element in Ms Word only by actually merging cells. So my suggestion is that you make a copy of the row and then you merge the two cells. Thus there will be two rows for the block, but TBS will consider the two rows as two alternative sections. But such rows will have the element <w:vMerge>.
By: ryan
Date: 2013-03-20
Time: 07:30

Re: How to merge table cells?

Thank you Skrol29.  You help me a lot.

I follow your instructions, and it works fine mostly.

But, you suggest "make a copy of the row and then you merge the two cells. Thus there will be two rows for the block, but TBS will consider the two rows as two alternative sections. But such rows will have the element <w:vMerge>."

It sounds like tricky and make some minor errors. I still can not make the merge cell work perfect.

Finally, I insert <w:vMerge> into the docx template manually and keep only one row. Then,  it works perfect.

<w:t>
  <w:vMerge w:val="restart" />
  [s.merge;att=w:vMerge#w:val]
</w:t>
By: Skrol29
Date: 2013-03-20
Time: 23:41

Re: How to merge table cells?

Ok,