Created
May 31, 2018 18:20
-
-
Save lchski/51df816de9f1b6fcc853545c33771100 to your computer and use it in GitHub Desktop.
A set of helpers I use for Google Sheets.
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
/** | |
* Retrieves the value associated with the variable from the “Base Variables” sheet. | |
* | |
* @param {string} variableName The variable to retrieve. The string must be the same as in the “Variable” column of the “Base Variables” sheet. | |
* @return The input multiplied by 2. | |
* @customfunction | |
*/ | |
function getBaseVar(variableName) { | |
// Get variables. | |
var variableSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Base Variables"); | |
var variableKeyValuePairs = variableSheet.getRange('A2:B').getValues(); | |
// Remove empty variable pairs. | |
variableKeyValuePairs = variableKeyValuePairs.filter(function(keyValuePair) { | |
return (keyValuePair[0].length !== 0) && (keyValuePair[1].length !== 0); | |
}); | |
// Find variable pair. | |
variableKeyValuePairs = variableKeyValuePairs.filter(function(keyValuePair) { | |
return keyValuePair[0] === variableName; | |
}); | |
// Return: if there's nothing found, empty string, else return the found value. | |
if (variableKeyValuePairs.length === 0) { | |
return ''; | |
} else { | |
return variableKeyValuePairs[0][1]; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment