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

grunt-gss-to-json

v0.1.4

Published

Grunt task that read a Google Spreadsheet and save as JSON.

Downloads

77

Readme

grunt-gss-to-json

Built with Grunt

Read Google Spreadsheet and save as JSON.

NPM

Using edit-google-spreadsheet grunt-gss-to-json read a Google Spreadsheet and saves it as a JSON-file, optionally as an array with each row transformed using a custom function.

Getting Started

This plugin requires Grunt ~0.4.5

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

npm install grunt-gss-to-json --save-dev

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

grunt.loadNpmTasks('grunt-gss-to-json');

The "gss_to_json" task

Overview

In your project's Gruntfile, add a section named gss_to_json to the data object passed into grunt.initConfig().

grunt.initConfig({
  gss_to_json: {
    dist: {
      options: {
        // edit-google-spreadsheet options go here
        debug: true,
        spreadsheetName: 'my-awesome-spreadsheet',
        worksheetName: 'Sheet1',
        // for better performance, use spreadsheetId and
        // worksheetId instead of name.
        spreadsheetId: '',
        worksheetId: '',
        // Choose from 1 of the 3 authentication methods:
        //    1. Username and Password
        username: '[email protected]',
        password: 'my-5uper-t0p-secret-password',
        // OR 2. OAuth
        oauth : {
          email: '[email protected]',
          keyFile: 'my-private-key.pem'
        },
        // OR 3. Token
        accessToken : {
          type: 'Bearer',
          token: 'my-generated-token'
        }

        // Specific grunt-gss-to-json options
        prettify: true,
        includeInfo: true,
        headerIsFirstRow: true
      },
      dest: 'file-to-write.json'
    }
  },
});

Please note that it is bad practice for security reasons to commit authentication credentials. See usage example below for an example of how to avoid that.

Options

Please see the edit-google-spreadsheet documentation for the authentication options passed along. This blog post have instructions of how to setup access to a Google Spreadsheet that is not shared publicly.

The grunt-gss-to-json specific options follow below.

options.prettify

Type: Boolean Default value: true

Save JSON prettified with indentation.

options.includeInfo

Type: Boolean Default value: true

If true the resulting file is an object with two properties; rows (the worksheet as JSON) and info containing spreadsheet info (total cells and rows, when last updated, and more, including spreadsheetId and worksheetId).

If false the resulting file will be the rows object as is.

options.headerIsFirstRow

Type: Boolean Default value: true

If true the first row is considered as a header and is excluded from the result.

options.transformRow

Type: Function Default value: null

The functions has two parameters, row and header. Both objects does look the same with property key being the column number and the value the cell content. Example:

{
  "1": "Pilotwings",
  "2": "SNES",
  "3": "SNSP-PW-SCN"
}

If a cell is empty that column is missing. See below for example usage.

Usage Examples

Default Options

In this example, oauth is used for authentication and each row is transformed into a key/value object where the key is the column header title lowercased, only including a-z.

grunt.initConfig({
  gss: grunt.file.readJSON('grunt-gss.json'),

  gss_to_json: {
    dev: {
      options: {
        spreadsheetId: "6RjQ5UX0mnKRXUb91dl6tDn5prczk2B600XuANWiHXjhw",
        worksheetId: "od6",
        oauth : {
          email: '<%= gss.oauth.email %>',
          keyFile: '<%= gss.oauth.keyFile %>'
        },

        includeInfo: true,

        transformRow: function(row, header) {
          var rowdata = {};
          Object.keys(row).forEach(function(col) {
            var key = header[col] ? header[col].toLowerCase().replace(/[^a-z]/g, "") : col;
            rowdata[key] = row[col];
          });
          return rowdata;
        }
      },
      dest: "games.json"
    }
  },});

Please note how authentication credentials are not included in Gruntfile.js but instead read from grunt-gss.json which should be in your .gitignore together with your keyfile to avoid committing sensitive information.

Example grunt-gss.json

{
    "oath": {
        "email": "123456-lotsofcharactersmakingupyourid@developer.gserviceaccount.com",
        "keyFile": "./my-key-file.pem"
    }
}

Contributing

Pull requests welcome.