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

Editing a script

New Here ,
Feb 24, 2025 Feb 24, 2025

I have found this freeware script to transpose table rows to coumns (Indesign Transpose Tables Script). Works amazingly well but one slight draw back is you have to do each table one by one. I am new to jS ro would like some advice on how to edit the script to make to work on all tables selected.  Script syntax below:

 

// Table Transpose v1.0
// by Iain Anderson
// indesign@funwithstuff.com

// USE ON BACKUP COPIES
// NO RESPONSIBILITY TAKEN FOR ANY DAMAGE CAUSED.

// free to use -- may not be sold
// please report bugs -- I'll try to fix any show-stoppers
// but cell borders are unlikely to be transposed any time soon


with(app){

var myTempContents;
var myTempFont;
var myTempSize;
var myTempStyle;
var myTemp;
var myExtraRows = 0;

var myTextFrame = selection[0];

// error catch

var myError = 0;

if (selection.length <= 0) {
myError = 1;
}
else if (selection[0].constructor.name!="TextFrame") {
myError = 2;
}
else if (selection[0].tables.length <= 0) {
myError = 3;
}

// error alerts
if (myError == 1) {
// Nothing is selected, so display an error message.
alert("Nothing is selected. Please select a text frame and try again.")
}
else if (myError == 2) {
// A non-text frame is selected, so display an error message.
alert("Please select a single text frame containing a table and try again.")
}
else if (myError == 3) {
// No table in selection, so display an error message.
alert("No table was found in this text frame. Please select a single text frame containing a table and try again.")
}

// go for it!
else {
var myTable = myTextFrame.tables.item(0);
var myRows = myTable.bodyRowCount;
var myColumns = myTable.columnCount;

myTable.unmerge();

// if the table is too tall, make it wider, remember original size
if (myRows > myColumns) {
for (var extraCols = myColumns; extraCols < myRows; extraCols++) {
myTable.columns.add(LocationOptions.atEnd);
}
var myOriginalSize = myColumns;
var myExtraRows = 1;
}

// if the table is too wide, make it taller, remember original size
else if (myRows < myColumns) {
for (var extraRows = myRows; extraRows < myColumns; extraRows++) {
myTable.rows.add(LocationOptions.atEnd);
}
var myOriginalSize = myRows;
var myExtraRows = 2;
}

// recheck sizes
myRows = myTable.bodyRowCount;
myColumns = myTable.columnCount;

// fill blank cells
for(var myRowCounter = 0; myRowCounter < myRows*myColumns; myRowCounter++){
if (myTable.cells.item(myRowCounter).contents == "") {
myTable.cells.item(myRowCounter).contents = " ";
}
}

var mySourceCell, myDestCell;

for(var myRowCounter = 0; myRowCounter < myRows; myRowCounter++){
for(var myColCounter = myRowCounter+1; myColCounter < myColumns; myColCounter++){

myCellA = myColCounter+(myRowCounter*myColumns);
myCellB = myRowCounter+(myColCounter*myColumns);

// text content
myTempContents = myTable.cells.item(myCellA).contents;
myTable.cells.item(myCellA).contents = myTable.cells.item(myCellB).contents;
myTable.cells.item(myCellB).contents = myTempContents;

// text size
myTempSize = myTable.cells.item(myCellA).paragraphs.item(0).pointSize;
myTable.cells.item(myCellA).paragraphs.item(0).pointSize = myTable.cells.item(myCellB).paragraphs.item(0).pointSize;
myTable.cells.item(myCellB).paragraphs.item(0).pointSize = myTempSize;

// text font and style
myTempFont = myTable.cells.item(myCellA).paragraphs.item(0).appliedFont;
myTempStyle = myTable.cells.item(myCellA).paragraphs.item(0).fontStyle;

myTable.cells.item(myCellA).paragraphs.item(0).appliedFont = myTable.cells.item(myCellB).paragraphs.item(0).appliedFont;
myTable.cells.item(myCellA).paragraphs.item(0).fontStyle = myTable.cells.item(myCellB).paragraphs.item(0).fontStyle;

myTable.cells.item(myCellB).paragraphs.item(0).appliedFont = myTempFont;
myTable.cells.item(myCellB).paragraphs.item(0).fontStyle = myTempStyle;


// text fill colour
myTempStyle = myTable.cells.item(myCellA).paragraphs.item(0).fillColor;
myTable.cells.item(myCellA).paragraphs.item(0).fillColor = myTable.cells.item(myCellB).paragraphs.item(0).fillColor;
myTable.cells.item(myCellB).paragraphs.item(0).fillColor = myTempStyle;

// cell fill colour
myTempStyle = myTable.cells.item(myCellA).fillColor;
myTable.cells.item(myCellA).fillColor = myTable.cells.item(myCellB).fillColor;
myTable.cells.item(myCellB).fillColor = myTempStyle;

// cell tint colour
myTempStyle = myTable.cells.item(myCellA).fillTint;
myTable.cells.item(myCellA).fillTint = myTable.cells.item(myCellB).fillTint;
myTable.cells.item(myCellB).fillTint = myTempStyle;

 


}
}

// shrink rows to original column total
if (myExtraRows == 1) {
myTable.bodyRowCount = myOriginalSize;
}

// shrink columns to original row total
else if (myExtraRows == 2) {
myTable.columnCount = myOriginalSize;
}
}
}

 

