GithubHelp home page GithubHelp logo

newsroomdev / gsheets Goto Github PK

View Code? Open in Web Editor NEW

This project forked from interactivethings/gsheets

0.0 2.0 1.0 134 KB

Get public Google Sheets as plain JavaScript/JSON

License: BSD 3-Clause "New" or "Revised" License

Makefile 3.97% JavaScript 92.86% HTML 3.17%

gsheets's Introduction

gsheets

Get public Google Sheets as plain JavaScript/JSON.

npm install gsheets -g

Works in Node.js

var gsheets = require('gsheets');

gsheets.getWorksheet('1iOqNjB-mI15ZLly_9lqn1hCa6MinqPc_71RoKVyCFZs', 'foobar', function(err, res) {
  console.log(res);
});

the browser (use the pre-built gsheets.js or gsheets.min.js)

<script src="../gsheets.js"></script>
<script>
  gsheets.getWorksheet('1iOqNjB-mI15ZLly_9lqn1hCa6MinqPc_71RoKVyCFZs', 'foobar', function(err, res) {
    console.log(res);
  });
</script>

and on the Command Line.

gsheets --key=1iOqNjB-mI15ZLly_9lqn1hCa6MinqPc_71RoKVyCFZs --title=foobar --pretty

Features

  • Plain JS/JSON data. No 'models'. Just use .map, .filter etc.
  • Correct handling of numeric cells (no formatted strings for numbers!)
  • Empty cells are converted to null
  • A bit of metadata (i.e. when a spreadsheet was updated)
  • Empty rows are omitted
  • Correct handling of empty worksheets

Non-features

  • Authorization (only works with published spreadsheets)
  • Querying, ordering, updating
  • Caching. Use a reverse proxy or implement your own caching strategy. I recommend this strongly since Google's API isn't the fastest and you don't want to hit rate limits.

Caveats

  • Beware of cells formatted as dates! Their values will be returned as Excel-style DATEVALUE numbers (i.e. based on the number of days since January 1, 1900)

Why not use another library?

There are a few libraries around which allow you to access Google Spreadsheets, most notably Tabletop. However, they all have one or several drawbacks:

  • They wrap the output in classes or models with a custom API, whereas all we really need is an array of JS objects
  • Tabletop just logs errors to the console which makes proper error handling impossible
  • Incorrect handling of numeric cell values (you only get a formatted string instead of the actual number, e.g. "123'456.79" instead of 123456.789)

Node API

var gsheets = require('gsheets');

getSpreadsheet(spreadsheetKey, callback)

Returns information about a spreadsheet including a list of worksheets.

gsheets.getSpreadsheet('MY_KEY', function(err, res) {
  // ...
});

Example Response:

{
  "updated": "2014-11-19T10:20:18.068Z",
  "title": "My Awesome Spreadsheet",
  "worksheets": [
    {
      "id": "od6",
      "title": "foobar"
    },
    // more worksheets ...
  ]
}

getWorksheet(spreadsheetKey, worksheetTitle, callback)

Returns the contents of a worksheet, specified by its title. Note that this generates two requests (to resolve a worksheet's title). If you know a worksheet's ID (e.g. via a previous call to listWorksheets), use getWorksheetById

For empty worksheets data is null.

gsheets.getWorksheet('MY_KEY', 'foobar' function(err, res) {
  // ...
});

Example Response:

{
  "updated": "2014-11-19T10:20:18.068Z",
  "title": "foobar",
  "data": [
    {
      "foo": "bar",
      "baz": 42,
      "boing": null
    },
    // more rows ...
  ]
}

getWorksheetById(spreadsheetKey, worksheetId, callback)

Returns the contents of a worksheet, specified by its ID.

For empty worksheets data is null.

gsheets.getWorksheetById('MY_KEY', 'od6' function(err, res) {
  // ...
});

Example Response:

{
  "updated": "2014-11-19T10:20:18.068Z",
  "title": "foobar",
  "data": [
    {
      "foo": "bar",
      "baz": 42,
      "boing": null
    },
    // more rows ...
  ]
}

Command Line

Write spreadsheet contents to a file as JSON or DSV.

gsheets --key [--id] [--title] [--out] [--pretty] [--dsv]
  --key     Spreadsheet key; Outputs spreadsheet info if no other option is provided
  --out     Output file; defaults to /dev/stdout
  --id      Worksheet ID; use either this or --title to get worksheet contents
  --title   Worksheet title; use either this or --id to get worksheet contents
  --pretty  Pretty-print JSON
  --dsv     Format as delimiter-separated values
  --csv     Shortcut for --dsv=,
  --tsv     Shortcut for --dsv=$'\t'

Development

Run the tests with

npm test

Have a look at the test spreadsheet

Author

Jeremy Stucki, Interactive Things

License

BSD, see LICENSE

gsheets's People

Contributors

jstcki avatar tpreusse avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.