RemcoE33

Depended dropdowns

Jul 4th, 2022 (edited)
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     RemcoE33
  3.     https://docs.google.com/spreadsheets/d/1AWufAtSCDN9bxJDPCc0oSN66BWHqRY_5xdn7AVwVByk/edit?usp=sharing
  4. */
  5.  
  6. //Setting the ref sheet where all the columns for the dropdowns are
  7. const dropDownRefSheet = 'Ref';
  8. //Make a list of all the sheet where you want the dropdowns to appear. A = 1, B = 2 etc..
  9. const validation = {
  10.   'Sample': {
  11.     columns: [1, 2, 3],
  12.     rowIsGreaterThen: 1
  13.   }
  14. }
  15.  
  16. function onEdit(e) {
  17.   const sheet = e.source.getActiveSheet();
  18.   const range = e.range;
  19.   const validatedSheet = validation[sheet.getName()]
  20.  
  21.   if (validatedSheet) {
  22.     const columns = validatedSheet.columns;
  23.     const currentColumn = range.getColumn();
  24.     const currentRow = range.getRow();
  25.     const max = Math.max(...columns);
  26.     const min = Math.min(...columns);
  27.     if (
  28.       //Column check
  29.       columns.includes(currentColumn) &&
  30.       //Row check
  31.       currentRow > validatedSheet.rowIsGreaterThen &&
  32.       //Make sure that we not make another drowdown after the last one.
  33.       currentColumn < max
  34.     ) {
  35.       //If you clear out the first dropdown, reset the rest.
  36.       if (!e.value && currentColumn == min) {
  37.         sheet.getRange(currentRow, min + 1, 1, (max - min) + 1)
  38.           .clearContent()
  39.           .clearDataValidations()
  40.       } else {
  41.         setDataValidation(e, columns)
  42.       }
  43.     }
  44.   }
  45. }
  46.  
  47. function setDataValidation(e, columns) {
  48.   const dropdownSheet = e.source.getSheetByName(dropDownRefSheet);
  49.   // Getting al the values from 1:1
  50.   const headers = dropdownSheet.getRange(1, 1, 1, dropdownSheet.getLastColumn()).getValues().flat();
  51.   //Get the column index from the value selected in the dropdown.
  52.   const selectedHeaderIndex = headers.indexOf(e.value);
  53.   //Get the right range for the validation rule.
  54.   const validationRange = dropdownSheet.getRange(2, selectedHeaderIndex + 1, dropdownSheet.getLastRow());
  55.   //Create validation rule based on range.
  56.   const validationRule = SpreadsheetApp.newDataValidation().requireValueInRange(validationRange).build();
  57.  
  58.   //Setting the next dropdown
  59.   e.range.offset(0, 1).clearContent().clearDataValidations();
  60.   e.range.offset(0, 1).setDataValidation(validationRule);
  61.  
  62.   /*
  63.     In this example there are 3 columns. Let say i have them all 3 filled in and i change the main category.
  64.     Then i want that i set the sub offet 1 and clear all the validations and values in all the columns after offset 1
  65.   */
  66.   const max = Math.max(...columns);
  67.   const currentColumn = e.range.getColumn()
  68.   const offsetTwo = currentColumn + 2
  69.  
  70.   if (offsetTwo <= max) {
  71.     e.source.getActiveSheet()
  72.       .getRange(e.range.getRow(), offsetTwo, (max - offsetTwo) + 1)
  73.       .clearContent()
  74.       .clearDataValidations()
  75.   }
  76.  
  77. }
  78.  
  79.  
  80.  
Add Comment
Please, Sign In to add comment