Skip to content

Instantly share code, notes, and snippets.

@nikushi
Created June 17, 2014 09:10
Show Gist options
  • Save nikushi/9b7dd61f3720ca728c17 to your computer and use it in GitHub Desktop.
Save nikushi/9b7dd61f3720ca728c17 to your computer and use it in GitHub Desktop.
gas utils
/**
* returns keys located at top of spreadsheet
*
* @param {Sheet} sh Sheet class
* @return {Array} array of keys
*/
function headerKeys(sh) {
return sh.getRange(1,1,1, sh.getLastColumn()).getValues()[0];
}
/**
* Convert a row to key-value hash according to keys input parameter
*
* @param {Array} array
* @param {Array} keys
* @return {Hash} key-value mapped
*/
function rowToHash(array, keys) {
var hash = {}
array.forEach(function(value, i) {
hash[keys[i]] = value
})
return hash
}
/**
* returns the range of data that belong to the specified key.
*
* @param {sheet} sh
* @param {string} key in headers(e.g. in A1:1)
* @return {range} range
*/
function getRangeBelongToKey(sh, key) {
var keys = headerKeys(sh);
var column_num = keys.indexOf(key)+1;
Logger.log(column_num + " " + key);
return sh.getRange(2, column_num, sh.getLastRow()-1, 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment