4 Replies Latest reply on Oct 12, 2009 2:23 PM by johnBking78

    table styles to all of document?

    johnBking78

      I have a script (kindly taken from this forum, see below) that applies table/cell styles to a tables you have selected, is there a way to apply this to all tables in the document in one go?

       

      its a basic style, just a header row body content and footer row.

       

       

      Many thanks.

      // using your version; I'd prefer to return 0 if table not found
      function findTable(obj) {
               while (obj.constructor.name != "Table") {
              obj = obj.parent;
              if (obj.constructor.name == "Application") {
                  throw "Can't find table"
              }
          }
          return obj
      }
      var myTable = 0;
      if (app.documents.length > 0 && app.selection.length > 0)
          myTable = findTable(app.selection[0]);
       
      // check for at least enough rows for 1 top row, 1 body row, 1 bottom row
      if (myTable != 0 && myTable.rows.length >= 3) {
          // delete all rules; set insets to 0
          myTable.cells.everyItem().appliedCellStyle = "[None]";
       
          // format bottom row
          // (remember rows uses 0-based count)
          a = myTable.rows.length - 1;
          myTable.rows[a].cells.everyItem().appliedCellStyle = "4.cell_body_last";
       
          // format middle rows
          first = 1;
           last = myTable.rows.length - 2;
          for (a = first; a <= last; ++a)
              myTable.rows[a].cells.everyItem().appliedCellStyle = "3.cell_body";
       
          // format top row
          a = 0;
          myTable.rows[a].cells.everyItem().appliedCellStyle = "2.cell_header";
      }
      

        • 1. Re: table styles to all of document?
          ramkumar.vp Level 3

          Hi Jamb,


          Use your script as funtion in the below coding:


              var  MyDoc;
              MyDoc = app.activeDocument;
              for(var x=MyDoc.groups.length-1;x>=0;x--)
              {
                  try
                  {
                      MyDoc.groups[x].ungroup();
                  }catch(e){}
              }
              for(var i=MyDoc.textFrames.length-1;i>=0;i--)
              {
                  for(var j=MyDoc.textFrames[i].tables.length-1;j>=0;j--)
                  {
                      try
                      {
                          var myTab = MyDoc.textFrames[i].tables[j];

                          myTab.select();

                          YourFunction(); // Call your function here.
                      }catch(e){}
                  }
              }


          Regards,

          Ramkumar

          • 2. Re: table styles to all of document?
            johnBking78 Level 1

            Hi ramkumar.vp

             

            I am a bit new to scripting, how do I call my function?

             

            Sorry to ask, and many thanks for you help!

             

             

            Kind Regards

             

            Adam

            • 3. Re: table styles to all of document?
              [Jongware] Most Valuable Participant

              (Stepping in for Ram)

               

              It's a bit easier than you think. Ram's code physically selects each table, and that's not even necessary. I'd change his

               

              var myTab = MyDoc.textFrames[i].tables[j];

                              myTab.select();

                              YourFunction(); // Call your function here

               

              (which works on a selection) to

               

              myTab = MyDoc.textFrames[i].tables[j];
              ProcessOneTable (myTab);
              

               

              (still using a variable, although that's not really necessary). Then change your code into a function, like this. Remove the lines

               

              var myTable = 0;
              if (app.documents.length > 0 && app.selection.length > 0)
                  myTable = findTable(app.selection[0]);

              and replace with

               

              function ProcessOneTable(myTable)
              {
              

               

              -- and add a closing brace after the very last line. As you can see, the function gets called with a variable "myTable" set to each one of the tables in your current document. The rest should stay unchanged.

              1 person found this helpful
              • 4. Re: table styles to all of document?
                johnBking78 Level 1

                Thank you very much [Jongware]!!!