2 Replies Latest reply on Nov 24, 2009 8:41 AM by [Jongware]

    Create/find tables and apply style

    jessevictoor Level 1

      Hello,

       

      I'm very new to scripting. I want to learn the basics like creating a table, creating anchored frames, textframes, etc.

      Now, I want to create/find tables in a document and I would like to apply styles. I saw a script to do this. When I paste the script in Extendscript CS4, than the program crash.

      The program crash in part 1. When I create another script with only part 2 (see //part 2) then there is no problem.

      What do i wrong? And can anyone tell me what part 1 of the script do?

       

      Thanks,

       

      // part 1

      // 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
      }

       

       

      // part 2


      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";
      }