Skip to content

Instantly share code, notes, and snippets.

@henriquebastos
Created July 26, 2019 02:05
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/2ab002071f35a41afb38b9ec9a0b5065 to your computer and use it in GitHub Desktop.
Save henriquebastos/2ab002071f35a41afb38b9ec9a0b5065 to your computer and use it in GitHub Desktop.
Dynamic Validator by Reference for Google Spreadsheets
function onEdit(e) {
var rule = e.range.getDataValidation();
if (rule != null) {
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