这是多余的吗?有没有更简单的写法?
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/