Home > Software engineering >  Nested Dropdown in Google Sheets
Nested Dropdown in Google Sheets

Time:08-03

In the sheet 'Dropdowns' I have the first column "Module" pulling from a range of headers on the sheet 'Values' =Values!$A$1:$L$1. Once an option in A2 is selected in the dropdown in the Module column, I need B2 to show the options under the matching column header in the Values sheet.

For example if Asset_Top is selected I need B2 to show a dropdown of:

  • asset_top__eyebrow__value
  • asset_top__title__value
  • asset_top__description__value

if Prize is selected then next cell over in column B should display a dropdown of:

  • prize__eyebrow__value
  • prize__title__value
  • prize__description__value
  • prize__body_copy__value
  • prize__bottom_copy__value

How can I get this to work? I need to have 30 rows of options in the sheet 'Dropdown' Here is a link to google sheet enter image description here

Update 2

Here is the variant of the code that works with range C2:D30 on sheets 'Dropdowns' and 'More_Dropdowns':

function onEdit(e) {
  var col = e.range.columnStart;
  if (col != 3) return; // column C

  var row = e.range.rowStart;
  if (row < 2 || row > 30) return; // rows from 2 to 30

  var sheet = e.range.getSheet();
  var sheets = ['Dropdowns', 'More_Dropdowns']; // sheets to watch
  if (!sheets.includes(sheet.getName())) return;

  var ss = e.source;
  var [header, ...data] = ss.getSheetByName('Values').getDataRange().getValues();
  var obj = {};
  header.forEach((x,i) => obj[x] = data.map(x => x[i]).filter(String));
  var list = obj[e.value]
  var rule = SpreadsheetApp.newDataValidation().requireValueInList(list).build();
  sheet.getRange(row,col 1).setDataValidation(rule).setValue(list[0]);
}
  • Related