18 Replies Latest reply on Nov 1, 2016 8:52 AM by Obi-wan Kenobi

    Find text in cell and change it to a cell style

    patrickbcm

      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

        • 1. Re: Find text in cell and change it to a cell style
          Obi-wan Kenobi Adobe Community Professional

          Hi,

           

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

           

          app.findGrepPreferences = NothingEnum.nothing;     
            //[Grep, cellStyleName]  
          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    
              
          var docTables = app.activeDocument.stories.everyItem().tables.everyItem().getElements();    
              
          o = docTables.length;    
          for(var i = 0; i < o;i++){    
              myTable = docTables[i];    
          l = grepSearchs.length;    
          while(l--){    
          app.findGrepPreferences.findWhat = grepSearchs[l][0];     
          var foundList = myTable.findGrep();     
          f = foundList.length;    
          while(f--){foundList[f].parent.appliedCellStyle = grepSearchs[l][1];}    
          }    
          }    
              
          app.findGrepPreferences = NothingEnum.nothing;  
          
          

           

           

          See:  Apply Table Cell Style Based on Text Search

           

          (^/)

          • 2. Re: Find text in cell and change it to a cell style
            Obi-wan Kenobi Adobe Community Professional

            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.]

            • 3. Re: Find text in cell and change it to a cell style
              patrickbcm Level 1

              Were can i found the translation version?

              • 4. Re: Find text in cell and change it to a cell style
                Obi-wan Kenobi Adobe Community Professional

                Google Translation! 

                 

                [personal version, not for free!]

                 

                (^/)

                • 5. Re: Find text in cell and change it to a cell style
                  Peter Kahrel Adobe Community Professional & MVP

                  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

                  • 6. Re: Find text in cell and change it to a cell style
                    Obi-wan Kenobi Adobe Community Professional

                    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

                     

                    (^/)

                    • 7. Re: Find text in cell and change it to a cell style
                      Obi-wan Kenobi Adobe Community Professional

                      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!"

                       

                      (^/)

                      • 8. Re: Find text in cell and change it to a cell style
                        Peter Kahrel Adobe Community Professional & MVP

                        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?

                        • 9. Re: Find text in cell and change it to a cell style
                          Obi-wan Kenobi Adobe Community Professional

                          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!! 

                           

                          (^/)

                          • 10. Re: Find text in cell and change it to a cell style
                            Kai Rübsamen Level 4

                            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[j];
                                  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[i];
                                // 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
                            
                            • 11. Re: Find text in cell and change it to a cell style
                              Obi-wan Kenobi Adobe Community Professional

                              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!

                               

                              (^/) 

                              • 12. Re: Find text in cell and change it to a cell style
                                Obi-wan Kenobi Adobe Community Professional

                                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!!! 

                                 

                                (^/)

                                • 13. Re: Find text in cell and change it to a cell style
                                  Johnwhite Level 1

                                  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

                                  • 14. Re: Find text in cell and change it to a cell style
                                    Obi-wan Kenobi Adobe Community Professional

                                    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[i].contents)) myCells[i].appliedCellStyle = "Red";
                                    

                                     

                                    (^/)

                                    • 15. Re: Find text in cell and change it to a cell style
                                      Jump_Over Level 5

                                      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

                                      • 16. Re: Find text in cell and change it to a cell style
                                        Johnwhite Level 1

                                        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

                                        • 17. Re: Find text in cell and change it to a cell style
                                          Jump_Over Level 5

                                          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

                                          • 18. Re: Find text in cell and change it to a cell style
                                            Obi-wan Kenobi Adobe Community Professional

                                            Jarek,

                                             

                                            That is a rather cutting answer!! 

                                             

                                            (^/)