Skip to main content
dublove
Legend
August 15, 2024
Answered

Is there a script to set all columns of a table at once to a specified width value?

  • August 15, 2024
  • 4 replies
  • 2526 views

Is there a script to set all columns of a table at once to a specified width value?
I have almost 50 tables with column widths that need to be adjusted to the same value.
I can only use the align to reference line method for now, as too many is quite time consuming.
It would be nice to have a script to set it.
It can set how many columns there are and specify the width of each column.

Thank you.

This topic has been closed for replies.
Correct answer flaming1

I have used this script for some years to format tables to a consistent size (I'm not sure where it came from, sorry).

 

It only does a single table at a time, but it is still pretty quick to do a whole lot as it just requires the table to be selected and then the script run.

 

You set the column widths in the 'var myWidths = [60, 10, 10, 10];' line of the script so it remembers the last figures you entered.

 

var myDoc = app.activeDocument;
var myWidths = [60, 10, 10, 10];
for(var T=0; T < myDoc.textFrames.length; T++){
for(var i=0; i < myDoc.textFrames[T].tables.length; i++){
for(var j=0; j < myWidths.length; j++){
myDoc.textFrames[T].tables[i].columns[j].width = myWidths[j];
}
}
}
alert("Table width updated successfully...");

4 replies

flaming1Correct answer
Inspiring
August 15, 2024

I have used this script for some years to format tables to a consistent size (I'm not sure where it came from, sorry).

 

It only does a single table at a time, but it is still pretty quick to do a whole lot as it just requires the table to be selected and then the script run.

 

You set the column widths in the 'var myWidths = [60, 10, 10, 10];' line of the script so it remembers the last figures you entered.

 

var myDoc = app.activeDocument;
var myWidths = [60, 10, 10, 10];
for(var T=0; T < myDoc.textFrames.length; T++){
for(var i=0; i < myDoc.textFrames[T].tables.length; i++){
for(var j=0; j < myWidths.length; j++){
myDoc.textFrames[T].tables[i].columns[j].width = myWidths[j];
}
}
}
alert("Table width updated successfully...");
dublove
dubloveAuthor
Legend
August 16, 2024

This is nice.But it changed all.

How to target only the table where the current cursor is?

it would be great to be able to store multiple sets of values for easy selection.
The alert line is unnecessary.
Thank you very much.

Community Expert
August 15, 2024

Hi @dublove ,

if you want really ALL cells for ALL tables in a document with a specific width you could use this one line of ExtendScript (JavaScript) code:

 

app.documents[0].stories.everyItem().tables.everyItem().cells.everyItem().width = "20 mm";

 

If you want to address the cells of a specific column in all of your tables like all first column cells use this:

app.documents[0].stories.everyItem().tables.everyItem().columns[0].cells.everyItem().width = "20 mm";

 

Or if you want all cells of all last columns, independently how many columns a table has, than this code:

app.documents[0].stories.everyItem().tables.everyItem().columns[-1].cells.everyItem().width = "20 mm";

 

Regards,
Uwe Laubender
( Adobe Community Expert )

Community Expert
August 15, 2024

Well, because all cells in one column must be the same width, one could also write:

app.documents[0].stories.everyItem().tables.everyItem().columns.everyItem().width = "20 mm";

Why? Table columns also have a width property. With this in mind for all first table columns in your document:

app.documents[0].stories.everyItem().tables.everyItem().columns[0].width = "20 mm";

For all last columns:

app.documents[0].stories.everyItem().tables.everyItem().columns[-1].width = "20 mm";

Note:

columns[0] addresses the first column.

columns[1] addresses the second column.

columns[-1] addresses the last column.

columns[-2] addresses the one before the last column.

 

To make columns[n] work, you need at least ONE table in your document, that has n + 1 columns.

 

Regards,
Uwe Laubender
( Adobe Community Expert )

brian_p_dts
Community Expert
Community Expert
August 15, 2024

Simple enough, assuming all tables in the doc all have 7 columns as illustrated in your screenshot: 

var widths = [
    "3 mm",
    "5 mm",
    "5 mm",
    "1 mm",
    "3 mm",
    "5 mm",
    "5 mm",
];

var i = widths.length;
while(i--) {
    app.activeDocument.stories.everyItem().tables.everyItem().columns.item(i).width = widths[i];
}
dublove
dubloveAuthor
Legend
August 15, 2024

?

Is there a full working script?

Robert at ID-Tasker
Legend
August 15, 2024
quote

?

Is there a full working script?


By @dublove

 

This is a full working script - but it will resize columns in all tables in the document to the same width - specified by the array at the beginning of the code. 

 

Kasyan Servetsky
Legend
August 15, 2024

Check out the Auto Column script.

dublove
dubloveAuthor
Legend
August 15, 2024

This script does not store the last settings?
It's a pain to have to re-enter it every time

A book may have 2-5 different tables.
It would be nice to be able to store 2-5 different scenarios

exmple:

Tab-A
Tab-B
Tab-C
Tab-D
Tab-E

Robert at ID-Tasker
Legend
August 15, 2024

@dublove

 

My ID-Tasker - you can save "table presets" as different Tasks and then run on current document - or whole servers full of INDD documents. 

 

You can either filter tables to be processed first - by number of columns/rows, formatting, etc. or define condition at the beginning of the Task - table will be skipped if condition hasn't been met. 

 

But it's PC only and full version is not free.