Skip to main content
Inspiring
April 5, 2023
Question

Script to convert first row of table to header row

  • April 5, 2023
  • 6 replies
  • 1596 views

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 has been closed for replies.

6 replies

Peter Kahrel
Community Expert
Community Expert
April 6, 2023

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);
  }
}

 

dpajewskiAuthor
Inspiring
April 6, 2023

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?

 

 

dpajewskiAuthor
Inspiring
April 6, 2023

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!

Peter Kahrel
Community Expert
Community Expert
April 6, 2023

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);
  }
}
dpajewskiAuthor
Inspiring
April 6, 2023

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

Error String: curTable is undefined

Peter Kahrel
Community Expert
Community Expert
April 6, 2023

You should the line at the top that defines myTables:

 

var myTables = app.activeDocument.stories.everyItem().tables.everyItem().getElements();
dpajewskiAuthor
Inspiring
April 6, 2023

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!

Peter Kahrel
Community Expert
Community Expert
April 5, 2023

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?

dpajewskiAuthor
Inspiring
April 6, 2023

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

 

Peter Kahrel
Community Expert
Community Expert
April 5, 2023

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

dpajewskiAuthor
Inspiring
April 5, 2023

Sorry, I meant to include the error!

 

Peter Kahrel
Community Expert
Community Expert
April 5, 2023

The second line should be

var curTable = myTables[i];

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

dpajewskiAuthor
Inspiring
April 5, 2023

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;

  }

}