Created
December 22, 2019 15:41
-
-
Save gtracy/7b4d7f9241986cb96b0a2ce6ff36f215 to your computer and use it in GitHub Desktop.
Google Drive API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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