A Google Sheets API v4 implementation that abstracts authorization.
npm install sheets-apibash
npm i sheets-api --save
`
`javascript
// sheets-client.js
const SheetsAPI = require('sheets-api');
const sheets = new SheetsAPI();
const SPREADSHEET_ID = "19uTpwB-PM9TtUGUCRMdgWdkH0pqEHVv3J6sjzCNoMRM";
// ValueRange(https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values#ValueRange)
let payload = {
spreadsheetId: SPREADSHEET_ID,
range: "Orders!A1:D1",
valueInputOption: 'USER_ENTERED',
resource : {
majorDimension: "ROWS",
values: [
["Door", "$15", "2", "3/15/2017"],
["Engine", "$100", "1", "3/20/2016"]
]
}
}
sheets
// Get me an authorized OAuth2 client thats ready to make requests.
.authorize()
// https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/append
// Using the spreadsheets.values collection, use the 'append' method to
// append data (payload) to a spreadsheet.
.then(auth => sheets.values('append', auth, payload))
// 'response' is an object that contains the response from the request to the
// API (append) and the OAuth2 client to be chained further. It looks like this:
// {auth:auth, response:response}
.then(response => sheets.values('get', response.auth, {
spreadsheetId: SPREADSHEET_ID,
range: 'Orders!A1:D1'
}))
// The results of the request to spreadsheets.values.get collection.
.then(response => {
console.log(response.response);
})
// Derp
.catch(e => console.error(e))
``