Highlighted

Script to find a cell style or paragraph style and change cell style according to cell content

New Here ,
Apr 22, 2020

Copy link to clipboard

Copied

I have a table with several columns of numbers. Some numbers need to have just a gray background.

 

Others need to have a colored background depending on the number. Using a decimal search allows me to differentiate those numbers from whole integers. But there is a final column that has different conditional formatting... a larger range of colors to be applied to numbers.

 

On this particular table, because it is a data merge and I don't want to repeat the headers over and over on the page, I have eliminated the Table Header Row other than on the master page, so now I don't have a way to differentiate that column (I had been applying the cell style based on each header, which worked great).

 

I can apply a cell style or a paragraph style to the content in the cell to disctinguish it from the other numbers, but I don't know how to *both* search for a cell or paragraph style *and* then still search for the range of numbers to apply the various colors. Does that make sense?

 

So I have

******

// All the tables in the active document! …
var myCells = app.activeDocument.stories.everyItem().tables.everyItem().cells.everyItem().getElements(),
C = myCells.length, c;


for ( c = 0; c < C; c++ ) {

if ( myCells[c].columns[0].cells[0].texts[0].contents.match(/\d\.\d/) && myCells[c].contents != "" ) {
if ( Number(myCells[c].contents) >= 0 && Number(myCells[c].contents) < 59.99 ) myCells[c].appliedCellStyle = "Grade Red";
if ( Number(myCells[c].contents) >= 60 && Number(myCells[c].contents) < 69.99 ) myCells[c].appliedCellStyle = "Grade Orange";
if ( Number(myCells[c].contents) >= 70 && Number(myCells[c].contents) < 79.99 ) myCells[c].appliedCellStyle = "Grade Yellow";
if ( Number(myCells[c].contents) >= 80 && Number(myCells[c].contents) < 89.99 ) myCells[c].appliedCellStyle = "Grade Green";
if ( Number(myCells[c].contents) >= 90 && Number(myCells[c].contents) <= 100 ) myCells[c].appliedCellStyle = "Grade Blue";
}

}
******

but for that first search

if ( myCells[c].columns[0].cells[0].texts[0].contents.match(/\d\.\d/) && myCells[c].contents != "" ){

 

I need it to also either search for Cell Style "Points Gray" or Paragraph Style "Points" but I can't figure out how to do that.

Adobe Community Professional
Correct answer by Manan Joshi | Adobe Community Professional

How about 

if ( myCells[c].columns[0].cells[0].texts[0].contents.match(/\d\.\d/) && myCells[c].contents != "" && (myCells[c].texts[0].appliedParagraphStyle.name == "Points" || myCells[c].appliedCellStyle.name = "Points Gray"))

-Manan 

Topics

Scripting

Views

75

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Script to find a cell style or paragraph style and change cell style according to cell content

New Here ,
Apr 22, 2020

Copy link to clipboard

Copied

I have a table with several columns of numbers. Some numbers need to have just a gray background.

 

Others need to have a colored background depending on the number. Using a decimal search allows me to differentiate those numbers from whole integers. But there is a final column that has different conditional formatting... a larger range of colors to be applied to numbers.

 

On this particular table, because it is a data merge and I don't want to repeat the headers over and over on the page, I have eliminated the Table Header Row other than on the master page, so now I don't have a way to differentiate that column (I had been applying the cell style based on each header, which worked great).

 

I can apply a cell style or a paragraph style to the content in the cell to disctinguish it from the other numbers, but I don't know how to *both* search for a cell or paragraph style *and* then still search for the range of numbers to apply the various colors. Does that make sense?

 

So I have

******

// All the tables in the active document! …
var myCells = app.activeDocument.stories.everyItem().tables.everyItem().cells.everyItem().getElements(),
C = myCells.length, c;


for ( c = 0; c < C; c++ ) {

if ( myCells[c].columns[0].cells[0].texts[0].contents.match(/\d\.\d/) && myCells[c].contents != "" ) {
if ( Number(myCells[c].contents) >= 0 && Number(myCells[c].contents) < 59.99 ) myCells[c].appliedCellStyle = "Grade Red";
if ( Number(myCells[c].contents) >= 60 && Number(myCells[c].contents) < 69.99 ) myCells[c].appliedCellStyle = "Grade Orange";
if ( Number(myCells[c].contents) >= 70 && Number(myCells[c].contents) < 79.99 ) myCells[c].appliedCellStyle = "Grade Yellow";
if ( Number(myCells[c].contents) >= 80 && Number(myCells[c].contents) < 89.99 ) myCells[c].appliedCellStyle = "Grade Green";
if ( Number(myCells[c].contents) >= 90 && Number(myCells[c].contents) <= 100 ) myCells[c].appliedCellStyle = "Grade Blue";
}

}
******

but for that first search

if ( myCells[c].columns[0].cells[0].texts[0].contents.match(/\d\.\d/) && myCells[c].contents != "" ){

 

I need it to also either search for Cell Style "Points Gray" or Paragraph Style "Points" but I can't figure out how to do that.

Adobe Community Professional
Correct answer by Manan Joshi | Adobe Community Professional

How about 

if ( myCells[c].columns[0].cells[0].texts[0].contents.match(/\d\.\d/) && myCells[c].contents != "" && (myCells[c].texts[0].appliedParagraphStyle.name == "Points" || myCells[c].appliedCellStyle.name = "Points Gray"))

-Manan 

Topics

Scripting

Views

76

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Adobe Community Professional ,
Apr 22, 2020

Copy link to clipboard

Copied

How about 

if ( myCells[c].columns[0].cells[0].texts[0].contents.match(/\d\.\d/) && myCells[c].contents != "" && (myCells[c].texts[0].appliedParagraphStyle.name == "Points" || myCells[c].appliedCellStyle.name = "Points Gray"))

-Manan 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...