Skip to main content
Philipp Haunstetter
Participating Frequently
September 24, 2019
Question

Script to find & replace cell style of a specific cell

  • September 24, 2019
  • 3 replies
  • 2550 views

Hello lovely InDesign friends,

 

I have a problem finding the right direction for my script we would use for all new catalogues in the future.

 

Starting scenario:

1. We have many tables with  tiny information.

2. One cell of each table has a main color provided in a cell style (Name: Green)

3. We need to change the cell style of these cells very easy along 100+ pages to another cell style (Name: Yellow)

4. The paragraph style has to be changed too (black / yellow) -> "Paragraph A" to "Paragraph B" when the cell is changed to . the "Yellow" cell style

 

I struggle so much getting all cells with the "Green" to "Yellow" cell style and had no success ...

Step 4 is out of my eyes at the moment 😉 

 

Starting situation:

 

 

Target situation:

 

Does anyone have a simple script to change the cell style of every cell using a cell style "XY" to cell style "AB".

 

var myDoc = app.activeDocument;

// Search for text with 6 digits, e.g. 137335
app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = "^\d{6}$";
var myFound = myDoc.findGrep();

// Loop for every cell found with myFound
for (i = 0; i < myFound.length; i++) {

// Check if the element is a cell
  if (myFound[i].parent.constructor.name == "Cell") {

// Change the cell style to Yellow
    myFound[i].parent.appliedCellStyle = "Yellow";

// Overrides changed styles
    var overrides = myFound[i].clearOverrides();
  }
}

 

I tried to get the if statement working to check also for appliedCellStyle …

// ...
if (myFound[i].parent.constructor.name == "Cell"; && myFound[i].parent.appliedCellStyle == "Green";) {
// ...

- Because the RegEx pattern isn't used by app.findGrepPreferences.findWhat = "^\d{6}$"; I tested everything with a fixed string like "137335". Would be cool if you can give me a hint on that too 😉

 

I hope everything was written clearly understandable 🙂

 

Thank you for your feedback anyways!

 

Greetings

This topic has been closed for replies.

3 replies

karthikS
Inspiring
September 25, 2019

Hi Philipp_Haunstetter,

Try below code:

 

 

    var myTablesRow = app.activeDocument.stories.everyItem().tables.everyItem().cells.everyItem().getElements();
    for (var t = 0; t < myTablesRow.length; t++) {
        try{
            myTable = myTablesRow[t];
            if (myTable.texts[0].appliedParagraphStyle.name == "XY") {
                myTable.appliedCellStyle = app.activeDocument.cellStyles.itemByName("AB");
                }//if
            }catch(e){                
                }
   }//for

 

 

Thanks,

Karthik S

 

Philipp Haunstetter
Participating Frequently
September 25, 2019

Hey Karthik,

 

thank you very much for your solution. It looks a little bit awkward at the beginning, but it is working really fine in my tests.

 

We use four cell styles and four paragraph styles for changing colors :

  • Artikel-Nr. Aktion (red)
  • Artikel-Nr. Neuheit (yellow)
  • Artikel-Nr. Katalognews (green)
  • Artikel-Nr. Neutral (gray)

 

Every second month the colored cells change a bit:

  1. red (white text) to gray (black text)
  2. green (white text) to gray (black text)
  3. yellow (black text) to green (white text)

 

New cells get manually filled with green, yellow and red color.

The next two month the loop starts again …

 

Here is the used code based on karthiks94217840 script.

I just added the three actions we want to use in one script.

var myTablesRow = app.activeDocument.stories.everyItem().tables.everyItem().rows.everyItem().cells.everyItem().getElements();
    for (var t = 0; t < myTablesRow.length; t++) {
        try {
            myTable = myTablesRow[t];
            if (myTable.texts[0].appliedParagraphStyle.name == "Artikel-Nr. Aktion") {
                myTable.appliedCellStyle = app.activeDocument.cellStyles.itemByName("Artikel-Nr. Neutral");
                }
            if (myTable.texts[0].appliedParagraphStyle.name == "Artikel-Nr. Katalognews") {
                myTable.appliedCellStyle = app.activeDocument.cellStyles.itemByName("Artikel-Nr. Neutral");
                }
            if (myTable.texts[0].appliedParagraphStyle.name == "Artikel-Nr. Neuheit") {
                myTable.appliedCellStyle = app.activeDocument.cellStyles.itemByName("Artikel-Nr. Katalognews");
                }
            }catch(e){
                }
   }

 

Thank you very much 🙂

 

Greetings,

Philipp

xxxxxxxxxxxxxxxxxxxxyyyy
Participating Frequently
September 24, 2019

Hi,

in your GREP-pattern you need an additional backslash before \d:

app.findGrepPreferences.findWhat = "^\\d{6}$";
Philipp Haunstetter
Participating Frequently
September 25, 2019
It is working for the script! I was wondering because in InDesign GREP ^\d{6}$ is working 😉 Thank you.
Legend
September 24, 2019

If the Green cell style will not longer be needed, you could edit that Cell Style to include your new criteria.

Philipp Haunstetter
Participating Frequently
September 24, 2019

Hi SJRiegel,

 

the cell styles have to stay in the catalogue files because we need them for future versions.

 

We have three stages for the cells:

1. new product in the catalogue

2. new product in general

3. product in sale

 

We start with 1. (Green), go to 2. (Yellow) and 3. (Red). When a product gets the 2. status, other products are added in the version and get status 1.

 

Thank you anyway for your thoughts 🙂

 

Greetings

Legend
September 24, 2019
OK, a bit of a workaround, then. Duplicate Green (for future use), then edit original Green to Yellow specs.