One slight issue It says NaN error when the text frame is not exactly square...
no part of the table can go past the gutter
The NaN is happening because you are skipping my getter function and writing your own loop, which is ok but you are getting the values after the table has been resized—if you don‘t want to use the getter function move your loop to before the table resize.
If the table/cell stroke value is known you could use it to adjust the final width, otherwise you would have to loop through the cells and get the max left and right stroke value, which could get tricky if there are different weights.
So in this example the left/right stroke is 5 points:
The stroke weight values are returned as points so I’ve set the measurement units to points at the top:
// Check if a document is open
if (app.documents.length > 0) {
var doc = app.activeDocument;
// Check if a table is selected
if (app.selection.length > 0 && app.selection[0] instanceof Table) {
//use points for measurements
app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS;
var table = app.selection[0];
var frame = table.parent;
// Get the selected text frame
var myTextFrame = frame;
// Get the number of columns and gutter width
var myColAmount = myTextFrame.textFramePreferences.textColumnCount;
var myGutterWidth = myTextFrame.textFramePreferences.textColumnGutter;
// Calculate the total width of the text frame and available width for columns
var myTextFrameBounds = myTextFrame.geometricBounds;
var myTotalWidth = myTextFrameBounds[3] - myTextFrameBounds[1];
var myAvailableWidth = myTotalWidth - (myGutterWidth * (myColAmount - 1));
// Calculate the width of each column
var myColumnWidth = myAvailableWidth / myColAmount;
//get the width of each column as a percentage of the table width
//get the widths before the table is resized
var ca = getColumnWidths(table);
// Set the table width to match the calculated column width minus a stroke value
// if the stroke value is unknown add a function to get
table.width = myColumnWidth-5;
//adjust the table’s column percentages
setColumnWidths(table, ca)
app.scriptPreferences.measurementUnit = AutoEnum.AUTO_VALUE;
alert("Table width set to match the column width of the text frame!");
} else {
alert("Please select a table before running the script.");
}
} else {
alert("Open a document before running the script.");
}
/**
* Gets an array of column widths as a percentage relative to the table width
* @ param the table
* @ return an array of column percentages
*
*/
function getColumnWidths(t){
var ca = []
var tw = t.width;
var tc = t.columns.everyItem().getElements()
for (var i = 0; i < tc.length; i++){
ca.push((tc[i].width/tw).toFixed(2))
//$.writeln(tc[i].width)
};
return ca
}
/**
* Set a table’s column widths to the provided array aof percentages
* @ param the table
* @ param an array of column percentages
* @ return void
*
*/
function setColumnWidths(t, a){
var tw = t.width;
var tc = t.columns.everyItem().getElements();
if (tc.length == a.length) {
var ci = tc[0].leftInset * a[0]
for (var i = 0; i < tc.length; i++){
tc[i].width = tw*a[i];
tc[i].leftInset = ci;
tc[i].rightInset = ci;
};
}
}
... View more