Skip to main content
Inspiring
January 16, 2025
Answered

A script to adjust the column widths of just the selected table

  • January 16, 2025
  • 1 reply
  • 593 views

Hi

 

I've used the following script a lot over the years. It's brilliant, but it changes every table in the document, and now I've come across a situation where I need a script that does the same thing, but just to the table that is selected. I thought I'd seen this discussed somewhere on the forums in the past but I can't find it.

 

Is it a simple change to make it target just the selected table?

 

I don't recall where I originally found this script, so apologies to the author.

 

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...");

 

Thanks in advance to anyone who may be able to assist.

Correct answer Robert at ID-Tasker

Try this - there is ZERO error checking - so before you run this script - select any Cell - not just text cursor in the Cell - you need to select any Cell. 

 

var myDoc = app.activeDocument;
var myWidths = [60, 10, 10, 10];
var myTable = app.selection[0].parentRow.parent;
for(var j=0; j < myWidths.length; j++){
myTable.columns[j].width = myWidths[j];
}
alert("Table width updated successfully...");

 

1 reply

Robert at ID-Tasker
Robert at ID-TaskerCorrect answer
Brainiac
January 16, 2025

Try this - there is ZERO error checking - so before you run this script - select any Cell - not just text cursor in the Cell - you need to select any Cell. 

 

var myDoc = app.activeDocument;
var myWidths = [60, 10, 10, 10];
var myTable = app.selection[0].parentRow.parent;
for(var j=0; j < myWidths.length; j++){
myTable.columns[j].width = myWidths[j];
}
alert("Table width updated successfully...");

 

Inspiring
January 16, 2025

That's perfect – thanks Robert.

I really appreciate your help.

Robert at ID-Tasker
Brainiac
January 16, 2025
quote

That's perfect – thanks Robert.

I really appreciate your help.


By @stevej61370215

 

So it works - great to hear. I've made it in the middle of the night, on my phone 😉 Always happy to help.

 

You could do a lot more - if you work on a PC and don't mind paid solutions 😉