Rewritting the script by using an array and Find/Change
Hi guys,
I'm enjoying writting scripts and try to slowly but surely improve the quality and efficiency of my code. A long way to go, I admit, besides being never ending…
Anyways,
I'd like to get some insight since I don't know how to make it right.
My first code is working. Not the problem here. It is just ugly and definitely amateur, and probably slow due to the bad writting. I want to make it better looking, possibly even making him faster in the process.
The second code is Not working though. That's the reason why I'm seeking help.
What is it about ?
- a script for changing the Cell Style of a table depending on its content. (it's for dealing with a 100+ pages long document so it's very useful for me, despite being a bit slow)
The code so far:
app.findTextPreferences.findWhat = "Text 1"
var myFound = myDoc.findText()
for(i=0; i<myFound.length; i++)
{ if(myFound[i].parent.constructor.name == "Cell")
{myFound[i].parent.appliedCellStyle = "Cell Style 1"
}
}
app.findTextPreferences.findWhat = "Text 2"
var myFound = myDoc.findText()
for(i=0; i<myFound.length; i++)
{
if(myFound[i].parent.constructor.name == "Cell")
{myFound[i].parent.appliedCellStyle = "Cell Style 1)"
}
}
As you can see,
- I'm using a simple F/C query
- I apply the same Cell Style 1 to both queries
I believe it would far better to write it in a way like below, for reading purposes and for adjustments that I will have to do along the way. I thought adding a loop before the script would be the way, but I'm missing something.
// Change to the new Cell Styles
var myQueries = [
["Text 1"],
["Text 2"],
["Text 3"],
]
for(i = 0 ; i < myQueries.length; i++){
try{
app.findTextPreferences.findWhat = myQueries[i];
var myFound = myDoc.findText()
for(j=0; j<myFound.length; j++)
{
if(myFound[j].parent.constructor.name == "Cell")
{
myFound[j].parent.appliedCellStyle = "Cell Style 1"
}
}
}catch(e){
}
}
Something (or many things) is wrong.
I hope you can help me see what to do here
Thanks for the support ^^
