javascript - 这是多余的吗?

标签 javascript google-apps-script triggers

这是多余的吗?有没有更简单的写法?

  if(e.range.getSheet().getName() == 'Estimate'){  
    var thisss = SpreadsheetApp.getActive().getSheetByName('Estimate');
  }

最佳答案

是的。这被称为 WET解决方案。

The DRY(Don't repeat yourself) principle is stated as "Every piece of knowledge must have a single, unambiguous, authoritative representation within a system".

简单地说,如果您跟踪一个代码分支(例如,一个变量信息),它应该只有一行到树/代码的根,并且不得在代码的其他地方重复。

if(e.range.getSheet().getName() == 'Estimate'){  
  var sheet1 = SpreadsheetApp.getActive().getSheetByName('Estimate');
}

这里有两个分支(e.range.getSheet()SpreadsheetApp.getActive().getSheetByName('Estimate'))来访问同一张表对象(工作表 1)。这可以修改为

var editedSheet = e.range.getSheet();
if(editedSheet.getName() === 'Estimate'){  
  //Do something with editedSheet here
}

Violations of DRY are typically referred to as WET solutions, which is commonly taken to stand for either "write everything twice", "we enjoy typing" or "waste everyone's time".

关于javascript - 这是多余的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56976851/

相关文章:

javascript - ul 和 li - 具有多列和绝对位置的动态宽度

javascript - ASP MVC 基本 AJAX Json 请求返回 null

javascript - Google电子表格为单元格或行设置唯一的ID或元数据

javascript - Google Apps 脚本 - 将应用程序脚本函数的输出返回到 html 文件 javascript 函数

javascript - Google Sheet 应用程序脚本时间戳条件

mysql - 仅在数据更改时触发的更新触发器后 MySQL 中的 NULL 处理

javascript - 如何选择在 ajax 请求中显示的内容?

javascript - 正则表达式 - 匹配重复标签的 "parent child"关系

google-apps-script - OnEdit(e) Google Sheets 清除相邻单元格

MYSQL触发器在两个表中保留相同的数据