2 Replies Latest reply on Nov 24, 2010 8:25 AM by AaronGHawkins

    Cell styles based on content from a data merge?

    STRONGBRAND

      Hello

       

      Is there a script (or a way) of setting cell styles (a simple cell colour fill) based on content inserted from a data merge?

       

      eg Content inserted inserted into a cell from merge has text saying "beginner" but the background needed to be blue, while the next content inserted "intermediate" it would force a style change to a red background.

        • 1. Re: Cell styles based on content from a data merge?
          [Jongware] Most Valuable Participant

          Sure, there might even be a few different ways.

          This quick script is fairly straightforward: from the current insertion point (the cursor) or selection (when you selected an entire cell, for example), it finds the table you are in by progressing 'upwards' until you find a table. Then it loops over all individual cells and calls a function to compare the 'contents' -- the literal text inside the cell -- against a list and return a cell style name for the interesting ones.

          In the function getStyleForText, you can add your new words ("beginner", "intermediate") and have it return a different cell style name for each one ("Red", "Blue").

          Be sure to only return cell style names that actually exist in your document; Javascript is good at doing what you tell it to but not at doing what you want.

           

          if (app.selection.length == 1)
          {
               myTable = app.selection[0];
               if (myTable.hasOwnProperty ("baseline"))
                    myTable = myTable.parent;
               if (myTable instanceof Cell)
                    myTable = myTable.parent;
               if (myTable instanceof Row)
                    myTable = myTable.parent;
               if (myTable instanceof Column)
                    myTable = myTable.parent;
               if (myTable instanceof Table)
               {
                    for (c=0; c<myTable.cells.length; c++)
                    {
                         style = getStyleForText (myTable.cells[c].contents);
                         if (style)
                              myTable.cells[c].appliedCellStyle = style;
                    }
               }
          }
          
          function getStyleForText (text)
          {
               switch (text)
               {
                    case "beginner": return "Red";
                    case "intermediate": return "Blue";
               }
               return null;
          }
           
          

          • 2. Re: Cell styles based on content from a data merge?
            AaronGHawkins Level 1

            I, too, am klooking for a script that can change the cell style based on contents. For example, I'd like to be able to select a column, and run a script that would take a cell containing "Red" and change the background to red. I'm very very new to JS and InDesign (I've had some classes in HTML, PHP, CSS, C+, etc). I've already set up the cell styles and the scripts I've found cannot be tweaked or freeze InDesign. I'm on XP with CS4. Any help is appreciated.