SC5 / google-sheets-api

Unofficial Google Sheets node API
MIT License
21 stars 16 forks source link

google-sheets-api

Build Status

An unofficial client for reading data from Google Sheets, since googleapis does not come with one.

Table of contents

Usage

  1. Install module

    npm install google-sheets-api
  2. Create a project in Google Developer Console, for example: "Sheets App"

  3. Enable Drive API for project under APIs & auth > APIs

  4. Create service auth credentials for project under APIs & auth > Credentials > Create new Client ID: Service account

  5. Collect the listed service email address

  6. Regenerate and download the P12 key

  7. Convert the .p12 file into .pem format:

    openssl pkcs12 -in *.p12 -nodes -nocerts > sheets.pem

    when prompted for password, it's notasecret

  8. Share the Sheets document to service email address using the Share button

  9. Pick up the Sheets document id from URL or Share dialog. Example:

    # Sheets document browser URL
    https://docs.google.com/a/sc5.io/spreadsheets/d/1FHa0vyPxXj3BtqigQ3LcwPoa7ldlRtUDx6fFV6CqkNE/edit#gid=0
    # Sheets document id
    1FHa0vyPxXj3BtqigQ3LcwPoa7ldlRtUDx6fFV6CqkNE
  10. Put it all together:

    var fs = require('fs');
    var Promise = require('polyfill-promise');
    var Sheets = require('google-sheets-api').Sheets;
    
    // TODO: Replace these values with yours
    var documentId = 'generated-by-sheets';
    var serviceEmail = 'generated-by-dev-console@developer.gserviceaccount.com';
    var serviceKey = fs.readFileSync('path/to/your/sheets.pem').toString();
    
    var sheets = new Sheets({ email: serviceEmail, key: serviceKey });
    
    sheets.getSheets(documentId)
    .then(function(sheetsInfo) {
      // NOTE: Using first sheet in this example
      var sheetInfo = sheetsInfo[0];
      return Promise.all([
        sheets.getSheet(documentId, sheetInfo.id),
        sheets.getRange(documentId, sheetInfo.id, 'A1:C3')
      ]);
    })
    .then(function(sheets) {
      console.log('Sheets metadata:', sheets[0]);
      console.log('Sheets contents:', sheets[1]);
    })
    .catch(function(err){
      console.error(err, 'Failed to read Sheets document');
    });
  11. Success!

API

Relevant API methods, see code for details and internal ones.

NOTE: All the methods returns a native (polyfilled when needed) Promise.

Sheets(options)

Initialize Sheets client with provided options

sheets.getSheets(id, sheetId)

Fetch info from one sheet

sheets.getSheet(id, sheetId)

Fetch info from one sheet

sheets.getRange(id, sheetId, rangeInfo)

Retrieve cells data based on given range

NOTE:

sheets.getCells(id, sheetId)

Fetch cell contents from one worksheet

sheets.getRowAndColCount(data)

Get total count of rows and columns in a data array

sheets.paddedEmptyMatrix(data)

Generate a empty matrix from startRow,startCol

sheets.paddedDataMatrix(data, rangePattern)

Creates a full, padded data matrix

Changelog

License

Module is MIT -licensed

Credit

Module is backed by