Copy link to clipboard
Copied
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;
}
}
}
Copy link to clipboard
Copied
// 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;
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Add a } (closing brace) after Eugene's last line.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
I can take a look again but can't get to it until tomorrow.
Copy link to clipboard
Copied
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();
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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);"
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
OK 🙂
It should be:
var myTable = myTables[t];
WITHOUT the ".item" 🙂
Checked and is working - before, I was posting from my phone, sorry.
Copy link to clipboard
Copied
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");
}
}
Copy link to clipboard
Copied
Thanks Robert. That works a treat! Exactly as I want it to. Thank you so much for your help.
Copy link to clipboard
Copied
You're welcome 🙂
I need to remember, that ".item" is for collections and "[ ]" alone are for arrays 😉
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more