Skip to content

Instantly share code, notes, and snippets.

@gentunian
Created March 27, 2020 17:36
Show Gist options
  • Save gentunian/17362eb0ad253a5160144595e1112804 to your computer and use it in GitHub Desktop.
Save gentunian/17362eb0ad253a5160144595e1112804 to your computer and use it in GitHub Desktop.
I think we should change the name :)
/**
* Usage:
* var foo = new SpreadShitter({
* sheetId: <this id is in the spreadsheet url>,
* googleApiKey: <any google api key>});
* var sheetName = "My sheet";
* var range ="!A1:A2";
* foo.getValue(sheetName, range, function(data, errorEvent) {
* // handle logix here...
* })
* @type {SpreadShitter}
*/
window.SpreadShitter = (function() {
function SpreadShitter(options) {
this.googleSheetsApiUrl = "https://sheets.googleapis.com/v4/spreadsheets";
this.sheetId = options.sheetId;
this.googleApiKey = options.googleApiKey;
this.getValue = function(sheetName, range, callback) {
var request = new XMLHttpRequest();
request.addEventListener("load", function(e) {
callback.call(this, this.responseText);
});
request.addEventListener("error", function(e) {
callback.call(this, undefined, e);
});
var url = new URL([
this.googleSheetsApiUrl,
this.sheetId,
"values",
sheetName + range
].join("/"));
url.searchParams.append("key", this.googleApiKey);
request.open("GET", url.href);
request.send();
}
}
return SpreadShitter
}());
@gentunian
Copy link
Author

gentunian commented Mar 27, 2020

Usage

function processData(data, err) {
  if (!err) {
    var result = JSON.parse(data);
    console.log(result.values.flat().reduce(function(acc, cur) { return acc + parseInt(cur); }, 0))
  }
}
var gss = new SpreadShitter({ sheetId: "1a7zu7v8fGouRyeKFbBxkZEManAzuelY4NeqMKeiu1ZA", googleApiKey: "AIzaSyB-vjmMDe97fpEjWYEcMbzEez61zrLeDRE"});
gss.getValue("Respuestas de formulario 1", "!D2:D", processData);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment