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

Format Script

Explorer ,
Nov 21, 2022 Nov 21, 2022

Copy link to clipboard

Copied

Hi all, is there or does anyone know a script that allows me to assign a predefined cell and character format to every 4th row in a table?
A script recording like in Photoshop does not exist in InDesign, right?
Thanks, Dietmar

TOPICS
Scripting , Type

Views

617

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 , Nov 22, 2022 Nov 22, 2022

Also, here's another approach: to get the table based on its Script Label. First select your whole table (click in a cell, then choose menu Table > Select > Table) and show the Script Label panel (choose menu Windows > Utility > Script Label) and type the label you want (name it similar to the way you would name a variable I guess, eg. "myTable").

- Mark

var table = getTableByLabel(app.activeDocument, 'myTable');

/**
 * Returns a table with matching script label.
 * @author m1b
 * @version 2022
...

Votes

Translate

Translate
Community Expert ,
Nov 21, 2022 Nov 21, 2022

Copy link to clipboard

Copied

This is not a script, but if you do numerous tables, have a look to this plugin:

https://www.woodwing.com/en/products/smart-styles-adobe-indesign-plugin

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 ,
Nov 21, 2022 Nov 21, 2022

Copy link to clipboard

Copied

Hi jmlevy,

thank you for your answer, but unfortunately I did not look for that.

Thanks Dietmar

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 ,
Nov 21, 2022 Nov 21, 2022

Copy link to clipboard

Copied

Hi @DietmarB, here is a script I wrote for this purpose. You need to set up cell styles for each row. So in my example, I have [ undefined, undefined, undefined, 'Body Alt' ] which means every 4th row will be set to 'Body Alt' style. The pattern will repeat.) You can put other cell style names instead of undefined if you want to apply styles to each row.

- Mark

/*
    by m1b
    here: https://community.adobe.com/t5/indesign-discussions/format-script/m-p/13361464
*/

// table cell style names for alternating rows:
// (leave undefined to skip row)
var cellStyleNames = [
    undefined,
    undefined,
    undefined,
    'Body Alt',
];


function main() {

    if (app.selection.length == 0) {
        alert('Please make a selection and run script again.')
        return;
    }

    if (
        !app.selection[0].hasOwnProperty('parentStory')
        || !app.selection[0].parentStory.tables[0].isValid
    ) {
        alert('No table found in selection.')
        return;
    }

    var doc = app.activeDocument,
        table = app.selection[0].parentStory.tables[0],
        rows = table.rows,
        alternator = 0,
        cellStyles = [],
        clearOverrides = true;

    // collect the cell styles
    cellStyleNamesLoop:
    for (var i = 0; i < cellStyleNames.length; i++) {

        if (cellStyleNames[i] == undefined) {
            cellStyles.push(undefined);
            continue cellStyleNamesLoop;
        }

        for (var j = 0; j < doc.allCellStyles.length; j++)
            if (cellStyleNames[i] == doc.allCellStyles[j].name)
                cellStyles.push(doc.allCellStyles[j]);

    }

    // must have at least two!
    if (cellStyles.length < 2) {
        alert('Not enough cell styles found.');
        return;
    }

    // apply to each row
    for (var i = 0; i < rows.length; i++) {

        if (rows[i].rowType !== RowTypes.BODY_ROW)
            continue;

        if (cellStyles[alternator] != undefined) {

            rows[i].cells.everyItem().appliedCellStyle = cellStyles[alternator];

            if (clearOverrides)
                rows[i].cells.everyItem().clearCellStyleOverrides(false);

        }

        // toggle the alternator
        alternator = (alternator + 1) % cellStyles.length;

    }

};


app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Set alternating row cell styles");

 

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 ,
Nov 21, 2022 Nov 21, 2022

Copy link to clipboard

Copied

Hi Mark,
yes, that could be the right starting point. But at the moment I can't get your script to run - I get the following message: No table found in section.
I have a table with a total of 690 rows. Each block has 6 rows, which are always repeated. These rows should be formatted as follows:
var cellStyleNames = [
'month',
'date',
'time',
'event name'
'organizer'
'Location'
];
If I understood you correctly, I have to select the complete table and then start your script - right?
Thanks, Dietmar

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 ,
Nov 22, 2022 Nov 22, 2022

Copy link to clipboard

Copied

Oh just select the text frame containing the table. I didn't really focus on how you get the table. In your situation you may prefer to get a reference to the table another way.

- 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
Community Expert ,
Nov 22, 2022 Nov 22, 2022

Copy link to clipboard

Copied

Also, here's another approach: to get the table based on its Script Label. First select your whole table (click in a cell, then choose menu Table > Select > Table) and show the Script Label panel (choose menu Windows > Utility > Script Label) and type the label you want (name it similar to the way you would name a variable I guess, eg. "myTable").

- Mark

var table = getTableByLabel(app.activeDocument, 'myTable');

/**
 * Returns a table with matching script label.
 * @author m1b
 * @version 2022-08-15
 * @Param {Document} doc - an Indesign Document.
 * @Param {String} findLabel - the label to search for.
 * @Returns {Table}
 */
function getTableByLabel(doc, findLabel) {

    if (
        doc == undefined
        || !doc.isValid
        || doc.stories.everyItem().tables.length == 0
    )
        return;

    var tables = doc.stories.everyItem().tables.everyItem().getElements();

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

        try {

            if (
                tables[i] != undefined
                && tables[i].isValid
                && tables[i].label == findLabel
            )
                return tables[i];

        }

        catch (error) {
            $.writeln('Invalid Table: "' + doc.stories.everyItem().tables.item(i).toSpecifier() + '"');
        }

    }

};

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 ,
Nov 22, 2022 Nov 22, 2022

Copy link to clipboard

Copied

LATEST

Thanks for this,

I will try it once on a new table and get back to you.

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