Last active
September 18, 2022 05:21
-
-
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)
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
// $ 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(',')) | |
} | |
}()) |
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
Example to fetch rows from https://docs.google.com/spreadsheets/d/1xPs4Ht-3PVBGgdNSxyN3p2F5TtOifgvunrEvYaiu_A4
where
SHEET_ID
is1xPs4Ht-3PVBGgdNSxyN3p2F5TtOifgvunrEvYaiu_A4