Skip to main content
Folobo
Known Participant
March 19, 2013
Question

Table: convert first row to header rows

  • March 19, 2013
  • 3 replies
  • 7518 views

Hi all and sorry for this question.

I find this script that auto convert to header the first row of a table (we know that using table style - if i set a cell style for header - InDesign do not recognizes the header row that i also set in excel).

So i find a script that do it - but have 2 problem: error: 45 (Error string: Object is invalid) Line 4 and works for all table of the document (the best could be if work only with the table selected or of the page). Do you have an idea to solve this?

var myTables = app.activeDocument.stories.everyItem().tables;

for (var i = 0; i < myTables.length; i++) {

    myTables.rows.firstItem().rowType = RowTypes.HEADER_ROW;

}

(Another problem: what is the best way to set a border table use cell style? I want stroke 1!

The solution to do a stroke to the text frame fitted is a bad way, i think. Can i have a confirm? Yes, i use a table style and cell style - but i see that table style do not override cell style. See also here: http://forums.adobe.com/message/5153224#5153224

Many thanks

This topic has been closed for replies.

3 replies

Participating Frequently
December 2, 2015

Try This,

Worked For me.

var myDoc = app.activeDocument;

      for(var T=0; T < myDoc.textFrames.length; T++){

         for(var i=0; i < myDoc.textFrames.tables.length; i++){

myDoc.textFrames.tables.rows[0].rowType = RowTypes.HEADER_ROW;

}

}

Trevor:
Braniac
March 21, 2013

Try this

#target indesign;

var myRows = app.activeDocument.stories.everyItem().tables.everyItem().rows[0].getElements(),

      myCells = app.activeDocument.stories.everyItem().tables.everyItem().cells.everyItem(),

      l = myRows.length;

while (l--) if (myRows.rowType == RowTypes.BODY_ROW) myRows.rowType = RowTypes.HEADER_ROW; // need to check that the top row is not a header so as not to throw a can't set error

myCells.leftEdgeStrokeWeight="1pt";

myCells.rightEdgeStrokeWeight="1pt";

myCells.topEdgeStrokeWeight="1pt";

myCells.bottomEdgeStrokeWeight="1pt";

Trevor

Trevor:
Braniac
March 21, 2013

To apply to selected page only change lines 2 and 3 to

var myRows = app.activeWindow.activePage.textFrames.everyItem().tables.everyItem().rows[0].getElements(),

      myCells = app.activeWindow.activePage.textFrames.everyItem().tables.everyItem().cells.everyItem(),

The best way to set the strokes for the cell would be to give them a cell style, so given that you have set your cell style "Cell Style 1" up you can use this.

app.activeWindow.activePage.textFrames.everyItem().tables.everyItem().cells.everyItem().appliedCellStyle ="Cell Style 1" // for cell on the page

app.activeDocument.stories.everyItem().tables.everyItem().cells.everyItem().appliedCellStyle ="Cell Style 1" // for every cell in the document

So you can put the code together as

#target indesign;
var myRows = app.activeWindow.activePage.textFrames.everyItem().tables.everyItem().rows[0].getElements(),
      myCells = app.activeWindow.activePage.textFrames.everyItem().tables.everyItem().cells.everyItem(),
      l = myRows.length;
while (l--) if (myRows.rowType == RowTypes.BODY_ROW) myRows.rowType = RowTypes.HEADER_ROW; // need to check that the top row is not a header so as not to throw a can't set error
myCells.appliedCellStyle ="Cell Style 1"

Folobo
FoloboAuthor
Known Participant
March 22, 2013

@Folobo – in that case you need a couple of cell styles for each and every occassion ;-)


First row:

Left upper corner, middle in row, right upper corner


All rows except first and last row:

Left, middle, right

Last row:
Left bottom corner, middle in row, right bottom corner

If your first row is formatted differently than row two, you'll need another set of cell styles…

Uwe


@Laubender Hi and really thanks! My problem is that i have to place all cell style in a table style (i import data with excel).

I understand your way but i have to set it with local formatting (out of cell style).

Trevor:
Braniac
March 20, 2013

You might find some useful points in this post

http://forums.adobe.com/message/4792539#4792539

Folobo
FoloboAuthor
Known Participant
March 20, 2013

Many thanks Trevor - but it is not so easy...

Anyway what do you think about point 2 here: http://forums.adobe.com/message/5153224#5153224#5153224

Really many thanks.