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

Need urgent help with alternating table fills!

Explorer ,
Jul 21, 2021 Jul 21, 2021

Copy link to clipboard

Copied

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.

TOPICS
How to , Scripting

Views

310

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 , Jul 21, 2021 Jul 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

...

Votes

Translate

Translate
Community Expert ,
Jul 21, 2021 Jul 21, 2021

Copy link to clipboard

Copied

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

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

 

 

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 ,
Jul 21, 2021 Jul 21, 2021

Copy link to clipboard

Copied

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.

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 ,
Jul 22, 2021 Jul 22, 2021

Copy link to clipboard

Copied

This.... is amazing. Perfect!

You've saved me hours of work and shown me again, the power of this community. Thanks so much, and thanks to Eugene too, as that article did have some interesting bits on there for me too.

Again, thanks so 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 ,
Jul 22, 2021 Jul 22, 2021

Copy link to clipboard

Copied

Nice!

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 Beginner ,
Jun 05, 2023 Jun 05, 2023

Copy link to clipboard

Copied

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!! 🙂

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 Beginner ,
Jun 05, 2023 Jun 05, 2023

Copy link to clipboard

Copied

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!! 🙂

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

Copy link to clipboard

Copied

Hi @rachelb2980007, here is a modified version for your case. Let me know how it goes. - Mark

/**
 * Sets cell style of alternating rows, resetting at each subhead cell style row.
 * To use, set the subheadCellStyleName and styleName1 and styleName2 in the settings object below.
 * by m1b
 * @discussion: https://community.adobe.com/t5/indesign/need-urgent-help-with-alternating-table-fills/m-p/12190571#M438057
 */

var settings = {
    subheadCellStyleName: "SUBCELL",
    styleName1: 'BODY',
    styleName2: 'BODY ALT',
}

function main() {

    var item = app.selection[0];

    if (!item) {
        alert('Please select a table and run script again.');
        return;
    }

    var table = item;

    if (table.parent && table.parent.constructor.name == 'Table')
        table = table.parent;

    if (
        table.hasOwnProperty('parentStory')
        && table.parentStory.tables.length > 0
    )
        table = table.parentStory.tables[0];

    if (
        table.constructor.name !== 'Table'
        && table.tables.length > 0
    )
        table = table.tables[0];

    if (table.constructor.name !== 'Table'
        || !table.isValid
    ) {
        alert('No table selected. Please try again.');
        return;
    }

    var rows = table.rows;
    var alternator = 0;

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

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

        if (rows[i].cells[0].appliedCellStyle.name == settings.subheadCellStyleName) {

            // row is a subhead style
            // reset alternator flag
            alternator = 0;
            continue;

        }

        // set body cell style according to alternator flag
        setStyle(
            app.activeDocument,
            rows[i].cells.everyItem(),
            'cellStyle',
            alternator == 0 ? settings.styleName1 : settings.styleName2,
            true
        );

        // switch the alternator between 0 and 1
        alternator = (alternator + 1) % 2;

    }

};

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


/**
 * Convenience function to set styles.
 * @author m1b
 * @param {Document} doc - an Indesign Document.
 * @param {any} item - the target item, can be an everyItem().
 * @param {String} styleType - "cellstyle", "paragraphstyle" or "characterstyle".
 * @param {String } styleName - the name of the style.
 * @param {Boolean} [clearOverrides] - whether to clean overrides (default: true).
 */
function setStyle(doc, item, styleType, styleName, clearOverrides) {

    if (
        doc == undefined
        || item == undefined
        || styleName == undefined
    )
        return;

    clearOverrides = clearOverrides !== false;

    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;

    }

};

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 Beginner ,
Jun 06, 2023 Jun 06, 2023

Copy link to clipboard

Copied

Thank you thank you!!!

I tried the new script and an error with line 73 popped up -- "setStyle is not a function". Is this something I can correct on my side?

Your help is much appreciated!

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

Copy link to clipboard

Copied

Hi @rachelb2980007, I'm not sure what's going wrong. Could you check that this line is in tact:

function setStyle(doc, item, styleType, styleName, clearOverrides) {

Or alternatively try copying the script again in case you missed anything. I'm away from my computer at the moment but when I get a chance I'll double check the script. 

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 Beginner ,
Jun 06, 2023 Jun 06, 2023

Copy link to clipboard

Copied

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!

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

Copy link to clipboard

Copied

LATEST

Excellent! Glad to help. 🙂

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