Skip to main content
Known Participant
June 13, 2023
Answered

Can you help me write a script for table, which can save a lot of work.

  • June 13, 2023
  • 3 replies
  • 1632 views

After running the script for a table:

1. Apply the 'tab' table style to the entire table

2. Convert the first row to a header

3. Add a blank row at the end of the table, convert it to "footer", and apply the "footer" cell style to the blank row, and set the footer behavior to 1.058mm;

 

Thank you.

This topic has been closed for replies.
Correct answer m1b

Hi @dublove5CFE, here's a script to do what you want I think.

- Mark

 

/**
 * Adjust selected tables.
 * @author m1b
 * @discussion https://community.adobe.com/t5/indesign-discussions/can-you-help-me-write-a-script-for-table-which-can-save-a-lot-of-work/m-p/13861606
 */
function main() {

    var doc = app.activeDocument,
        tables = getSelectedTables(doc),
        tableStyle = getByName(doc.allTableStyles, 'Tabs'),
        footerStyle = getByName(doc.allCellStyles, 'Footer');

    if (tables.length == 0)
        alert('No tables selected.');

    for (var i = tables.length - 1; i >= 0; i--) {

        var table = tables[i];

        // apply table style
        table.appliedTableStyle = tableStyle;

        // set all rows height
        table.rows.everyItem().autoGrow = true;
        table.rows.everyItem().minimumHeight = '10mm';

        // convert first row to header
        if (table.rows[0].rowType == RowTypes.BODY_ROW)
            table.rows[0].rowType = RowTypes.HEADER_ROW;

        // adjust footer row
        var footerRow = table.rows.lastItem();
        if (footerRow.contents.join('').length > 0)
            footerRow = table.rows.add();
        if (footerRow.rowType !== RowTypes.FOOTER_ROW)
            footerRow.rowType = RowTypes.FOOTER_ROW;
        footerRow.autoGrow = false;
        footerRow.height = '1.058mm';
        footerRow.cells.everyItem().appliedCellStyle = footerStyle;

    }

};

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Adjust Tables');


/**
 * Returns array of Tables,
 * based on document's selection.
 * @author m1b
 * @version 2023-06-15
 * @param {Document} doc - an Indesign Document.
 * @returns {Array<Table>}
 */
function getSelectedTables(doc) {

    if (
        doc == undefined
        || doc.selection == undefined
    )
        return [];

    var sel = doc.selection,
        tables = [];

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

        if (sel[i].hasOwnProperty('tables'))
            for (var j = 0; j < sel[i].tables.length; j++)
                tables.push(sel[i].tables[j]);

        if (sel[i].parent.constructor.name == 'Table')
            tables.push(sel[i].parent);

        if (sel[i].parent.parent.constructor.name == 'Table')
            tables.push(sel[i].parent.parent);

        if (sel[i].constructor.name == 'Table')
            tables.push(sel[i]);


    }

    return tables;

};

/**
 * Returns a style matching name.
 * @author m1b
 * @version 2022-08-14
 * @param {Array<style>} styles - an array or collection of styles.
 * @param {String} name - the name to match.
 * @returns {style}
 */
function getByName(styles, name) {

    for (var i = 0; i < styles.length; i++)
        if (styles[i].name == name)
            return styles[i];

};

 

Edit 2023-06-14: set all rows' heights and footer row's height.

Edit 2023-06-15: now correctly adds footer row.

Edit 2023-06-15: footer row only added if last row isn't empty. Improved getSelectedTables function.

Edit 2023-06-16: now correctly adjusts footer row even if already a footer row.

3 replies

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
June 14, 2023

Hi @dublove5CFE, here's a script to do what you want I think.

- Mark

 

/**
 * Adjust selected tables.
 * @author m1b
 * @discussion https://community.adobe.com/t5/indesign-discussions/can-you-help-me-write-a-script-for-table-which-can-save-a-lot-of-work/m-p/13861606
 */
