11 Replies Latest reply on Dec 2, 2015 10:16 AM by briank83163125

    Table: convert first row to header rows

    Folobo Level 1

      Hi all and sorry for this question.

      I find this script that auto convert to header the first row of a table (we know that using table style - if i set a cell style for header - InDesign do not recognizes the header row that i also set in excel).

       

      So i find a script that do it - but have 2 problem: error: 45 (Error string: Object is invalid) Line 4 and works for all table of the document (the best could be if work only with the table selected or of the page). Do you have an idea to solve this?

       

      var myTables = app.activeDocument.stories.everyItem().tables;
      
      for (var i = 0; i < myTables.length; i++) {
          myTables[i].rows.firstItem().rowType = RowTypes.HEADER_ROW;
      }
      

       

       

      (Another problem: what is the best way to set a border table use cell style? I want stroke 1!

      The solution to do a stroke to the text frame fitted is a bad way, i think. Can i have a confirm? Yes, i use a table style and cell style - but i see that table style do not override cell style. See also here: http://forums.adobe.com/message/5153224#5153224

       

      Many thanks

        • 1. Re: Table: convert first row to header rows
          Trevorׅ Adobe Community Professional

          You might find some useful points in this post

          http://forums.adobe.com/message/4792539#4792539

          • 2. Re: Table: convert first row to header rows
            Folobo Level 1

            Many thanks Trevor - but it is not so easy...

            Anyway what do you think about point 2 here: http://forums.adobe.com/message/5153224#5153224#5153224

             

            Really many thanks.

            • 3. Re: Table: convert first row to header rows
              Trevorׅ Adobe Community Professional

              Try this

               

              #target indesign;
              var myRows = app.activeDocument.stories.everyItem().tables.everyItem().rows[0].getElements(),
                    myCells = app.activeDocument.stories.everyItem().tables.everyItem().cells.everyItem(),
                    l = myRows.length;
              while (l--) if (myRows[l].rowType == RowTypes.BODY_ROW) myRows[l].rowType = RowTypes.HEADER_ROW; // need to check that the top row is not a header so as not to throw a can't set error
              
              myCells.leftEdgeStrokeWeight="1pt";
              myCells.rightEdgeStrokeWeight="1pt";
              myCells.topEdgeStrokeWeight="1pt";
              myCells.bottomEdgeStrokeWeight="1pt";
              

               

              Trevor

              • 4. Re: Table: convert first row to header rows
                Trevorׅ Adobe Community Professional

                To apply to selected page only change lines 2 and 3 to

                var myRows = app.activeWindow.activePage.textFrames.everyItem().tables.everyItem().rows[0].getElements(),
                      myCells = app.activeWindow.activePage.textFrames.everyItem().tables.everyItem().cells.everyItem(),
                

                The best way to set the strokes for the cell would be to give them a cell style, so given that you have set your cell style "Cell Style 1" up you can use this.

                 

                app.activeWindow.activePage.textFrames.everyItem().tables.everyItem().cells.everyItem().appliedCellStyle ="Cell Style 1" // for cell on the page
                app.activeDocument.stories.everyItem().tables.everyItem().cells.everyItem().appliedCellStyle ="Cell Style 1" // for every cell in the document
                

                 

                So you can put the code together as

                 

                #target indesign;
                var myRows = app.activeWindow.activePage.textFrames.everyItem().tables.everyItem().rows[0].getElements(),
                      myCells = app.activeWindow.activePage.textFrames.everyItem().tables.everyItem().cells.everyItem(),
                      l = myRows.length;
                while (l--) if (myRows[l].rowType == RowTypes.BODY_ROW) myRows[l].rowType = RowTypes.HEADER_ROW; // need to check that the top row is not a header so as not to throw a can't set error
                myCells.appliedCellStyle ="Cell Style 1"

                • 5. Re: Table: convert first row to header rows
                  Folobo Level 1

                  Hi Trevor and really many many thanks.

                  I have to do some text because i have some problems to run your example.

                  Anyway i use cell and table style.

                  The problem is that if you use cell style you can not have a table border (because cell style override table border)!

                  So the question: what is the way to have a table border?

                  • 6. Re: Table: convert first row to header rows
                    Laubender Adobe Community Professional & MVP

                    @Folobo – in that case you need a couple of cell styles for each and every occassion ;-)


                    First row:

                    Left upper corner, middle in row, right upper corner


                    All rows except first and last row:

                    Left, middle, right

                     

                    Last row:
                    Left bottom corner, middle in row, right bottom corner

                     

                    If your first row is formatted differently than row two, you'll need another set of cell styles…

                     

                    Uwe

                    • 7. Re: Table: convert first row to header rows
                      Trevorׅ Adobe Community Professional

                      Hi all,

                       

                      Well I have been realy lacking sleep, as can be seen by the times of some of my posts.

                       

                      No need to set up loads of cell styles.

                       

                      Just set up the one cell style and one table style that uses that cell style. Set the table border with the table style.

                      Then try this.

                       

                      #target indesign;
                      var myTables = app.activeWindow.activePage.textFrames.everyItem().tables.everyItem(),
                            myRows = myTables.rows[0].getElements(),      
                            l = myRows.length;
                      while (l--) if (myRows[l].rowType == RowTypes.BODY_ROW) myRows[l].rowType = RowTypes.HEADER_ROW; // need to check that the top row is not a header so as not to throw a can't set error
                      myTables.cells.everyItem().appliedCellStyle ="Cell Style 1" // to make sure that even cell with manual cellstyles applied to them are Zapped
                      myTables.appliedTableStyle = "Table Style 1";
                      myTables.clearTableStyleOverrides (); 
                      myTables.cells.everyItem().clearCellStyleOverrides(true); // true clears undefinded overides / false clears just defined overrided, take your pick, if in doubt, guess
                      

                       

                      Works for me.

                      • 8. Re: Table: convert first row to header rows
                        Laubender Adobe Community Professional & MVP

                        I'm still in doubt if clearing the table style overrides will work in every occasion…
                        But, of course, I will give it a try. Provided I'll find time to create a complicated enough test scenario.

                         

                        OT: lack of sleep? Yes. Let's starting fresh with that :-)

                         

                        Uwe

                        • 9. Re: Table: convert first row to header rows
                          Folobo Level 1

                          @Laubender Hi and really thanks! My problem is that i have to place all cell style in a table style (i import data with excel).

                          I understand your way but i have to set it with local formatting (out of cell style).

                          • 10. Re: Table: convert first row to header rows
                            Trevorׅ Adobe Community Professional

                            Folobo

                             

                            What didn't work with the code in 7 above?

                             

                            did you make the  change

                            clearCellStyleOverrides (false)

                            • 11. Re: Table: convert first row to header rows
                              briank83163125 Level 1

                              Try This,

                               

                              Worked For me.

                               

                              var myDoc = app.activeDocument;

                                    for(var T=0; T < myDoc.textFrames.length; T++){

                                       for(var i=0; i < myDoc.textFrames[T].tables.length; i++){

                              myDoc.textFrames[T].tables[i].rows[0].rowType = RowTypes.HEADER_ROW;

                              }

                              }