I'm trying to update a cell with the date and time when a specific range of cells are edited.
I've been messing around with the following code, but after checking the execution log, the trigger has failed on every occasion.
Where am I gong wrong?
function onEdit(e) {
var range1 = e.range.getSheet().getRange("D260:D264");
var range2 = e.range.getSheet().getRange("P260:P264");
var range3 = e.range.getSheet().getRange("S260:S264");
var range4 = e.range.getSheet().getRange("AE260:AE264");
var range5 = e.range.getSheet().getRange("AJ260:AJ264");
var range6 = e.range.getSheet().getRange("D53");
var range7 = e.range.getSheet().getRange("N53");
if (e.range.intersects(range1) || e.range.intersects(range2) || e.range.intersects(range3) || e.range.intersects(range4) || e.range.intersects(range5) || e.range.intersects(range6) || e.range.intersects(range7)) {
var dateCell = e.range.getSheet().getRange("W247");
dateCell.setValue(new Date());
}
}
The reason you have this error is because e.range doesn't have a Method of intersect. I do think that you are trying to check the range if it is within your list of ranges to watch for. Basically on Google Apps Script when you use getRange() you will get a Range Object and not a set hence you will need something to get the values you can use for comparison. Please check the solution I created you can use this as a reference on your project the difference with this is I did not list every ranges on the array just enough to do some test.
What the code does is it has a list of Ranges to compare, your watched ranges. I get the A1 Notation of the edited Range check if the Range being edited is with the watched range then execute the setValue().
function onEdit(e) {
var watchedRange = ["D260","D264","D261","D262","D263"];
var rangeA1 = e.range.getA1Notation();
if(watchedRange.includes(rangeA1)){
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
ss.getRange("W247").setValue(new Date());
}}
Google Apps Script - Simple Trigger
Helpful Reminder: When checking Simple Triggers, it is also helpful to learn about Execution Log that is part of the Google Apps Script platform this way you can learn more about the issues your code has encountered. It is on the far left side of your browser when opening Google Apps Script. On the Navigation Bar you can click the Execution to check the execution's information.