我有switch()
需要包含多个条件才能成为真实
在线社区告诉我,我应该在两种情况下将它们分开然后定义它。像这样:
function changeGrep(searchFor){
app.findGrepPreferences.findWhat = searchFor;
var myFound = myDoc.findGrep();
for(i=0; i<myFound.length; i++){
switch(searchFor){
case "^201\\d$":
case myFound[i].parent.fillColor == app.activeDocument.swatches.item(14):
myFound[i].parent.fillColor = app.activeDocument.swatches.item(3);
break;
case "^-?\\+?\\(?((\\d+,)?(\\d+,)?(\\d+)(\\.\\d+)?%?\\)?)$":
case myFound[i].parent.fillColor == app.activeDocument.swatches.item(14):
myFound[i].parent.fillColor = app.activeDocument.swatches.item(4);
break;
}
}
}
changeGrep("^-?\\+?\\(?((\\d+,)?(\\d+,)?(\\d+)(\\.\\d+)?%?\\)?)$");
changeGrep("^201\\d$");
首先,整个表格都以红色(14)
放置。如果两个条件为真,则应更改颜色。但是,它并不关心第二个case
。
有关如何在extendcript中执行此操作的任何想法?
答案 0 :(得分:1)
你可以试试这个香草JS解决方案。将true
传递给switch
语句,然后每个案例都可以包含一个条件。
switch (true) {
case (<condition> && <condition>):
// do something
break;
case (searchFor === '^201\\d$'):
// do other thing
break;
case ((searchFor === '^201\\d$') && (myFound[i].parent.fillColor == app.activeDocument.swatches.item(14)):
// code
break;
}
答案 1 :(得分:1)
如果你跳过switch
声明,我认为这可能会更清晰(也更快)。
我首先要弄清楚,你的替换样本将是什么,然后在循环中你只需要弄清楚,如果父对象有正确的替换样本,如果是,请替换为替换样本。 / p>
function changeGrep(searchFor){
app.findGrepPreferences.findWhat = searchFor;
var myFound = myDoc.findGrep();
var myReplaceSwatch;
if(searchFor === "^201\\d$") {
myReplaceSwatch = app.activeDocument.swatches.item(3);
} else if (searchFor === "^-?\\+?\\(?((\\d+,)?(\\d+,)?(\\d+)(\\.\\d+)?%?\\)?)$") {
myReplaceSwatch = app.activeDocument.swatches.item(4);
}
for (var i = 0; i < myFound.length; i++) {
if(myReplaceSwatch && myFound[i].parent.fillColor === app.activeDocument.swatches.item(14) {
myFound[i].parent.fillColor = myReplaceSwatch;
}
}
}
changeGrep("^-?\\+?\\(?((\\d+,)?(\\d+,)?(\\d+)(\\.\\d+)?%?\\)?)$");
changeGrep("^201\\d$");
或者,为了简化更多,你也可以将样本的索引传递给函数:
function changeGrep(searchFor, swatchIndex){
app.findGrepPreferences.findWhat = searchFor;
var myFound = myDoc.findGrep();
var myReplaceSwatch = app.activeDocument.swatches.item(swatchIndex);
for (var i = 0; i < myFound.length; i++) {
if(myReplaceSwatch && myFound[i].parent.fillColor === app.activeDocument.swatches.item(14) {
myFound[i].parent.fillColor = myReplaceSwatch;
}
}
}
changeGrep("^-?\\+?\\(?((\\d+,)?(\\d+,)?(\\d+)(\\.\\d+)?%?\\)?)$", 4);
changeGrep("^201\\d$", 3);