Situation:
I have the following script, that runs in OnEdit, but I want to tune this script to run only when someone adds new rows to the sheet.
Script:
function DataValidation2(e)
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getActiveSheet();
if (s.getName() == "Sheet1"){
var cell = s.getRange('C3:C');
var rule = SpreadsheetApp.newDataValidation().setAllowInvalid(false).requireValueInList(['0:15:00', '0:20:00']).build();
cell.setDataValidation(rule);
}
}
If possible execute this script only when someone add new row using the boton add include at the end of the sheet?
Best Regards,
There is a special trigger to detect such changes : onChange
You can even create it programmatically as described in the documentation :
and then a function like this that will be called on every change that looks at the number of rows and do something if it changed...and does nothing if the change was something else.
You can add more conditions to handle other cases, for example what to do id rows where deleted
EDIT following Wchiquito 's comment
here is another (simpler) version that works pretty well too :