Skip to main content
Known Participant
August 3, 2015
Answered

Help with a script to find text in a Table Cell and apply Cell style

  • August 3, 2015
  • 1 reply
  • 1396 views

Hi,

I’m building up the script that Jongware wrote in his post here http://indesignsecrets.com/tackling-tables-through-scripting.php – I’m trying to create a variable in which I can add a number of different bits of text, in this instance it’s different UK locations i.e. ‘London’, ‘South East’, ‘Scotland’ etc. I just need the script to apply the Cell Style – ‘District Cell’ – to any cell that contains any of the text in the variable. Below is the script, if anyone can help I’d be thankful.

function checkWhichTable()

{

// ensure the user made a selection

if (app.selection.length != 1)

return null;

var currentTable = app.selection[0];

if (currentTable.hasOwnProperty("baseline"))

{

currentTable = app.selection[0].parent;

}

while (currentTable instanceof Cell || currentTable instanceof Row || currentTable instanceof Column)

currentTable = currentTable.parent;

if (!(currentTable instanceof Table))

{

// No table selected

return null;

}

return currentTable;

}

app.doScript(checkUserSelection, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "Process Table");

function checkUserSelection ()

{

var a_table = checkWhichTable();

if (a_table == null)

{

if (confirm("No table selected. Do you want to process *all* tables?") == false)

return;

allTables = app.activeDocument.stories.everyItem().tables.everyItem().getElements();

for (aTable=0; aTable<allTables.length; aTable++)

{

processTable (allTables[aTable]);

}

} else

{

processTable (a_table);

}

}

function processTable(table)

{

// do something here!

//Find Text in Cell and apply Cell Style

var textInCell=['London', 'Scotland', 'South West'];

for (i=0; i<table.cells.length; i++)

{

if (table.cells.texts[0].contents==textInCell)

table.cells.appliedCellStyle = "District Cell";

}

}

This topic has been closed for replies.
Correct answer Jump_Over

Hi,

Modify this function:

function processTable(table)

{

//Find Text (exactly as it is) in Cell and apply Cell Style

var

  textInCell=['London', 'Scotland', 'South West'],

  mFound, cFound;

app.findGrepPreferences = null;

app.findGrepPreferences.findWhat = "^(" + cities.join("|") + ")$";

mFound = table.findGrep();

while( cFound = mFound.pop() )

  cFound.parent.appliedCellStyle = "District Cell";

}

Jarek

1 reply

Jump_Over
Jump_OverCorrect answer
Legend
August 3, 2015

Hi,

Modify this function:

function processTable(table)

{

//Find Text (exactly as it is) in Cell and apply Cell Style

var

  textInCell=['London', 'Scotland', 'South West'],

  mFound, cFound;

app.findGrepPreferences = null;

app.findGrepPreferences.findWhat = "^(" + cities.join("|") + ")$";

mFound = table.findGrep();

while( cFound = mFound.pop() )

  cFound.parent.appliedCellStyle = "District Cell";

}

Jarek

Known Participant
August 3, 2015

Cheers for the help.

I get 'cities undefined' error. In line 09 you've used 'cities.join' where as i think it should be 'textInCell.join' - I've changed it to  'textInCell.join' and it works.

Thanks for your help.