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

How to select columns (and rows) non-sequentially

Community Beginner ,
Feb 21, 2024 Feb 21, 2024


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.

TOPICS
How to , Performance , Scripting
730
Translate
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 , Feb 22, 2024 Feb 22, 2024

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 
...
Translate
Community Expert ,
Feb 21, 2024 Feb 21, 2024

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

Translate
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 ,
Feb 21, 2024 Feb 21, 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

Translate
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 ,
Feb 22, 2024 Feb 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";




Translate
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
LEGEND ,
Feb 22, 2024 Feb 22, 2024

As already mentioned - you can't select multiple, non consecutive columns - you need to format them one-by-one - in a loop.

 

Translate
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 ,
Feb 22, 2024 Feb 22, 2024

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');

 

Translate
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 ,
Feb 23, 2024 Feb 23, 2024

Screenshot 2024-02-23 at 8.20.28 AM.png

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

M.

Translate
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 ,
Feb 23, 2024 Feb 23, 2024

scripts.jpg

 

Translate
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 ,
Feb 23, 2024 Feb 23, 2024

Great to hear! Here is a great article on "everyItem" and "getElements": part 1 and part 2 (by @Marc Autret). They might be too advanced for a beginner to scripting—but keep them in mind for later. Otherwise, just keep scripting and you will learn what you need as you go, and people here will always help if they see you are trying hard.

- Mark

Translate
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 ,
Feb 23, 2024 Feb 23, 2024
LATEST

Mark, I really appreciate this information.

Your supposition that it was possible to handle this problem, against all odds, has been an encouragement in this area of scripting.

I remember reading some time ago in this forum that someone called a Jongware script a piece of cake. Same here. Thank you. Best regards.

Translate
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