npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@imaginary-maths/gsheeter

v1.0.1

Published

JS library for reading Google Sheets as JSON data

Downloads

6

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:

  1. Go to the Google Developers Console
  2. Select your project or create a new one (and then select it)
  3. 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
  1. 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)
  1. 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.