【问题标题】:Google Spreadsheet Script : ELSE statement overriding IF statementGoogle 电子表格脚本:ELSE 语句覆盖 IF 语句
【发布时间】:2014-01-29 19:14:03
【问题描述】:

我编写了一个 Google 电子表格脚本,该脚本可以更改输入国家代码所在行的特定国家单元格的背景颜色。我的 ELSE 语句将 BG 颜色返回为白色,因此如果一个单元格没有国家代码,它的 BG 仍然是白色。

我正在使用 toString().match,因此我可以在一个单元格中包含诸如“AU、BEfr、BEnl”之类的输入,并分别突出显示它们的所有单元格。

在我开始添加更多行之前,它运行得很好。也许我不小心删除了一些东西?

正确的国家/地区单元格突出显示 1 秒钟,然后立即返回白色背景。

我在这里缺少什么?

非常感谢!

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'); 
      }
   }
};

【问题讨论】:

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


    【解决方案1】:

    发生的情况是,您有很多要单独评估的 if 语句,然后是要评估的最终 if-else 语句。只要输入不是“CAen”,您似乎会将背景重置为白色。

    更改您的代码以阅读

             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'); 
          }
    

    当前的代码写得很糟糕。您可以简单地将toString() 结果放入一个临时变量并对其进行比较(不知道为什么需要match 函数而不是== 运算符)。此外,您可以看到将突出显示放入其自己的函数中,该函数采用字符串参数和颜色。

    【讨论】:

    • 感谢您帮助安迪!确实,该编辑确实有效,但是将所有更改为“else if”意味着多个国家/地区的语言环境输入(例如“AU,BEfr,BEnl”)不起作用。这是我需要的主要功能。通过研究 StackOverflow 和我非常基本的 JS 知识(如果有的话),我从字面上破解了这一点。
    • 如果您尝试匹配单元格值中的任何这些字符串,请考虑使用 RegEx 或更复杂的匹配逻辑。这样你也可以有一个更干净的 if-else 块。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-09
    • 1970-01-01
    相关资源
    最近更新 更多