8 Replies Latest reply on Jun 19, 2017 12:33 AM by Laubender

    Adding footer to an existing table using scripts

    revathiv80720177

      Hi,

       

      I need to add header and footer to an existing tables using script. I tried this,

       

      myTable.rows.item(-1).rowType = RowTypes.FOOTER_ROW;

       

      But It shows an error: Cannot set row type.

       

      How shall I get it.  Please do guide me.

       

      Regards,

      Revathi

        • 1. Re: Adding footer to an existing table using scripts
          Ananth@desgin Level 3

          Hi,

          var curDoc = app.documents[0]; 

          var myTables = app.selection[0];

          var allRows = myTables.rows;

          myTables.rows.add(LocationOptions.AFTER, allRows[1]);

          if (myTables.rows[-1].rowType == RowTypes.BODY_ROW) {  

               myTables.rows[-1].rowType = RowTypes.FOOTER_ROW;  

          }

           

          Thanks

          • 2. Re: Adding footer to an existing table using scripts
            Laubender Adobe Community Professional & MVP

            Hi Revathi,

            your code should work.

             

            Provide a screenshot of the table's last rows using the "Insert Image" widget of the forum editor showing the cell's edges.

             

            Two reasons I know of why this would not work ( there may be others ):

            1. The last row is already a footer row

            2. You have vertically merged cells in the last rows of your table

             

            In both cases a $ID/kCantSetRowTypeError is thrown.


            Case 2 is the problematic one.

            There could be vertically merged cells from rows above the last row.
            A vertically merged cell would be not part of the last row.

             

            Simply unmerging vertically merged cells would shift the contents to the top cell of the unmerged ones.

            And maybe there are horizontally merged cells in rows above the last row as well. So simply unmerging all cells of the rows above the last row in a table could change the layout of your table dramatically.

             

            Regards,
            Uwe

            1 person found this helpful
            • 3. Re: Adding footer to an existing table using scripts
              Laubender Adobe Community Professional & MVP

              Let's discuss a possible strategy for case 2:

              You have vertically merged cells in the last rows of your table

               

              One algorithm could be:


              If the last row of a table is not a footer row:

              Select rows step by step until the menu action "$ID/To Footer" is enabled.
              Then invoke the menu action.

               

              That's it. No unmerging. The minimal number of necessary rows are converted to footer rows.
              I think, that is the only reasonable thing you can do without asking the layouter what should be done in case of vertically merged cells.

               

              Why using a selection with the menu command?
              Because the line below is not working in case one cell in rows[-2] is merged with rows[-1]:

               

              // Will not work if vertically cells in rows[-2] were merged with cells in rows[-1]:
              table.rows.itemByRange(-2,-1).rowType = RowTypes.FOOTER_ROW;
              

               

              EDIT: Also another note on itemByRange().rowType :
              It is not working at all. Merged or no merged cells.

               

              Regards,
              Uwe

              • 4. Re: Adding footer to an existing table using scripts
                Laubender Adobe Community Professional & MVP

                To make that case 2 more clear I did a small sample table with some merged cells.

                Ran a snippet on it where every cell's contents is the cell's name. That reveal something about its merged status.


                That snippet will only work with text type cells:

                 

                var table = app.documents[0].stories[0].tables[0];
                var cells = table.cells.everyItem().getElements();
                
                for(var n=0;n<cells.length;n++)
                {
                    cells[n].contents = cells[n].name;
                };
                

                 

                Here a visualisation of my test table before and after I converted the minimum number of rows to footer rows:

                 

                MinNumberOfFooterRows-Table-1.png

                 

                Regards,
                Uwe

                • 5. Re: Adding footer to an existing table using scripts
                  revathiv80720177 Level 1

                  Hi Uwe,

                  Thanks for your guidance.

                   

                  I am having a normal table in InDesign document. I tried to add a Footer to that normal table. Herewith I have provided the sample table below for your reference.

                       

                  a





                  b





                  c










                   

                  For any code I received the same error like, Object does not support the property or methods 'rows' and undefined is an object.

                  Please do guide me.

                  • 6. Re: Adding footer to an existing table using scripts
                    Laubender Adobe Community Professional & MVP

                    Hi Revathi,

                     

                    please post a screenshot where guides and hidden characters are showing.
                    Also provide a sample document for download using a service like Dropbox and post a link.
                    Do also an IDML export file and a IDMS snippet file. I have to look into that case.

                     

                    Thanks,
                    Uwe

                    • 7. Re: Adding footer to an existing table using scripts
                      Obi-wan Kenobi Adobe Community Professional

                      … Because too many people (here and elsewhere) think the Jedi isn't enough cool since he plays with Javascript!!! …

                       

                      /*
                          0177_HeaderFooter-Table_MichelAllio.jsx
                          Script written by Michel Allio [2017/06/17]
                          
                          Object: Convert rows[0] to Header and rows[-1] to Footer 
                      */
                      
                      
                      app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;        
                      app.doScript(main, ScriptLanguage.JAVASCRIPT, [], UndoModes.ENTIRE_SCRIPT, "Header + Footer! …" );  
                              
                      function main()      
                      {
                      
                      
                          (function () {  
                            
                              var myDoc = app.activeDocument;
                              
                              function getTables () {
                                  // Document [No Selection]
                                  if ( app.selection.length == 0 ) return app.documents[0].stories.everyItem().tables.everyItem().getElements();
                                  // Selected Story by Insertion Point
                                  else if ( app.selection[0] instanceof InsertionPoint && app.selection[0].parent instanceof Story ) return app.selection[0].parentStory.tables.everyItem().getElements();
                                  // Selected Text
                                  else if ( app.selection[0] instanceof Text && app.selection[0].parent instanceof Story ) return app.selection[0].tables.everyItem().getElements();
                                  // Selected Text Frame
                                  else if ( app.selection[0] instanceof TextFrame ) return app.selection[0].tables.everyItem().getElements();
                                  // Selected Table by Insertion Point
                                  else if ( app.selection[0].parent instanceof Cell ) return [app.selection[0].parent.parent];    
                                  // Selected Table by Cell/Cells
                                  else if ( app.selection[0].parent instanceof Table ) return [app.selection[0].parent];    
                                  // Selected Table by Table
                                  else if ( app.selection[0] instanceof Table ) return [app.selection[0]];    
                              }    
                              
                              function Header_Footer (tables) {
                                  for (var t = 0; t < tables.length; t++) {  
                                      try {  
                                          var myTable = tables[t];
                                          if ( myTable.rows.length > 2 ) {
                                              //--------------------------------------------------------------------------------------------------------------------------------------------
                                              myTable.rows[0].rowType == RowTypes.BODY_ROW && myTable.rows[0].rowType = RowTypes.HEADER_ROW;
                                              myTable.rows[-1].rowType == RowTypes.BODY_ROW && myTable.rows[-1].rowType = RowTypes.FOOTER_ROW; 
                                              //--------------------------------------------------------------------------------------------------------------------------------------------
                                          }
                                      } catch (_) { } 
                                      // Fail silently  
                                  }  
                              }  
                             
                              if (app.documents.length > 0)  Header_Footer ( getTables ());  
                            
                          }());
                      
                      
                      }
                      

                       

                      Note all "table(s) selection" kinds!

                       

                      @ Uwe, thanks (as usual) for your comments! 

                       

                      (^/)

                      • 8. Re: Adding footer to an existing table using scripts
                        Laubender Adobe Community Professional & MVP

                        Hi Revathi,

                        back to your first post.

                        And the error message you sent by mail in the meantime.

                         

                        Here your screenshot of the error message:

                         

                        screenshot.png

                         

                        If you addressed variable myTables with:

                        var myTables = app.selection[0];
                        

                        and then worked on with that variable as if the selection would be a table the case is clear.

                        You did not address a table object, but a text frame.

                         

                        Select the table and not the text frame holding the table and run:

                        app.selection[0].rows.item(-1).rowType = RowTypes.FOOTER_ROW;
                        

                         

                        On my German Mac the command for selecting a table is alt+cmd+A if the cursor is in a cell.
                        You'll find your command with the context menu.

                         

                        Regards,
                        Uwe