Copy link to clipboard
Copied
Is it even possible?
Playing with this sample it seems possible but I can't quite figure out how to make it work.
var doc =app.ActiveDoc;
var flow = doc.MainFlowInDoc;
var tbl = 0;
var textItems = flow.GetText(Constants.FTI_TblAnchor);
for (var i = 0; i < textItems.len; i += 1)
{
tbl = textItems.obj;
var tblColWidths = new Metrics (2 * 72 * 65536, 2 * 72 * 65536);
tbl.TblColWidths = tblColWidths;
}
Unlimited amounts of tables in each document for the most part, with 4 to 6 columns each. I'm fairly baffled here, anyone that could help or at least direct me I would appreciate it.
The problem is you're only passing widths for two columns, regardless of how many columns the original table has. You'll need to compute the new widths separately for each table. The following code should do what you're looking for:
var flow = app.ActiveDoc.MainFlowInDoc;
var targetTbleWidth = Math.floor(6.25 * 72 * 65536); // Assumed text frame of 6.25 inches
var tbl;
var textItems = flow.GetText(Constants.FTI_TblAnchor);
for (var i = 0; i < textItems.len; i += 1)
{
tbl = textItems.obj;
...
Copy link to clipboard
Copied
The problem is you're only passing widths for two columns, regardless of how many columns the original table has. You'll need to compute the new widths separately for each table. The following code should do what you're looking for:
var flow = app.ActiveDoc.MainFlowInDoc;
var targetTbleWidth = Math.floor(6.25 * 72 * 65536); // Assumed text frame of 6.25 inches
var tbl;
var textItems = flow.GetText(Constants.FTI_TblAnchor);
for (var i = 0; i < textItems.len; i += 1)
{
tbl = textItems.obj;
scaledColWidths = scaleTblMetrics(tbl, targetTbleWidth)
tbl.TblColWidths = scaledColWidths;
}
function scaleTblMetrics(tbl, targetWidth)
{
var currentColWidths = tbl.TblColWidths
var currentWidth = tbl.TblWidth;
var scaleFactor = targetWidth/currentWidth;
var targetColWidths = new Metrics;
for (var i = 0; i < currentColWidths.length; i++) targetColWidths.push(Math.floor(currentColWidths * scaleFactor));
return targetColWidths;
}
Copy link to clipboard
Copied
I didn't realize it didn't format the code correctly in my orginal post, sorry about that.
You're my hero! I would of never been able to figure this out!
Copy link to clipboard
Copied
Minor corrections (need de-reference indexes):
Main flow should have
tbl= textItems[i].obj;
scaleTableMetrics should have
targetColWidths.push(Math.floor(currentColWidths[i] * scaleFactor));