Created
June 27, 2019 18:07
-
-
Save henriquebastos/f87108544b0c240487da78e0ea2b8a28 to your computer and use it in GitHub Desktop.
Google Spreadsheet Data Validation by Reference
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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