Skip to content

Instantly share code, notes, and snippets.

@lchski
Created May 31, 2018 18:20
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 lchski/51df816de9f1b6fcc853545c33771100 to your computer and use it in GitHub Desktop.
Save lchski/51df816de9f1b6fcc853545c33771100 to your computer and use it in GitHub Desktop.
A set of helpers I use for Google Sheets.
/**
* 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