Skip to main content
Known Participant
July 21, 2021
Answered

Need urgent help with alternating table fills!

  • July 21, 2021
  • 2 replies
  • 1322 views

On the attached file is a table. In it, I link to an XLSX and apply some styles (para and cells). Now, on import, the table doesn't adopt all these things and the borders etc. need to be removed and formatted... can't seem to bake those into the table style.

Also, the alternating fill I need (white/15%C) SHOULD be white first, after each sub heading. No tables doesn't support subheadings as such so I'm hoping there's a way to make the alternating fill restart after each para style change? Any ideas? All help really appreciated.

This topic has been closed for replies.
Correct answer m1b

Hi @Smallbadger, if Eugene's link (a great article!) doesn't solve your problem I suspect it might be because of your subhead rows, which could complicate things. I've written a quick script to set each row of a table in alternating cell styles, resetting when encountering subhead rows. If you are happy to play with scripting, you may be able to adapt it to your specific needs.

To use it you will need to (1) add a new cell style called "BODY ALT", based on "BODY" and give it a cell fill colour of 15% cyan (or whatever) *and* set the BODY cell style's fill colour to "[Paper]" (or whatever you want). Once you've done that, select your table and run the script.

Note: The script explicitly applies the "SUBCELL" style to the subhead cells—comment out that line if you don't want this.

 

 

/*
    by m1b
    here: https://community.adobe.com/t5/indesign/need-urgent-help-with-alternating-table-fills/m-p/12190571#M438057
*/

// table cell style names:
var styleName1 = 'BODY',
    styleName2 = 'BODY ALT',
    subheadStyleName = "SUBCELL";


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

function main() {

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

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

    } else {

        var _table = app.selection[0].parentStory.tables[0];
        var _rows = _table.rows;
        var alternator = 0;

        for (var i = 0; i < _rows.length; i++) {
            if (_rows[i].rowType === RowTypes.BODY_ROW) {
                if (_rows[i].cells.length == 1) {
                    // row is a subhead (all cells are merged into one)
                    setStyle(
                        app.activeDocument,
                        _rows[i].cells[0],
                        'cellStyle',
                        subheadStyleName,
                        true
                    );
                    // reset alternator flag
                    alternator = 0;
                } else {
                    // set body cell style according to alternator flag
                    setStyle(
                        app.activeDocument,
                        _rows[i].cells.everyItem(),
                        'cellStyle',
                        alternator == 1 ? styleName2 : styleName1,
                        true
                    );
                    // switch the alternator between 0 and 1
                    alternator = (alternator + 1) % 2;
                }
            }
        }
    }


    // just a convenience function for setting styles
    function setStyle(doc, item, styleType, styleName, clearOverrides) {
        if (doc == undefined || item == undefined || styleName == undefined) return;

        if (clearOverrides == undefined) clearOverrides = true;
        switch (styleType.toLowerCase()) {
            case 'cellstyle':
                if (doc.cellStyles.itemByName(styleName).isValid) {
                    item.appliedCellStyle = doc.cellStyles.itemByName(styleName);
                    if (clearOverrides) item.clearCellStyleOverrides(false);
                } else {
                    throw ('Cell Style "' + styleName + '" not found.');
                }
                break;
            case 'paragraphstyle':
                if (doc.paragraphStyles.itemByName(styleName).isValid) {
                    item.texts[0].paragraphs.everyItem().appliedParagraphStyle = doc.paragraphStyles.itemByName(styleName);
                    if (clearOverrides) item.texts[0].clearOverrides(OverrideType.ALL);
                } else {
                    throw ('Paragraph Style "' + styleName + '" not found.');
                }
                break;
            case 'characterstyle':
                if (doc.characterStyles.itemByName(styleName).isValid) {
                    item.texts[0].paragraphs.everyItem().appliedCharacterStyle = doc.characterStyles.itemByName(styleName);
                    if (clearOverrides) item.texts[0].clearOverrides(OverrideType.CHARACTER_ONLY);
                } else {
                    throw ('Character Style "' + styleName + '" not found.');
                }
                break;
            default:
                throw ('Unrecognised styleType "' + styleType + '".');
                break;
        }
    }
}

 

 

 Let me know if that works for you.

- Mark

 

Edit: the downside of this script is that it will apply the style to the entire row, so if you have different cell styles for different columns, it won't work, although with a bit of finesse it can be adjusted. One possibility is to change the "clearOverrides" flag on the setStyles function calls to false, but it will depend on how the table is set up whether this works. Or you could adjust the script to set each cell of each row specifically.

2 replies

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
July 21, 2021

