Highlighted

Applying Cell Style to Word and Row in the Same time

Engaged ,
Mar 24, 2020

Copy link to clipboard

Copied

Hi Professionals, I'm Trying to find word in table cell then if founded the script shouid Apply the Cell Style to the word and all the row even if found more than one , here is my try :

 

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");
//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);
 processTable(table);
}

}
function processTable(table)
{
  // do something here!
  //Find Text in Cell and apply Cell Style
  var textInCell="Friday";
  //var myRegEx = new RegExp("\\b\textInCell\b");
  for (i=0; i<table.cells.length; i++); 
  for  (i=0;i<table.rows.length; i++);
  {
    if (table.cells[i].texts[0].contents.match(textInCell));
      table.cells[i].appliedCellStyle = "YourCellStyle";
      table.rows[i].cells.everyItem().appliedCellStyle = "YourCellStyle";
}
}

 

Please Help me.. thanks in advance

Adobe Community Professional
Correct answer by Manan Joshi | Adobe Community Professional

Change your function definiton to the following

function processTable(table) {
    // do something here!
    //Find Text in Cell and apply Cell Style
    var textInCell = "Friday";
    for (i = 0; i < table.cells.length; i++)
    {
        if (table.cells[i].texts[0].contents.match(textInCell))
			table.cells[i].rows[0].cells.everyItem().appliedCellStyle = "YourCellStyle";
    }
}

 

The idea is that once you find the cell which matches the content, you get its row and then apply the style to all the cells in that row. Also i have seen a big mistake in your code, you have added ; after the for and if statement, this actaully causes the for and if to work on a blank line and hence it does not serve any purpose. Look at my code i have removed these

 

-Manan

Topics

Scripting

Views

308

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Applying Cell Style to Word and Row in the Same time

Engaged ,
Mar 24, 2020

Copy link to clipboard

Copied

Hi Professionals, I'm Trying to find word in table cell then if founded the script shouid Apply the Cell Style to the word and all the row even if found more than one , here is my try :

 

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");
//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);
 processTable(table);
}

}
function processTable(table)
{
  // do something here!
  //Find Text in Cell and apply Cell Style
  var textInCell="Friday";
  //var myRegEx = new RegExp("\\b\textInCell\b");
  for (i=0; i<table.cells.length; i++); 
  for  (i=0;i<table.rows.length; i++);
  {
    if (table.cells[i].texts[0].contents.match(textInCell));
      table.cells[i].appliedCellStyle = "YourCellStyle";
      table.rows[i].cells.everyItem().appliedCellStyle = "YourCellStyle";
}
}

 

Please Help me.. thanks in advance

Adobe Community Professional
Correct answer by Manan Joshi | Adobe Community Professional

Change your function definiton to the following

function processTable(table) {
    // do something here!
    //Find Text in Cell and apply Cell Style
    var textInCell = "Friday";
    for (i = 0; i < table.cells.length; i++)
    {
        if (table.cells[i].texts[0].contents.match(textInCell))
			table.cells[i].rows[0].cells.everyItem().appliedCellStyle = "YourCellStyle";
    }
}

 

The idea is that once you find the cell which matches the content, you get its row and then apply the style to all the cells in that row. Also i have seen a big mistake in your code, you have added ; after the for and if statement, this actaully causes the for and if to work on a blank line and hence it does not serve any purpose. Look at my code i have removed these

 

-Manan

Topics

Scripting

Views

309

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Adobe Community Professional ,
Mar 25, 2020

Copy link to clipboard

Copied

Change your function definiton to the following

function processTable(table) {
    // do something here!
    //Find Text in Cell and apply Cell Style
    var textInCell = "Friday";
    for (i = 0; i < table.cells.length; i++)
    {
        if (table.cells[i].texts[0].contents.match(textInCell))
			table.cells[i].rows[0].cells.everyItem().appliedCellStyle = "YourCellStyle";
    }
}

 

The idea is that once you find the cell which matches the content, you get its row and then apply the style to all the cells in that row. Also i have seen a big mistake in your code, you have added ; after the for and if statement, this actaully causes the for and if to work on a blank line and hence it does not serve any purpose. Look at my code i have removed these

 

-Manan

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Community Beginner ,
Mar 26, 2020

Copy link to clipboard

Copied

Hi Manan,

 

What if instead of "text" content, the script will search for a "grep expression" to make it more specific.

Because "Friday" can have a text following it (e.g. "Friday Madness") and it will still match the search and apply the cell style. While if we use grep expression (which is "^Friday\z"), it will only match a cell with the word "Friday" in it.

 

I like the construction of the script and it is much easier to understand. I just wonder how will it turn if we use grep instead of text? Thanks!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Adobe Community Professional ,
Mar 27, 2020

Copy link to clipboard

Copied

Hi Martin,

look up method match() for strings.

Usually it takes a regular expression as argument.

If something else is given as argument, e.g. a string, JavaScript tries to convert it to a regular expression internally.

 

If you really want to do an InDesign GREP search ( perhaps with table.findGrep() ) you get your cell row with something like this below ( some pseudo code is also included ) :

 

var myCellStyleName = "YourCellStyle";
var myCellStyle = app.documents[0].cellStyles.itemByName( myCellStyleName );

/*
	Some code between to define a given table.
	And also to do some grep preferences for find etc.pp.
*/

var findResultArray = table.findGrep();

for( var n=0; n<findResultArray.length; n++ )
{
	var text = findResultArray[n].texts[0] ;
	var cell = text.parent;
	var row = cell.rows[0];
	
	// Better use the object here instead of a string:
	row.cells.everyItem().appliedCellStyle = myCellStyle;

};

 

 

Regards,
Uwe Laubender

( ACP )

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Community Beginner ,
Mar 28, 2020

Copy link to clipboard

Copied

Hi Uwe,

 

It worked! Thanks again for the 2nd time!

 

- Martin

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Engaged ,
Mar 27, 2020

Copy link to clipboard

Copied

Dear Manan Joshi

 

Thank you very much indeed

 

Best Regards

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...