Skip to main content
saltwatertrout
Known Participant
March 23, 2020
Question

Script to search tables in a document and apply cell style based on Column Title

  • March 23, 2020
  • 2 replies
  • 3172 views

Hello,

I'm trying to get a script for applying cell styles based on content. Since I'm really new to scripting I can't get this to work as I want to. I have a script that will find a number and change the cell style based on the number, but I need to use a second formula for finding the cell to apply the cell style to, based on the Column Title.

 

So for example I have four columns that are "ranks," and those would just have red, yellow, or green backgrounds applied (which I change with a cell style)(e.g. 0-49.99, 50.00-69.99, 70.00-100). But then I have four columns that are "grades," and they have more variety in the colors (red, orange, yellow, green, blue; e.g. 0-24.99, 25.0-44.99, 45.0-59.99, 60.0-79.99, 80.0-100).

 

The following script does what I want for the first situation, but it then I can't run the second script because it will change the colors I already changed using the first script. Is there a way to search by column title and apply the formula, so I don't have to go through and manually select each text frame to apply the second forumula? Does that make sense?

 

Can anyone help me with this?

 

var myDoc = app.activeDocument ;
var myFrame = app.selection[0];

if(app.selection <1){

alert ("Please select a table or a frame containing a table.");

}
app.findGrepPreferences = app.changeGrepPreferences = null;

app.findGrepPreferences.findWhat = "^[6-7]\\d+(\\.\\d+)?";

var myFound = myFrame.findGrep();


for( var i=0; i<myFound.length; i++)
{
if( myFound[i].parent.constructor.name == "Cell" )
{
myFound[i].parent.appliedCellStyle = "Grade Yellow";
var overrides = myFound[i].clearOverrides(); //this is the new line added in this content
};
};
app.findGrepPreferences = app.changeGrepPreferences = null;

app.findGrepPreferences.findWhat = "^[8]\\d+(\\.\\d+)?";

var myFound = myFrame.findGrep();


for( var i=0; i<myFound.length; i++)
{
if( myFound[i].parent.constructor.name == "Cell" )
{
myFound[i].parent.appliedCellStyle = "Grade Green";
var overrides = myFound[i].clearOverrides(); //this is the new line added in this content
};
};
app.findGrepPreferences = app.changeGrepPreferences = null;

app.findGrepPreferences.findWhat = "^[9]\\d+(\\.\\d+)?";

var myFound = myFrame.findGrep();


for( var i=0; i<myFound.length; i++)
{
if( myFound[i].parent.constructor.name == "Cell" )
{
myFound[i].parent.appliedCellStyle = "Grade Blue";
var overrides = myFound[i].clearOverrides(); //this is the new line added in this content
};
};

 

This topic has been closed for replies.

2 replies

Community Expert
March 25, 2020

Hi,

 

As per the idea that Jongware gave, your code would be something like the following

for (var i = 0; i < myFound.length; i++) {
    if (myFound[i].parent.constructor.name == "Cell") 
	{
		if (myFound[i].parent.columns[0].cells[0].texts[0].contents.match(/^GRADE/))
			myFound[i].parent.appliedCellStyle = "Grade Yellow";
		else if(myFound[i].parent.columns[0].cells[0].texts[0].contents.match(/^RANK/))
			myFound[i].parent.appliedCellStyle = "Rank Yellow";
        var overrides = myFound[i].clearOverrides(); //this is the new line added in this content
    }
}

So the idea is that when you have found the content, you check what is the text in the top cell of the column where that text was found, based on that you can choose which which cell style you want to apply. Here i used a sample Rank Yellow cell style to demonstrate the line where you can add which style you want to use in case the text is found under Rank column.

 

-Manan

-Manan
saltwatertrout
Known Participant
March 27, 2020

This is fabulous, thank you. But again, seems to have no effect when I run the script in InDesign.

saltwatertrout
Known Participant
March 28, 2020

i will gladly pay someone to help with this...i really need to get this script working!!!

Jongware
Community Expert
Community Expert
March 24, 2020

Basedd on your current code, it's easiest to grab the text of the top cell in the column that you are testing. Below your current lines

 

if( myFound[i].parent.constructor.name == "Cell" )

 

add this further test:

if (myFound[i].parent.columns[0].cells[0].texts[0].contents.match(/^GRADE/))

 

and then the next line will only be run if the text matches.

saltwatertrout
Known Participant
March 24, 2020

Thank you for the reply. I really have no idea what I'm doing so I literally just pasted that line with no other characters directly below the first line you reference.

 

if( myFound[i].parent.constructor.name == "Cell" )
if (myFound[i].parent.columns[0].cells[0].texts[0].contents.match(/^GRADE/))

 

Is that syntax correct? Because it seems to have no effect when I run the script. I apologize; I know I am in way over my head here.