15 Replies Latest reply on Jul 16, 2018 2:11 AM by steveg87687011

    Table cell height change script

    steveg87687011 Level 1

      Hi all

       

      I've searched widely but have been unable to find an answer that works for me.

       

      I have a task to repeat on hundreds of documents.

       

      There is a table in each document that contains cells of two different heights - 4mm & 5mm.

      I would like to reduce the depth of each cell (every cell in the table) by 1mm to end up with cell heights of 3mm & 4mm.

      There are no header footer cells in table.

       

       

      would someone be able to help me with a script to achieve this change?

       

       

      MTIA Steve

        • 1. Re: Table cell height change script
          BarlaeDC Adobe Community Professional

          HI,

           

          When you say you have cells of differing heights, is that per row, i.e. one row of cells is 5mm and another is 4 mm?

           

          Regards

           

          Malcolm

          • 2. Re: Table cell height change script
            steveg87687011 Level 1

            Hi Malcolm,

             

            A good job you asked. It's actually two tables, one on top of the other - they contain data from Excel named ranges.

            On some pages it's one table, some pages two tables (as below) and some pages x3 tables - all following same 'style'.

             

            The depth of each row is either 5mm or 4mm, giving a look of a 1mm paragraph space below.

             

            I'd like to reduce the depth of all rows by 1mm - all 4mm to 3mm, all 5mm to 4mm.

             

            screen shot.png

            Steve

            • 3. Re: Table cell height change script
              BarlaeDC Adobe Community Professional

              Hi,

               

              This is the code I use to resize cells in a table, there may be better ways but I find this works pretty consistently.

               

               

              var myCells = app.activeDocument.textFrames.everyItem().tables.everyItem().cells.everyItem().getElements();
              for ( var i = 0; i < myCells.length; i++)
              {
                  myCells[i].height = myCells[i].height -1 ;
              }
              app.activeDocument.textFrames.everyItem().tables.everyItem().recompose;
              

               

              Please note that once you get to a certain cell height, the cell wont get any smaller.

               

              Hope this helps.

               

              Malcolm

              • 4. Re: Table cell height change script
                payalm68947498 Level 2

                Hey you can try this,

                    var ad = 0;
                    var allDoc = app.documents;
                    while(ad < allDoc.length){
                        var stories = app.documents[ad].stories;
                        var st = 0;
                        while(st < stories.length){
                            var allTab = stories[st].tables;
                            var tb = 0;
                            while(tb < allTab.length){
                                var maxHeight = null;
                                var rw = 0;
                                var rows = allTab[tb].rows;
                                while(rw < rows.length){
                                    maxHeight = rows[rw].height;
                                    if(maxHeight == 4 || maxHeight == 5){
                                        rows[rw].height = rows[rw].height-1;
                                        }
                                    rw++;
                                    }
                                tb++;
                                }
                            st++;
                            }
                        ad++;
                        }
                

                 

                Regards,

                Payal

                • 5. Re: Table cell height change script
                  steveg87687011 Level 1

                  Hi Malcolm,

                  Thanks for the script, it works nicely - up to a point.

                   

                  If you look at the image I posted yesterday you'll see that the table has

                   

                  i) rows of a single cell and

                  ii) rows of two cells (split vertically).

                   

                  All of the 5mm, single cell rows, reduce perfectly to 4mm.

                   

                  The script is reducing some (but not all) of the 4mm (x2 cell) rows to 2mm?

                   

                  I'm trying to decipher what is causing what seems like an odd inconsistency.

                   

                  Is it possible to change the script to only run on a selected table?

                   

                   

                   

                   

                  Steve

                  • 6. Re: Table cell height change script
                    BarlaeDC Adobe Community Professional

                    Hi,

                     

                    I can't believe I never spotted that problem before here is updated code for that problem.

                     

                    var myDoc = app.activeDocument;
                    
                    var myRows = app.activeDocument.textFrames.everyItem().tables.everyItem().rows.everyItem().getElements();
                    for ( var i = 0; i < myRows.length; i++)
                    {
                        var myCells = myRows[i].cells;
                        for ( var j = 0; j < myCells.length; j++)
                        {
                            if ( j === 0)
                            {
                                myCells[j].height = myCells[j].height -1 ;
                             }
                            else
                            {
                                  myCells[j].height = myCells[0].height;
                             }
                         }
                    }
                    app.activeDocument.textFrames.everyItem().tables.everyItem().recompose;
                    

                     

                    It is possible to run it on a selected table, I will try and get a chance to create a sample for that today if possible.

                     

                    Regards

                     

                    Malcolm

                    • 7. Re: Table cell height change script
                      Laubender Adobe Community Professional & MVP

                      Hi,

                      consider that some cells are perhaps set with autoGrow to true.

                      And also consider inset values like topInset and bottomInset as well.

                      Also see into minimumHeight and maximumHeight.

                       

                      Regards,
                      Uwe

                      • 8. Re: Table cell height change script
                        steveg87687011 Level 1

                        Hi payalm68947498

                         

                        Thanks, but your script is returning error below

                         

                        javascript error.png

                        • 9. Re: Table cell height change script
                          payalm68947498 Level 2

                          use try catch in that place

                          try{    
                              var ad = 0;  
                              var allDoc = app.documents;  
                              while(ad < allDoc.length){  
                                  var stories = app.documents[ad].stories;  
                                  var st = 0;  
                                  while(st < stories.length){  
                                      var allTab = stories[st].tables;  
                                      var tb = 0;  
                                      while(tb < allTab.length){  
                                          var maxHeight = null;  
                                          var rw = 0;  
                                          var rows = allTab[tb].rows;  
                                          while(rw < rows.length){  
                                              maxHeight = rows[rw].height;  
                                              if(maxHeight == 4 || maxHeight == 5){  
                                                  rows[rw].height = rows[rw].height-1;  
                                                  }  
                                              rw++;  
                                              }  
                                          tb++;  
                                          }  
                                      st++;  
                                      }  
                                  ad++;  
                                  } 
                              }
                          catch(e){}
                          
                          1 person found this helpful
                          • 10. Re: Table cell height change script
                            steveg87687011 Level 1

                            Hi Malcolm

                             

                            unfortunately it doesn't like that!

                             

                            I do love the lengths people go to help out on these forums, but always feel guilty when things turn out more involved than first appear.

                            Thank you!

                             

                            error.png

                            • 11. Re: Table cell height change script
                              Laubender Adobe Community Professional & MVP

                              Hi Steve,

                              you saved the script in the wrong format!

                              You need plain text only, not as formatted text as the error suggests: {\rtf

                               

                              See:

                              Indiscripts :: Indiscripts for Dummies

                               

                              Regards,
                              Uwe

                              • 12. Re: Table cell height change script
                                steveg87687011 Level 1

                                Thanks Uwe - now I've remedied that it works perfectly!

                                • 13. Re: Table cell height change script
                                  steveg87687011 Level 1

                                  Thanks guys,

                                   

                                  I now have two options to perform the changes I need!

                                   

                                  Malcolm - I look forward to seeing the option to select & change individual tables, as I have other tables on the page containing images that I need to remain 'as is'.

                                   

                                  Many thanks all

                                   

                                   

                                  Steve

                                  • 14. Re: Table cell height change script
                                    BarlaeDC Adobe Community Professional

                                    Hi,

                                     

                                    To use the selected Page items, means you could select multiple items and if they have tables they should be changed. although if there is an easy rule to stop a table from being processed that might be easier to build into the script, for example: if table contains an image, do not change.

                                     

                                    but here is the code, works for the simple 2 table sample document I used for testing.

                                     

                                    var myDoc = app.activeDocument;
                                    var mySelectedPageItems = myDoc.selectedPageItems;
                                    // replaced to allow for the selected page item
                                    //var myRows = app.activeDocument.textFrames.everyItem().tables.everyItem().rows.everyItem().getElements();
                                    for ( var k = 0; k < mySelectedPageItems.length; k++)
                                    {
                                        var curPageItem = mySelectedPageItems[k];
                                        var myRows = curPageItem.tables.everyItem().rows.everyItem().getElements();
                                        for ( var i = 0; i < myRows.length; i++)
                                        {
                                            var myCells = myRows[i].cells;
                                            for ( var j = 0; j < myCells.length; j++)
                                            {
                                                if ( j === 0)
                                                {
                                                    myCells[j].height = myCells[j].height -1 ;
                                                }
                                                else
                                                {
                                                    myCells[j].height = myCells[0].height;
                                                }
                                            }
                                        }
                                    }
                                    app.activeDocument.textFrames.everyItem().tables.everyItem().recompose;
                                    

                                     

                                     

                                    Hope this helps

                                     

                                    Malcolm

                                    1 person found this helpful
                                    • 15. Re: Table cell height change script
                                      steveg87687011 Level 1

                                      Thanks very much Malcolm,
                                      I'm looking forward to trying it out Monday morning!

                                       

                                      Now Monday am - It works like a dream!

                                       

                                      Thanks very much for your help

                                       

                                      Steve

                                      1 person found this helpful