Hi @Smallbadger, if Eugene's link (a great article!) doesn't solve your problem I suspect it might be because of your subhead rows, which could complicate things. I've written a quick script to set each row of a table in alternating cell styles, resetting when encountering subhead rows. If you are happy to play with scripting, you may be able to adapt it to your specific needs.

To use it you will need to (1) add a new cell style called "BODY ALT", based on "BODY" and give it a cell fill colour of 15% cyan (or whatever) *and* set the BODY cell style's fill colour to "[Paper]" (or whatever you want). Once you've done that, select your table and run the script.

Note: The script explicitly applies the "SUBCELL" style to the subhead cells—comment out that line if you don't want this.

 

 

/*
    by m1b
    here: https://community.adobe.com/t5/indesign/need-urgent-help-with-alternating-table-fills/m-p/12190571#M438057
*/

// table cell style names:
var styleName1 = 'BODY',
    styleName2 = 'BODY ALT',
    subheadStyleName = "SUBCELL";


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

function main() {

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

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

    } else {

        var _table = app.selection[0].parentStory.tables[0];
        var _rows = _table.rows;
        var alternator = 0;

        for (var i = 0; i < _rows.length; i++) {
            if (_rows[i].rowType === RowTypes.BODY_ROW) {
                if (_rows[i].cells.length == 1) {
                    // row is a subhead (all cells are merged into one)
                    setStyle(
                        app.activeDocument,
                        _rows[i].cells[0],
                        'cellStyle',
                        subheadStyleName,
                        true
                    );
                    // reset alternator flag
                    alternator = 0;
                } else {
                    // set body cell style according to alternator flag
                    setStyle(
                        app.activeDocument,
                        _rows[i].cells.everyItem(),
                        'cellStyle',
                        alternator == 1 ? styleName2 : styleName1,
                        true
                    );
                    // switch the alternator between 0 and 1
                    alternator = (alternator + 1) % 2;
                }
            }
        }
    }


    // just a convenience function for setting styles
    function setStyle(doc, item, styleType, styleName, clearOverrides) {
        if (doc == undefined || item == undefined || styleName == undefined) return;

        if (clearOverrides == undefined) clearOverrides = true;
        switch (styleType.toLowerCase()) {
            case 'cellstyle':
                if (doc.cellStyles.itemByName(styleName).isValid) {
                    item.appliedCellStyle = doc.cellStyles.itemByName(styleName);
                    if (clearOverrides) item.clearCellStyleOverrides(false);
                } else {
                    throw ('Cell Style "' + styleName + '" not found.');
                }
                break;
            case 'paragraphstyle':
                if (doc.paragraphStyles.itemByName(styleName).isValid) {
                    item.texts[0].paragraphs.everyItem().appliedParagraphStyle = doc.paragraphStyles.itemByName(styleName);
                    if (clearOverrides) item.texts[0].clearOverrides(OverrideType.ALL);
                } else {
                    throw ('Paragraph Style "' + styleName + '" not found.');
                }
                break;
            case 'characterstyle':
                if (doc.characterStyles.itemByName(styleName).isValid) {
                    item.texts[0].paragraphs.everyItem().appliedCharacterStyle = doc.characterStyles.itemByName(styleName);
                    if (clearOverrides) item.texts[0].clearOverrides(OverrideType.CHARACTER_ONLY);
                } else {
                    throw ('Character Style "' + styleName + '" not found.');
                }
                break;
            default:
                throw ('Unrecognised styleType "' + styleType + '".');
                break;
        }
    }
}

 

 

 Let me know if that works for you.

- Mark

 

Edit: the downside of this script is that it will apply the style to the entire row, so if you have different cell styles for different columns, it won't work, although with a bit of finesse it can be adjusted. One possibility is to change the "clearOverrides" flag on the setStyles function calls to false, but it will depend on how the table is set up whether this works. Or you could adjust the script to set each cell of each row specifically.

rachelb2980007
Participant
June 5, 2023

This is excellent and solves my issue -- BUT, how do I modify the script for subheader rows that aren't all merged together (so separate columns but all have same style). Right now, the script skips any subheaders that aren't merged into 1 row, despite all cells in the row being SUBCELL style. Please help!! 🙂

m1b
Community Expert
Community Expert
June 6, 2023

It worked!! I copied the entire script again and it works now!  User error indeed.  Thank you so so much for this -- you've saved me so much time and opened my eyes to the programming features within InDesign that I've never used before. ❤️ Have a wonderful day!


Excellent! Glad to help. 🙂

Community Expert
July 21, 2021

There's a good write up here that will have some excellent tips for you.

https://creativepro.com/preserve-the-appearance-of-text/