5 Replies Latest reply on Apr 12, 2013 8:42 AM by andreasf

    [JS][CS4] - Conditional statements in a table?




      I am trying to write a script that looks at the content of the last column of a table and based on what it finds applies formatting to the first column, for every row of the table.


      For example, in each row if the last column has a 6 digit number that starts with "4" I want the first column to apply one of my swatches.  If the number starts with a "3" I want it to apply a different swatch.


      I tried using the regular expression object since I'm fairly comfortable with GREP syntax but I keep getting errors.  I just don't know how to use the object correctly or if this is even the right approach.


      Here is the code:


      if (myTable.rows.item().everyItem().cells.item(-1).contents == RegExp((4)+\d+))

           {myTable.rows.item().everyItem().cells.item(0).fillColor = myDocument.swatches.item("Blue");}


      I am brand new to scripting so very open to any help.




        • 1. Re: [JS][CS4] - Conditional statements in a table?
          Laubender Adobe Community Professional & MVP



          will give you an array which you can loop through.



          gives you the same array.


          See the following script:


          //a table is selected:
          var _table = app.selection[0];
          //The following checks will be incremented by 1 every time the loop goes through.
          //If the checks number is equal the number of cells we loop through, 
          //the first column of the table will be filled by a specified color:
          //Default is 0:
          var _check3 = 0;
          var _check4 = 0;
          //Our array we need to check:
          var _contentsArray = _table.columns[-1].contents;
          //regEx matches a certain number followed by 5 arbitrary numbers
          //the .source-trick is by John Hawkinson; no need for extra escaping backslashes:
          for(var n=0;n<_contentsArray.length;n++){
          if(_check3 === _contentsArray.length){_table.columns[0].fillColor = "Magenta"};
          if(_check4 === _contentsArray.length){_table.columns[0].fillColor = "Blue"};


          Oh, and btw you can use the following feature of the forum software to highlight your code:

          >> Syntax Highlighting > Java



          • 2. Re: [JS][CS4] - Conditional statements in a table?
            booyajamo Level 1

            Hi Uwe,


            Thank you for taking the time to help me!  Not sure what I am doing wrong but the script isn't appearing to do anything. It runs without any errors, but the first column does not have color in it and the last column do have numbers in them which should trigger the fill color.


            I copied the script you have above and added:




            just prior to all the stuff you have under "see the following script".  I figured I needed to add that line so it selected the table - because when I delete it, the script says the var_contentsArray is an undefined object.


            The entire table is highlighted when the script is finished if that means anything ??


            Any help is greatly appreciated.




            • 3. Re: [JS][CS4] - Conditional statements in a table?
              Laubender Adobe Community Professional & MVP

              Hi, Lindsay!


              You can be sure that I tested that script on a table in InDesign CS4.
              Maybe this script is oversimplistic.


              If you added myTable.select() to script without declaring the var myTable you should get an error out of the ESTK.

              You have to select a full table at the beginning before running the script from ESTK or InDesign.


              To test it yourself I have to send you two IDML files by E-Mail of my test document here together with the script that I ran from the ESTK.
              Please send me a private message with your E-Mail address or answer that one I already sent to you.


              AND all cells in the last column have to match a number beginning with "3" or "4" that is 6 digits long in total.



              • 4. Re: [JS][CS4] - Conditional statements in a table?
                Haakenlid Level 3

                Lindsay, can you post a screenshot of how you want your table to look like? Your description is a bit ambiguous. Do you want the entire first column to have the same colour, like Laubender's script does, or do you want each cell in the first row to have a different swatch?

                • 5. Re: [JS][CS4] - Conditional statements in a table?

                  Is there anyway to adjust the script above to search for text content - and if found – apply "Magenta" as a fill color to that cell.



                  Apple        20

                  Orange      10


                  If the cell contains "Apple" the fill color of that cell should be color swatch "Magenta"



                  Have been searching for a solution to this for weeks – so any help most appriciated ;-)