Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Script to convert first row of table to header row

Explorer ,
Apr 05, 2023 Apr 05, 2023

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? 

TOPICS
Scripting
1.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 05, 2023 Apr 05, 2023

The second line should be

var curTable = myTables[i];

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 05, 2023 Apr 05, 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;

  }

}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 05, 2023 Apr 05, 2023

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 05, 2023 Apr 05, 2023

Sorry, I meant to include the error!

dpajewski_0-1680727860471.png

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 05, 2023 Apr 05, 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 06, 2023 Apr 06, 2023

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

dpajewski_0-1680781912052.png

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 06, 2023 Apr 06, 2023

You should the line at the top that defines myTables:

 

var myTables = app.activeDocument.stories.everyItem().tables.everyItem().getElements();
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 06, 2023 Apr 06, 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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 06, 2023 Apr 06, 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);
  }
}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 06, 2023 Apr 06, 2023

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

Error String: curTable is undefined

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 06, 2023 Apr 06, 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);
  }
}

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 06, 2023 Apr 06, 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?

 

dpajewski_0-1680794140547.png

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 06, 2023 Apr 06, 2023
LATEST

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
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines