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

New Here ,
Jan 03, 2012 Jan 03, 2012

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.

TOPICS
Scripting

Views

927

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct Answer

Participant , Jan 03, 2012 Jan 03, 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 inchesvar tbl; var textItems = flow.GetText(Constants.FTI_TblAnchor); for (var i = 0; i < textItems.len; i += 1) {          tbl = textItems.obj; ...

Likes

translate

Translate

Translate
Participant ,
Jan 03, 2012 Jan 03, 2012

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;

          }

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 04, 2012 Jan 04, 2012

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!

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 09, 2019 Oct 09, 2019

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

 

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines