3 Replies Latest reply on Aug 20, 2014 3:55 AM by MohamedYousif

    Script to select last row of a long table in every page

    MohamedYousif

      I have a problem in a script written to automatically flow long table over pages and i need now to select the last row in every page.

      For Example, i have a long table consists of 120 row and will flow over 3 pages i need to select the last row in every page so i will select row number 41, 88, and 120, So, how can i write a code to select this.

      NOTE: i can not know the number of rows in every page so, the rows number in the first page, second, and third page may be different.

      I wrote a Code depend on the rows height as the following:

       

      var rows = textFrameOri.tables[0].rows;
      var rowsHeight = 0;
          // GET THE EXACT NUMBER OF LAST ROW BEFORE BREAK and GIVE STYLE
                  var originalHeight = textFrame.geometricBounds[2] - textFrame.geometricBounds[0];
                  for(i = 0; i<rows.length; i++)
                  {
                      var height =rows[i].height;
                      rowsHeight +=height;
                      if(rowsHeight == originalHeight)
                      {
                          index = rows[i].index;
                          break;
                     }
                      if(rowsHeight > originalHeight)
                      {
                          index = rows[i - 1].index;
                          break;
                      }
                  }
              rows[index].cells.everyItem().appliedCellStyle = "Body.bottom";
      
      

      but this was not accurate. Please any help i will be grateful.

       

      Thanks in advance,

        • 1. Re: Script to select last row of a long table in every page
          Laubender Adobe Community Professional & MVP

          @Mohamed – you can do it in a different way.

           

          You could identify the text frame a specific cell is sitting in by looking at it's first insertionPoint.

           

          myCell.texts[0].insertionPoints[0].parentTextFrames[0].id;
          

           

          will get you the unique ID of that frame. If that changes, your table is flowing to the next text frame.

          And you can get the page the text frame is sitting on by doing this:

           

          myCell.texts[0].insertionPoints[0].parentTextFrames[0].parentPage;
          

           

          This will return an Page Object or null, if the text frame is on the pasteboard.

           

          If the text frame is on a page, you can get the unique document offset of the page:

           

          myCell.texts[0].insertionPoints[0].parentTextFrames[0].parentPage.documentOffset;
          

           

          If that will change, you know that the table is flowing to a different page.

           

          So all we have to do is to identify the cell before this happens and access the row it is sitting in:

           

          myCellBeforeMyCell.parentRow;
          

           

          How can we know the cell before? It's just the one with the index number of the cell that has the page change minus 1.

           

          You can loop all cells of your table and react to the results of documentOffset.
          Do not forget to include the last row of the table after looping. After that one there will be no change.

           

          To get the array of all cells just do:

           

          var myCells = myTable.cells.everyItem().getElements();
          for(var n=0;n<myCells.length;n++){
          //Your code here for checks on every individual cell.
          };
          

           

          Uwe

          • 2. Re: Script to select last row of a long table in every page
            Laubender Adobe Community Professional & MVP

            One warning though: If cells are merged vertically, parentRow will return the first row of the merged cells. Not the last one you might be interested in. You have to check for parentRow.rowSpan to detect how many rows the individual cell is spanning. Add that to your calculations.

             

            Uwe

            • 3. Re: Script to select last row of a long table in every page
              MohamedYousif Level 1

              Simply Perfect , that is the way to help another person Thank You, when you think in a way you think that way is the smartest way ever, but it is not. actually i did not use your snippet but your idea make me think in another way and it was perfect .

              Thank You Laubender you made my day