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. 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?
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.
Assuming that the sheet name is "Sheet2"
, you can try this:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.