5 Replies Latest reply on Aug 14, 2009 7:35 AM by MooHaHa

    Indesign tables using JavaScript

    MooHaHa

      I'm pretty new to scripting and was hoping someone could help me script an InDesign table. I already have the styles set up. What I need is some advice in applying styles to specific rows - so, applying header and footer styles, and applying style to the remaining rows. Any help would be very much appreciated as I'm tearing out what little hair I have...

       

      Cheers

       

      Dave

        • 1. Re: Indesign tables using JavaScript
          Dave Saunders Level 4

          Your question is a bit vague. What's the difficulty? Identifying the table, the cells, the styles? Applying the styles? Applying the styles and getting the results you expected?

           

          Do you have some code that doesn't work that we could help you with?

           

          Dave

          • 2. Re: Indesign tables using JavaScript
            MooHaHa Level 1

            Hi Dave

             

            Thanks for your speedy reply, and apologies for my slack one.

             

            Here's the thing. Say I’ve got a table with ten rows, I need to apply one cell style to the first, another to the second to ninth, and another to the last (the cell styles are already set up in the document). I thought that I needed to specify the rows by doing something like:

             

            for (a=0; a<2; a++){
                with(myTable.rows[a]){
            myTable.cells.everyItem().appliedCellStyle = "3.cell_body";
            }
                    }

             

            I understand that this means 'a' will continue to run while a is less than 2, which indeed it does. However, I don't know what code/variables to use if I want to style just the top row. What would I use rather than 'a++'? I'm thinking that I need to be using 'if' and 'else' statements, but am unaware of the correct protocol.

             

            Hope this makes sense - look forward to your reply!

             

            Cheers

             

            Dave

            • 3. Re: Indesign tables using JavaScript
              MooHaHa Level 1

              Right, I've made some progress with this. I've got a table with fourteen rows. I want to apply "2.cell_header" to row 1, "3.cell_body" to rows 2-13 and "4.cell_body_last" to row 14. What I have is:

               

              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 = findTable(app.selection[0]);

               

              //delete all rules; set insets to 0
                  with(myTable.cells.everyItem()){
                      myTable.cells.everyItem().appliedCellStyle = "[None]";
                  }
                 

              a=0

               

               

              //format bottom row
              if (a=14);
                  with(myTable.rows[a]){
              myTable.cells.everyItem().appliedCellStyle = "4.cell_body_last";
              }

               


              //format middle rows
              if (a>=2 && a<=13);
                      with(myTable.rows[a]){
              myTable.cells.everyItem().appliedCellStyle = "3.cell_body";
              }

               

              //format top rows
                  if (a=1);
                  with(myTable.rows[a]){
              myTable.cells.everyItem().appliedCellStyle = "2.cell_header";
              }

               

              And, obviously, it's turning all rows to style "2.cell_header" because of the "everyItem" command. What can I replace this with to apply style to specific rows? And do I need to be redefining "a"? What glaring and positively simple thing am I including/omitting?

               

              I realise that these may seem ridiculous questions to those more advanced than me (ie everyone), but would really appreciate some help, or just a pointer to a good source (book, website).

               

              Cheers

               

              Dave

              • 4. Re: Indesign tables using JavaScript
                SteveCaine

                Dave,

                 

                The key is that Table.rows has its own .cells property, a collection of just the cells in that row. So, adapting your code:

                 

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

                }

                Hope this helps,
                Steve Caine

                • 5. Re: Indesign tables using JavaScript
                  MooHaHa Level 1

                  Sorted! Nice one Steve, really appreciate it. You're a gent and a scholar.

                   

                  Have yourself a top weekend!

                   

                  Dave