[英]How do you set a google sheets macro with the "OnEdit" function to run only on one sheet?
I'm currently trying to make a budget spending tracker that automatically populates a date next to each purchase I track.我目前正在尝试制作一个预算支出跟踪器,它会在我跟踪的每次购买旁边自动填充一个日期。 The setup is that on sheet 2, column c is where I add my expense, and column B is where the date should be added.设置是在工作表 2 上,c 列是我添加费用的位置,B 列是应添加日期的位置。 The code I am currently using is pasted below.我目前使用的代码粘贴在下面。
`function onEdit(e) {
var row = e.range.getRow();
var dateCell = e.range.getSheet().getRange(row, 2);
if (!dateCell.getValue()) {
dateCell.setValue(new Date()).setNumberFormat("MM/dd/YY");
}
}`
This is working fine except for one thing, it is activating in every spreadsheet, not just spreadsheet 2. How can I setup my code so that it only runs when another column in spreadsheet 2 is changed?这工作正常,除了一件事,它在每个电子表格中激活,而不仅仅是电子表格 2。如何设置我的代码以便它仅在电子表格 2 中的另一列更改时运行?
Thank you谢谢
In order for your onEdit(e)
trigger to work only on Sheet2
you should add an if
condition where you check if the name of the sheet is the one that you want the trigger be executed on.为了让您的onEdit(e)
触发器仅在Sheet2
上工作,您应该添加一个if
条件,您可以在其中检查工作表的名称是否是您希望在其上执行触发器的名称。
Assuming that the sheet name is "Sheet2"
, you can try this:假设工作表名称是"Sheet2"
,你可以试试这个:
function onEdit(e) {
var sheet = e.range.getSheet();
if (sheet.getName() === "Sheet2") {
var row = e.range.getRow();
var dateCell = sheet.getRange(row, 2);
if (!dateCell.getValue()) {
dateCell.setValue(new Date()).setNumberFormat("MM/dd/YY");
}
}
}
Reference参考
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.