Skip to main content
Participating Frequently
February 22, 2024
Answered

How to select columns (and rows) non-sequentially

  • February 22, 2024
  • 2 replies
  • 831 views


This line of Java code

columns [1].

allows me to select that column. However, I need to select, for example, 2, 4, 6. How do I write it?

If I try

columns [2,4,6].

it only takes the 6 and omits the others.

This topic has been closed for replies.
Correct answer m1b

Hi @Mariana TF, I think this would best be explained with code. Please have a look at this, and see what you think. If you are new to coding, it may help to do some tutorials on "for loops". Special methods such as "everyItem" and "getElements" are specific to Indesign, so you will need to look those up in that context. Let me know how you go.

- Mark

 

 

/**
 * Demonstration of working with specific columns/cells.
 * Here we target the body cells from specific columns of every table.
 * @author m1b
 * @discussion https://community.adobe.com/t5/indesign-discussions/how-to-select-columns-and-rows-non-sequentially/m-p/14440216
 */
function main() {

    var doc = app.activeDocument;

    // make a place to store the cells
    var myCells = [];

    // get all the tables from document, as array
    var tables = doc.stories.everyItem().tables.everyItem().getElements();

    // collect cells from the 3rd, 5th and 7th column from each table
    for (var i = 0; i < tables.length; i++) {

        myCells = myCells
            .concat(tables[i].columns[2].cells.everyItem().getElements())
            .concat(tables[i].columns[4].cells.everyItem().getElements())
            .concat(tables[i].columns[6].cells.everyItem().getElements());

    }

    // for each collected cell, check if it is
    // a body row, and set the paragraph style
    for (var i = 0; i < myCells.length; i++) {

        if (RowTypes.BODY_ROW === myCells[i].rowType)
            myCells[i].cells.everyItem().texts[0].appliedParagraphStyle = "Right";

    }

};

app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Set table column style');

 

2 replies

m1b
Community Expert
Community Expert
February 22, 2024

Hi @Mariana TF, so you want an array that contains whichever columns you choose? How about this:

var columns = myTable.columns;
var myColumns = [columns[2], columns[4], columns[6]];

 Does that make sense?

- Mark

Participating Frequently
February 22, 2024

Mark, thanks.

I tried to integrate your code but my structure needs to be rewritten as
It is giving errors.

Please let me know if I can have a second chance>
My original code was:

app.documents[0].

    stories.everyItem().

    tables.everyItem().

    columns[5].

    cells.everyItem().

    texts[0].

    appliedParagraphStyle =

    "Right";




Participating Frequently
February 23, 2024

Mark, thanks for your time and kindness. Perfect pieces.
I am studying and will get deeper in "everyItem" and "getElements" .

M.


Community Expert
February 22, 2024

InDesign cannot select rows non-consecutively - what you are you trying to do - there might be another way.