切换案例数量未知 - 默认情况稍有变化?

时间:2014-12-10 14:48:19

标签: javascript google-apps-script switch-statement google-sheets

我有一个包含未知数量案例的switch语句。在案例1之后,每个案例输出相同的公式。公式中唯一的区别是reasonaleLabels数组中的数字。它从案例2中的reasonaleLabels [0]开始;然后在案例3中,它是理性标签[1];案例4 - 理由标签[2];等等。是否有一种方法可以设置一个默认情况,如果出现这种情况,会增加基本数据列表内的数字?

for(i = 0; i < drugs.length; i++){
            rationaleTable[i] = [];
          for(j = 0; j < rationaleLabel.length; j++){
            switch(j){
              case 0:
                rationaleTable[j] = drugs[i];
                break;
              case 1:
                rationaleTable[j] = drugs[i];
                break;
              case 2:
                rationaleTable[j] = "=COUNTIFS('All Events'!$D:$D,\"*" + rationaleLabels[0] + "*\" , 'All Events;!$E:$E,\"" + drugs[i] + "*\")";
                break;  
              case 3:
                rationaleTable[j] = "=COUNTIFS('All Events'!$D:$D,\"*" + rationaleLabels[1] + "*\" , 'All Events;!$E:$E,\"" + drugs[i] + "*\")";
                break;  
              case 4:
                rationaleTable[j] = "=COUNTIFS('All Events'!$D:$D,\"*" + rationaleLabels[2] + "*\" , 'All Events;!$E:$E,\"" + drugs[i] + "*\")";
                break;   
              case 5:
                rationaleTable[j] = "=COUNTIFS('All Events'!$D:$D,\"*" + rationaleLabels[3] + "*\" , 'All Events;!$E:$E,\"" + drugs[i] + "*\")";
                break;  
              case 6:
                rationaleTable[j] = "=COUNTIFS('All Events'!$D:$D,\"*" + rationaleLabels[4] + "*\" , 'All Events;!$E:$E,\"" + drugs[i] + "*\")";
                break;  
              case 7:
                rationaleTable[j] = "=COUNTIFS('All Events'!$D:$D,\"*" + rationaleLabels[5] + "*\" , 'All Events;!$E:$E,\"" + drugs[i] + "*\")";
                break;       
               
            }      
          }
        }

3 个答案:

答案 0 :(得分:2)

这可能有效:

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

  if(j==0 || j==1) {
    rationaleTable[j] = drugs[i];
  } else {
     rationaleTable[j] = "=COUNTIFS('All Events'!$D:$D,\"*" + rationaleLabels[j -2 ] + "*\" , 'All Events;!$E:$E,\"" + drugs[i] + "*\")";
  }
}

答案 1 :(得分:1)

会像Chris那样做,但这里仍然是Switch Case Idea的修改版本:

for(i = 0; i < drugs.length; i++){
            rationaleTable[i] = [];
          for(j = 0; j < rationaleLabel.length; j++){
            switch(j){
              case 0:                    
              case 1:
                rationaleTable[j] = drugs[i];
                break;
              default:
                rationaleTable[j] = "=COUNTIFS('All Events'!$D:$D,\"*" + rationaleLabels[j-2] + "*\" , 'All Events;!$E:$E,\"" + drugs[i] + "*\")";
                break;                  
               
            }      
          }
        }

答案 2 :(得分:1)

您可以在默认情况下实施案例2到7(我的意思是后备案例) 我不知道您使用的是哪种语言,但语法应该非常接近以下内容:

switch(j) {
 ...
 default:
    rationaleTable[j] = "=COUNTIFS('All Events'!$D:$D,\"*"" + rationaleLabel[j-2] + ...
}