function main() {

    var doc = app.activeDocument,
        tables = getSelectedTables(doc),
        tableStyle = getByName(doc.allTableStyles, 'Tabs'),
        footerStyle = getByName(doc.allCellStyles, 'Footer');

    if (tables.length == 0)
        alert('No tables selected.');

    for (var i = tables.length - 1; i >= 0; i--) {

        var table = tables[i];

        // apply table style
        table.appliedTableStyle = tableStyle;

        // set all rows height
        table.rows.everyItem().autoGrow = true;
        table.rows.everyItem().minimumHeight = '10mm';

        // convert first row to header
        if (table.rows[0].rowType == RowTypes.BODY_ROW)
            table.rows[0].rowType = RowTypes.HEADER_ROW;

        // adjust footer row
        var footerRow = table.rows.lastItem();
        if (footerRow.contents.join('').length > 0)
            footerRow = table.rows.add();
        if (footerRow.rowType !== RowTypes.FOOTER_ROW)
            footerRow.rowType = RowTypes.FOOTER_ROW;
        footerRow.autoGrow = false;
        footerRow.height = '1.058mm';
        footerRow.cells.everyItem().appliedCellStyle = footerStyle;

    }

};

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Adjust Tables');


/**
 * Returns array of Tables,
 * based on document's selection.
 * @author m1b
 * @version 2023-06-15
 * @param {Document} doc - an Indesign Document.
 * @returns {Array<Table>}
 */
function getSelectedTables(doc) {

    if (
        doc == undefined
        || doc.selection == undefined
    )
        return [];

    var sel = doc.selection,
        tables = [];

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

        if (sel[i].hasOwnProperty('tables'))
            for (var j = 0; j < sel[i].tables.length; j++)
                tables.push(sel[i].tables[j]);

        if (sel[i].parent.constructor.name == 'Table')
            tables.push(sel[i].parent);

        if (sel[i].parent.parent.constructor.name == 'Table')
            tables.push(sel[i].parent.parent);

        if (sel[i].constructor.name == 'Table')
            tables.push(sel[i]);


    }

    return tables;

};

/**
 * Returns a style matching name.
 * @author m1b
 * @version 2022-08-14
 * @param {Array<style>} styles - an array or collection of styles.
 * @param {String} name - the name to match.
 * @returns {style}
 */
function getByName(styles, name) {

    for (var i = 0; i < styles.length; i++)
        if (styles[i].name == name)
            return styles[i];

};

 

Edit 2023-06-14: set all rows' heights and footer row's height.

Edit 2023-06-15: now correctly adds footer row.

Edit 2023-06-15: footer row only added if last row isn't empty. Improved getSelectedTables function.

Edit 2023-06-16: now correctly adjusts footer row even if already a footer row.

Known Participant
June 14, 2023

Oh, my dear m1b.

Thank you very much. It works, but there are some issues.

After running, it will display as follows:

No blank lines added at the bottom, I need to add blank lines at the bottom.

And set the exact row height to 1.058mm, which is accurate, not the minimum value.

 

Another request: Can you set the height of the table body row to 10mm? I don't know why this cannot be set in the style.

Whether the cursor can be in the table is equivalent to selecting the entire table.

Thank you.

m1b
Community Expert
Community Expert
June 15, 2023

How to determine the final line,

If the last row is empty, there is no need to add a new empty row, and the operation can be performed directly on this row.

If there is content in the last line, add a blank line and then operate on this line.


Hi @dublove5CFE, can you check if I've corrected these problems in the code above?

Community Expert
June 13, 2023

All the above you can actualy do with a correct built table style. 

 

Well, you can't insert rows using a table style, or convert row types.

Participating Frequently
June 13, 2023

Hi,
All the above you can actualy do with a correct built table style. So I wonder about the context?

1. Is it already a table or just content that you want to transform into a table. You need to have different approaches depending on.