Skip to content

Instantly share code, notes, and snippets.

@gtracy
Created December 22, 2019 15:41
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 gtracy/7b4d7f9241986cb96b0a2ce6ff36f215 to your computer and use it in GitHub Desktop.
Save gtracy/7b4d7f9241986cb96b0a2ce6ff36f215 to your computer and use it in GitHub Desktop.
Google Drive API
const config = require('../config');
const {google} = require('googleapis');
const Google = function() {
var self = this;
this.sheets = google.sheets({
version: 'v4',
auth: config.googleAPI.key
});
};
/**
* Fetch a list of data spreadsheets
*
*/
Google.prototype.getSheetData = function(callback) {
this.sheets.spreadsheets.values.get({
spreadsheetId: config.sheet.id,
range: config.sheet.range,
},(err, res) => {
if (err) {
console.error('The API returned an error.');
throw err;
}
const rows = res.data.values;
if (rows.length === 0) {
console.log('No data found.');
callback(undefined);
} else {
// build up an object for each row
var totals = [];
for (const row of rows) {
// push in a custom set of object properties
totals.push({
name : `${row[0]}`,
start_date : `${row[1]}`
});
}
callback(totals);
}
});
};
module.exports = Google;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment