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

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

Explorer ,
Jun 13, 2023 Jun 13, 2023

Copy link to clipboard

Copied

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.

TOPICS
Bug , How to , Scripting

Views

984

Translate

Translate

Report

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 , Jun 14, 2023 Jun 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 (tabl
...

Votes

Translate

Translate
Community Beginner ,
Jun 13, 2023 Jun 13, 2023

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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 ,
Jun 13, 2023 Jun 13, 2023

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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 ,
Jun 14, 2023 Jun 14, 2023

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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 ,
Jun 14, 2023 Jun 14, 2023

Copy link to clipboard

Copied

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.

mypic.png

Votes

Translate

Translate

Report

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 ,
Jun 14, 2023 Jun 14, 2023

Copy link to clipboard

Copied

Hi @dublove5CFE, I have updated the script above to hopefully match your needs. Let me know how it goes.

- Mark

Votes

Translate

Translate

Report

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 ,
Jun 14, 2023 Jun 14, 2023

Copy link to clipboard

Copied

Hi m1b, thank you very much.

You still haven't understood what I mean.

You need to add a blank row at the end of the table and then manipulate the blank row.

It is not an operation on the 'G' line.

Set the height of the last blank row to an accurate 1.058mm and apply the 'footer' cell style.

Votes

Translate

Translate

Report

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 ,
Jun 14, 2023 Jun 14, 2023

Copy link to clipboard

Copied

Footer (newly added blank line) line height 1.058, all other lines are 10mm

58.png

Votes

Translate

Translate

Report

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 ,
Jun 15, 2023 Jun 15, 2023

Copy link to clipboard

Copied

Sorry @dublove5CFE I somehow removed the line that actually added the new row. Please try the updated code.

- Mark

Votes

Translate

Translate

Report

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 ,
Jun 15, 2023 Jun 15, 2023

Copy link to clipboard

Copied

Great, you're too great.

Thank you.

 

        table.rows.everyItem().minheight = '10mm';

 

How to write the minimum line height of 10mm.

What I want is not an exact (absolute) value

 

Also, before running the script, can it be possible to not select all tables, and the cursor in the table is also equivalent to selecting all tables

Votes

Translate

Translate

Report

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 ,
Jun 15, 2023 Jun 15, 2023

Copy link to clipboard

Copied

 

 table.rows.everyItem().minimumHeight = '10mm';

 

I understand this.

I cannot edit my post, there is no 'edit' option。

-----------I also hope to know about this one below--------------

Also, before running the script, can it be possible to not select all tables, and the cursor in the table is also equivalent to selecting all tables

Votes

Translate

Translate

Report

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 ,
Jun 15, 2023 Jun 15, 2023

Copy link to clipboard

Copied

So do you want the script to adjust all tables in document?

(By the way, I have improved the getSelectedTables function and made the correction to the footerRow logic.) 

- Mark

Votes

Translate

Translate

Report

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 ,
Jun 15, 2023 Jun 15, 2023

Copy link to clipboard

Copied

Curious and strange.

I have manually dragged  the 'Footer row' before,

The script is no longer valid for this line. Why is this?

The file is here.

 

6860.png

Votes

Translate

Translate

Report

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 ,
Jun 15, 2023 Jun 15, 2023

Copy link to clipboard

Copied

 

        table.rows.everyItem().minimumHeight = '10.0mm';

 

This doesn't seem completely correct?

You set the minimaHeight to 10mm.

After operation, the display shows that the row height is an accurate value (absolute value?) of 5.233 millimeters.

You need to manually change the precision (absolute?) option to 'minimum'.

Why is this?

69000.png

Votes

Translate

Translate

Report

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 ,
Jun 15, 2023 Jun 15, 2023

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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 ,
Jun 15, 2023 Jun 15, 2023

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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 ,
Jun 15, 2023 Jun 15, 2023

Copy link to clipboard

Copied

Hi~m1b

Perfect~

Thank you very, very much

Votes

Translate

Translate

Report

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 ,
Jun 15, 2023 Jun 15, 2023

Copy link to clipboard

Copied

Excellent! 🙂

Votes

Translate

Translate

Report

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 ,
Jun 16, 2023 Jun 16, 2023

Copy link to clipboard

Copied

Hello, m1b.

Have you seen this.

Manually dragged rows will not be affected by the script.

------

Curious and strange.

I have manually dragged  the 'Footer row' before,

The script is no longer valid for this line. Why is this?

The file is here.

 

6860.png

Votes

Translate

Translate

Report

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 ,
Jun 16, 2023 Jun 16, 2023

Copy link to clipboard

Copied

Hi @dublove5CFE, thanks for the demo file. I think the problem is that my script doesn't adjust the footer row if there is already a footer row. I changed the logic now—see updated code above.

- Mark

Votes

Translate

Translate

Report

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 ,
Jun 16, 2023 Jun 16, 2023

Copy link to clipboard

Copied

LATEST

Tested,Perfect.

Absolute awesome~

Thank you very much.

 

 

Votes

Translate

Translate

Report

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