@imaginary-maths/gsheeter
v1.0.1
Published
JS library for reading Google Sheets as JSON data
Downloads
1
Readme
gSheeter
Read Google Sheets as JSON data.
This is a thin wrapper around the google-spreadsheet npm module with simplified (Promise based) usage and instructions to cover our most common cases.
Usage
const GSheeter = require('@imaginary-maths/gsheeter');
GSheeter.get(
spreadsheetID,
worksheetID
).then((data) => {
// ...
});
or for authenticated access:
const GSheeter = require('@imaginary-maths/gsheeter');
const credentials = require('./client_secret.json');
GSheeter.get(
spreadsheetID,
worksheetID,
{
credentials,
}
).then((data) => {
// ...
});
where client_secret.json
has the service account credentials (see section "Protected Sheets" below).
Arguments:
- spreadsheetID: The first long alphanumeric string in the sheet's URL.
- worksheetID: The worksheet number, starting from 1.
Returns: An array of objects (one per row) with properties based on the columns (named after the text on the first row).
NOTE: The Google Spreadsheets API might add an id
property to returned rows, which will be
overriden if any of the columns is named id
.
Google Sheet format
The first row should contain column names. The names will be mangled if they include spaces, capitals or symbols. It's recommended to use compatible column names from the start (all lowercase alphanumeric characters).
If the sheet has a completely empty row any rows underneath it will not be read.
Feel free to use formatting, it won't affect read values.
You can use formulas in the sheet, but you'll only access the calculated values.
Accessing Google Spreadsheets
Public access (no credentials)
To open a Google Spreadsheet without authenticating it must be explicitly published
using the File > Publish to the web
menu option (in Google Sheets). It's not enough
to make the sheet publicly accessible through the regular sharing settings.
Protected sheets
To access protected (not public) sheets we can create a service account and give it access to the sheet. The procedure is as follows:
- Go to the Google Developers Console
- Select your project or create a new one (and then select it)
- Enable the Drive API for your project
- In the sidebar on the left, expand APIs & auth > APIs
- Search for "drive"
- Click on "Drive API"
- click the blue "Enable API" button
- Create a service account for your project
- In the sidebar on the left, expand APIs & auth > Credentials
- Click blue "Add credentials" button
- Select the "Service account" option
- Select "Furnish a new private key" checkbox
- Select the "JSON" key type option
- Click blue "Create" button
- your JSON key file is generated and downloaded to your machine (it is the only copy!)
- note your service account's email address (also available in the JSON key file)
- Share the doc (or docs) with your service account using the email noted above
After this use the account's email address to explicitly give it read access to the sheet through its Share button, as if this was the email address of a regular user you're sharing it with.
See the README file of the google-spreadsheet module repo for instructions for environments where a local file cannot be saved (like HEROKU).
IMPORTANT: Make sure to add the JSON credentials file to .gitignore
to avoid storing it in the repository.
More information
For more information on accessing Google Spreadsheets see the README file of the google-spreadsheet module repo.
License
Copyright (c) 2020 IMAGINARY gGmbH
Licensed under the MIT license. See the LICENSE file.