Skip to main content
Inspiring
August 9, 2019
Answered

how to define if (myCells[n].appliedCellStyle = xxxx)?

  • August 9, 2019
  • 4 replies
  • 1004 views

Hi experts,

my script like this:

var docs = app.documents;

    for (var i = docs.length-1; i >= 0; i--) {

            var myCells = docs.stories.everyItem().tables.everyItem().cells.everyItem().getElements(); 

            for(var n = 0; n < myCells.length; n++) 

            { 

                var mCellStyles = docs.allCellStyles;

                for ( var k = 0; k < mCellStyles.length; k++) {

                    myCellstyle = mCellStyles.name.match(/^99|8|7|6/);

                    if (myCells.appliedCellStyle = myCellstyle) {  

                          myCells.contents = " "  

                        }

                    }

                } 

            }

aim to let if the cell which applied cell styles name.match 99 98 97 96

the contents = " "

but it seems not working,

could someone please tell me how to fix it.

thanks

regard

John

This topic has been closed for replies.
Correct answer Peter Kahrel

The regular expression is wrong. It should be /^9[9876]/. And using match() for a simple test is expensive because JS tries to create an array. test() is better, it returns true or false. This should work, and is quicker:

for (var n = 0; n < myCells.length; n++) {

  if (/^9[9876]/.test (myCells.appliedCellStyle.name)) {

    myCells.contents = " ";

  }

}

P.

4 replies

JohnwhiteAuthor
Inspiring
August 9, 2019

thank you Peter,

thank so much.

John

Peter Kahrel
Community Expert
Peter KahrelCommunity ExpertCorrect answer
Community Expert
August 9, 2019

The regular expression is wrong. It should be /^9[9876]/. And using match() for a simple test is expensive because JS tries to create an array. test() is better, it returns true or false. This should work, and is quicker:

for (var n = 0; n < myCells.length; n++) {

  if (/^9[9876]/.test (myCells.appliedCellStyle.name)) {

    myCells.contents = " ";

  }

}

P.

JohnwhiteAuthor
Inspiring
August 9, 2019

thank you guys,

I change this like to:

if (myCells.appliedCellStyle == myCellstyle)

but still not working,

could you please tell me how to fix it?

thanks

regard

John

Sunil Yadav
Legend
August 9, 2019

Hi Johnwhite​,

Try this:

var docs = app.documents; 

for (var i = docs.length-1; i >= 0; i--) { 

    var myCells = docs.stories.everyItem().tables.everyItem().cells.everyItem().getElements();   

    for(var n = 0; n < myCells.length; n++){

        var mCellStyles = myCells.appliedCellStyle;

        var myCellstyle = mCellStyles.name.match(/^99|8|7|6/);

        if (myCellstyle != null){

              myCells.contents = " ";

            }

        }

    }

Best

Sunil

Community Expert
August 9, 2019

Hi John,

= will assign a cell style.

== means is equal to the cell style.

Regards,
Uwe

Jongware
Community Expert
Community Expert
August 9, 2019

As was already noted in the previous how define if (myCells.fillColor = "Black")