Skip to main content
Known Participant
October 20, 2022
Answered

Alternate fills color is not working for table

  • October 20, 2022
  • 2 replies
  • 938 views

After applying alternate fills to the table in InDesign , when I insert or add row to the table. The alternate fills color not working. Is there any script to solve this?

This topic has been closed for replies.
Correct answer m1b

Hi @Mahesh12, here is a script that sets table rows to alternating cell styles. It's a quick one, so please test and let me know how it goes.

- Mark

 

/*
    by m1b
    here: https://community.adobe.com/t5/indesign-discussions/alternate-fills-color-is-not-working-for-table/td-p/13280870
*/


// table cell style names for alternating rows:
var cellStyleNames = [
    'Body',
    '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++)
        for (var j = 0; j < doc.allCellStyles.length; j++)
            if (cellStyleNames[i] == doc.allCellStyles[j].name) {
                cellStyles.push(doc.allCellStyles[j]);
                continue cellStyleNamesLoop;
            }

    // 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) {

            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 table styles");

 

2 replies

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
October 20, 2022

Hi @Mahesh12, here is a script that sets table rows to alternating cell styles. It's a quick one, so please test and let me know how it goes.

- Mark

 

/*
    by m1b
    here: https://community.adobe.com/t5/indesign-discussions/alternate-fills-color-is-not-working-for-table/td-p/13280870
*/


// table cell style names for alternating rows:
var cellStyleNames = [
    'Body',
    '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++)
        for (var j = 0; j < doc.allCellStyles.length; j++)
            if (cellStyleNames[i] == doc.allCellStyles[j].name) {
                cellStyles.push(doc.allCellStyles[j]);
                continue cellStyleNamesLoop;
            }

    // 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) {

            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 table styles");

 

Mahesh12Author
Known Participant
November 3, 2022

It's working.. Thank you 

Steve Werner
Community Expert
Community Expert
October 20, 2022

I duplicated your layout and adding extra rows were created:

 

I created this in InDesign CC 2022 version 17.4

 

I suspect that there is corruption in your file. You might try saving the file as an IDML file (File > Save As > InDesign CS4 or later [IDML]. Then reopen the IDML and try again.

 

Or you many need to restore your InDesign preferences and caches:

 

Adobe InDesign: Rebuilding Preferences and Cache Fileshttps://www.rockymountaintraining.com › adobe-indesi...

 

Mahesh12Author
Known Participant
October 20, 2022

I want to do this with script.. is there any script to do this.? 

Thanks

Community Expert
October 20, 2022

What do you require the script to do? 

Alternating fills is already a setting within Tables.

 

You can also setup a Table Style that would automate the application of this to other tables.

And you edit the Style to change all your tables globally throughout your document.

 

https://redokun.com/blog/indesign-table-styles

 

Again - we'd know what you require specifically a Script to do as each script is typically designed for specific scenarios.