Skip to main content
geirb9262873
Known Participant
June 18, 2022
Answered

Swapping columns in InDesign with a java script

  • June 18, 2022
  • 2 replies
  • 2370 views

Hello,

 

I have a document with many tables with the same Table Style.

All the tables have two columns.

I have managed to make a java script (jsx) that can change the width of the two columns and applied this automatically to all the tables in the document.

 

Now, I would like to swap the two columns in every table in the document, so that the leftmost column becomes the rightmost one. 

 

If anyone has a few lines of code to accomplish this, it would be great.

 

With best regards from,

 

 

Geir Braathen

Correct answer rob day

Glad it's solved! Just a caveat on Rob's script—being beautiful and brief—if you had multiple text styles (eg. a word in italic) in the moved column it will be gone afterwards.

 

I have heard of someone using python to script Adobe products but when I looked into it they were basically using python to deliver javascript commands... so no, not a good option, sadly.

- Mark


(eg. a word in italic) in the moved column it will be gone afterwards.

 

Hi Mark, I wouldn’t be surprised if other problems pop up but the text formatting might be solved with a simple copy and paste after the properties have been set. This seems to work:

 

/**
* Swaps the first two columns of a table 
* @ param the table 
* @ return void 

*/
function swapColumns(t){
    var nc = t.columns.add(LocationOptions.BEFORE, t.columns[0]);
    var c = nc.cells;
    var lc = t.columns[2].cells;

    for (var i = 0; i < c.length; i++){
        c[i].properties = lc[i].properties;
        app.select(lc[i].texts[0])
        app.copy()
        app.select(c[i].texts[0])
        app.paste()
    };   
    t.columns[2].remove();
}

 

 

2 replies

rob day
Community Expert
Community Expert
June 18, 2022

Hi Geir, Does this work?

 

 


var table=app.activeDocument.selection[0].tables[0];
swapColumns(table);


/**
* Swaps the first two columns of a table 
* @ param the table 
* @ return void 
*/
function swapColumns(t){
    var nc = t.columns.add(LocationOptions.BEFORE, t.columns[0]);
    var c = nc.cells
    var lc = t.columns[2].cells

    for (var i = 0; i < c.length; i++){
        c[i].properties = lc[i].properties
    };   
    t.columns[2].remove();
}

 

 

 

m1b
Community Expert
Community Expert
June 18, 2022

OMG @rob day! Using the "properties" property is brilliant! My script stupidly loops over them all! Nice one. 🙂

- Mark

 

Edit: I hope you don't mind, I edited my function to apply the properties the way you did, which is very convenient, but I also noticed that you then need to move the content in a more deliberate way, to ensure that text styles and graphics come with it correctly. (See my function.)

rob day
Community Expert
Community Expert
June 19, 2022

I hope you don't mind

 

No probelm.

m1b
Community Expert
Community Expert
June 18, 2022

Hi @geirb9262873, I have previously written code to do this. See my answer here. Let me know if it works for you as the code is quite untested. (Also, sadly, it is more than just a few lines only!)

- Mark

 

Edit: If you try my function, you would use something like this:

// swap first and second columns
moveRowOrColumn(table.columns[1], LocationOptions.BEFORE, table.columns[0]);
geirb9262873
Known Participant
June 19, 2022

Hi Mark,

 

Thank you for your reply.

I would love to try your code, but I get a bit lost in the long script you link to, so I am not sure how to apply the one-liner you give here. I tried a bit, but I get error messages.

I am new to scripting in InDesign, so I depend on some complete script that works without too much tweaking. 

 

Cheers,

 

Geir

m1b
Community Expert
Community Expert
June 19, 2022

Ah I see. Not to worry. To use my code as it is (in the other post) delete these lines:

   /*
        example 1:
        duplicate first row after itself
    */
    var newRow = duplicateRowOrColumn(table.rows[0], LocationOptions.AFTER);
    // then convert to body row
    if (newRow != undefined && newRow.isValid)
        newRow.rowType = RowTypes.BODY_ROW;

But which tables do you want it to work with? Just the first selected table? All tables in a story? All in a document? I'll need to adjust the way the script gets the table(s) depending on what you want.

- Mark