Skip to content

Instantly share code, notes, and snippets.

@mattash
Created April 8, 2014 22:53
Show Gist options
  • Save mattash/10205228 to your computer and use it in GitHub Desktop.
Save mattash/10205228 to your computer and use it in GitHub Desktop.
function doGet(request) {
var output = ContentService.createTextOutput();
var data = {};
var id = "ID";
var sheet = "SHEETNAME";
var ss = SpreadsheetApp.openById(id);
if (sheet) {
data = readData_(ss, sheet);
}
output.setContent(JSON.stringify(data));
output.setMimeType(ContentService.MimeType.JSON);
return output;
}
function readData_(ss, sheetname, properties) {
if (typeof properties == "undefined") {
properties = getHeaderRow_(ss, sheetname);
//properties = properties.map(function(p) { return p.(/\s+/g, '_'); });
}
var rows = getDataRows_(ss, sheetname);
var data = [];
for (var r = 0, l = rows.length; r < l; r++) {
var row = rows[r];
var record = {};
for (var p in properties) {
record[properties[p]] = convert_(row[p]);
}
data.push(record);
}
return data;
}
function convert_(value) {
if (value === "true") return true;
if (value === "false") return false;
return value;
}
function getDataRows_(ss, sheetname) {
var sh = ss.getSheetByName(sheetname);
return sh.getRange(2, 1, sh.getLastRow() - 1, sh.getLastColumn()).getValues();
}
function getHeaderRow_(ss, sheetname) {
var sh = ss.getSheetByName(sheetname);
return sh.getRange(1, 1, 1, sh.getLastColumn()).getValues()[0];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment