5 Replies Latest reply on Nov 20, 2014 5:37 AM by Kevin J Fagan

    Autosize for a table column based on the maximum text length

    Harald61

      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

        • 1. Re: Autosize for a table column based on the maximum text length
          Chinnadk Level 4

          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[i].cells,
                              max = [];
                          for(var j=0;j<_cells.length;j++)
                          {
                                  var width = [],
                                      _lines = _cells[j].lines;
                                  for(var k =0;k<_lines.length;k++)
                                  {
                                          width.push(_lines[k].endHorizontalOffset - _lines[k].horizontalOffset + (_cells[j].leftInset + _cells[j].rightInset));
                                      }
                                  max.push(Math.max.apply(null,width));
                              }
                          _columns[i].width = Math.max.apply(null,max)
                      }
              }
          else 
          {
                  alert("Select a table")
              }
          

           

          Regards,

          Chinna

          • 2. Re: Autosize for a table column based on the maximum text length
            Laubender Adobe Community Professional & MVP

            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)

            • 3. Re: Autosize for a table column based on the maximum text length
              Laubender Adobe Community Professional & MVP

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

               

              See the followig screen shots.

               

              1. Before running the script:

              TableBeforeRunningScriptByChinna.png

               

               

              2. After running your script:

               

              TableAfterRunningScriptByChinna.png

               

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

               

               

               

              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

              • 4. Re: Autosize for a table column based on the maximum text length
                [Jongware] Most Valuable Participant

                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.

                • 5. Re: Autosize for a table column based on the maximum text length
                  Kevin J Fagan

                  Chinnadk  is there a way for the above script to be applied globally (to every table in a document)? That would be HUGE!!!!!