[英]ExtendScript multiple conditions in switch
I have a switch()
that needs to contain multiple conditions for it to be true. 我有一个
switch()
,它需要包含多个条件才能正确。
The online community tells me I should separate them in two cases and then define it. 在线社区告诉我,应该将它们分为两种情况,然后进行定义。 like so:
像这样:
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$");
To be complete at first the entire table is placed in a red color (14)
. 首先,为了完整起见,将整个桌子放置为红色
(14)
。 If two conditiond are true it should change the color. 如果两个条件为真,则应更改颜色。 However it does't care about the second
case
. 但是,它不关心第二种
case
。
Any ideas on how to do this in extendscript? 有关如何在extendscript中执行此操作的任何想法?
You could try this as a vanilla JS solution. 您可以尝试将其作为普通的JS解决方案。 Pass
true
to switch
statement, and then each case can contain a condition. 将
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;
}
I think this could be a lot cleaner (and also faster), if you skip the switch
statement. 我认为,如果您跳过
switch
语句,这可能会更清洁(并且也会更快)。
I would first figure out, what your replacement swatch is going to be and then in the loop you just need to figure out, if the parent object has the correct swatch for replacement, if so, replace with the replacement swatch. 我首先要弄清楚替换样例将是什么,然后在循环中只需弄清楚父对象是否具有正确的替换样例,如果是,则用替换样例进行替换。
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$");
Or, to simplify even more, you could just pass the index of the swatch to the function as well: 或者,为了进一步简化,您也可以将色板的索引也传递给函数:
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.