I have a document where certain tables need to have certain cell styles applied according to the values contained within each cell. Here's an example of the final output:

(Effectively, all numbers from 00.00-69.99 will have one style, and numbers from 70.00-89.99 will have another style. I'm selecting for the former with the GREP ^[2-6]\d+(\.\d+)? and the latter with ^[7-8]\d+(\.?\d+)?)

My current approach is using Grep styles within the paragraph style for the cells to color code the values themselves, making it easier for me to spot and change them individually. This is still a pretty tedious approach, though, especially since there are several pages of tables where I'd need to do this.

This seemed like the kind of thing that could be automated via scripting, so I did some digging and I found a script for a similar solution here: How to apply a table cell style based on grep search?, which I modified to suit a grep-based search approach rather than a text-based one. My modified code is below:

The problem? Running the script doesn't give me any errors, but it also doesn't do anything. And I don't know JS well enough to really troubleshoot why that's the case. Also, I'm not sure how to make it find the two separate conditions that must be searched for within the same script.

I figured more experienced eyes than mine could point out what I'm doing wrong. I appreciate your help!

No joy; the script doesn't produce errors, but it has no effect either. In the console all it says is "result:undefined."

I searched through the forums and the net at large to find some other scripts I could try and tweak, but I wasn't able to get those working either. I'm sure what I want is technically possible. I think the main part where things are failing is the Grep: it seems like InDesign isn't actually searching for anything before proceeding through the rest of the script.

Perfect, that did the trick! So I take it slashes are one of those characters JS doesn't recognize unless they're escaped, even within quotes?

Here's the updated code. I modified it to only work on the selected table or frame, and added an alert to let the user know this. Is there a way to make the code stop executing after the error is detected? Currently it throws error #21 after showing the alert (because myFrame isn't defined)

Below is a simple script that I made (i'm not the best scripting, but it works).

With you document open in InDesign, run the script and it will parse through the entire document finding text frames that are tables, then it pushes (and parses to integer) each individual cell's content into an array, then cycles through that array while still being on the active table and determines if said value of said cell needs to have a cell style (colored background) applied by the range of the value of that cell's content (hope that made sense).

Additionally, the beginning of the script was to programmatically make a couple cell styles. You remove that bit of code and just use cell styles already setup in the InDesign document by inserted the name where indicated below in the script.