Skip to main content
matthewk24889660
Participating Frequently
February 24, 2025
Question

Editing a script

  • February 24, 2025
  • 1 reply
  • 1152 views

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;
}
}
}

 

1 reply

Community Expert
February 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;

matthewk24889660
Participating Frequently
February 24, 2025

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

matthewk24889660
Participating Frequently
February 25, 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();

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?