2 Replies Latest reply on Jun 2, 2009 10:21 PM by Dr.Romano

    Apply styles to table

    Dr.Romano Level 1

      Hello. Can somebody help me with simple scipt for table layout: merge selected cells, then aplly character, paragraph and cell style to merged cell?

      Now i have this:

       

      try {app.selection[0].appliedCharacterStyle = app.activeDocument.characterStyles.item("mystyle")}catch (e) {exit()};
      try {app.selection[0].appliedParagraphStyle = app.activeDocument.paragraphStyles.item("mystyle")}catch (e) {exit()};
      try {app.selection[0].appliedCellStyle = app.activeDocument.cellStyles.item("mystyle")}catch (e) {exit()};


      It's apply character and paragraph style, but only for text, not for cell exactly.

        • 1. Re: Apply styles to table
          Dave Saunders Level 4

          You have to analyze what you're really trying to do. Tables don't have the paragraph and character style properties; the text in the cells of the tables do. Similarly, the cells have a cell style, not the table.

           

          You need something along these lines (assumes a selection inside a table and styles with the names here exist at the top level):

           

          var myTable = findTable(app.selection[0]);
          myTable.cells.everyItem().texts[0].appliedParagraphStyle = "myNewStyle";
          myTable.cells.everyItem().texts[0].appliedCharacterStyle = "Italic";
          myTable.cells.everyItem().appliedCellStyle = "myCellStyle";

           

          function findTable(obj) {
              while (obj.constructor.name != "Table") {
                  obj = obj.parent;
                  if (obj.constructor.name == "Application") {
                      throw "Can't find table"
                  }
              }
              return obj
          }

          • 2. Re: Apply styles to table
            Dr.Romano Level 1

            Thanks for advice Dave, i'm really newbie in programming and miss this point about correct selection (in spite of evidential )

            Simple from specialist look's like:

            var doc = app.activeDocument;
            var myCell = doc.selection[0].merge (doc.selection[0].rows[0]);
            myCell.appliedCellStyle = doc.cellStyles.item ('style');
            myCell.texts.everyItem ().appliedCharacterStyle = doc.characterStyles.item ('style');
            myCell.texts.everyItem ().appliedParagraphStyle = doc.paragraphStyles.item ('style');

            But, in any case, every example with comments help me understand how it works...