6 Replies Latest reply on Oct 18, 2014 2:55 AM by Peter Kahrel

    Adjust column width in tables

    VeluVK Level 1

      Hi,

       

      Anyone of you please provide the script for the below requirement.

       

      All tables in a document:

       

      1) All ‘$’ columns should be fixed width “1p”

      2) All empty column should be “1p”

      3) All ‘%’  and ‘)’ column should be “1p”

      4) All superscript content column like [1, (1), *] should be “1p”

      5) Remaining all columns (except first column) should be “6p” width and values are right aligned

      6) First column always variable width

      7) All underlines values should be 0.5 bottom border, border only need ‘$’ column and ‘value’ column (not in ‘)’ ‘%’ 1, (1), *) columns.

      8) Also table should be fit within text area (100%)

       

      For example All grey shaded column should be “1p” width and left aligned, All pink shaded column should be “6pica” width and values are right aligned.

       

      input.JPG

       

       

       

      I need the below output:

       

      output.JPG

       

      Regards,

      Velu

        • 1. Re: Adjust column width in tables
          Chinnadk Level 4

          Hi Velu,

           

          Try this,

           

          var doc = app.activeDocument,
              w = doc.documentPreferences.pageWidth;
              sel = doc.selection[0];
          
          
          if(sel instanceof Table)
          {
                  var col = sel.columns;
                      _left = sel.parent.parentPage.marginPreferences.left;
                      _right = sel.parent.parentPage.marginPreferences.right, allcolwidth = 0;
                  for(var i=1;i<col.length;i++)
                  {
                          var cell = col[i].cells, cont = "";
                          for(var j=0;j<cell.length;j++)
                          {
                                  cont += cell[j].contents;
                              }
                          if(cont == "")
                          {
                                  col[i].width = "1p";
                                  allcolwidth += col[i].width;
                              }
                          else if(cont.indexOf("$") !=-1)
                          {
                                  col[i].width = "1p";
                                  allcolwidth += col[i].width;
                              }
                          else
                          {
                                  col[i].width = "6p";
                                  allcolwidth += col[i].width;
                                  for(var k=0;k<col[i].cells.length;k++)
                                  {
                                          if(col[i].cells[k].contents !="")
                                          {
                                                  var paras = col[i].cells[k].paragraphs;
                                                  for(var l=0;l<paras.length;l++)
                                                  {
                                                          paras[l].justification = Justification.RIGHT_ALIGN;
                                                      }
                                              }
                                      }
                              }
                      }
                  col[0].width = w - _left - _right - allcolwidth;
              }
          else
          {
                  alert("Select a table...")
              }
          

           

          Regards,

          Chinna

          • 2. Re: Adjust column width in tables
            BEGINNER_X Level 3

            Excellent Stuff Chinna....

            • 3. Re: Adjust column width in tables
              VeluVK Level 1

              Hi Chinna,

               

              Thank you very much for providing script. The Script is working but some of the functions having problems.

               

              For example see the below screen shot:

               

              In this table [closing parenthesis, *,  % and superscript characters (1) (2)) are occurs in some columns, in that column width also converted to (6pica). but it should be (1pica) width only.

               

              Please fix the  ), %, * and superscript characters, columns to 1pica width and left aligned.

               

               

               

              output.JPG

               

               

              And also i have run the script for below table, it shows an error.

               

              input2.JPG

               

              output2.JPG

               

              Hi Chinna,

               

              Text area width totally 42 picas only, i think number of columns is the problem. Once other columns (%, ), *, etc..)  will fix, the above error maybe solve.

               

              Regards,

              Velu

              • 4. Re: Adjust column width in tables
                VeluVK Level 1

                Hi Chinna,

                 

                I tried little bit, can you check and correct the below code?


                1) I need the superscript character columns into 1pica width

                2)

                 

                 

                 

                 

                 

                 

                var doc = app.activeDocument, 

                    w = doc.documentPreferences.pageWidth; 

                    sel = doc.selection[0];

                 

                    if(sel instanceof Table) 

                    { 

                            var col = sel.columns; 

                                _left = sel.parent.parentPage.marginPreferences.left; 

                                _right = sel.parent.parentPage.marginPreferences.right, allcolwidth = 0; 

                            for(var i=1;i<col.length;i++) 

                            { 

                                    var cell = col[i].cells, cont = ""; 

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

                                    { 

                                            cont += cell[j].contents; 

                                        } 

                                    if(cont == "") 

                                    { 

                                            col[i].width = "1p"; 

                                            allcolwidth += col[i].width; 

                                        } 

                                    else if(cont.indexOf("$") !=-1) 

                                    { 

                                            col[i].width = "1p"; 

                                            allcolwidth += col[i].width; 

                                        } 

                                    else if(cont.indexOf("*") !=-1) 

                                    { 

                                            col[i].width = "1p"; 

                                            allcolwidth += col[i].width; 

                                           

                                        } 

                                    else if(cont.indexOf("%") !=-1) 

                                    { 

                                            col[i].width = "1p"; 

                                            allcolwidth += col[i].width; 

                 

                                        } 

                                   else if(cont.indexOf(")") !=-1) 

                                    { 

                                            col[i].width = "1p"; 

                                            allcolwidth += col[i].width; 

                 

                                        } 

                            

                                    else 

                                    { 

                                            col[i].width = "5p"; 

                                            allcolwidth += col[i].width; 

                                            for(var k=0;k<col[i].cells.length;k++) 

                                            { 

                                                    if(col[i].cells[k].contents !="") 

                                                    { 

                                                            var paras = col[i].cells[k].paragraphs; 

                                                            for(var l=0;l<paras.length;l++) 

                                                            { 

                                                                    paras[l].justification = Justification.RIGHT_ALIGN; 

                                                                } 

                                                        } 

                                                } 

                                        } 

                                } 

                            col[0].width = w - _left - _right - allcolwidth; 

                        } 

                    else 

                    { 

                            alert("Select a table...") 

                        }

                • 5. Re: Adjust column width in tables
                  VeluVK Level 1

                  Hi Chinna,

                   

                  I tried little bit changes in your previous script, can you please check and correct?


                  1) Can you please include (superscript character) columns into 1pica width

                  2) Sometimes empty columns are not converted into "1p" because of spanning (see the below table), Can you please ignore the spanned columns while running the script or put any input dialogue box for "number of rows to ignore".


                  input.JPG

                   

                     

                  var doc = app.activeDocument, 

                      w = doc.documentPreferences.pageWidth; 

                      sel = doc.selection[0];

                   

                      if(sel instanceof Table) 

                      { 

                              var col = sel.columns; 

                                  _left = sel.parent.parentPage.marginPreferences.left; 

                                  _right = sel.parent.parentPage.marginPreferences.right, allcolwidth = 0; 

                              for(var i=1;i<col.length;i++) 

                              { 

                                      var cell = col[i].cells, cont = ""; 

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

                                      { 

                                              cont += cell[j].contents; 

                                          } 

                                      if(cont == "") 

                                      { 

                                              col[i].width = "1p"; 

                                              allcolwidth += col[i].width; 

                                          } 

                                      else if(cont.indexOf("$") !=-1) 

                                      { 

                                              col[i].width = "1p"; 

                                              allcolwidth += col[i].width; 

                                          } 

                                      else if(cont.indexOf("*") !=-1) 

                                      { 

                                              col[i].width = "1p"; 

                                              allcolwidth += col[i].width; 

                                             

                                          } 

                                      else if(cont.indexOf("%") !=-1) 

                                      { 

                                              col[i].width = "1p"; 

                                              allcolwidth += col[i].width; 

                   

                                          } 

                                     else if(cont.indexOf(")") !=-1) 

                                      { 

                                              col[i].width = "1p"; 

                                              allcolwidth += col[i].width; 

                   

                                          } 

                              

                                      else 

                                      { 

                                              col[i].width = "5p"; 

                                              allcolwidth += col[i].width; 

                                              for(var k=0;k<col[i].cells.length;k++) 

                                              { 

                                                      if(col[i].cells[k].contents !="") 

                                                      { 

                                                              var paras = col[i].cells[k].paragraphs; 

                                                              for(var l=0;l<paras.length;l++) 

                                                              { 

                                                                      paras[l].justification = Justification.RIGHT_ALIGN; 

                                                                  } 

                                                          } 

                                                  } 

                                          } 

                                  } 

                              col[0].width = w - _left - _right - allcolwidth; 

                          } 

                      else 

                      { 

                              alert("Select a table...") 

                          }

                   

                   

                  Regards,

                  Velu

                  • 6. Re: Re: Adjust column width in tables
                    Peter Kahrel Adobe Community Professional & MVP

                    Chinna,

                     

                    In this code:

                     

                      var col = sel.columns; 
                                    _left = sel.parent.parentPage.marginPreferences.left; 
                                    _right = sel.parent.parentPage.marginPreferences.right, allcolwidth = 0;
                    

                     

                    _left and _right are defined as global variables. To make them local (as you no doubt intended),  in lines 1 and 2 replace ; with ,

                     

                    Also, instead of var col = sel.columns use var col = sel.columns.everyItem().getElements() so that the script processes an array rather than a collection, which speeds up things enormously.

                     

                    Peter