TOPICS
Scripting
1.4K
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 24, 2025 Feb 24, 2025
quote

// go for it!
else {
var myTable = myTextFrame.tables.item(0);
var myRows = myTable.bodyRowCount;
var myColumns = myTable.columnCount;


By @matthewk24889660

Try changing this part to

 

// go for it!
else {
    // Loop through all the tables in the selected text frame
    for (var t = 0; t < myTextFrame.tables.length; t++) {
        var myTable = myTextFrame.tables.item(t);
        var myRows = myTable.bodyRowCount;
        var myColumns = myTable.columnCount;

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
New Here ,
Feb 24, 2025 Feb 24, 2025

Hi Eugene, thanks for your help. Sadly this didn't work I am getting an error.

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 24, 2025 Feb 24, 2025

Add a } (closing brace) after Eugene's last line.

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
New Here ,
Feb 24, 2025 Feb 24, 2025

Hi Peter,

 

Okay that works now, but I see from reading the script it is only working for one text box. On my document I want to look at all multiple text frames and change the tables in each text frame running the script once.

 

Something like:

app.activeDocument.textFrames.everyItem().texts.everyItem()..tables.everyItem();

 

Just don't know how to add this to get it working! Any assistance appreciated.

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 24, 2025 Feb 24, 2025

I can take a look again but can't get to it until tomorrow. 

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 24, 2025 Feb 24, 2025

@matthewk24889660 

 

NEVER EVER work on TextFrames - you need to work on STORIES.

 


@matthewk24889660 wrote:

 

Something like:

app.activeDocument.textFrames.everyItem().texts.everyItem()..tables.everyItem();


 

app.activeDocument.stories.everyItem().tables.everyItem();
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
New Here ,
Feb 25, 2025 Feb 25, 2025

How do integrate this in to Eugenes coding above or the original to get the document to find all stories and the tables within? Is it a case of editng the begining selection statement? var myTextFrame = selection[0];

Or is a bit more compliated than that?

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 25, 2025 Feb 25, 2025

@matthewk24889660 

 

Slight modification to what Eugene suggested:

 

// go for it!
else {
    // Loop through ALL the tables in the WHOLE active document
    var myTables = app.activeDocument.stories.everyItem().tables.everyItem().getElements();
    for (var t = 0; t < myTables.length; t++) {
        var myTable = myTables.item[t];
        var myRows = myTable.bodyRowCount;
        var myColumns = myTable.columnCount;

 

And - just in case - Peter's comment was to add "}" AFTER the whole code - not the last line of Eugene's code.

 

 

EDIT - Square brackets in the "myTables.item[t];" - not "myTables.item(t);"

 

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
New Here ,
Feb 26, 2025 Feb 26, 2025

Hi Robert,

 

Thank you for your extra code on this. Sadly it has thrown up the error which I don't understand. Can you help? Looks like it doesn't like the myTables.item[t]; bit. When I run the script it asks me to make a selection still. Could this be the issue? Does that matter that that is still part of the original script?

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 26, 2025 Feb 26, 2025

@matthewk24889660 

 

I'm sorry, but I've no idea what is wrong 😞

 

I'm also not a JS guy - but looking at examples on internet - and then this:

 

    alert(myTables.length);

 

showing number of elements in the myTables array correctly - means my code should work?

 

@Peter Kahrel - could you please help?

 

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 26, 2025 Feb 26, 2025

@matthewk24889660 

 

OK 🙂

 

It should be:

 

        var myTable = myTables[t];

 

WITHOUT the ".item" 🙂

 

Checked and is working - before, I was posting from my phone, sorry.

 

RobertatIDTasker_0-1740570000958.png

 

RobertatIDTasker_1-1740570012687.png

 

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 26, 2025 Feb 26, 2025

@matthewk24889660 

 

Finall code - without the need for selecting anything:

// Table Transpose v1.0
// by Iain Anderson
// indesign@funwithstuff.com

// USE ON BACKUP COPIES
// NO RESPONSIBILITY TAKEN FOR ANY DAMAGE CAUSED.

// free to use -- may not be sold
// please report bugs -- I'll try to fix any show-stoppers
// but cell borders are unlikely to be transposed any time soon


with(app){

var myTempContents;
var myTempFont;
var myTempSize;
var myTempStyle;
var myTemp;
var myExtraRows = 0;


// Loop through ALL the tables in the WHOLE active document
try
{
var myTables = app.activeDocument.stories.everyItem().tables.everyItem().getElements();
} catch(e){};
if (myTables)
{
//    app.select(myTables[t]);
    for (var t = 0; t < myTables.length; t++) {
        var myTable = myTables[t];
        var myRows = myTable.bodyRowCount;
        var myColumns = myTable.columnCount;

myTable.unmerge();

// if the table is too tall, make it wider, remember original size
if (myRows > myColumns) {
for (var extraCols = myColumns; extraCols < myRows; extraCols++) {
myTable.columns.add(LocationOptions.atEnd);
}
var myOriginalSize = myColumns;
var myExtraRows = 1;
}

// if the table is too wide, make it taller, remember original size
else if (myRows < myColumns) {
for (var extraRows = myRows; extraRows < myColumns; extraRows++) {
myTable.rows.add(LocationOptions.atEnd);
}
var myOriginalSize = myRows;
var myExtraRows = 2;
}

// recheck sizes
myRows = myTable.bodyRowCount;
myColumns = myTable.columnCount;

// fill blank cells
for(var myRowCounter = 0; myRowCounter < myRows*myColumns; myRowCounter++){
if (myTable.cells.item(myRowCounter).contents == "") {
myTable.cells.item(myRowCounter).contents = " ";
}
}

var mySourceCell, myDestCell;

for(var myRowCounter = 0; myRowCounter < myRows; myRowCounter++){
for(var myColCounter = myRowCounter+1; myColCounter < myColumns; myColCounter++){

myCellA = myColCounter+(myRowCounter*myColumns);
myCellB = myRowCounter+(myColCounter*myColumns);

// text content
myTempContents = myTable.cells.item(myCellA).contents;
myTable.cells.item(myCellA).contents = myTable.cells.item(myCellB).contents;
myTable.cells.item(myCellB).contents = myTempContents;

// text size
myTempSize = myTable.cells.item(myCellA).paragraphs.item(0).pointSize;
myTable.cells.item(myCellA).paragraphs.item(0).pointSize = myTable.cells.item(myCellB).paragraphs.item(0).pointSize;
myTable.cells.item(myCellB).paragraphs.item(0).pointSize = myTempSize;

// text font and style
myTempFont = myTable.cells.item(myCellA).paragraphs.item(0).appliedFont;
myTempStyle = myTable.cells.item(myCellA).paragraphs.item(0).fontStyle;

myTable.cells.item(myCellA).paragraphs.item(0).appliedFont = myTable.cells.item(myCellB).paragraphs.item(0).appliedFont;
myTable.cells.item(myCellA).paragraphs.item(0).fontStyle = myTable.cells.item(myCellB).paragraphs.item(0).fontStyle;

myTable.cells.item(myCellB).paragraphs.item(0).appliedFont = myTempFont;
myTable.cells.item(myCellB).paragraphs.item(0).fontStyle = myTempStyle;


// text fill colour
myTempStyle = myTable.cells.item(myCellA).paragraphs.item(0).fillColor;
myTable.cells.item(myCellA).paragraphs.item(0).fillColor = myTable.cells.item(myCellB).paragraphs.item(0).fillColor;
myTable.cells.item(myCellB).paragraphs.item(0).fillColor = myTempStyle;

// cell fill colour
myTempStyle = myTable.cells.item(myCellA).fillColor;
myTable.cells.item(myCellA).fillColor = myTable.cells.item(myCellB).fillColor;
myTable.cells.item(myCellB).fillColor = myTempStyle;

// cell tint colour
myTempStyle = myTable.cells.item(myCellA).fillTint;
myTable.cells.item(myCellA).fillTint = myTable.cells.item(myCellB).fillTint;
myTable.cells.item(myCellB).fillTint = myTempStyle;

 


}
}

// shrink rows to original column total
if (myExtraRows == 1) {
myTable.bodyRowCount = myOriginalSize;
}

// shrink columns to original row total
else if (myExtraRows == 2) {
myTable.columnCount = myOriginalSize;
}
}
}
else
{
alert("No tables to process");
}
}

 

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
New Here ,
Feb 26, 2025 Feb 26, 2025

Thanks Robert. That works a treat! Exactly as I want it to. Thank you so much for your help.

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 26, 2025 Feb 26, 2025
LATEST

@matthewk24889660

 

You're welcome 🙂 

 

I need to remember, that ".item" is for collections and "[ ]" alone are for arrays 😉 

 

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