• This repository has been archived on 27/Jan/2022
  • Stars
    star
    184
  • Rank 209,187 (Top 5 %)
  • Language
    JavaScript
  • License
    The Unlicense
  • Created over 13 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Google Spreadsheet Data API for node.js & the browser

google-spreadsheets

Build Status Dependency Information Code Climate Test Coverage npm

A simple node.js library to read data from a Google Spreadsheet.

Quickstart

npm install google-spreadsheets --save
var GoogleSpreadsheets = require('google-spreadsheets');

// OPTIONAL: if you want to perform authenticated requests.
// You must install this dependency yourself if you need it.
var google = require('googleapis');

var oauth2Client = new google.auth.OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URL);
// Assuming you already obtained an OAuth2 token that has access to the correct scopes somehow...
oauth2Client.setCredentials({
	access_token: ACCESS_TOKEN,
	refresh_token: REFRESH_TOKEN
});

GoogleSpreadsheets({
	key: '<spreadsheet key>',
	auth: oauth2Client
}, function(err, spreadsheet) {
	spreadsheet.worksheets[0].cells({
		range: 'R1C1:R5C5'
	}, function(err, cells) {
		// Cells will contain a 2 dimensional array with all cell data in the
		// range requested.
	});
});

In browser

Build browser bundle with npm run build. Then include lib/spreadsheets.browser.min.js in your HTML:

<script src='http://url.to/spreadsheets.browser.min.js'></script>

Usage is same as above, module is available at window.Spreadsheets:

window.Spreadsheets(options, callback);

Authentication

By default, google-spreadsheets will attempt requests for a spreadsheet as an unauthenticated (anonymous) user. There are some caveats to this, which you should read about in the Anonymous Requests section below.

As shown in the example above, you can depend on Google's official googleapis module and provide it to google-spreadsheets. This will allow you to easily make OAuth2 or JWT authenticated requests. See the googleapis project for more detailed information about configuring authentication.

API

GoogleSpreadsheets(opts, callback)

Loads a Spreadsheet from the API. opts may contain the following:

  • key: (required) spreadsheet key
  • auth: (optional) authentication key from Google ClientLogin

GoogleSpreadsheets.rows(opts, callback)

Loads a set of rows for a specific Spreadsheet from the API. Note that this call is direct, you must supply all auth, spreadsheet and worksheet information.

opts:

  • key: (required) spreadsheet key
  • worksheet: (required) worksheet id. Can be a numeric index (starting from 1), or the proper string identifier for a worksheet.
  • start: (optional) starting index for returned results
  • num: (optional) number of results to return
  • auth: (optional) authentication key from Google ClientLogin
  • sq: (optional) structured query (not URL encoded) - https://developers.google.com/google-apps/spreadsheets/#sending_a_structured_query_for_rows

GoogleSpreadsheets.cells(opts, callback)

Loads a group of cells for a specific Spreadsheet from the API. Note that this call is direct, you must supply all auth, spreadsheet and worksheet information.

opts:

  • key: (required) spreadsheet key
  • worksheet: (required) worksheet id. Can be a numeric index (starting from 1), or the proper string identifier for a worksheet.
  • range: (optional) A range (in the format of R1C1) of cells to retrieve. e.g R15C2:R37C8. Range is inclusive.
  • auth: (optional) authentication key from Google ClientLogin

Spreadsheet object

Object returned from GoogleSpreadsheets() call. This object has the following properties:

  • title: title of Spreadsheet
  • updated: date Spreadsheet was last updated.
  • author: object containing name and email of author of Spreadsheet.
  • worksheets: Array of Worksheets contained in this spreadsheet.

Worksheet object

Represents a single worksheet contained in a Spreadsheet. Obtain this via Spreadsheet.worksheets.

A Worksheet has the following properties:

  • rowCount: number of rows in worksheet.
  • colCount: number of columns in worksheet.
  • Worksheet.rows(opts, cb): convenience method to call Spreadsheets.rows, just pass in start and num - will automatically pass spreadsheet key, worksheet id, and auth info (if applicable)
  • Worksheet.cols(opts, cb): convenience method to call Spreadsheets.cols, will automatically pass spreadsheet key, worksheet id, and auth info (if applicable). opts can contain range, etc.

Anonymous Requests

As mentioned earlier, google-spreadsheets defaults to issuing anonymous requests to the API. This will only work for reading Google Spreadsheets that have had link sharing enabled for "Anyone on the internet", and have been published to the web.

Furthermore, the Google Spreadsheets Data API reference and developers guide is a little ambiguous about how you access a "published" public Spreadsheet.

If you wish to work with a Google Spreadsheet without authenticating, the Spreadsheet in question must be published using the "Publish to the web..." feature in the File menu in the Google Spreadsheets GUI. Use the setting "Entire Document" to share all tabs, or the name of an individual Sheet to publish only that Sheet. You can publish multiple Sheets one at a time. If you publish only some of the Sheets in your Google Spreadsheet the spreadsheet.worksheets array will only count published Sheets. Use the "Web page" setting.

