Git Product home page Git Product logo

node-edit-google-spreadsheet's Introduction

Node - Edit Google Spreadsheet

Currently, there are about 3 different node modules which allow you to read data off Google Spreadsheets, though none with a good write API. Enter edit-google-spreadsheet. A simple API for reading and updating Google Spreadsheets.

Install

npm install edit-google-spreadsheet

Basic Usage

Create sheet with client login:

  var Spreadsheet = require('edit-google-spreadsheet');

  Spreadsheet.create({
    debug: true,
    username: '...',
    password: '...',
    spreadsheetName: 'node-edit-spreadsheet',
    worksheetName: 'Sheet1',
    callback: sheetReady
  });

Note: Using the options spreadsheetName and worksheetName will cause lookups for spreadsheetId and worksheetId. Use spreadsheetId and worksheetId for improved performance.

Create sheet with OAuth:

  var Spreadsheet = require('edit-google-spreadsheet');

  Spreadsheet.create({
    debug: true,
    oauth : {
      email: '[email protected]',
      keyFile: 'private-key.pem'
    },
    spreadsheetName: 'node-edit-spreadsheet',
    worksheetName: 'Sheet1',
    callback: sheetReady
  });

Update sheet:

  function sheetReady(err, spreadsheet) {
    if(err) throw err;

    spreadsheet.add({ 3: { 5: "hello!" } });

    spreadsheet.send(function(err) {
      if(err) throw err;
      console.log("Updated Cell at row 3, column 5 to 'hello!'");
    });
  }

Read sheet:

  function sheetReady(err, spreadsheet) {
    if(err) throw err;

    spreadsheet.receive(function(err, rows, info) {
      if(err) throw err;
      console.log("Found rows:", rows);
      // Found rows: { '3': { '5': 'hello!' } }
    });

  }

Metadata

Get metadata

  function sheetReady(err, spreadsheet) {
    if(err) throw err;
    
    spreadsheet.metadata(function(err, metadata){
      if(err) throw err;
      console.log(metadata);
      // { title: 'Sheet3', rowCount: '100', colCount: '20', updated: [Date] }
    });
  }

Set metadata

  function sheetReady(err, spreadsheet) {
    if(err) throw err;
    
    spreadsheet.metadata({
      title: 'Sheet2'
      rowCount: 100,
      colCount: 20
    }, function(err, metadata){
      if(err) throw err;
      console.log(metadata);
    });
  }

WARNING: all cells outside the range of the new size will be silently deleted

More add Examples

Batch edit:

spreadsheet.add([[1,2,3],
                 [4,5,6]]);

Batch edit starting from row 5:

spreadsheet.add({
  5: [[1,2,3],
      [4,5,6]]
});

Batch edit starting from row 5, column 7:

spreadsheet.add({
  5: {
    7: [[1,2,3],
        [4,5,6]]
  }
});

Formula building with named cell references:

spreadsheet.add({
  3: {
    4: { name: "a", val: 42 }, //'42' though tagged as "a"
    5: { name: "b", val: 21 }, //'21' though tagged as "b"
    6: "={{ a }}+{{ b }}"      //forumla adding row3,col4 with row3,col5 => '=D3+E3'
  }
});

Note: cell a and b are looked up on send()

API

Spreadsheet.create( options )

See Options below

spreadsheet.add( obj | array )

Add cells to the batch. See examples.

spreadsheet.send( [options,] callback( err ) )

Sends off the batch of add()ed cells. Clears all cells once complete.

options.autoSize When required, increase the worksheet size (rows and columns) in order to fit the batch (default false).

spreadsheet.receive( callback( err , rows , info ) )

Recieves the entire spreadsheet. The rows object is an object in the same format as the cells you add(), so add(rows) will be valid. The info object looks like:

{
  spreadsheetId: 'ttFmrFPIipJimDQYSFyhwTg',
  worksheetId: 'od6',
  worksheetTitle: 'Sheet1',
  worksheetUpdated: '2013-05-31T11:38:11.116Z',
  authors: [ { name: 'jpillora', email: '[email protected]' } ],
  totalCells: 1,
  totalRows: 1,
  lastRow: 3,
  nextRow: 4
}
spreadsheet.metadata( [data, ] callback )

Get and set metadata

Note: when setting new metadata, if rowCount and/or colCount is left out, an extra request will be made to retrieve the missing data.

Options

callback

Function returning the authenticated Spreadsheet instance.

debug

If true, will display colourful console logs outputing current actions.

username password

Google account - Be careful about committing these to public repos.

oauth

OAuth configuration object. See google-oauth-jwt. By default oauth.scopes is set to ['https://spreadsheets.google.com/feeds'] (https if useHTTPS)

spreadSheetName spreadsheetId

The spreadsheet you wish to edit. Either the Name or Id is required.

workSheetName worksheetId

The worksheet you wish to edit. Either the Name or Id is required.

useHTTPS

Whether to use https when connecting to Google (default: true)

Todo

  • Create New Spreadsheets
  • Read specific range of cells
  • Option to cache auth token in file

FAQ

  • Q: How do I append rows to my spreadsheet ?
  • A: Using the info object returned from receive(), one could always begin add()ing at the nextRow, thereby appending to the spreadsheet.

Credits

Thanks to googleclientlogin for easy Google API ClientLogin Tokens

References

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.