Skip to main content
Belimisimus*
Participant
July 10, 2014
Answered

A script to apply cell style to entire rows containing a specific text

  • July 10, 2014
  • 4 replies
  • 2095 views

Hi,

I am using InDesign CC 2014

Does anyone know how to apply a cellstyle based on text contents found in a cell in javascript?

I need to search the document and change cell style in entire row to "cellStyle01" if the text "someText" appear in cell.

I've found similar script here in forum which apply table style, but don't know how to change it to apply cell style!

var curDoc = app.activeDocument;

var allTables = curDoc.stories.everyItem().tables.everyItem();

 

app.findTextPreferences = app.changeTextPreferences = null;

app.findTextPreferences.findWhat = "someText";

var allFounds = allTables.findText();

app.findTextPreferences = app.changeTextPreferences = null;

 

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

var curFound = allFounds;

if ( curFound.length == 1 ) {

var curFoundParent = curFound[0].parent; 

// curFoundParent.parent.appliedTableStyle = curDoc.tableStyles.itemByName( "tableStyle" ); 

curFoundParent.parent.appliedCellStyle = curDoc.cellStyles.itemByName("cellStyle01"); 

}

}

Thanks!

This topic has been closed for replies.
Correct answer Laubender

You are using the wrong kind of quotes for the string.

Use the straight quotes.

Not:

But: "

Uwe

4 replies

Participating Frequently
September 1, 2022

Hello,

 

I found this script and I really want to modify it to use it but when I try to run it i get the following error message:

 

"undefined is not an object"

source: var cellsInRow = curFound.parent.parentRow.cells.everyItem();

 

Using InDesign 2022, Do you know why that happens?

 

My code:

 

var curDoc = app.activeDocument;
var allTables = curDoc.stories.everyItem().tables.everyItem();

app.findGrepPreferences = app.changeGrepPreferences = null;
app.findGrepPreferences.findWhat = "^Total";
app.findGrepPreferences.appliedParagraphStyle = app.activeDocument.paragraphStyleGroups.itemByName("Portfolio Holdings Table").paragraphStyles.itemByName("Body_Slate_Left Issuer");
var allFounds = allTables.findGrep();
app.findGrepPreferences = app.changeGrepPreferences = null;

for ( var i = 0; i < allFounds.length; i++ ) {
    var tableFound = allFounds;
    if ( tableFound.length > 0 ) {
        for ( var j = 0; j < tableFound.length; j++ ) {
            var curFound = tableFound;
var cellsInRow = curFound.parent.parentRow.cells.everyItem();        
            cellsInRow.appliedCellStyle = app.activeDocument.cellStyleGroups.itemByName("Portfolio Holdings Table").cellStyles.itemByName("Total Row");
            cellsInRow.clearCellStyleOverrides( true );
        } // end for
    } // end if
} // end for

Participant
September 7, 2015

Can this script be altered to find "EnDashes" and then apply the cell style to the entire row? I'm new to scripting and I'm not really sure how to get it to work. I keep getting the following error.

LaubenderCommunity ExpertCorrect answer
Community Expert
September 9, 2015

You are using the wrong kind of quotes for the string.

Use the straight quotes.

Not:

But: "

Uwe

Participant
September 9, 2015

I will change them and give it a try.

Thank you!

Kai Rübsamen
Participating Frequently
July 11, 2014

Hi,

in this new case you cannot simply change the last line, because yo want to find cells and rows and not the whole table.

Try this changed version:

var curDoc = app.activeDocument;

var allTables = curDoc.stories.everyItem().tables.everyItem();

app.findTextPreferences = app.changeTextPreferences = null;

app.findTextPreferences.findWhat = "some Text";

var allFounds = allTables.findText();

app.findTextPreferences = app.changeTextPreferences = null;

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

    var tableFound = allFounds;

    if ( tableFound.length > 0 ) {

        for ( var j = 0; j < tableFound.length; j++ ) {

            var curFound = tableFound;

            var cellsInRow = curFound.parent.parentRow.cells.everyItem();        

            cellsInRow.appliedCellStyle = curDoc.cellStyles.itemByName( "cellStyle01" );

            cellsInRow.clearCellStyleOverrides( true );

        } // end for

    } // end if

} // end for

Belimisimus*
Participant
July 11, 2014

Hi,

both solutions are working and did not find any errors.

Thanks for the help, this script will significantly speed up my work.


Ivan

Chinnadk
Legend
July 11, 2014

Hi,

Try this,

var doc = app.activeDocument, 

    _tables = doc.stories.everyItem().textStyleRanges.everyItem().getElements(), 

    i, j, k, l, a, _rows,_cells, rowlen; 

for(i =0;i<_tables.length;i++) 

    for(j =0;j<_tables.tables.length;j++) 

    { 

        _rows = _tables.tables.rows; 

        for(k =0;k<_rows.length;k++) 

        { 

            _cells = _rows.cells; 

            for(l =0;l<_cells.length;l++) 

            { 

                if(_cells.contents == "someText") 

                { 

                    rowlen = _cells.parent.cells.length; 

                    while(rowlen--) 

                    { 

                          _cells[rowlen].appliedCellStyle = doc.cellStyles.item("cellStyle01"); 

                        } 

                    } 

                } 

            } 

        } 

     }

Regards,

Chinna