Skip to main content
andreasf
Inspiring
June 18, 2013
Answered

How to apply a table cell style based on grep search?

  • June 18, 2013
  • 5 replies
  • 5895 views

Anyone that know how to make a script that searches in an Indesign table for

-> All cells where the text starts with "Banana"

-> And apply cell style "Yellow" to these cells?

I have been searching on forums for somedays and got this tip: https://github.com/seuzo/regex_cellstyle/blob/master/regex_cellstyle.jsx

-> But that script is in japanese and also use dialogs.

All help most appriciated!

This topic has been closed for replies.
Correct answer BEGINNER_X

Hi Frankemans,

var myDoc = app.activeDocument

app.findTextPreferences = app.changeTextPreferences = null

app.findTextPreferences.findWhat = "Banana"

var myFound = myDoc.findText()

for(i=0; i<myFound.length; i++)

{

    if(myFound.parent.constructor.name == "Cell")

    {

   myFound.parent.appliedCellStyle = "Yellow"

      var overrides = myFound.clearOverrides()          //this is the new line added in this content

   

    }

}

Thanks

Beginner

5 replies

Community Expert
February 3, 2020

Just another annotation:
The code is NOT using a GREP search. It's just a plain TEXT search.

 

Regards,
Uwe Laubender

( ACP )

 

 

Known Participant
February 3, 2020

It works on Design CC 2020! Thanks a lot!

Note : In the Cell styles pannel, if the cell style ("yellow") is in a group of styles, the script doesn't find it. I guess it is logical but it is good to remember that :).

 

Yes, I have seen there was a difference between Text and GREP, but thanks for reminding me about it, it is really easy to confuse the two of them ^^.

Since your reply, I also have tested he GREP research (thanks to another post), which worked pretty well after changing some lines. I used it to search several words at the same time, it is very effective too.

 

Can I ask you another question?

I would like to :

- format the "pear" cells in green

- format the "prune or cherry or blueberries" cells in violet

- format the "apple" cells in red.

For now, I created one file for each request, which is maybe not the more convenient thing to do. Is it possible to cumulate them on one single file? I guess it is not very complicated but I am a total newbie in Javascript, I don't know how the code works :}

Best
Ecureuil

 

 

Community Expert
February 3, 2020

Hi Ecureuil,

the problem indeed lies in the posted code.

This code was damaged when the thread was transferred to the new InDesign forum last year.

Inside the for-loop iterator i is missing.

 

Here the restored code:

var myDoc = app.activeDocument ;
app.findTextPreferences = app.changeTextPreferences = null ;
app.findTextPreferences.findWhat = "Banana" ;

var myFound = myDoc.findText() ;

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

 

Also note:

The original is from 2013 and may have worked for InDesign CS6.

I did not test it

 

Regards,
Uwe Laubender

( ACP )

Known Participant
February 3, 2020

Hi!

I have a problem with this script : every time I try to run it, InDesign CC 2020 tells me that there is an error. It says :

error 21

undefined is not an object

Line : 13

Source :  if(myFound.parent.constructor.name == "Cell")

 

Is there something I missed? I thought I just had to replace Banana and Yellow by whatever I wanted and that's all...

If this problem is solved, I have another question : can I use one and only script to apply different formats to other cells containing specific cells? (For example I want all the "2018" and "2019" cells in blue, all the "Investissements" and "Concessions" cells in green).

Thank you very much

Ecureuil

Known Participant
February 3, 2020

...Has the fact that InDesign is in french in my computer anything to do with this?

Thanks a lot

Ecureuil

Community Expert
February 3, 2020

No. The restored code I posted should run in any language version of InDesign.

 

As I already said: The code was damaged by transferring this old thread from the Jive-based old forum to this new one. Definitely not your fault.

 

Regards,
Uwe Laubender

( ACP )

Participant
July 8, 2019

Hi,

Am I able to add multiple if/else statements in this script?

Asking as I need to shade table cells based on the value within them.

Such as I need a 1 to be light blue, 2 to be a bit darker, 3 a little darker, and so on.

Many, many thanks in advance.

BEGINNER_X
Legend
June 18, 2013

Hi Frankeman,

Please try the below code, may it should be helpful:

var myDoc = app.activeDocument

app.findTextPreferences = app.changeTextPreferences = null

app.findTextPreferences.findWhat = "Banana"

var myFound = myDoc.findText()

for(i=0; i<myFound.length; i++)

{

    if(myFound.parent.constructor.name == "Cell")

    {

   

   myFound.parent.appliedCellStyle = "Yellow"

    }

}

Suppose the code is working fine for you, then please click correct answers.

thanks

Beginner_X

andreasf
andreasfAuthor
Inspiring
June 18, 2013

Almost there!

The script applies the correct cell style but with overrides.

-> See attached file.

-> Any way to make a script that clears all overrides?

BEGINNER_X
BEGINNER_XCorrect answer
Legend
June 18, 2013

Hi Frankemans,

var myDoc = app.activeDocument

app.findTextPreferences = app.changeTextPreferences = null

app.findTextPreferences.findWhat = "Banana"

var myFound = myDoc.findText()

for(i=0; i<myFound.length; i++)

{

    if(myFound.parent.constructor.name == "Cell")

    {

   myFound.parent.appliedCellStyle = "Yellow"

      var overrides = myFound.clearOverrides()          //this is the new line added in this content

   

    }

}

Thanks

Beginner