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

ExtendScript: How to convert selected table rows to Header?

Guide ,
Apr 27, 2025 Apr 27, 2025

My table header usually has 1-4 rows, most of the time 1-2rows.

I sometimes need to determine which table header rows already exist.
Next I need to convert to body rows,  or copy the table header rows.

 

Before I applied the style, I determined the number of table headers by manually selecting them.
The table header rows are the rows I selected.
How should this code be expressed?

 

After applying table styles, if I ever have table headers.
How do I determine which rows are table headers?

88.png

TOPICS
Feature request , How to , Scripting
1.7K
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

correct answers 1 Correct answer

Community Expert , Apr 30, 2025 Apr 30, 2025

Okay @dublove you just want a script to simply convert the selected cells to header rows and other rows to body rows. Easy right? Haha, here you go...

- Mark

 

/**
 * @file Convert Table Row Types.js
 *
 * Will convert the selected rows to header rows
 * and also convert any non-selected header rows
 * to body rows.
 *
 * Note: will convert the minimum number of rows
 * necessary to obey the rules around converting rows
 * with merged cells. This may be *more* than the
 * selected rows.
 *
 * @a
...
Translate
Guide ,
Aug 27, 2025 Aug 27, 2025

@m1b 

Edit 2025-08-26: updated getRows function.

I was in high spirits all day.

 

It still seems unable to switch to BODY_ROW.
No prompts are displayed.

By all accounts, this success:

 
function toHeader() {
    var doc = app.activeDocument,
        selectedRows = getRows(doc.selection[0]);
    var headerRows = convertToRowType(selectedRows, RowTypes.HEADER_ROW);
};
toHeader();

That body should be fine like this:

toBody();
function toBody() {

    var doc = app.activeDocument,
        selectedRows = getRows(doc.selection[0]);
    if (0 === selectedRows.length)
        return alert('Please select some table rows and try again.')
    //var headerRows = convertToRowType(selectedRows, RowTypes.HEADER_ROW);
    //var bodyRows = [],
    //    table = headerRows[0].parent,
    //    lastHeaderRowIndex = headerRows[headerRows.length - 1].index;
    //for (var i = lastHeaderRowIndex + 1; i < table.rows.length; i++)
    //    if (RowTypes.HEADER_ROW === table.rows[i].rowType)
    //        bodyRows.push(table.rows[i]);
    //var bodyRows = convertToRowType(bodyRows, RowTypes.BODY_ROW);
    var bodyRows = convertToRowType(selectedRows, RowTypes.BODY_ROW);
}

 

However, the third line below returned an error: Unable to set row type.

    for (var i = 0; i < rows.length; i++)
        if (rows[i].rowType !== rowType)
            rows[i].rowType = rowType;
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
Guide ,
Aug 27, 2025 Aug 27, 2025

@m1b  @rob day 

lastHeaderRowIndex + 1 makes no sense.
This must already be BODY_ROW. Unselected rows don't need conversion.

for (var i = lastHeaderRowIndex + 1; i < table.rows.length; i++)
alert(i);
if (RowTypes.HEADER_ROW === table.rows[i].rowType)
bodyRows.push(table.rows[i]);

Should be changed to:

for (var i = lastHeaderRowIndex; i >= 0; i--)
alert(i)
if (RowTypes.HEADER_ROW === table.rows[i].rowType)
bodyRows.push(table.rows[i]);

 

Just make the change here.
Two are needed here. But I don't know how to make the front automatically determine which one to select.

   
    if (to == "RB") {
        // R to B set the rowType of each row
        for (var i = rows.length - 1; 0 <= i; i--) {
            if (rows[i].rowType !== rowType)
                rows[i].rowType = rowType;
        }
    }

    if (to == "BR") {
        // B to R set the rowType of each row
        for (var i = 0; i < rows.length; i++) {
            if (rows[i].rowType !== rowType)
                rows[i].rowType = rowType;
        }
    }
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 ,
Aug 27, 2025 Aug 27, 2025
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
Guide ,
Aug 27, 2025 Aug 27, 2025

Just make the changes as I instructed.
The m1b version is already good.

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
Guide ,
Sep 03, 2025 Sep 03, 2025
LATEST

@m1b 

I encountered a slightly complex table.
It seems the conversion failed.

 

Attachments included.

tab2.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
Guide ,
Apr 29, 2025 Apr 29, 2025

The code doesn't make sense, my goal is to identify the selected row (and then I'll do the table header conversion).
But it seems to return the selected table?

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