6 Replies Latest reply on Jan 27, 2009 9:06 AM by (Dave_Saunders)

    Javascript to iterate over every cell in a table

      Hello experts

      I am importing XML tables into Indesign with attributes that specify the cell style, paragraph style, and character style.

      Indesign's rendering behaviour is quite odd as it displays some, but not all of the table correctly! When I click into a cell, the correct cell, paragraph and character styles are selected but the cell-style doesn't seem to be applied properly, especially the cell rules. If I click the cell style, then the rule is displayed.

      I need a work-round for this problem while Adobe are looking into it.

      Could someone write a Javascript that iterates through a table, cell by cell, finds the cellstyle that is currently applied and then re-applies that style? I'm hoping this will get round my problem.

      I currently have no experience with Javascript, so any help would be appreciated.

      I am running Indesign 5.0.4


        • 1. Re: Javascript to iterate over every cell in a table
          Level 1
          Bear in mind that every cell rule that is not on the edge of the table is shared. Thus, the look of any particular rule is determined by the order in which the cells are drawn, which is left to right then top to bottom.

          Could this be the reason you're seeing what you're seeing?

          Reapplying the cell styles might not do the job and the effect will be different depending on the order you do it.

          I just created a pair of cell styles that won't live comfortably together. One applies a 1 point black rule left and right, the other applies a 0 point rule at left. These compete with each other. Apply them like a checkerboard and the result will depend on whether you go from top-left to bottom-right or the other direction.

          This script demonstrates:
          (function() {
            if (app.documents.length > 0
                && app.selection.length > 0) {
              var myTable = findTable(app.selection[0]);
              if (myTable == null) { return }
              var myStyles = myTable.cells.everyItem().appliedCellStyle;
              for (var j = myStyles.length -1; j >= 0; j--) {
          //~     for (var j = 0; myStyles.length > j; j++) {
                if (myStyles[j].name != "None") {
                  myTable.cells[j].appliedCellStyle = myStyles[j];

          function findTable(obj) {
            if (obj instanceof TextFrame) {
              return obj.tables[0];
            while (obj.constructor.name != "Table") {
              obj = obj.parent;
              if (obj.constructor.name == "Application") {
                return null;
            return obj
          } // end findTable
          Click in a table and run it. Change the loop to go the other way (just comment out the currently active for loop and activate the other one). If you get the same result, your cell styles are compatible and can coexist in adjacent cells, but if you don't the problem is your styles not InDesign behaving badly when rendering.

          • 2. Re: Javascript to iterate over every cell in a table
            thepinkpanther Level 1
            Hi Dave

            Thanks for the quick response.

            I did notice the way Indesign draws cell rules, but I don't think that is my problem, but I'd like to run your script to be sure.

            I apologise, but my scripting knowledge is limited and I cannot get it to run within ESTK. The line below errors:

            if (app.documents.length > 0 && app.selection.length > 0) {

            saying 'undefined is not an object'

            Could you please point me in the right direction?

            Kind regards.

            • 3. Re: Javascript to iterate over every cell in a table
              Peter Kahrel Adobe Community Professional & MVP

              Something you try is this: force InDesign to redraw the screen, e.g. by toggling preview and normal screen modes. This might work as you say that merely clicking in a cell shows the lines.

              As to Dave's script, it may not run if you don't tell it to talk to InDesign. In the ESTK, look at the top left of the app. There are two dropdowns there; in the left one, select InDesign CS3.

              • 4. Re: Javascript to iterate over every cell in a table
                Level 1
                Hi Kevin,

                Peter beat me to it. When running from ESTK, make sure that the target application is InDesign CS3.

                I make that mistake about 20 times a week.

                • 5. Re: Javascript to iterate over every cell in a table
                  thepinkpanther Level 1
                  Hi Peter & Dave

                  Yes, targeting Indesign resolved the problem.

                  Having run the script I can now see how Indesign renders the table, and yes, I have cellstyle rule conflicts.

                  So, I see two work rounds. One, re-jig my cellstyles so there are no conflicts (see note below), or two, write a script that re-applies cellstyles to only styles that contains rules.

                  Note: My table has 10 columns, the last row has one cell which spans all 10 columns and a rule is applied to the top of the cell. Applying the left-to-right top-to-bottom render, this rule should span across all 10 columns (as it is applied to the top of the spanned row), but only applies itself to the top of the first column, leaving the other nine blank. Any ideas?

                  Kind regards.

                  • 6. Re: Javascript to iterate over every cell in a table
                    Level 1
                    Did you run the script counting up or down? For the script to apply that rule across the whole of the last row, it must happen after the styles were applied to the row above it.