Git Product home page Git Product logo

spreadsheet-sql's Introduction

CircleCI

spreadsheet-sql

Getting Google spreadsheet data by using like SQL.

Outline

This node module can extract user data from google spreadsheet. And you can use SQL like syntax. e.g: SELECT * WHERE A = "user1". Of course we support private spreadsheet data also.

Install

$ npm install spreadsheet-sql

Google Spreadsheet Query Language Specification

You can use the syntax like SQL according to google spreadsheet query language specification.

Examples

Public Spreadsheet

var PublicSpreadsheet = require('spreadsheet-sql').PublicSpreadsheet;

// first argument is spreadsheet key, second argument is worksheet name.
// spreadsheet key is included spreadsheet URL.
// e.g: "https://docs.google.com/spreadsheets/d/SPREADSHEET_KEY"
var spreadsheet = new PublicSpreadsheet('SPREADSHEET_KEY', 'WORKSHEET_NAME');
return spreadsheet.query('SELECT * WHERE A = "user1"')
  .then(result => {
    console.log(result);
  });

Executing above snippet, you can get json format result.

[{
  "column1": "user1",
  "column2": "John",
  "column3": "Smith"
}]

Private Spreadsheet

For private spreadsheet you need to get google OAuth 2.0 credentials before using this module. Please see Using OAuth 2.0 to Access Google APIs page for details. In almost cases you can generate new credentials in API console. After generating new OAuth 2.0 credential you usually have 3 credential values.

  1. Client ID
  2. Client Secret
  3. Redirect URN

And also you can get manually Oauth 2.0 refresh token optionally. You need to give above 3 values to the constructor of PrivateSpreadSheet. From v0.3.0 refresh token became to be optional.

var PrivateSpreadsheet = require('spreadsheet-sql').PrivateSpreadsheet;

var spreadsheet = new PrivateSpreadsheet(
  'SPREADSHEET_KEY',
  'WORKSHEET_NAME',
  'CLIENT_ID',
  'CLIENT_SECRET',
  'REDIRECT_URN',
  'REFRESH_TOKEN' // refresh token is optional
);
return spreadsheet.query('SELECT * WHERE A = "user1"')
  .then(result => {
    console.log(result);
  });

About Handling Expiry of Access Token and Refresh Token

As we mentioned refresh token is optional from v0.3.0. Because googleapis/google-auth-library-nodejs module can handle access token expiry.

So when you prefer use this module for getting spreadsheet data just only once, we recommend you to use without refresh token. Though when you need to get spreadsheet data repeatedly, you should use this module with refresh token.

It means googleapis/google-auth-library-nodejs tries to refresh access token when the access token is over expiry. If you would like to understand in code level, please check here.

Declaring Columns Like RDB

This module assumes first row as headers. So you have to create following structure on spreadsheet.

username last_name first_name
user1 John Smith
user2 John Smith

Test

We use ava test framework.

npm test

Build

We use webpack + ts-loader.

npm run build

License

WTFPL. haha!

spreadsheet-sql's People

Contributors

jupemara avatar diogenes-moreira 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.