'Data validation based on a first Data validation using Two onEdit functions in the same script
I need help, I'm using an onEdit function to implement data validation based on a sheet. It is working fine when I use it alone. I can choose a type in Colonne 4 and a list appears in Colonne 5. Then I'm trying to repeat this for colonne 7 based on the choice made in colonne 5. I know an onedit function can only be used ones in a script, so I tried to do the second one in a new project but it didn't work. So I tried to do onEdit1 and onEdit2 and call them in a single onEdit(e) function but I had no success either. I would appreciate any help on that. Thanks in advance!
///////////////////Two data validation based on two different sheet, the second depends from the first one///////////////
var ss = SpreadsheetApp.getActive();
var parameters_sheet1 = ss.getSheetByName('Bases de données');
var parameters_sheet2 = ss.getSheetByName('Validation - Quantité');
var data_sheet = ss.getSheetByName('1. Dispatching');
var campAndGroup1 = parameters_sheet1.getRange(2, 1,parameters_sheet1.getLastRow()-1,2).getValues();
var campAndGroup2 = parameters_sheet2.getRange(2, 1,parameters_sheet2.getLastRow()-1,2).getValues();
function dataval() {
var list = ["a", "b", "g"];
applyValidationToCell(list,cell);
}
function onEdit(e){
onEdit1()
onEdit2()
}
function onEdit1(e){
var activeCell= e.range;
var val = activeCell.getValue();
var r= activeCell.getRow();
var c = activeCell.getColumn();
var wsName= activeCell.getSheet().getName();
if(wsName=="1. Dispatching" && c === 4 && r>5){
if(val=== ""){
data_sheet.getRange(r,5).clearContent();
data_sheet.getRange(r,5).clearDataValidations();
} else {
var filteredGroups = campAndGroup.filter(function(g){return g[0]=== val});
var listToApply = filteredGroups.map(function(g){return g[1]});
var cell = data_sheet.getRange(r,5);
applyValidationToCell(listToApply,cell);
}
}
}
function onEdit2(e){
var activeCell= e.range;
var val = activeCell.getValue();
var r= activeCell.getRow();
var c = activeCell.getColumn();
var wsName= activeCell.getSheet().getName();
if(wsName=="1. Dispatching" && c === 5 && r>5){
if(val=== ""){
data_sheet.getRange(r,7).clearContent();
data_sheet.getRange(r,7).clearDataValidations();
} else {
var filteredGroups = campAndGroup.filter(function(g){return g[0]=== val});
var listToApply = filteredGroups.map(function(g){return g[1]});
var cell = data_sheet.getRange(r,7);
applyValidationToCell(listToApply,cell);
}
}
}
function applyValidationToCell(list,cell){
var rule = SpreadsheetApp
.newDataValidation()
.requireValueInList(list)
.setAllowInvalid(false)
.build();
cell.setDataValidation(rule);
}
///////////////////fin validation///////////////
Solution 1:[1]
You missed to pass the edit event object.
Simple fix: Replace
function onEdit(e){
onEdit1()
onEdit2()
}
by
function onEdit(e){
onEdit1(e)
onEdit2(e)
}
NOTE: The code can be optimized (reduce the code lines and the execution time) but that looks to be beyond the scope of the question.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
