Skip to main content
Known Participant
August 13, 2014
Question

Autosize for a table column based on the maximum text length

  • August 13, 2014
  • 3 replies
  • 1701 views

Hello,

InDesign CS6 allows you to have text frames auto-size to fit the text into them.

This is not possible for a column in a table. I have to develop this feature now in a script.

I think I have to use the InsertionPoints to calculate the width of a cell.

Has someone a code snippet?

Thank you in advance.

Harald

This topic has been closed for replies.

3 replies

Jongware
Community Expert
Community Expert
August 14, 2014

I discussed handling tables a while ago on InDesignSecrets: http://indesignsecrets.com/tackling-tables-through-scripting.php

At the bottom of that post is a link to one of my very own favorite "proud to have written" scripts: AutoFormatTable. It may even work as-is for your intended use.

As Uwe rightfully notes, there are several basic problems when attempting to measure string widths; indeed my own script, proud of it as I am, cannot handle rotated tables or cells either. Spanned columns confuse it as well; there is no good rule of thumb on how to handle this in tables. (I remember feverishly searching for HTML guidance, as this problem seems to have been solved in every slightly-above-average web browser years ago! But no dice there; no good algorithm descriptions, and I did not feel like reading the entire webkit source code.)

Cell overflows can be avoided by simply making the width of a cell wider and wider until it Does No More Overset.

Community Expert
August 14, 2014

Hi, Harald!
You have some answers and some questions about your task in that other* forum :-)
In case you track only this thread I put my questions here as well:

1. Is there a maximum of column width that should be honored?
2. Maybe a minimum of column width?
3. Is there a maximum of table width that should be honored?

Just some comments about using endHorizontalOffset:

1. Is only working as expected, if the table is not rotated.
2. Is only working as expected, if the cell's texts are not rotated

3. Is problematic, if cells are overflowing

4. Is problematic, if the table is overflowing

Alternatively one could use number of lines (like Gerald did in the other* forum).
Or check for the height of a table while changing the width of a column.

Uwe

* The other forum in German:

Automatisch die Spaltenbreite einer Tabelle an den längsten Text anpassen - Adobe InDesign Skriptwerkstatt - HilfDirSel…

(Hope that link will work)

Chinnadk
Legend
August 14, 2014

Hi Harald,

Try this.

var doc = app.activeDocument,

    _selection = app.selection[0];

   

if(_selection instanceof Table)

{

        var _columns = _selection.columns;

        for(var i =0;i<_columns.length;i++)

        {

                var _cells = _columns.cells,

                    max = [];

                for(var j=0;j<_cells.length;j++)

                {

                        var width = [],

                            _lines = _cells.lines;

                        for(var k =0;k<_lines.length;k++)

                        {

                                width.push(_lines.endHorizontalOffset - _lines.horizontalOffset + (_cells.leftInset + _cells.rightInset));

                            }

                        max.push(Math.max.apply(null,width));

                    }

                _columns.width = Math.max.apply(null,max)

            }

    }

else

{

        alert("Select a table")

    }

Regards,

Chinna

Community Expert
August 14, 2014

@Chinna – did you test your code well?
I get strange results.

See the followig screen shots.

1. Before running the script:

2. After running your script:

@Harald – Gerald's script would result in something like that. And I think this is the right approach.

I just changed one parameter in Gerald's script in the while loop. Went from 8 to 50.

Post #4 from Gerald in the other forum:

Automatisch die Spaltenbreite einer Tabelle an den längsten Text anpassen - Adobe InDesign Skriptwerkstatt - HilfDirSel…

Uwe