12 Replies Latest reply on Mar 22, 2016 10:03 AM by Obi-wan Kenobi

    Find the first line of a table on each page when the table runs on several pages…

    Obi-wan Kenobi Adobe Community Professional

      Hi,

       

      Can we find the first line of a table on each page and and select all the lines after when the table runs on several pages ?…

       

      Thanks in advance! 

        • 1. Re: Find the first line of a table on each page when the table runs on several pages…
          Laubender Adobe Community Professional & MVP

          Hi Obi-wan,

          "first line" on a page is a bit vague.


          If you mean the first row of a table in a text frame, yes, we could map all indexes of rows of a well-formed table, that are first. Well-formed in the sense, that there are no overset cells or parts of the table is in overset text.


          And with that information derive the page (or the spread, in case parts of the table are positioned on the pasteboard), then in a next step see if a page change comes along and select for example all rows of a table:

          1. In a text frame.

          2. In a bunch of text frames when on the same page.

           

          Maybe starting with some text selected in a text cell.

           

          To start and to throw some code in here, check what I'm doing in this thread:

          Re: I need to export all the table in "png" fomat, but continuied table also exported same as pdf..

           

          It's not exactly what you need, but the idea can be developed from this.
          If graphic cells are involved (CC 2015) we have to jump through some extra hoops.

           

          Uwe

          • 2. Re: Find the first line of a table on each page when the table runs on several pages…
            Trevorׅ Adobe Community Professional

            Hi Obi,

             

            This should do what you want providing that the textFames are set up to start more or less at the same point, i.e. the table on the next page starts above the first baseLine of the bottom row of the table on the previous page.

             

            function tableRows(myTable) {
                var doc, secondTable, secondTableTextCotainers = [], myBaselines, c, l, previousBaseLine, currentBaseLine
                    firstLineRows = [], nonFirstLineRows = [];
                doc = app.properties.activeDocument;
                if (!doc) {
                    alert("Jerk");
                    exit();
                };
                if (!myTable) {
                    app.findTextPreferences = null;
                    app.findTextPreferences.findWhat = "\x16";
                    var tables = doc.findText(false);
                    if (!tables.length) exit();
                    myTable = tables[0].tables[0];
                }
                myBaselines = myTable.rows.everyItem().cells[0].insertionPoints[0].baseline;
                l = myBaselines.length;
                previousBaseLine = myBaselines[0];
                for (c = 0; c < l; c++) {
                    currentBaseLine = myBaselines[c];
                    if (currentBaseLine > previousBaseLine) {
                        nonFirstLineRows.push(myTable.rows[c]);
                        myTable.rows[c].fillColor = "Yellow";
                    }
                    else {
                        firstLineRows.push(myTable.rows[c]);
                        myTable.rows[c].fillColor = "Magenta";
                    }
                    previousBaseLine = currentBaseLine;
                }
                return {firstLineRows: firstLineRows, nonFirstLineRows: nonFirstLineRows}
            }
            
            app.doScript("var tr = tableRows()", ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT);
            alert("firstLineRows length: " + tr.firstLineRows.length + "\nnonFirstLineRows length: " + tr.nonFirstLineRows.length);
            

            HTH

             

            Trevor

             

            P.s. You can only "select" consecutive rows.

            • 3. Re: Find the first line of a table on each page when the table runs on several pages…
              Trevorׅ Adobe Community Professional

              After seeing Uwe's answer I would do this.

              function tableRows(myTable) {
                  var doc, secondTable, secondTableTextCotainers = [], myTextFrame, c, l, previousTextFrame, currentTextFrame
                      firstLineRows = [], nonFirstLineRows = [];
                  doc = app.properties.activeDocument;
                  if (!doc) {
                      alert("Jerk");
                      exit();
                  };
                  if (!myTable) {
                      app.findTextPreferences = null;
                      app.findTextPreferences.findWhat = "\x16";
                      var tables = doc.findText(false);
                      if (!tables.length) exit();
                      myTable = tables[0].tables[0];
                  }
                  myTextFrame = myTable.rows.everyItem().cells[0].insertionPoints[0].parentTextFrames;
                  l = myTextFrame.length;
                  for (c = 0; c < l; c++) {
                      currentTextFrame = myTextFrame[c][0];
                      if (currentTextFrame != previousTextFrame) {
                          nonFirstLineRows.push(myTable.rows[c]);
                          myTable.rows[c].fillColor = "Yellow";
                      }
                      else {
                          firstLineRows.push(myTable.rows[c]);
                          myTable.rows[c].fillColor = "Magenta";
                      }
                      previousTextFrame = currentTextFrame;
                  }
                  return {firstLineRows: firstLineRows, nonFirstLineRows: nonFirstLineRows}
              }
              
              app.doScript("var tr = tableRows()", ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT);
              alert("firstLineRows length: " + tr.firstLineRows.length + "\nnonFirstLineRows length: " + tr.nonFirstLineRows.length);
              

               

              You could tweak it as needed

               

              Trevor

              • 4. Re: Find the first line of a table on each page when the table runs on several pages…
                Obi-wan Kenobi Adobe Community Professional

                Thanks all 2!  We are on the right way!

                 

                I need to quietly read you before making more comments! See you later! 

                 

                [ I've tested Trevor's script! Very interesting! (problem if table header took in account)  ;-) ]

                • 5. Re: Find the first line of a table on each page when the table runs on several pages…
                  Trevorׅ Adobe Community Professional

                  Hi Obi,

                   

                  Obi-wan Kenobi wrote:

                  ...

                  [ I've tested Trevor's script! Very interesting! (problem if table header took in account)  ;-) ]

                   

                  Trevor Wrote:

                  You could tweak it as needed

                   

                  Dump this at the beginning of the loop and tweak as needed

                  if (myTable.rows[c].rowType != RowTypes.BODY_ROW) continue;
                  

                   

                  Trevor

                   

                  P.s. Thanks for the PM.

                  • 6. Re: Find the first line of a table on each page when the table runs on several pages…
                    Laubender Adobe Community Professional & MVP

                    Hi Trevor,

                    I like your solution (with some precautions).

                     

                    Note for Obi-wan:
                    The tweak is also working for footer rows.

                     

                    There are two situations where detecting the first row will not work as expected, because parentTextframe of insertionPoints[0] will return undefined . This is immediately evident for tables, where all body cells of the first column meet one of two special conditions.

                     

                    myTable.rows.everyItem().cells[0].insertionPoints[0].parentTextFrames;
                    

                     

                    The code above will return something like the line below for a table in one single text frame with three header rows and two footer rows where all body cells in the first column are overset or are not showing the "end of story"-marker:

                     

                    /* RETURNS:
                    [object TextFrame],[object TextFrame],[object TextFrame],,,,,,[object TextFrame],[object TextFrame]
                    */
                    

                     

                    Condition 1:

                    The cell is overset

                     

                    Condition 2:

                    Technically seen the cell is NOT overset (showing no red dot), is containing nothing but a single insertion point, has a fixed height and the pointSize of insertionPoints[0] is too large to show the "end of story"-marker in the cell.

                     

                    Depending, if all body cells in column one meet condition 2

                    or only the first body cell of column one in every threaded text frame meets condition 2,

                     

                    the script will yield "crazy" results like this:

                     

                    Before:

                    1-BeforeRunningTrevorsScript.png

                     

                    After:

                     

                    2-AfterRunningTrevorsScript.png

                     

                    The green colored rows are the header rows, the blue colored rows are the footer rows.
                    The yellow and magenta rows are the colored ones after running Trevor's script with the tweak applied.


                    "Yellow" should stand for "first body row in text frame", "Magenta" for all following rows in the same text frame.

                     

                    Uwe

                    • 7. Re: Find the first line of a table on each page when the table runs on several pages…
                      Trevorׅ Adobe Community Professional

                      @Uwe, Good points, I shall leave them to Obi to sort out.

                       

                      @Obi please mark the question as answered

                       

                      Trevor

                      • 8. Re: Find the first line of a table on each page when the table runs on several pages…
                        Obi-wan Kenobi Adobe Community Professional

                        Hi Trevor and Uwe,

                         

                        What I would like to do is merging Trevor's and Jarek's scripts – playing with cell styles – and limit the research to tables with a specific table style!

                        I would like to treat all the tables with the table style: "Table_XXX".

                        This style defines the "Red" cell style for the header and the "Blue" cell style to the other cells.

                        Jarek's script "alternates" Blue" lines and "Yellow" lines ["Yellow" is the third cell style]

                        If the table runs on several pages, the Trevor's approach will play the game!

                         

                        Jarek's script:

                         

                        var 

                          mDoc = app.activeDocument, 

                          mTables = mDoc.stories.everyItem().tables.everyItem().getElements(), 

                          rowsToOmit = 1,     //     how many rows to exclude 

                          rowIndexToStart = 1,     //     which row is the starting one to apply a style 

                          mStart, mEnd, step, 

                          mTables, cTable, 

                          cCell, cSpan, cRange, 

                          mCellStyle = mDoc.cellStyles.item("Yellow"); 

                         

                          while ( cTable = mTables.pop() ) { 

                               mCells = cTable.columns[0].cells.itemByRange(rowsToOmit,-1).cells.everyItem().getElements();

                               for (step = rowIndexToStart; step < mCells.length; step +=2) { 

                                    cCell = mCells[step]; 

                                    cSpan = cCell.rowSpan - 1; 

                                    mStart = cCell.parentRow.index; 

                                    mEnd = mStart + cSpan; 

                                    cRange = cTable.rows.itemByRange(mStart,mEnd).cells.everyItem(); 

                                    cRange.appliedCellStyle = mCellStyle; 

                                    } 

                               } 

                        • 9. Re: Find the first line of a table on each page when the table runs on several pages…
                          Trevorׅ Adobe Community Professional

                          So why don't you just loop through the documents tables and run my function on the ones with the applied table style?

                          myTable = tables[0].tables[0];

                          give you the first table find

                          myTable = tables;

                          Gives you any array of all the tables specifiers.

                          some semi code

                          n = myTable.length;

                          while (n--) {

                          t = myTable[n].tables[0];

                          if !t.appliedTableStyle != myTableStyle) continue;

                          .... etc ....

                          • 10. Re: Find the first line of a table on each page when the table runs on several pages…
                            Obi-wan Kenobi Adobe Community Professional

                            … Because I'm not able to write such a script! too newbie for this! … and I'm immediately very sorry to not be able to play this game! 

                            • 11. Re: Find the first line of a table on each page when the table runs on several pages…
                              Laubender Adobe Community Professional & MVP

                              Hi Obi-wan,

                              do you want something like an alternating fill (or cell style) for rows where the first body row in a threaded table will always start the same?

                               

                              AlternatingRows-DifferentApproach.png

                               

                              Uwe

                              • 12. Re: Find the first line of a table on each page when the table runs on several pages…
                                Obi-wan Kenobi Adobe Community Professional

                                Uwe, yes! When a table runs on several pages, the idea is to begin one each page with the same first cell style (in your sample: "Yellow" cell style), even if we have a header!  Jarek's approach is interesting because table merging lines are taken in account!