简体   繁体   中英

Setting a “subrange” list of values in a dropdown (data validation), based on selected value from a range in Google Sheets

glorious Internet. I seek your help.

I've imported a spreadsheet from OpenOffice, and the "subrange" dropdown list (from data validation) is broken.

Basically, Column B = "Category", and Column C = "Subcategory" -- Column B data validation works properly, populates the standard Category dropdown list (it's just a Range within the same sheet, in the example below D2:E2). But then, based on the Category value, Column C should update a dropdown list of subcategories.

ie

row/col D E
2 Fruit Vegetable
3 Apple Carrot
4 Banana Onion

B7 = dropdown list "Fruit / Vegetable" I pick fruit

C7 should then update to a dropdown list "Apple / Banana"

I feel like a potential avenue might be using a Script (was looking to this for inspiration: Getting a range from a range in Google Apps scripting )

but never having used Google Scripting before, I need some basic help (like, even if I have a properly working function CreateSubranges defined in macros.gs -- but how do I get it to trigger every time a Fruit is selected?)

Any advice you have is greatly appreciated: Thank you :-)

If you don't need anything too fancy and it is for just one cell, then this formula will do. Just change the ranges to the correct sheet name/range and it will work for you. The formula gets the value selected from sheet 1 and checks if it matches Fruits or Vegetable. If it matches either of those then B3:B or C3:C is selected. I then constrain the array to only go as far as the count in B3:B...assuming that your two columns have the same number of elements. I then add this range to the data validation for C2 on Sheet one.

=Array_Constrain(ArrayFormula(if(Sheet1!B2="Fruit",B3:B,if(Sheet1!B2="Vegetable",C3:C,""))),countif(B3:B,"<>"),1)

Demo of the category and sub category下拉演示

Showing each separate tab and what happens when something is selected. 数据验证和动态选择

If you are needing a dynamic subcategory for each row then the following onEdit script will set the data validation rules based on what was selected. This does require a small setup of creating the data validation for fruit and vegetable on Sheet2 D2 and E2.

 function onEdit(e) {
  
  var ss = e.source;
  var value = e.value;
  var activeSheet = ss.getSheetName();
  var range = e.range;
  var currentRow = range.getRow();
  var currentColumn = range.getColumn();
  
  
  if(activeSheet == 'Sheet1' && currentRow > 1 && currentColumn == 2 && value == 'Fruit') {
    
    //IF THE ROW IS > 1 AND THE COLUMN = 2 AND FRUIT WAS SELECTED, THEN IT WILL COPY THE VALIDATION RULES FROM 
    //SHEET2 D2.
    var fruitRule = ss.getSheetByName('Sheet2').getRange(2, 4).getDataValidation().copy();
    range.offset(0,1).setDataValidation(fruitRule);
    return;
    
  } else if(activeSheet == 'Sheet1' && currentRow > 1 && currentColumn == 2 && value == 'Vegetable') {
    
    //IF THE ROW IS > 1 AND THE COLUMN = 2 AND VEGETABLE WAS SELECTED, THEN IT WILL COPY THE VALIDATION RULES FROM 
    //SHEET2 E2.
    var vegRule = ss.getSheetByName('Sheet2').getRange(2, 5).getDataValidation().copy();
    range.offset(0,1).setDataValidation(vegRule);
    return;
    
  } else if(activeSheet == 'Sheet1' && currentRow > 1 && currentColumn == 2 && value == null) {
    //IF THE ROW IS > 1 AND THE COLUMN = 2 AND VALUE WAS DELETED, THEN IT WILL CLEAR THE VALIDATION RULES 
    //FROM THE ADJACENT CELL.
    range.offset(0,1).clearDataValidations();
    
  }
  
}

脚本演示

Hope this helps, In the future. it would be much easier to show an example with a demo sheet provided.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM