10 Replies Latest reply on Jun 19, 2012 5:00 PM by crayz77

    Find text and convert to table using JS

    crayz77 Level 1

      Can any one help before I go crazy.

       

      With Javascript I would like to be to search for some text using a grep expression, then convert the selected text into a table?

       

      Any ideas?

       

      Cheers

      Craig V

        • 1. Re: Find text and convert to table using JS
          -hans- Level 4

          HI,

           

          tested in 5.5

           

           

          #target InDesign

          app.findGrepPreferences = NothingEnum.nothing;

          app.findGrepPreferences.findWhat = "\\d{2}\.{1}\\d{2}\.{1}\\d{4}"; // European Datestring: 12.10.1967

          var myResults = app.activeDocument.findGrep(true);   

           

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

              var singleResult = myResults[i];

              singleResult.convertToTable() //have a look at Jongware for options ...

              }

          1 person found this helpful
          • 2. Re: Find text and convert to table using JS
            crayz77 Level 1

            Thank you worked a treat.

             

            Another question you might be able to help with. I am trying to calculate how many columns in an existing tables, then add more columns to the tables until they reache 20.

            Now the script I have written works fine when the cells of the tables are empty, but as soon the cells have text(data) in them the scipt errors, any ideas why?

             

             

             

            var myTable = 0;

            var tableNum = 0;

             

             

            convertTables()

             

             

            //select table

            function convertTables() {

            if (app.documents.length > 0 && app.selection.length > 0)

                myTable = app.select(app.activeDocument.pages[0].textFrames[0].tables[tableNum]);

               

            addColumn ()

             

             

            }

             

             

            //add columns to table

            function addColumn() {

             

             

            myTable = app.selection[0];

               

            var number =  myTable.columnCount;

               

            if (number < 20) {

                  myTable.columns.add(LocationOptions.atEnd,myTable);

                  addColumn();

            }

            else

            {

            convertTables();

                }

            }

            • 3. Re: Find text and convert to table using JS
              crayz77 Level 1

              Ok found a solution replaced

               

              var number =  myTable.columnCount;

               

              with.

               

              var number = app.activeDocument.pages[0].textFrames[0].tables[tableNum].rows[0].cells.length;

               

              :-)

               

              Sometimes it takes asking the question to find the answer for yourself. lol

              • 4. Re: Find text and convert to table using JS
                [Jongware] Most Valuable Participant

                > ... as soon the cells have text(data) in them the scipt errors ...

                 

                Can you perhaps tell what error you get? It might be helpful.

                • 5. Re: Find text and convert to table using JS
                  crayz77 Level 1

                  I fixed the errorr by doing the above adjustment. Now the problem I have is that it only works on page 1 of the document and fo the life of me I can't get it to work on all pages?\

                   

                  I'm using:

                   

                  app.activeDocument.pages[0].textFrames[0].tables[tableNum];

                   

                  to select the text, but this only gets page 1 and frame 1, how do I adapt this to select all the text on all the pages?

                  • 6. Re: Find text and convert to table using JS
                    crayz77 Level 1

                    Actually figured that one out as well. So the final full script for finding the text, converting it to tables, then standardising and styling the table reads . . .

                     

                     

                    //find text and convert to table

                    //set grep prefs to none

                    app.findGrepPreferences = NothingEnum.nothing;

                    app.changeGrepPreferences = NothingEnum.nothing;

                     

                     

                    //grep search

                    app.findGrepPreferences.findWhat = "((Vintage)(.*)\\r)((WR)(.*)\\r)((Drink)(.*))";

                     

                     

                    //confirm grep has found something

                    var myResults = app.activeDocument.findGrep(true);  

                     

                     

                    //convert to table

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

                       var singleResult = myResults[i];

                       singleResult.convertToTable("\t", "\r",);

                       }

                     

                     

                     

                     

                     

                     

                    //set variables

                    var myTable = 0;

                    var tableNum = 0;

                     

                     

                    //tables and cell style add.

                    app.activeDocument.tableStyles.add({name:"Table1"});

                    app.activeDocument.cellStyles.add({name:"Body"});

                    app.activeDocument.cellStyles.add({name:"Column1"});

                     

                     

                    //call function

                    convertTables()

                     

                     

                    //select table

                    function convertTables() {

                    if (app.documents.length > 0 && app.selection.length > 0)

                        app.select(app.activeDocument.stories[0].tables[tableNum]);

                        addColumn ()

                    }

                     

                     

                    //add columns to table

                    function addColumn() {

                     

                     

                    myTable = app.activeDocument.stories[0].tables[tableNum];

                       

                    var number =  app.activeDocument.stories[0].tables[tableNum].rows[0].cells.length;

                       

                    if (number < 20) {

                          myTable.columns.add(LocationOptions.atEnd,myTable);

                          addColumn();

                    }

                    else

                    {

                    tableFormat();   

                        }

                    }

                     

                     

                     

                     

                    //style table

                    function tableFormat() {

                            myTable.cells.everyItem().height = "16pt";

                            myTable.cells.everyItem().width = "10mm";

                            myTable.columns[0].cells.everyItem().width = "30mm";

                            myTable.appliedTableStyle = "Table1";

                            myTable.cells.everyItem().appliedCellStyle = "Body";

                            myTable.columns[0].cells.everyItem().appliedCellStyle = "Column1";

                           

                           

                           

                    //loop till end       

                    if (myTable != app.activeDocument.stories[0].tables[-1]) {

                        tableNum = tableNum+1;

                            convertTables();

                         }

                    else {

                         }

                    }

                     

                     

                     

                     

                    Any refinements are welcome, and thanks for the help!

                    • 7. Re: Find text and convert to table using JS
                      crayz77 Level 1

                      On last question for anyone.

                       

                      Script runs great but crashes out with an Error Number 27, Error String: Stack overrun?

                      Any ideas?

                      • 8. Re: Find text and convert to table using JS
                        [Jongware] Most Valuable Participant

                        It contains a recursive function:

                         

                        function addColumn ()

                        ...

                          if (number < 20) {

                           myTable.columns.add(LocationOptions.atEnd,myTable);

                           addColumn();

                          }

                         

                        This is not necessary at all. Use a simple loop:

                         

                        while (number < 20)

                        {

                           myTable.columns.add(LocationOptions.atEnd,myTable);

                           number++;
                        }

                         

                        There is some pretty weird looping logic in your call chain:

                         

                        addColumn() -> tableFormat() -> convertTables() -> addColumn()

                         

                        That is, the routine that gets called first (addColumn) always seems to end up calling itself again. It will do so for some thousands of iterations and then Presto! Stack Overflow.

                        1 person found this helpful
                        • 9. Re: Find text and convert to table using JS
                          crayz77 Level 1

                          I'm new to scripting so its bound to be bloated with pointless excess script, so any advice that can improve my script is much appreciated.

                          Will give your solution a go in the morning when I get to work.

                           

                          Thanks once again.

                          • 10. Re: Find text and convert to table using JS
                            crayz77 Level 1

                            Have taken your advice and simplified the script and it doesn't crash any more, also added in a "check for exisiting cell and table style names".

                            So here it is:

                             

                             

                             

                            app.findGrepPreferences = NothingEnum.nothing;

                            app.changeGrepPreferences = NothingEnum.nothing;

                             

                            app.findGrepPreferences.findWhat = "((Vintage)(.*)\\r)((WR)(.*)\\r)((Drink)(.*))";

                             

                            var myResults = app.activeDocument.findGrep(true);  

                             

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

                               var singleResult = myResults[i];

                               singleResult.convertToTable("\t", "\r",);

                               }

                             

                             

                             

                            try{

                            app.activeDocument.cellStyles.item("Body").name;

                            }

                            catch (myError){

                            app.activeDocument.cellStyles.add({name:"Body"});

                            }

                             

                             

                            try{

                            app.activeDocument.cellStyles.item("Column1").name;

                            }

                            catch (myError){

                            app.activeDocument.cellStyles.add({name:"Column1"});

                            }

                             

                             

                            try{

                            app.activeDocument.tableStyles.item("Table1").name;

                            }

                            catch (myError){

                            app.activeDocument.tableStyles.add({name:"Table1"});

                            }

                             

                             

                             

                            var myTable = 0;

                            var tableNum = 0;

                             

                            convertTables()

                             

                            function convertTables() {

                             

                            while (myTable != app.activeDocument.stories[0].tables[-1]) {

                                

                                if (app.documents.length > 0 && app.selection.length > 0)

                                app.select(app.activeDocument.stories[0].tables[tableNum]);

                             

                            var myTable = app.activeDocument.stories[0].tables[tableNum];

                            var number =  app.activeDocument.stories[0].tables[tableNum].rows[0].cells.length;

                             

                            while (number < 12) {

                               myTable.columns.add(LocationOptions.atEnd,myTable);

                               number++;

                            }

                                    myTable.cells.everyItem().height = "16pt";

                                    myTable.cells.everyItem().width = "12mm";

                                    myTable.columns[0].cells.everyItem().width = "20mm";

                                    myTable.appliedTableStyle = "Table1";

                                    myTable.cells.everyItem().appliedCellStyle = "Body";

                                    myTable.columns[0].cells.everyItem().appliedCellStyle = "Column1";

                                

                                 tableNum++;       

                            }

                            }