Created
January 8, 2020 15:18
-
-
Save boxmein/cbe877a0f7e86b80f94005a8015e6a73 to your computer and use it in GitHub Desktop.
This function appends a single row to Google Sheets and repeats itself every 100 milliseconds.
This file contains hidden or 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 googleapis = require('googleapis'); | |
// Setup: | |
// (Maybe it's possible to get Client ID, Client Secret, Spreadsheet ID from OAuth Playground? See the link:) | |
// https://developers.google.com/oauthplayground/ | |
// | |
// Otherwise: | |
// 1. Go to Google Cloud Platform Console > (create a project if needed) > APIs & Services > Library. | |
// Look for "Sheets" and enable the API. | |
// 2. Go to GCP Console > APIs & Services > OAuth consent screen. EDIT APP and set it to Internal, | |
// and allow the "../auth/drive.file" scope. | |
// 3. Go to GCP Console > APIs & Services > Credentials. Create new > OAuth client ID > Web application. | |
// 4. Copy the client ID & client secret, and save them to the environment (CLIENT_ID and CLIENT_SECRET). | |
// 5. Walk through the OAuth flow with that client ID and secret, get an auth code. Exchange it for a | |
// refresh token. This step requires coding or using cURL to fire at APIs. | |
// 6. Get a Spreadsheet and Sheet ID from the above stage. | |
const clientId = process.env.CLIENT_ID; | |
const clientSecret = process.env.CLIENT_SECRET; | |
const refreshToken = process.env.REFRESH_TOKEN; | |
const spreadsheetId = process.env.SPREADSHEET_ID; | |
const sheetId = process.env.SHEET_ID; | |
const oauth2Client = new googleapis.google.auth.OAuth2({ | |
clientId, | |
clientSecret, | |
}); | |
oauth2Client.setCredentials({ | |
refresh_token: refreshToken, | |
}); | |
const sheets = googleapis.google.sheets({ version: 'v4' }); | |
let counter = 0; | |
function makeAppendCellsRequest() { | |
counter += 1; | |
return { | |
appendCells: { | |
sheetId, | |
rows: [ | |
{ | |
values: [ | |
{ userEnteredValue: { stringValue: 'Hello ' + counter } }, | |
{ userEnteredValue: { stringValue: 'World' } }, | |
{ userEnteredValue: { stringValue: 'World' } }, | |
{ userEnteredValue: { stringValue: 'World' } }, | |
{ userEnteredValue: { stringValue: 'World' } }, | |
{ userEnteredValue: { stringValue: 'World' } }, | |
{ userEnteredValue: { stringValue: 'World' } }, | |
{ userEnteredValue: { stringValue: 'World' } }, | |
{ userEnteredValue: { stringValue: 'World' } }, | |
{ userEnteredValue: { stringValue: 'World' } }, | |
{ userEnteredValue: { stringValue: 'World' } }, | |
{ userEnteredValue: { stringValue: 'World' } }, | |
{ userEnteredValue: { stringValue: 'World' } }, | |
{ userEnteredValue: { stringValue: 'World' } }, | |
{ userEnteredValue: { stringValue: 'World' } }, | |
{ userEnteredValue: { stringValue: 'World' } }, | |
{ userEnteredValue: { stringValue: 'World' } }, | |
{ userEnteredValue: { stringValue: 'World' } }, | |
{ userEnteredValue: { stringValue: 'World' } }, | |
{ userEnteredValue: { stringValue: 'World' } }, | |
], | |
} | |
], | |
fields: '*', | |
}, | |
}; | |
} | |
async function appendRow() { | |
console.log('Appending row'); | |
const result = await sheets.spreadsheets.batchUpdate({ | |
spreadsheetId, | |
auth: oauth2Client, | |
resource: { | |
requests: [ | |
makeAppendCellsRequest(), | |
], | |
}, | |
}); | |
console.log('Appended row, result: ', result); | |
} | |
async function loopAppendRows() { | |
await appendRow(); | |
setTimeout(loopAppendRows, 100); | |
} | |
loopAppendRows(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment