Gists
This is a sample script for retrieving values by header title for Spreadsheet. This is created by Google Apps Script. The main script is as follows.
Main script :
When the instance is retrieved, all data of the sheet is analyzed. So when the each value is retrieved, the speed is fast.
function GetValueByKey(sheetname) {
return new getValueByKey(sheetname);
};
(function(r) {
var getValueByKey;
getValueByKey = (function() {
getValueByKey.name = "getValueByKey";
function getValueByKey(sheetname) {
var alldata, e, header, ss;
try {
ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetname);
} catch (error) {
e = error;
throw new Error("Error: No sheetname (" + sheetname + ").");
}
alldata = ss.getDataRange().getValues();
header = alldata[0];
alldata.shift();
this.manageddata = (function(header, alldata) {
var i, j, k, key, len, len1, result, temp, value;
result = [];
for (j = 0, len = alldata.length; j < len; j++) {
value = alldata[j];
temp = {};
for (i = k = 0, len1 = header.length; k < len1; i = ++k) {
key = header[i];
temp[key] = value[i];
}
result.push(temp);
}
return {
values: result,
headerLength: header.length,
dataLength: alldata.length
};
})(header, alldata);
}
getValueByKey.prototype.getValue = function(index, key) {
return this.manageddata.values[index][key];
};
getValueByKey.prototype.getAllValues = function() {
return this.manageddata;
};
return getValueByKey;
})();
return r.getValueByKey = getValueByKey;
})(this);
Demo
Following sheet is a sample sheet for this.