Skip to content

Instantly share code, notes, and snippets.

@henriquebastos
Created June 27, 2019 18:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save henriquebastos/f87108544b0c240487da78e0ea2b8a28 to your computer and use it in GitHub Desktop.
Save henriquebastos/f87108544b0c240487da78e0ea2b8a28 to your computer and use it in GitHub Desktop.
Google Spreadsheet Data Validation by Reference
/* Google Spreadsheet Data Validation by Reference
Author: Henrique Bastos <henrique@bastos.net>
License: MIT
*/
function onEdit(e) {
var rule = e.range.getDataValidation();
if (rule == null) {
return;
}
var value = e.range.getCell(1,1).getValue();
var criteria = rule.getCriteriaType();
var validationRange = rule.getCriteriaValues()[0];
var i = findInRange(value, validationRange.getValues());
var ref = validationRange.getCell(1,1).offset(i, 0).getA1Notation();
e.range.getCell(1,1).setFormula("=" + ref);
}
function findInRange(value, tableValues){
for (var i = 0; i < tableValues.length; i++) {
if(tableValues[i][0] == value) {
return i;
}
}
return -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment