Skip to main content
revathiv80720177
Known Participant
June 16, 2017
Question

Adding footer to an existing table using scripts

  • June 16, 2017
  • 3 replies
  • 1319 views

Hi,

I need to add header and footer to an existing tables using script. I tried this,

myTable.rows.item(-1).rowType = RowTypes.FOOTER_ROW;

But It shows an error: Cannot set row type.

How shall I get it.  Please do guide me.

Regards,

Revathi

This topic has been closed for replies.

3 replies

Community Expert
June 19, 2017

Hi Revathi,

back to your first post.

And the error message you sent by mail in the meantime.

Here your screenshot of the error message:

If you addressed variable myTables with:

var myTables = app.selection[0];

and then worked on with that variable as if the selection would be a table the case is clear.

You did not address a table object, but a text frame.

Select the table and not the text frame holding the table and run:

app.selection[0].rows.item(-1).rowType = RowTypes.FOOTER_ROW;

On my German Mac the command for selecting a table is alt+cmd+A if the cursor is in a cell.
You'll find your command with the context menu.

Regards,
Uwe

Community Expert
June 17, 2017

Hi Revathi,

your code should work.

Provide a screenshot of the table's last rows using the "Insert Image" widget of the forum editor showing the cell's edges.

Two reasons I know of why this would not work ( there may be others ):

1. The last row is already a footer row

2. You have vertically merged cells in the last rows of your table

In both cases a $ID/kCantSetRowTypeError is thrown.


Case 2 is the problematic one.

There could be vertically merged cells from rows above the last row.
A vertically merged cell would be not part of the last row.

Simply unmerging vertically merged cells would shift the contents to the top cell of the unmerged ones.

And maybe there are horizontally merged cells in rows above the last row as well. So simply unmerging all cells of the rows above the last row in a table could change the layout of your table dramatically.

Regards,
Uwe

Community Expert
June 17, 2017

Let's discuss a possible strategy for case 2:

You have vertically merged cells in the last rows of your table

One algorithm could be:


If the last row of a table is not a footer row:

Select rows step by step until the menu action "$ID/To Footer" is enabled.
Then invoke the menu action.

That's it. No unmerging. The minimal number of necessary rows are converted to footer rows.
I think, that is the only reasonable thing you can do without asking the layouter what should be done in case of vertically merged cells.

Why using a selection with the menu command?
Because the line below is not working in case one cell in rows[-2] is merged with rows[-1]:

// Will not work if vertically cells in rows[-2] were merged with cells in rows[-1]:

table.rows.itemByRange(-2,-1).rowType = RowTypes.FOOTER_ROW;

EDIT: Also another note on itemByRange().rowType :
It is not working at all. Merged or no merged cells.

Regards,
Uwe

Community Expert
June 17, 2017

To make that case 2 more clear I did a small sample table with some merged cells.

Ran a snippet on it where every cell's contents is the cell's name. That reveal something about its merged status.


That snippet will only work with text type cells:

var table = app.documents[0].stories[0].tables[0];

var cells = table.cells.everyItem().getElements();

for(var n=0;n<cells.length;n++)

{

    cells.contents = cells.name;

};

Here a visualisation of my test table before and after I converted the minimum number of rows to footer rows:

Regards,
Uwe

Anantha Prabu G
Legend
June 17, 2017

Hi,

var curDoc = app.documents[0]; 

var myTables = app.selection[0];

var allRows = myTables.rows;

myTables.rows.add(LocationOptions.AFTER, allRows[1]);

if (myTables.rows[-1].rowType == RowTypes.BODY_ROW) {  

     myTables.rows[-1].rowType = RowTypes.FOOTER_ROW;  

}

Thanks

Thanks,PrabuDesign smarter, faster, and bolder with InDesign scripting.