Skip to content

Instantly share code, notes, and snippets.

@sschwartzman
Last active June 27, 2023 15:37
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sschwartzman/ed34dec66231acd9092051170d0ed6dc to your computer and use it in GitHub Desktop.
Save sschwartzman/ed34dec66231acd9092051170d0ed6dc to your computer and use it in GitHub Desktop.
Create a Multi-Select Drop-Down for any cell in Google Sheets
const separator = ', ';
const dvType = SpreadsheetApp.DataValidationCriteria.VALUE_IN_RANGE;
function onEdit(e) {
if (e.value != null && e.oldValue != null && e.value !== "") {
var dataValidation = e.range.getDataValidation();
if(dataValidation != null && dataValidation.getCriteriaType() == dvType &&
e.value.indexOf(separator) < 0 && e.oldValue.indexOf(e.value) < 0) {
e.range.setValue(e.oldValue + separator + e.value);
}
}
}
@sschwartzman
Copy link
Author

Based on https://spreadsheetpoint.com/multiple-selection-drop-down-google-sheets/

Improvements:

  • Works on any cell, only depends on which cell was edited (not on which spreadsheet or cell is currently active)
  • Only proceeds if the edit was on a single cell and that the cell wasn't blanked out (aka reset)
  • Checks for if the cell has "data validation" enabled for "list from a range"
  • Only proceeds if the new value itself isn't just an edit of the existing value
  • Uses a global 'separator' variable for easy modification

@aldrincab
Copy link

Hello sir, just a question. How should the code go if you only want this to work on a specific tab? Thank you

@is-rubes
Copy link

I got the following error

TypeError: Cannot read properties of undefined (reading 'value')
onEdit @ Code.gs:4

does this have something to do with validating e.value and e.oldValue? how do I do that?

@play150
Copy link

play150 commented Jun 27, 2023

I have the same error! :c

edit: i realized that it throws the error in the AppScript editor, but it's executing the script when I make edits in the spreadsheet (based on the execution history). However, it still doesn't seem to be doing anything on the sheet (the original script from spreadsheetpoint is working though).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment