简体   繁体   English

Google Spreadsheet Script:ELSE语句覆盖IF语句

[英]Google Spreadsheet Script : ELSE statement overriding IF statement

I've hacked together a Google Spreadsheet script that changes the background color of a specific country cell of the row a country code was entered into. 我整理了一个Google Spreadsheet脚本,该脚本可更改输入国家/地区代码的行中特定国家/地区单元格的背景颜色。 My ELSE statement returns the BG color to white, so if a cell doesn't have a country code, it's BG remains white. 我的ELSE语句将BG颜色返回为白色,因此,如果一个单元格没有国家/地区代码,则其BG保持白色。

I am using a toString().match, so I can include input such as: "AU, BEfr, BEnl" in one cell and highlight all of their cells individually. 我使用的是toString()。match,因此我可以在一个单元格中包含诸如“ AU,BEfr,BEnl”之类的输入,并分别突出显示其所有单元格。

It was working great for awhile until I started adding more lines. 在开始添加更多行之前,它一直运行良好。 Perhaps I've deleted something by accident? 也许我是偶然删除了一些内容?

The correct country cell highlights for 1 second and then instantly returns to a white background. 正确的国家/地区单元突出显示1秒钟,然后立即返回白色背景。

What am I missing here? 我在这里想念什么?

Many thanks! 非常感谢!

function onEdit() {
   var ss = SpreadsheetApp.getActiveSpreadsheet();
   var s = ss.getSheetByName('R186');
   var values1Rule1 = s.getRange('Locales').getValues();
   var color1 = '#ADD8E6';

   for (var row in values1Rule1) {
      for (var col in values1Rule1[row]) {
         if(values1Rule1[row][col].toString().match("AR") == "AR")
            s.getRange(s.getRange('AR').offset(row,col,1,1).getA1Notation()).setBackgroundColor(color1);
         if(values1Rule1[row][col].toString().match("AU") == "AU") 
            s.getRange(s.getRange('AU').offset(row,col,1,1).getA1Notation()).setBackgroundColor(color1);
         if(values1Rule1[row][col].toString().match("BEfr") == "BEfr") 
            s.getRange(s.getRange('BEfr').offset(row,col,1,1).getA1Notation()).setBackgroundColor(color1);
         if(values1Rule1[row][col].toString().match("BEnl") == "BEnl") 
            s.getRange(s.getRange('BEnl').offset(row,col,1,1).getA1Notation()).setBackgroundColor(color1);
         if(values1Rule1[row][col].toString().match("BR") == "BR")
            s.getRange(s.getRange('BR').offset(row,col,1,1).getA1Notation()).setBackgroundColor(color1);
         if(values1Rule1[row][col].toString().match("CAen") == "CAen")
            s.getRange(s.getRange('CAen').offset(row,col,1,1).getA1Notation()).setBackgroundColor(color1);

         else 
            s.getRange(s.getRange('I3:BO').offset(row,0,1,200).getA1Notation()).setBackgroundColor('white').setFontColor('black'); 
      }
   }
};

What is happening is that you have a lot of if-statements that will be evaluated separately, and then a final if-else-statement that will be evaluated. 发生的事情是,您有许多if语句将被分别评估,然后是最终的if-else语句将被评估。 So long as the input is not "CAen", it seems you will be resetting the background to white. 只要输入的不是“ CAen”,看来您将背景重置为白色。

Change your code to read 更改代码以阅读

         if(values1Rule1[row][col].toString().match("AR") == "AR")
            s.getRange(s.getRange('AR').offset(row,col,1,1).getA1Notation()).setBackgroundColor(color1);
         else if(values1Rule1[row][col].toString().match("AU") == "AU") 
            s.getRange(s.getRange('AU').offset(row,col,1,1).getA1Notation()).setBackgroundColor(color1);
         else if(values1Rule1[row][col].toString().match("BEfr") == "BEfr") 
            s.getRange(s.getRange('BEfr').offset(row,col,1,1).getA1Notation()).setBackgroundColor(color1);
         else if(values1Rule1[row][col].toString().match("BEnl") == "BEnl") 
            s.getRange(s.getRange('BEnl').offset(row,col,1,1).getA1Notation()).setBackgroundColor(color1);
         else if(values1Rule1[row][col].toString().match("BR") == "BR")
            s.getRange(s.getRange('BR').offset(row,col,1,1).getA1Notation()).setBackgroundColor(color1);
         else if(values1Rule1[row][col].toString().match("CAen") == "CAen")
            s.getRange(s.getRange('CAen').offset(row,col,1,1).getA1Notation()).setBackgroundColor(color1);
         else 
            s.getRange(s.getRange('I3:BO').offset(row,0,1,200).getA1Notation()).setBackgroundColor('white').setFontColor('black'); 
      }

The current code is pretty poorly written otherwise. 否则,当前代码编写得很差。 You can simply get the toString() result into a temporary variable and compare on that (don't know why you need the match function instead of the == operator). 您可以简单地将toString()结果放入一个临时变量中并进行比较(不知道为什么需要match函数而不是==运算符)。 Also, you can see about putting your highlighting into its own function that takes a string argument and a color. 此外,您还可以看到将突出显示放入带有字符串参数和颜色的函数中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM