Jump to content

Column width based on longest field value


davers

Recommended Posts

Is it possible to set column widths automatically depending on the longest Field value? And also set a maximum width for the column?

 

I'm trying to create a bilingual envelope (see attached) where the gutter width is fixed, and begins after the longest row in first column. Column 1 has a maximum width of 2.5"

envelopeColumns.pdf

Link to comment
Share on other sites

Assuming that the blocks of text are in two Formatted Text resources named "English Address Block" and "French Address Block", and that your text frame is named "Address", this should work:

// These should match your resource and frame names, and width requirements:
var textFirstColumn = Resource("English Address Block").content;
var textSecondColumn = Resource("French Address Block").content;
var targetFrameName = "Address";
var maxColumnWidthInches = 2.5;
var GutterWidthInches = 0.2;

// Don't edit below here!
if (FusionPro.inValidation)
   textFirstColumn = "This only works in composition.";

var FrameWidth = FindTextFrame(targetFrameName).GetSettableTextWidth();

var tm = new FusionProTextMeasure;
tm.CalculateTextExtent(textFirstColumn);
if (tm.messages)
   ReportError("Text Measurement error: " + tm.messages);

var FirstColumnWidth = Math.min(tm.textWidth, maxColumnWidthInches * 7200);
var GutterWidth = GutterWidthInches * 7200;

var table = new FPTable;
table.AddColumns(FirstColumnWidth, GutterWidth, FrameWidth - GutterWidth - FirstColumnWidth);
table.AddRows(1);

//table.Rows[0].Cells[0].SetBorders("Thin", "Black", "Top", "Bottom", "Left", "Right");
var CellMargins = new FPTableMargins;
CellMargins.Top = 0;
CellMargins.Bottom = 0;
CellMargins.Left = 0;
CellMargins.Right = 0;

table.Rows[0].Cells[0].Content = textFirstColumn;
table.Rows[0].Cells[0].Margins = CellMargins;
table.Rows[0].Cells[2].Content = textSecondColumn;
table.Rows[0].Cells[2].Margins = CellMargins;
return table.MakeTags();

Edited by Dan Korn
Link to comment
Share on other sites

Thanks for the reply Dan,

 

I created the Formatted Text Resources and named the Text Frame "Address", but I get an error message

 

line 22: ReferenceError : FPTable is not defined. I attached a screenshot.

 

What am I doing wrong? How can I fix that?

 

// I have a very limited knowledge of JavaScript!;

 

Thanks,

 

Domenic

RuleError.thumb.jpg.e3b88c5907d9a3b803de17c80480c694.jpg

Link to comment
Share on other sites

line 22: ReferenceError : FPTable is not defined. I attached a screenshot.

 

What am I doing wrong? How can I fix that?

Oh, you're using version 6. The FPTable object is new in FusionPro 7.0.

 

For earlier versions of FusionPro, this can done by outputting the table tags directly. Please refer to the Tags Reference Guide.

Link to comment
Share on other sites

Hi Dan,

 

I went through the Tag Reference Guide, and they just give examples on how to set specific column widths. My first and third column widths are dependent on variable data to a maximum width of 2.5", the 2nd column (gutter) is a fixed width

 

Domenic

Link to comment
Share on other sites

I went through the Tag Reference Guide, and they just give examples on how to set specific column widths. My first and third column widths are dependent on variable data to a maximum width of 2.5", the 2nd column (gutter) is a fixed width

Yes, you'll have to take the code from the example I posted:

var table = new FPTable;
table.AddColumns(FirstColumnWidth, GutterWidth, FrameWidth - GutterWidth - FirstColumnWidth);
table.AddRows(1);

And port it to output the table tags directly, like so:

var table = '<table columns=3>';
table += '<column width=' + FirstColumnWidth + '>';
table += '<column width=' + GutterWidth + '>';
table += '<column width=' + (FrameWidth - GutterWidth - FirstColumnWidth) + '>';

And so on. Or you'll need to update to FusionPro 7 to use the FPTable object.

Link to comment
Share on other sites

  • 1 year later...

Hi Dan,

 

Thanks for your help. We have updated to version 7.2 and I was able to use your original code for creating 2 columns.

 

3 questions:

1) How do i get the columns to align at the bottom of the text box. Clicking the align bottom button on the Text Frame palette doesn't work, neither does entering code "table.Rows[0].Cells[0].VAlign = "Bottom";".

 

2) Regarding your original code, when i create the Formatted Text Resources and place Rules in it, it won't display correctly in my preview. Any suggestions?

 

3) If i can't use Rules in the Formatted Text Resources, how do i prevent a line breaking after a space in a field e.g. the postal code H2E 4X2 will break onto the next line?

 

Thanks

Link to comment
Share on other sites

Thanks for your help. We have updated to version 7.2 and I was able to use your original code for creating 2 columns.

Okay, you're still a bit behind the latest 8.2 version, but 7.2 is a good step up from where you were.

3 questions:

1) How do i get the columns to align at the bottom of the text box. Clicking the align bottom button on the Text Frame palette doesn't work, neither does entering code "table.Rows[0].Cells[0].VAlign = "Bottom";".

Add these lines near the top:

textFirstColumn = textFirstColumn.replace(/<p /gi, '<p cellalignment="bottom" ');
textSecondColumn = textSecondColumn.replace(/<p /gi, '<p cellalignment="bottom" ');

The problem is that "cellalignment" is an attribute of the <p> tag, not of the <cell> tag, so while it gets set properly on the first paragraph of the text from the Formatted Text Resource, it gets overridden again by subsequent <p style="(no style)"> tags. (I suppose that FusionPro should be changed so that vertical alignment is truly a property of the cell.)

2) Regarding your original code, when i create the Formatted Text Resources and place Rules in it, it won't display correctly in my preview. Any suggestions?

Exactly how is it not displaying correctly? Calling out a rule works fine for me in my little test job. I would do a full composition (not a Preview) and look for any warnings in the log (.msg) file.

3) If i can't use Rules in the Formatted Text Resources, how do i prevent a line breaking after a space in a field e.g. the postal code H2E 4X2 will break onto the next line?

Well, generally, you can do something like this to prevent text from breaking:

return Field("postal code").replace(/ /g, " ");

But that would still require a rule to be called out from the resource. So we need to figure out your question 2. Although an alternative would be to just hard-code all the tags for the address blocks instead of maintaining them in the Text Editor as Formatted Text Resources.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...