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

sheetfig

v0.1.0

Published

Export Google Sheets data to JSON config files

Downloads

1

Readme

Sheetfig

Store your data in a Google Sheet and download it to JSON. What could possibly go wrong?

Overview

Sheetfig is a command line utility to intelligently download data from a Google spreadsheet to local JSON files. This lets you use Google Sheets as a mini CMS and then export it into JSON config files. The rows of a sheet become objects in an array with the columns as its properties.

It uses spreadsheet-to-json under the hood to hit the Google Drive API, but adds additional niceties to determine what data gets exported:

  • Name object properties based on column headers
  • Save data as primitives, arrays or nested objects
  • Include rows and columns based on checkboxes or CLI options
  • Save multiple worksheets using its (tab) names as filenames

Usage

  • Install globally via npm -g i sheetfig.
  • Create a new Google Sheet.
  • To download data from a Google sheet, you will need to create a Google service account under a Google Cloud project with the Google Sheets API added. This is possible under the free access tier.
  • At the end of the service account creation process, create a private key of the type JSON and download it. The client_email and private_key properties from this key file will be needed as credentials to download your Google Sheet data. Sheetfig can accept them via CLI options, a local .env file or the GOOGLE_CLIENT_EMAIL and GOOGLE_PRIVATE_KEY environment variables.
  • Share access to the sheet for the client_email (Viewer access is sufficient).
  • Add a few columns and rows of data to the sheet and use the first row as your field names. Rename the worksheet (tab) from Sheet1 to test.json.
  • Run sheetfig download -s SHEETID, where SHEETID is the long random number in the Google Sheet URL after spreadsheets/d/ and before /edit. It will likely start with a 1. You can also specify it as SHEET_ID in the .env file or as an environment variable with the same name.
  • You should now have a test.json file in your current directory.

Run sheetfig --help for more options.

The export process ignores formatting. If a cell contains a formula, the results of that formula will be exported.

Note that certain usage limits apply to how frequent you can use the Google Sheets API.

Customising the export process

By default, sheetfig will download every row and every column from a sheet into an array of objects, using the value of a column's first row for the property names (AKA the 'fields'). Checkboxes become booleans, integers and floats are parsed accordingly and everything else exports as a string. The worksheet name is used as the filename, with each worksheet exporting to a separate file.

You can pass in additional CLI options to determine which columns or rows to export:

  • Pass in a comma-separated list of fields via -x or --exclude-fields and -i or --include-fields to exclude or only include specific fields.
  • Pass in a range of line numbers via -r or --ignore-rows to not export them.
  • Specify a row number via -n or --include-instructions with a range of checkboxes to only include columns with a checked checkboxexample).

Arrays and objects can also produced based on the field names used in the header row:

  • Add a period to the field name and data from that column will be nested. Two columns called name.first and name.last produce a name object of { first: ..., last: ... }.
  • An underscore followed by a number adds the cell data under that position in an array (lang_0 becomes land[0]).

Example

This sample sheet listing the planets in our solar system contains a notes field that we don't want to export and includes headers to create a moons array and a nested status property. It also has a more 'friendly' row of headers in line 3 including a dynamic cell in A3 that counts the number of planets listed:

Google Sheet with stats on planets

Running sheetfig download -s 1ar2hlURAhuvtwZEDnNRzn-3WdRepRSjC-x2vII_FnCY -n 2 -r 3 would use row 2 to determine what columns to export and ignore row 3, to produce a JSON file that starts/end like this:

[
  {
    "name": "Mercury",
    "diameter": 4879,
    "distanceFromSun": 57.9
  },
  ...
  {
    "name": "Pluto",
    "moonCount": 5,
    "diameter": 2370,
    "distanceFromSun": 5906.4,
    "moons": [
      "Charon",
      "Nix"
    ],
    "status": {
      "dwarfPlanet": true
    }
  }
]

At this point, you could hide the top two rows in the sheet to eliminate the cognitive overhead of the extra config data. You could also remove row 2 entirely and run sheetfig download -s 1ar2hlURAhuvtwZEDnNRzn-3WdRepRSjC-x2vII_FnCY -x notes -r 2 to explicitly exclude the notes field via a CLI option.

Links

- Joost Schuur