Skip to main content
Inspiring
October 26, 2012
Answered

Merging cells in a row...

  • October 26, 2012
  • 2 replies
  • 1610 views

I'd like to be able to turn the first table into the second. I see that there is a cell merge, but I can't seem to figure out the syntax.

Thanks in advance for your help.

This topic has been closed for replies.
Correct answer Jongware

rowCellCount is simply either rows.cells.length or rows.columns.length -- without trying, I think the first gives the actual count of cells in a row (merged or not), and the second the original number of columns.

To merge, use http://jongware.mit.edu/idcs5js/pc_Cell.html#merge like this:

table = app.selection[0];

if (table.hasOwnProperty("baseline")) table = table.parent;

if (table instanceof Cell) table = table.parent;

if (table instanceof Column) table = table.parent;

if (table instanceof Row) table = table.parent;

if (table instanceof Table)

{

          table.rows[0].cells[0].merge (table.rows[0].cells[1]);

}

.. the first 6 lines are to make sure the script knows where it is. This way, it will work with the text cursor inside a cell (every text object has a "baseline" property; I think it's a trick I learned from Dave Saunders), or with a cell, column, row, or the entire table selected. After locating the table, it's just a matter of following the description of "Cells: Merge".

Oh wait, now I have a table, 3 cells per row and the first 2 cells merged, to test my first assertion on. Running this

alert (table.rows[0].cells.length);

alert (table.rows[0].columns.length);

alert (table.rows[1].cells.length);

alert (table.rows[1].columns.length);

alerts "2", "3", "3" and "3", just as I thought.

2 replies

KuddRowwAuthor
Inspiring
October 26, 2012

If someone can think of a better answer I'll still give out points. Ultimately I'd love to be able to have a rowCellCount like the bodyRowCount in tables. C'est la vie.

Jongware
Community Expert
JongwareCommunity ExpertCorrect answer
Community Expert
October 26, 2012

rowCellCount is simply either rows.cells.length or rows.columns.length -- without trying, I think the first gives the actual count of cells in a row (merged or not), and the second the original number of columns.

To merge, use http://jongware.mit.edu/idcs5js/pc_Cell.html#merge like this:

table = app.selection[0];

if (table.hasOwnProperty("baseline")) table = table.parent;

if (table instanceof Cell) table = table.parent;

if (table instanceof Column) table = table.parent;

if (table instanceof Row) table = table.parent;

if (table instanceof Table)

{

          table.rows[0].cells[0].merge (table.rows[0].cells[1]);

}

.. the first 6 lines are to make sure the script knows where it is. This way, it will work with the text cursor inside a cell (every text object has a "baseline" property; I think it's a trick I learned from Dave Saunders), or with a cell, column, row, or the entire table selected. After locating the table, it's just a matter of following the description of "Cells: Merge".

Oh wait, now I have a table, 3 cells per row and the first 2 cells merged, to test my first assertion on. Running this

alert (table.rows[0].cells.length);

alert (table.rows[0].columns.length);

alert (table.rows[1].cells.length);

alert (table.rows[1].columns.length);

alerts "2", "3", "3" and "3", just as I thought.

KuddRowwAuthor
Inspiring
October 29, 2012

Nifty! Great explanation. Much appreciated.

KuddRowwAuthor
Inspiring
October 26, 2012

app.activeDocument.textFrames[0].tables[0].cells[0].split(HorizontalOrVertical.VERTICAL);