9 Replies Latest reply on Apr 7, 2015 3:17 AM by BSKTCreation

    How to make a script for expand column width

    Johnwhite Level 1

      Hi experts,

       

      Is that possible to make a script for expand the column width aim to let the columns show up all the overset text?

       

      Regard

      John

        • 1. Re: How to make a script for expand column width
          BSKTCreation Level 2

          Yes you can.

           

          You use something like...

           

          BE_resizeColumnsToFitContents(app.activeDocument.stories[0].tables[0]);
          
          
          function BE_resizeColumnsToFitContents(tableToEdit) {
              for (var i = 0; i < tableToEdit.columns.length; i++) {
                  while (tableToEdit.columns[i].overflows === true) {
                      tableToEdit.columns[i].width += 1;
                      tableToEdit.columns[i].recompose();
                      }
                  }
              }
          
          
          
          
          

           

          Brett

          • 2. Re: How to make a script for expand column width
            Johnwhite Level 1

            Hi Brett

             

            Thank you for your help.

             

            But it seems nothing response to me.

             

            even I changed it to this:

             

            BE_resizeColumnsToFitContents(app.activeDocument.stories.everyItem().tables.everyItem()); 

             

            function BE_resizeColumnsToFitContents(tableToEdit) { 

                for (var i = 0; i < tableToEdit.columns.length; i++) { 

                    while (tableToEdit.columns[i].overflows === true) { 

                        tableToEdit.columns[i].width += 1; 

                        tableToEdit.columns[i].recompose(); 

                        } 

                    } 

                }

             

            still nothing.

             

            Regard

            John

            • 3. Re: How to make a script for expand column width
              BSKTCreation Level 2

              Hi John,

               

              Sorry about that, try this, it'll count through all the tables in your document.

               

              myTables = app.documents[0].stories.everyItem().tables.everyItem().getElements();
              
              
              for (var t = 0; t < myTables.length; t++) {
                  BE_resizeColumnsToFitContents(myTables[t]);
                  }
              
              
              function BE_resizeColumnsToFitContents(tableToEdit) {
                  for (var i = 0; i < tableToEdit.columns.length; i++) {
                      while (tableToEdit.columns[i].overflows === true) {
                          tableToEdit.columns[i].width += 1;
                          tableToEdit.columns[i].recompose();
                          }
                      }
                  }
              
              
              
              
              
              
              

               

              Brett

              • 5. Re: How to make a script for expand column width
                Laubender Adobe Community Professional & MVP

                @Brett – I'd be cautious using a while loop without a control, that will stop it.

                There could be circumstances, that a cell will still overflow whatever you do with the width of a column.

                 

                I would stop the growth of a column at the width of the page perhaps.

                And then revert to its original width.

                 

                Uwe

                • 6. Re: How to make a script for expand column width
                  BSKTCreation Level 2

                  Yeah that's true, I normally do that (have a max column width) and pair this with a kerning script that controls the glyph, word and letter spacing.

                  • 7. Re: How to make a script for expand column width
                    BSKTCreation Level 2

                    Hi John,

                     

                    As Uwe advised it would be wise to put a stop on any while loop in case the condition is never fulfilled which would mean the script will break.

                     

                    Without knowing what specifically you are working on you could make the below amendments to do this.

                     

                    myTables = app.documents[0].stories.everyItem().tables.everyItem().getElements();
                    
                    
                    for (var t = 0; t < myTables.length; t++) {
                        BE_resizeColumnsToFitContents(myTables[t], 200);
                        }
                    
                    
                    function BE_resizeColumnsToFitContents(tableToEdit, tableMaxWidth) {
                        for (var i = 0; i < tableToEdit.columns.length; i++) {
                            while (tableToEdit.columns[i].overflows === true) {
                                if (tableToEdit.width < tableMaxWidth) {
                                    tableToEdit.columns[i].width += 1;
                                    tableToEdit.columns[i].recompose();
                                    }
                                else {
                                    alert("Column " + i + " contents too large for column.");
                                    break;
                                    }
                                }
                            }
                        }
                    
                    
                    
                    
                    

                     

                    I won't put any extra functions in this because it might not be what you're after. To use this you just define the table width as a second argument to the function (but you could change this parameter to something else, like page, column or cell width). And, if you want to have a fail action you just put it in the 'else' part.

                     

                    Brett

                    • 8. Re: How to make a script for expand column width
                      Level 1

                      Is this possible to decrease the  column width and  based on the text

                      • 9. Re: How to make a script for expand column width
                        BSKTCreation Level 2

                        Hi Sreekarthik,

                         

                        Yes it's pretty easy to using the same code with 2 additional lines (lines 11 and 12 below).

                         

                        myTables = app.documents[0].stories.everyItem().tables.everyItem().getElements();  
                        
                        
                        for (var t = 0; t < myTables.length; t++) {  
                            BE_resizeColumnsToFitContents(myTables[t], 200);  
                            }  
                        
                        
                        function BE_resizeColumnsToFitContents(tableToEdit, tableMaxWidth) {  
                            for (var i = 0; i < tableToEdit.columns.length; i++) {
                                tableToEdit.columns[i].width = 1;
                                tableToEdit.columns[i].recompose(); 
                                while (tableToEdit.columns[i].overflows === true) {  
                                    if (tableToEdit.width < tableMaxWidth) { 
                                        tableToEdit.columns[i].width += 1;  
                                        tableToEdit.columns[i].recompose();  
                                        }  
                                    else {  
                                        alert("Column " + i + " contents too large for column.");  
                                        break;  
                                        }  
                                    }  
                                }  
                            } 
                        
                        
                        
                        
                        

                         

                        What we're doing is making all the columns small and extending them out to the minimum width that will remove the overflows property.

                         

                        Brett