You may discover that a particular public spreadsheet you're trying to anonymously read may not have had this treatment, so your best bet is to issue authenticated requests for the spreadsheet (or contact the owner and ask them to fix their spreadsheet).

Further possibilities for this library

  • Edit functionality
  • Sorting/filtering on row listing
  • Filtering on cell listing.

Links

License

node-google-spreadsheets is free and unencumbered public domain software. For more information, see the accompanying UNLICENSE file.

More Repositories

1

node-stream-buffer

Readable and Writable Streams that use backing Buffers.
JavaScript
230
star
2

logstash-output-kinesis

A Kinesis output plugin for Logstash that uses KPL.
Ruby
59
star
3

node-fastcgi-stream

FastCGI protocol implementation.
JavaScript
28
star
4

node-stream-splitter

Stream splitting. Like a boss.
CoffeeScript
25
star
5

rmarsh

Ruby Marshal 4.8 encoder/decoder in Golang. Why? Who knows.
Go
15
star
6

node-fastcgi-application

Run Node under a FCGI process manager.
JavaScript
12
star
7

home-cluster

HCL
10
star
8

node-etcd-leader

Etcd leader election implementation for Node.js
JavaScript
8
star
9

deathbycaptcha.js

DeathByCaptcha for Node.js
CoffeeScript
7
star
10

srs.js

Javascript implementation of Sender Rewriting Scheme.
JavaScript
7
star
11

oxideboy

Yet another Gameboy emulator written in Rust.
Rust
5
star
12

node-clusterphone

Node.js cluster messaging made easy.
JavaScript
5
star
13

node-debian-control-parser

Debian control data parser.
CoffeeScript
5
star
14

NodeServ

Pure-node web server.
JavaScript
5
star
15

gud-gadget

Rust
5
star
16

phrog

🐸 Mobile device greeter
C
5
star
17

node-traceurified-module

JavaScript
4
star
18

jnntp

NNTP client for Java (using Netty).
Java
4
star
19

javastorm

Netstorm bot: Botsy source + JavaStorm (incomplete rewrite of NS in Java... no idea why)
Java
4
star
20

go-dash-javadocset

Go implementation of Kapeli/javadocset
Go
3
star
21

appdb

CoffeeScript
3
star
22

node-duplex-stream

Composable Stream abstraction.
JavaScript
3
star
23

asyncevents

Extension to EventEmitter to facilitate asynchronous firing of events, and asynchronous handling of those events.
JavaScript
3
star
24

heroku-buildpack-brunch

Buildpack for apps that use Brunch.io
3
star
25

site2go

Java
2
star
26

citycycledata

JavaScript
2
star
27

gwt-mce

TinyMCE API bindings for GWT.
Java
2
star
28

socketio-cluster

Node cluster support for Socket.IO 1.0
JavaScript
2
star
29

cricos_scrape

CRICOS (http://cricos.deewr.gov.au/) database scraper using Scrapy.
Python
2
star
30

csgo-rank-sniffer

Sniff player ranks out of a demo file
Go
2
star
31

workstation-config

Dockerfile
2
star
32

lk-msm8916

C
2
star
33

archlinux-msm8916

Shell
2
star
34

icondb

C
1
star
35

hcloud-fip-k8s

Go
1
star
36

personal-infrastructure

1
star
37

cork-blog-jekyll

Jekyll-like blog handler for Cork
CoffeeScript
1
star
38

samcday.github.io

CSS
1
star
39

freshmine

Redmine -> Freshbooks integration
Python
1
star
40

gwt-stickywidget

Java
1
star
41

cork

Pluggable static site generator, powered by Node.js
CoffeeScript
1
star
42

cork-content-markdown

Markdown content plugin for Cork.
CoffeeScript
1
star
43

example-cork

Example site using Cork.
CoffeeScript
1
star
44

bincrawl

JavaScript
1
star
45

cork-layout-coffeekup

CoffeeKup layouts for Cork.
CoffeeScript
1
star
46

hotpotato

Hand-off HTTP connections between Node cluster workers.
JavaScript
1
star
47

node-powerdns

PowerDNS REST client for Node.js
JavaScript
1
star
48

gwt-functionproxy

Pass GWT Java callbacks to Javascript with ease.
Java
1
star
49

usb-kvm

Rust
1
star
50

grails-binders

Custom data binders made easy!
Groovy
1
star
51

mailbutler

Personal email proxy.
Ruby
1
star
52

couchdb-json-logs

Raw CouchDB log lines go in. JSON log records go out.
JavaScript
1
star
53

elasticsearch-oomonster

Have you checked your closet for the OOMonster lately?
Java
1
star
54

cork-assets-less

LESS assets for Cork.
CoffeeScript
1
star
55

samcday.com.au-cork

samcday.com.au - Cork powered
CoffeeScript
1
star