[My Goal] Using Google Apps Scripts and Google Sheets, I want to create a second level dependent drop down list, where if I select a value from the first drop down list, it'll allow me to select another value from the next drop down list. After selecting the second drop down list, I want to be able to set 6 values across each column. For example, under "Nutrition Plan Sheet" screenshot, if I select "Protein" for Column B, another drop down list will be available under Column C to select the ingredient. Which in this case is "Chicken Breast." Now, what I'm also trying to do is to have Column D-H to be auto-filled when I select a value for Column C. Note: The ingredients are coming from a table under a separate sheet called "Food Database," which I've also attached a screenshot.
[The issue] I'm unable to figure out a way to auto-fill the Column D-H when selecting a value for Column C under the "Nutrition Plan" sheet.
[What I've been able to do so far] I was able to setup a code with the help from a YouTube video (link: https://www.youtube.com/watch?v=s-I8Z4nTDak&list=PL32yvu15_0Tha8QZBTyAF0NCAreowqOFJ), but I don't understand how to set values to each Column.
[Screenshots]
- Nutrition Plan Sheet Screenshot: enter image description here
- Food Database Sheet Screenshot: enter image description here
[Code]
var wsNameNutritionPlan = "Nutrition Plan";
var wsNameFoodDatabase = "Food Database";
var firstLevelColumn = 2;
var secondLevelColumn = 3;
var thirdLevelColumn = 4;
var ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(wsNameNutritionPlan);
var wsFoodDatabase = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(wsNameFoodDatabase);
var foodDatabase = wsFoodDatabase.getRange(3, 2, wsFoodDatabase.getLastRow()-2, 7).getValues();
function onEdit(e) {
var activeCell = e.range;
var val = activeCell.getValue();
var r = activeCell.getRow();
var c = activeCell.getColumn();
var wsName = activeCell.getSheet().getName();
if(wsName === wsNameNutritionPlan && c === firstLevelColumn && r > 6) {
applyFirstLevelValidation(val, r);
} else if(wsName === wsNameNutritionPlan && c === secondLevelColumn && r > 6) {
applySecondLevelValidation(val, r);
}
}
function applyFirstLevelValidation(val, r){
if(val === "") {
ws.getRange(r, secondLevelColumn).clearContent();
ws.getRange(r, secondLevelColumn).clearDataValidations();
ws.getRange(r, thirdLevelColumn).clearContent();
ws.getRange(r, thirdLevelColumn).clearDataValidations();
} else {
ws.getRange(r, secondLevelColumn).clearContent();
ws.getRange(r, thirdLevelColumn).clearContent();
ws.getRange(r, thirdLevelColumn).clearDataValidations();
var filteredFoodDatabase = foodDatabase.filter(function(f) {return f[0] === val});
var listToApply = filteredFoodDatabase.map(function(f) {return f[1]});
var cell = ws.getRange(r, secondLevelColumn);
}
}
function applySecondLevelValidation(val, r){
if(val === "") {
ws.getRange(r, thirdLevelColumn).clearContent();
ws.getRange(r, thirdLevelColumn).clearDataValidations();
} else {
ws.getRange(r, thirdLevelColumn).clearContent();
var firstLevelColValue = ws.getRange(r, firstLevelColumn).getValue();
var filteredFoodDatabase = foodDatabase.filter(function(f) {return f[0] === firstLevelColValue && f[1] === val});
var listToApply = filteredFoodDatabase.map(function(f) {return f[2]});
var cell = ws.getRange(r, thirdLevelColumn);
applyValidationToCell(listToApply, cell);
}
}
function applyValidationToCell(listToApply, cell) {
var rule = SpreadsheetApp.newDataValidation().requireValueInList(listToApply).setAllowInvalid(false).build();
cell.setDataValidation(rule);
}