5 Replies Latest reply on Jul 10, 2014 11:36 AM by Jennifer Hearing

    Apply table style based on cell content

    Jennifer Hearing Level 1

      I am using InDesign 5 and am on a Mac, OS 10.8.5.

       

      Does anyone know how to apply a table style based on text contents found in a cell in javascript?

      I need to search the document and change every table style to "verbatim orange LONG" if the text "HS:" appear in the header row.

       

      I've been playing with the scripts found in the following 3 threads, but can't get it to apply a table style.

       

      Thanks!

       

      Apply Table Cell Style Based on Text Search and How to apply a table cell style based on grep search? and Re: Find PStyle and apply Cell Style in Table

        • 1. Re: Apply table style based on cell content
          Kai Rübsamen Adobe Community Professional

          Hi,

           

          I’m not sure, if this will work in CS5, since I have here only CS6 up, but try this one:

           

          var curDoc = app.activeDocument;
          var allTables = curDoc.stories.everyItem().tables.everyItem(); 
          
          app.findTextPreferences = app.changeTextPreferences = null;
          app.findTextPreferences.findWhat = "HS:";
          var allFounds = allTables.findText();
          app.findTextPreferences = app.changeTextPreferences = null;
          
          for ( var i = 0; i < allFounds.length; i++ ) {
              var curFound = allFounds[i];
              var curFoundParent = curFound[0].parent;
              if ( curFoundParent.parentRow.rowType == RowTypes.headerRow ) {
                  curFoundParent.parent.appliedTableStyle = curDoc.tableStyles.itemByName( "verbatim orange LONG" );
              }
          }
          

           

          –Kai

          • 2. Re: Apply table style based on cell content
            Jennifer Hearing Level 1

            Kai,

             

            Thank you for your reply! I tried the script and it "almost" works.

            I get an error message "Error 21: Undefined is not an object." Then it says the error is on line 11:   var curFoundParent = curFound[0].parent; 

            It does change the first table in the document that has "HS:", but none of the other tables with "HS:". Not sure what the snag is.

            • 3. Re: Apply table style based on cell content
              Kai Rübsamen Adobe Community Professional

              Hm, that means, that the syntax in this line work not in every case. Can you sent me a testfile with the right an one wrong table via link to the forum or private mail?

               

              I will try to solve the problem.

              • 4. Re: Re: Apply table style based on cell content
                Kai Rübsamen Adobe Community Professional

                Here are the corrected lines:

                 

                var curDoc = app.activeDocument;
                var allTables = curDoc.stories.everyItem().tables.everyItem(); 
                
                app.findTextPreferences = app.changeTextPreferences = null;
                app.findTextPreferences.findWhat = "HS:";
                var allFounds = allTables.findText();
                app.findTextPreferences = app.changeTextPreferences = null;
                
                for ( var i = 0; i < allFounds.length; i++ ) {
                    var curFound = allFounds[i];
                    if ( curFound.length == 1 ) {
                        var curFoundParent = curFound[0].parent;
                        if ( curFoundParent.parentRow.rowType == RowTypes.headerRow ) {
                            curFoundParent.parent.appliedTableStyle = curDoc.tableStyles.itemByName( "verbatim orange LONG" );
                        }
                    }
                }
                
                • 5. Re: Re: Apply table style based on cell content
                  Jennifer Hearing Level 1

                  Yes, that works great! Thanks!