Skip to main content
Inspiring
June 15, 2012
Question

Find text and convert to table using JS

  • June 15, 2012
  • 1 reply
  • 2872 views

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

This topic has been closed for replies.

1 reply

Inspiring
June 15, 2012

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;

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

    }

crayz77Author
Inspiring
June 18, 2012

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();

    }

}

crayz77Author
Inspiring
June 18, 2012

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