Script to apply paragraph style to text found in table cell
Learn / Forums / InDesign Add-ons (Scripts, Scripting, and Plug-ins) / Script to apply paragraph style to text found in table cell
Tagged: cells, paragraph style, paragraph styles, script, Scripting, select text, Tables
- This topic has 5 replies, 2 voices, and was last updated 7 years ago by Michel Allio for FRIdNGE.
-
AuthorPosts
-
-
April 13, 2017 at 10:09 am #93697Kate BasartParticipant
I am trying to patch together a script that will find specific phrases that are in various tables in my long document and change the paragraph style. I’m very new to scripting but so far the script I have pieced together is finding the phrases, I just don’t know how to get it to apply the style. I’m not sure if what I need to do next is somehow make sure the found text is selected? Any help would be most appreciated. Thanks!
Here is what I have so far:
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)
{// fit cells to text
table.rows.everyItem().height = “36pt”;
table.cells.everyItem().autoGrow == “true”;//Find Text in Cell and apply Paragraph Style
var textInCell=[‘Activity’, ‘Possible Answers’];
var myRegEx = new RegExp(“\\b(“+textInCell.join(“|”)+”)\\b”);
for (i=0; i<table.contents.length; i++)
{if (table.contents[i].match(myRegEx))
{
table.contents[i].appliedParagraphStyle = “table H2”;
}
}
}Thanks!
-
April 13, 2017 at 3:37 pm #93701Michel Allio for FRIdNGEParticipant
function checkWhichTable()
{
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)) 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 (var a = 0; a < allTables.length; a++) processTable (allTables[a]);
} else processTable (a_table);
}function processTable(myTable)
{
myTable.rows.everyItem().height = “36pt”;
myTable.cells.everyItem().autoGrow = true;
var myCells = myTable.cells.everyItem().getElements(),
C = myCells.length,
myTextInCell = [“Activity”, “Possible Answers”],
myRegEx = new RegExp(“\\b(” + myTextInCell.join(“|”) + “)\\b”);
for ( var c = 0; c < C; c++) if (myCells[c].texts[0].contents.match(myRegEx)) myCells[c].texts[0].paragraphs[0].appliedParagraphStyle = “table H2”;
}//__(^/)
-
April 13, 2017 at 6:09 pm #93702Kate BasartParticipant
Thank you Obi-wan! Almost perfect except only works on the first instance in any given cell. Is it possible to change it to grab any instance in all cells?
-
April 14, 2017 at 7:28 am #93717Michel Allio for FRIdNGEParticipant
function checkWhichTable() {
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)) 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 (var a = 0; a < allTables.length; a++) processTable (allTables[a]);
} else processTable (a_table);
}function processTable(myTable) {
myTable.rows.everyItem().height = “36pt”;
myTable.cells.everyItem().autoGrow = true;
var myParas = myTable.cells.everyItem().texts[0].paragraphs.everyItem().getElements(),
P = myParas.length,
myTextInCell = [“Activity”, “Possible Answers”],
myParaStyle = “table H2”,
myRegEx = new RegExp(“\\b(” + myTextInCell.join(“|”) + “)\\b”);
for ( var p = 0; p < P; p++) if (myParas[p].contents.match(myRegEx)) myParas[p].appliedParagraphStyle = myParaStyle;
}//__(^/)
-
April 17, 2017 at 8:05 am #93730Kate BasartParticipant
Fantastic, works perfectly. Thank you!
-
April 17, 2017 at 8:13 am #93731Michel Allio for FRIdNGEParticipant
Cool!
(^/) Join The Rebellion!
-
-
AuthorPosts
- You must be logged in to reply to this topic.