Skip to content

Instantly share code, notes, and snippets.

@micalevisk
Last active September 18, 2022 05:21
Show Gist options
  • Save micalevisk/9bc831bd4b3e5a3f62b9810330129c59 to your computer and use it in GitHub Desktop.
Save micalevisk/9bc831bd4b3e5a3f62b9810330129c59 to your computer and use it in GitHub Desktop.
Dead simple NodeJS script to read public Google Sheets as CSV. (just the first sheet)
// $ npm install -g google-spreadsheet
const { GoogleSpreadsheet } = require('google-spreadsheet')
const API_KEY = '<YOUR-SUPER-SECRET-API-KEY>' // See: https://developers.google.com/sheets/api/guides/authorizing#APIKey
const SHEET_ID = '<target-sheet-id>' // spreadsheet key is the long id in the sheets URL
const doc = new GoogleSpreadsheet(SHEET_ID)
doc.useApiKey(API_KEY)
;(async function (){
// https://theoephraim.github.io/node-google-spreadsheet/#/classes/google-spreadsheet-worksheet
await doc.loadInfo() // loads document properties and worksheets
//console.log('>>', doc.title)
// https://theoephraim.github.io/node-google-spreadsheet/#/classes/google-spreadsheet-worksheet?id=basic-sheet-properties
const sheet = doc.sheetsByIndex[0]
const rows = await sheet.getRows({ offset:0, /*limit:5*/ })
console.log('# ' + rows[0]._sheet.headerValues.join(','))
for (const row of rows) {
console.log(row._rawData.join(','))
}
}())
@micalevisk
Copy link
Author

micalevisk commented Apr 27, 2020

Example to fetch rows from https://docs.google.com/spreadsheets/d/1xPs4Ht-3PVBGgdNSxyN3p2F5TtOifgvunrEvYaiu_A4
where SHEET_ID is 1xPs4Ht-3PVBGgdNSxyN3p2F5TtOifgvunrEvYaiu_A4

demo

@micalevisk
Copy link
Author

for public sheets, this one is better: https://github.com/fureweb-com/public-google-sheets-parser

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