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

Find text in cell and change it to a cell style

Explorer ,
Oct 27, 2016 Oct 27, 2016

Copy link to clipboard

Copied

Hello,

i have a table in indesign CS6 and i'am looking for a script that for example look for number 100 in a cell and change it to cell style 1.

I hope someone can help me.

Kind regards,

Patrick

TOPICS
Scripting

Views

2.9K

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

correct answers 1 Correct answer

LEGEND , Oct 27, 2016 Oct 27, 2016

Hi,

This code is really beautiful! Thanks to Hans-Gerd Claßen [ -hans-]! 

  1. app.findGrepPreferences = NothingEnum.nothing;    
  2.   //[Grep, cellStyleName] 
  3. var grepSearchs = [["\\<[0-5]{1}[0-9]{0,1}\\>|\\<[6-9]\\>", 'Rot'], ["\\<[6-7]{1}[0-9]{1}\\>", 'Gelb'], ["\\<[8-9]{1}[0-9]{1}\\>|\\<100\\>",   'Gruen']]; //0-59 -> Rot ., 60-79 Gelb und 80-100 Grün   
  4.    
  5. var docTables = app.activeDocument.stories.everyItem().tables.everyItem().getElements();   
  6.    
  7. o = docTables.length;   
  8. for(var i = 0; i < o;i+
...

Votes

Translate

Translate
LEGEND ,
Oct 27, 2016 Oct 27, 2016

Copy link to clipboard

Copied

Hi,

This code is really beautiful! Thanks to Hans-Gerd Claßen [ -hans-]! 

  1. app.findGrepPreferences = NothingEnum.nothing;    
  2.   //[Grep, cellStyleName] 
  3. var grepSearchs = [["\\<[0-5]{1}[0-9]{0,1}\\>|\\<[6-9]\\>", 'Rot'], ["\\<[6-7]{1}[0-9]{1}\\>", 'Gelb'], ["\\<[8-9]{1}[0-9]{1}\\>|\\<100\\>",   'Gruen']]; //0-59 -> Rot ., 60-79 Gelb und 80-100 Grün   
  4.    
  5. var docTables = app.activeDocument.stories.everyItem().tables.everyItem().getElements();   
  6.    
  7. o = docTables.length;   
  8. for(var i = 0; i < o;i++){   
  9.     myTable = docTables;   
  10. l = grepSearchs.length;   
  11. while(l--){   
  12. app.findGrepPreferences.findWhat = grepSearchs[0];    
  13. var foundList = myTable.findGrep();    
  14. f = foundList.length;   
  15. while(f--){foundList.parent.appliedCellStyle = grepSearchs[1];}   
  16. }   
  17. }   
  18.    
  19. app.findGrepPreferences = NothingEnum.nothing; 

See:  Apply Table Cell Style Based on Text Search

(^/)

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
LEGEND ,
Oct 28, 2016 Oct 28, 2016

Copy link to clipboard

Copied

When you only have to play the game on a single table, Ichikawa Seuzo has written for this [in 2009!] an awesome script he called "Regex_cellstyle.jsx".

Selecting table all content, just play it!

Based on a grep research, define your regex and the cell style to be applied! 

[ UI in japanese I've personally translated in English/French!  =D ]

Capture d’écran 2016-10-28 à 09.44.19.png

Capture d’écran 2016-10-28 à 09.43.48.png

(^/)

[I'm actually working on a "all tables" doc target transposition of this script.]

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
Explorer ,
Oct 28, 2016 Oct 28, 2016

Copy link to clipboard

Copied

Were can i found the translation version?

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
LEGEND ,
Oct 28, 2016 Oct 28, 2016

Copy link to clipboard

Copied

Google Translation! 

[personal version, not for free!]

(^/)

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 ,
Oct 28, 2016 Oct 28, 2016

Copy link to clipboard

Copied

Michel -- You take someone else's script, translate a few phrases in the script's interface, then say 'personal version, not for free'. Are you suggesting that you want to be paid for someone else's script?

Peter

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
LEGEND ,
Oct 28, 2016 Oct 28, 2016

Copy link to clipboard

Copied

Peter, Absolutely not!

I only said that I won't give this personal version.

I've taken a lot of time to explore and translate I. Seuzo's gitHub.com pages [save Japanese/English Google translations].

… So, everybody can do it! … for free! 

seuzo (seuzo) · GitHub

(^/)

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
LEGEND ,
Oct 28, 2016 Oct 28, 2016

Copy link to clipboard

Copied

I've taken a lot of time too to translate in French a great part of your great book:

Scripting InDesign CS3/4 with JavaScript - O'Reilly Media

… I won't never sale or give this version! If somebody wants a translated version, he can do himself [as me] or ask your editor to do it! (maybe for free!

… and, if your editor contacts me to "buy" me my french translation [considering it as a "translation service delivery"] to publish it, I promise you too to tell him: "No, 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
Community Expert ,
Oct 28, 2016 Oct 28, 2016

Copy link to clipboard

Copied

You can't sell or give away the translated book because the English edition is copyrighted. The script you found on Github is an entirely different matter. A bit stingy to keep it to yourself. Why not ask the script's author if you can share the script with the translated interface and return some of the favours you received in this forum?

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
LEGEND ,
Oct 28, 2016 Oct 28, 2016

Copy link to clipboard

Copied

As you and as you know, I'm not stingy! 

… I just don't consider this forum as a supermarket when too many people come to take to many things for free!

But your idea is interesting: I'm going to contact Ichikawa about this question! 

Last comment: as you know, there will always be someone interested when free, really much less people when not!! 

(^/)

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
Enthusiast ,
Oct 28, 2016 Oct 28, 2016

Copy link to clipboard

Copied

Hi Patrick,

I wrote in August a similiar script and provided it to the facebook-GREP-Community.

No interface at the moment, but you can simply use multiple grep by adding a line after line 13.

If nothing is selected, all tables in your document are changed. If one table is selected, only this table is honored.

Maybe it is useful.

Kai

// Kai Rübsamen, www.ruebiarts.de

app.doScript(main, ScriptLanguage.JAVASCRIPT, [], UndoModes.ENTIRE_SCRIPT, "style cells based on content");

function main() {

var tbl = getTable();

var styleRow = false;

var styleColumn = false;

changeTable("\\b[1-4]?\\d%", "cellStyle01");

changeTable("\\b[5]\\d%", "cellStyle02");

// - - - - - - - - - - - - - - - - - -  FUNCTIONS  - - - - - - - - - - - - - - - - - - - -

function getTable() {

  if (app.documents.length == 0) {

    alert ("Open a document!");

    exit();

  }

  // die Auswahl speichern

  var sel = app.selection;

  var nSel = sel.length;

  // ... und prüfen

  if (nSel == 0) {

    var res = confirm("Warning:\rDo you want to style all tables in your document?\nOtherwise select a table and start the script again!");

    if (res) {

      var curDoc = app.activeDocument;

      return curDoc.stories.everyItem().tables.everyItem().getElements();

    }

    else {

      exit();

    }

  }

  else if (nSel == 1) {

    var curSel = sel[0];

    // es darf entweder 'Table', 'Cell' oder ein Text innerhalb einer Tabelle ausgewählt sein

    if (/Table|Cell/.test(curSel.constructor.name) == true || (curSel.hasOwnProperty("baseline") && curSel.parent.constructor.name == "Cell")) {

      while (curSel.constructor.name != "Table") {

        curSel = curSel.parent;

      }

      return curSel;

    }

    else {

      alert("Hey, wrong selection!\rSelect a table.");

      exit();

    }

  }

  else {

    // wenn mehr wie 1 ausgewählt ist

    alert("Hey, wrong selection!\rSelect a table.");

    exit();

  }

}

// - - - - - - - - - - - - - - - - - - - - - - - - -

function changeTable(f, s) {

  app.findGrepPreferences = app.changeGrepPreferences = null;

  app.findGrepPreferences.findWhat = f;

  // In der Variablen 'tbl' steckt entweder eine Tabelle oder ein Array. Da eine einzelne

  // Tabelle keine Eigenschaft 'length' hat, wird mit dem 'constructor' geprüft.

  if (tbl.constructor.name ==  "Array") {

    // eine Schleife durch alle Tabellen im Dokument

    for (var j = 0; j < tbl.length; j++) {

      // die aktuelle Tabelle durchsuchen

      var curTable = tbl;

      applyStyle(curTable, s);

    }

  }

  else if (tbl.constructor.name == "Table") {

    // hier wird nur die jeweils ausgewählte Tabelle behandelt

    applyStyle(tbl,s);

  }

}

function applyStyle(curTable, s) {

  var curDoc = app.activeDocument;

  // alle Fundstellen in EINER Tabelle

  var tableFound = curTable.findGrep();

 

  for (var i = 0; i < tableFound.length; i++) {

    var curFound = tableFound;

    // die Zelle, in der sich die aktuelle Fundstelle befindet

    var parCell = curFound.parent;

    if (!(styleRow || styleColumn)) {

      parCell.appliedCellStyle = curDoc.cellStyles.itemByName(s);

      parCell.clearCellStyleOverrides(true);

    }

    else if (styleRow) {

      // von der Zelle zur Zeile zu allen Zellen (man kann nur Zellen ein Format zuweisen)

      var cellsInRow = parCell.parentRow.cells.everyItem();

      // das Format zuweisen

      cellsInRow.appliedCellStyle = curDoc.cellStyles.itemByName(s);

      // sicherheitshalber Abweichungen löschen

      cellsInRow.clearCellStyleOverrides(true);

    }

    else if (styleColumn) {

      // von der Zelle zur Zeile zu allen Zellen (man kann nur Zellen ein Format zuweisen)

      var cellsInCol = parCell.parentColumn.cells.everyItem();

      // das Format zuweisen

      cellsInCol.appliedCellStyle = curDoc.cellStyles.itemByName(s);

      // sicherheitshalber Abweichungen löschen

      cellsInCol.clearCellStyleOverrides(true);

    }

  }

}

app.findGrepPreferences = app.changeGrepPreferences = null;

} // end main

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
LEGEND ,
Oct 28, 2016 Oct 28, 2016

Copy link to clipboard

Copied

Hi Kai,

Could you provide us (for free of course!) a version with the comments in English, not German!

Eventually, a total version in French could be appreciated!

(^/) 

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
LEGEND ,
Oct 28, 2016 Oct 28, 2016

Copy link to clipboard

Copied

Halloween soon! … to scare you ! 

Apply cells styles depending on grep researches! …

Type your grep code ["a+"] then choose what cell style to be applied ["Blue"]!

Capture d’écran 2016-10-28 à 22.14.34.png

If you want to add a second research, click on "+".

Type your new grep code ["b+"] then choose what cell style to be applied ["Red"]!

Capture d’écran 2016-10-28 à 22.09.30.png

… A third research, click again on "+".

Type your new grep code ["c+"] then choose what cell style to be applied ["Green"]!

Capture d’écran 2016-10-28 à 22.09.54.png

Play "OK"!

If no selected text or no selected story, all the tables will be treated.

Capture d’écran 2016-10-28 à 22.11.02.png

If a table is selected (cursor in it), the script will only treat it!

Capture d’écran 2016-10-28 à 22.11.54.png

If you want to delete a step ["b+" grep code/"Red" cell style], just click on "–" on its UI line!

Capture d’écran 2016-10-28 à 22.15.29.png

Only for fear! Happy Halloween!!! 

(^/)

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
New Here ,
Aug 07, 2019 Aug 07, 2019

Copy link to clipboard

Copied

LATEST

Obi-wan, where can I find the script you are demonstrating here? I am happy to translate on my own. is it a derivative of the regex_cellstyle.jsx?

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 ,
Oct 31, 2016 Oct 31, 2016

Copy link to clipboard

Copied

Hi experts,

How can I change it only work on selected cells.

I change it into like this:

app.findGrepPreferences = app.changeGrepPreferences = null;

function appliedCellStyle(){ 

    if(!app.selection[0].hasOwnProperty("baseline")){return}; 

        if(app.selection[0].parent.constructor.name === "Cell"){ 

            app.findGrepPreferences.findWhat = "HK\\$’000";

            app.changeGrepPreferences.appliedCellStyle = "u3";

            app.selection[0].changeGrep();

            return; 

            } 

        }

app.findGrepPreferences = app.changeGrepPreferences = null;

but not working.

could someone please show me how?

thanks

Regard

John

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
LEGEND ,
Nov 01, 2016 Nov 01, 2016

Copy link to clipboard

Copied

Hi John,

I'm not an expert! [Code to be validated!] 

var reg = new RegExp("@");

var myCells = app.selection[0].cells;

for(var i = 0; i < myCells.length; i++)  if(reg.test(myCells.contents)) myCells.appliedCellStyle = "Red";

(^/)

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
Mentor ,
Nov 01, 2016 Nov 01, 2016

Copy link to clipboard

Copied

Hi John,

This code you pasted is working but only till lines #3 OR #4.

If selection IS NOT a TEXT (!hasOwnProperty("baseline") ==> break

Else (If TEXT is a CELL) ==> work

Else ==> break

Jarek

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 ,
Nov 01, 2016 Nov 01, 2016

Copy link to clipboard

Copied

Thank you Jarek, thank Kenobi, thank so much!

So How can I make this expression woking?

app.findGrepPreferences = app.changeGrepPreferences = null;
function appliedCellStyle(){ 
    if (!app.selection[0].hasOwnProperty("baseline")) continue, else break; 
        if(app.selection[0].parent.constructor.name === "Cell"){ 
            app.findGrepPreferences.findWhat = "HK\\$’000";
            app.changeGrepPreferences.appliedCellStyle = "u3";
            app.selection[0].changeGrep();
            else break; 
            } 
        }
app.findGrepPreferences = app.changeGrepPreferences = null;

Thank you Regard

John

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
Mentor ,
Nov 01, 2016 Nov 01, 2016

Copy link to clipboard

Copied

Hi,

Use Obi-wan's code with your data (regex and style name).

I am only saying that your code is going to break a job for any cases.

2nd one is even worse, sorry.

Jarek

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
LEGEND ,
Nov 01, 2016 Nov 01, 2016

Copy link to clipboard

Copied

Jarek,

That is a rather cutting answer!! 

(^/)

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