Download Google sheets as CSVs using a service account.
- Create a Google service account
- Share your spreadsheet with that account
- Provide credentials from that account to
gsdownload
along with a sheet ID and it will download each worksheet as a CSV file to a location you specify
Be careful not to leak your private key. Provide it as an environment variable/ secret and don't commit it to your repository
This mainly developed for use in a CI context so...
To use gsdownload in a CI context (like Github actions for example) you need to
- set
GOOGLE_CLIENT_EMAIL
andGOOGLE_PRIVATE_KEY
as environment variables / secrets for github actions you can do this in the repo Settings->Secrets->Actions i.e.https://github.com/<username/orgname>/<reponame>/settings/secrets/actions
- make sure your task runner can install & run node scripts and in your CI workflow globally install gsdownload
npm i -g @tomgp/gsdownload
- in your CI workflow add the command
getsheet <sheetID> <output_directory>
and the worksheet CSVs should hopefully appear inoutput_directory
const { getSheet } = require('./index.js');
getSheet( <Spreadsheet_ID>, <Google_service_account_email_address>, <Service_account_private_key>, <Output_directory>);