• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Script to find & replace cell style of a specific cell

Community Beginner ,
Sep 24, 2019 Sep 24, 2019

Copy link to clipboard

Copied

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:

Starting situationStarting situation

 

 

Target situation:

Screenshot 2019-09-24 at 16.13.29.png

 

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

TOPICS
Scripting

Views

1.3K

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
community guidelines
Community Expert ,
Sep 24, 2019 Sep 24, 2019

Copy link to clipboard

Copied

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

Votes

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
community guidelines
Community Beginner ,
Sep 24, 2019 Sep 24, 2019

Copy link to clipboard

Copied

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

Votes

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
community guidelines
Community Expert ,
Sep 24, 2019 Sep 24, 2019

Copy link to clipboard

Copied

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

Votes

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
community guidelines
Contributor ,
Sep 24, 2019 Sep 24, 2019

Copy link to clipboard

Copied

Hi,

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

app.findGrepPreferences.findWhat = "^\\d{6}$";

Votes

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
community guidelines
Community Beginner ,
Sep 24, 2019 Sep 24, 2019

Copy link to clipboard

Copied

It is working for the script! I was wondering because in InDesign GREP ^\d{6}$ is working 😉 Thank you.

Votes

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
community guidelines
Contributor ,
Sep 24, 2019 Sep 24, 2019

Copy link to clipboard

Copied

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

 

Votes

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
community guidelines
Community Beginner ,
Sep 25, 2019 Sep 25, 2019

Copy link to clipboard

Copied

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

Votes

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
community guidelines
Community Beginner ,
Sep 25, 2019 Sep 25, 2019

Copy link to clipboard

Copied

LATEST

Hey folks,

 

here is a small clip of the script working through the cells.

 

greetings

Philipp

Votes

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
community guidelines