Skip to main content
Participant
January 3, 2012
Answered

Can you "Resize Columns: By Scalling to Widths Totalling:" the entire document?

  • January 3, 2012
  • 1 reply
  • 1141 views

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.

This topic has been closed for replies.
Correct answer

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;

          }

1 reply

Correct answer
January 3, 2012

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;

          }

BJD79Author
Participant
January 4, 2012

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!