Skip to main content
Inspiring
Apr 5, 2023 7:24 PM UTC
Question

Script to convert first row of table to header row

Hi, so I've used this script a few weeks ago and it worked great. Now for some reason, I'm getting an error "cannot set row type" when running it. 

 

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

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

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

}

 

All of my tables have a style set with the correct cell styles applied to the table style. I did see another script that said it worked even if some tables already had a header row applied, but that one gave me an error as well.

 

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

 

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

  var curTable = myTables;

  var firstRow = curTable.rows[0];

  if (firstRow.rowType != RowTypes.HEADER_ROW) {

    firstRow.rowType = RowTypes.HEADER_ROW;

  }

}

 

Any idea of what might be wrong with it? 

This topic is closed to new replies. Start a new post to keep the conversation going.

13 replies

Peter Kahrel
Community Expert
Community Expert
Apr 6, 2023 2:48 PM UTC

Ugh. . .   Spot the difference:

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

for (var i = 0; i < myTables.length; i++) {
  var firstRow = myTables[i].rows[0];
  try {
    firstRow.rowType = RowTypes.HEADER_ROW;
  } catch (e) {
    alert ('Problem in table ' + firstRow.contents);
    alert (e);
  }
}

 

Translate
dpajewskiAuthor
Inspiring
Apr 6, 2023 3:18 PM UTC

Well now I have a new error! It looks like it's aimed at the titles of my header rows. When I hit OK, I get another box that says Error: cannot set row type. Sure you don't want to give up yet?

 

 

Translate
dpajewskiAuthor
Inspiring
Apr 6, 2023 3:20 PM UTC

I take that back. After I posted this, I ran it again, got the same error box. BUT all of my tables now have header rows. I'm stumped but it worked eventually! I can't thank you enough for your help and patience!

Translate
Peter Kahrel
Community Expert
Community Expert
Apr 6, 2023 2:28 PM UTC

Don't give up!

It should be like this:

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

for (var i = 0; i < myTables.length; i++) {
  var firstRow = curTable.rows[0];
  try {
    firstRow.rowType = RowTypes.HEADER_ROW;
  } catch (e) {
    alert ('Problem in table ' + firstRow.contents);
    alert (e);
  }
}
Translate
dpajewskiAuthor
Inspiring
Apr 6, 2023 2:37 PM UTC

I think I'm trying to get as many error strings as possible!

Error String: curTable is undefined

Translate
Peter Kahrel
Community Expert
Community Expert
Apr 6, 2023 1:09 PM UTC

You should the line at the top that defines myTables:

 

var myTables = app.activeDocument.stories.everyItem().tables.everyItem().getElements();
Translate
dpajewskiAuthor
Inspiring
Apr 6, 2023 2:26 PM UTC

Still getting an error.

Error String: undefined is not an object

This is what my script looks like:

for (var i = 0; i < myTables.length; i++) {
  var myTables = app.activeDocument.stories.everyItem().tables.everyItem().getElements();
  var firstRow = curTable.rows[0];
  try {
    firstRow.rowType = RowTypes.HEADER_ROW;
  } catch (e) {
    alert ('Problem in table ' + firstRow.contents);
    alert (e);
  }
}

I think I give up, I'll just manually make them a header row. I don't want to keep bugging you!

Translate
Peter Kahrel
Community Expert
Community Expert
Apr 5, 2023 9:04 PM UTC

Well, you could try it like this:

for (var i = 0; i < myTables.length; i++) {
  var curTable = myTables[i];
  var firstRow = curTable.rows[0];
  try {
    firstRow.rowType = RowTypes.HEADER_ROW;
  } catch (e) {
    alert ('Problem in table ' + firstRow.contents);
    alert (e);
  }
}

If a row can't be converted the alert shows you the content of the first row and the error, so at least you know which table caused the problem. Can you tell from that table what the problem is?

Translate
dpajewskiAuthor
Inspiring
Apr 6, 2023 11:52 AM UTC

I wish I knew what it all meant, I appreciate you helping me out!

 

Translate
Peter Kahrel
Community Expert
Community Expert
Apr 5, 2023 8:49 PM UTC

In what way did that not work? An error? No result? Something else?

Translate
dpajewskiAuthor
Inspiring
Apr 5, 2023 8:51 PM UTC

Sorry, I meant to include the error!

 

Translate
Peter Kahrel
Community Expert
Community Expert
Apr 5, 2023 7:49 PM UTC

The second line should be

var curTable = myTables[i];

i.e. add [i] at the end of the line (before the semicolon).

Translate
dpajewskiAuthor
Inspiring
Apr 5, 2023 8:37 PM UTC

Hmmm, that didn't work for me.

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

 

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

  var curTable = myTables[i];

  var firstRow = curTable.rows[0];

  if (firstRow.rowType != RowTypes.HEADER_ROW) {

    firstRow.rowType = RowTypes.HEADER_ROW;

  }

}
Translate