I would like to run function onEdit(e) to remove Data validation with these 2 criterias as below:
- Remove for columns G,H,I,K if L is "no need".
- Remove for columns M,N,O,Q if R is "no need<2>"
My script runs well for the first criteria. However, it does not work for the second. I can not figure out the solution. Pls help me to correct the script.
function onEdit(e) {
var range = e.range;
var sheet = range.getSheet();
var row = range.getRow();
var values = sheet.getRange(`L${row}`).getValues()[0];
var r = sheet.getRangeList(["G", "H", "I", "K"].map(f => `${f}${row}`));
if (values.includes("no need")) {
r.clearDataValidations().setValue("no need");
} else {
var rules = ["'Data validation'!$G$5:$G$1003", "'Data validation'!$H$5:$H$1003", "'Data validation'!$I$5:$I$1003", "'Data validation'!$K$5:$K$1003"];
r.getRanges().forEach((rr, i) => rr.setDataValidation(SpreadsheetApp.newDataValidation().requireValueInRange(e.source2.getRange(rules[i]), true).build()));
}
var values1 = sheet.getRange(`R${row1}`).getValues()[0];
var r1 = sheet.getRangeList(["M", "N", "O", "Q"].map(f1 => `${f1}${row1}`));
if (values1.includes("no need<2>")) {
r1.clearDataValidations().setValue("no need<2>");
} else {
var rules1 = ["'Data validation'!$M$5:$M$1003", "'Data validation'!$N$5:$N$1003", "'Data validation'!$O$5:$O$1003", "'Data validation'!$Q$5:$Q$1003"];
r1.getRanges().forEach((rr1, i) => rr1.setDataValidation(SpreadsheetApp.newDataValidation().requireValueInRange(e.source.getRange(rules1[i]), true).build()));
}
}
Many thanks
Here is my GG sheet link:
https://docs.google.com/spreadsheets/d/1osCKdZsvaKTFJyD2wfcCoQtIfGxTR6ovt9eE7zabyzs/edit?usp=sharing