google-spreadsheet

Google Sheets API (v4) wrapper for Node.js

README

google-spreadsheet

The most popular Google Sheets API wrapper for javascript

NPM version CircleCI Known Vulnerabilities NPM

- multiple auth options - service account (w/ optional impersonation), OAuth 2.0, API key (read-only)
- cell-based API - read, write, bulk-updates, formatting
- row-based API - read, update, delete (based on the old v3 row-based calls)
- managing worksheets - add, remove, resize, change title, formatting

Docs site -

🚨 Google Deprecation Warning - affects older version (v2) of this module 🚨

>

Google is phasing out their old v3 api, which the older version of this module used. Originally they were going to shut it down on March 3rd 2020, but have pushed that date back to June 2021.



Regardless, please upgrade to the latest version of this module (v3) which uses the newer sheets v4 API


🌈 Installation - npm i google-spreadsheet --save or yarn add google-spreadsheet


Examples

_the following examples are meant to give you an idea of just some of the things you can do_

IMPORTANT NOTE - To keep the examples concise, I'm calling await at the top level which is not allowed by default in most versions of node. If you need to call await in a script at the root level, you must instead wrap it in an async function like so:


  1. ``` js
  2. (async function() {
  3.   await someAsyncFunction();
  4. }());
  5. ```


The Basics

  1. ``` js
  2. const { GoogleSpreadsheet } = require('google-spreadsheet');

  3. // Initialize the sheet - doc ID is the long id in the sheets URL
  4. const doc = new GoogleSpreadsheet('<the sheet ID from the url>');

  5. // Initialize Auth - see https://theoephraim.github.io/node-google-spreadsheet/#/getting-started/authentication
  6. await doc.useServiceAccountAuth({
  7.   // env var values are copied from service account credentials generated by google
  8.   // see "Authentication" section in docs for more info
  9.   client_email: process.env.GOOGLE_SERVICE_ACCOUNT_EMAIL,
  10.   private_key: process.env.GOOGLE_PRIVATE_KEY,
  11. });

  12. await doc.loadInfo(); // loads document properties and worksheets
  13. console.log(doc.title);
  14. await doc.updateProperties({ title: 'renamed doc' });

  15. const sheet = doc.sheetsByIndex[0]; // or use doc.sheetsById[id] or doc.sheetsByTitle[title]
  16. console.log(sheet.title);
  17. console.log(sheet.rowCount);

  18. // adding / removing sheets
  19. const newSheet = await doc.addSheet({ title: 'hot new sheet!' });
  20. await newSheet.delete();
  21. ```
More info:



Working with rows

  1. ``` js
  2. // create a sheet and set the header row
  3. const sheet = await doc.addSheet({ headerValues: ['name', 'email'] });

  4. // append rows
  5. const larryRow = await sheet.addRow({ name: 'Larry Page', email: 'larry@google.com' });
  6. const moreRows = await sheet.addRows([
  7.   { name: 'Sergey Brin', email: 'sergey@google.com' },
  8.   { name: 'Eric Schmidt', email: 'eric@google.com' },
  9. ]);

  10. // read rows
  11. const rows = await sheet.getRows(); // can pass in { limit, offset }

  12. // read/write row values
  13. console.log(rows[0].name); // 'Larry Page'
  14. rows[1].email = 'sergey@abc.xyz'; // update a value
  15. await rows[1].save(); // save updates
  16. await rows[1].delete(); // delete a row
  17. ```
More info:



Working with cells

  1. ``` js
  2. await sheet.loadCells('A1:E10'); // loads range of cells into local cache - DOES NOT RETURN THE CELLS
  3. console.log(sheet.cellStats); // total cells, loaded, how many non-empty
  4. const a1 = sheet.getCell(0, 0); // access cells using a zero-based index
  5. const c6 = sheet.getCellByA1('C6'); // or A1 style notation
  6. // access everything about the cell
  7. console.log(a1.value);
  8. console.log(a1.formula);
  9. console.log(a1.formattedValue);
  10. // update the cell contents and formatting
  11. a1.value = 123.456;
  12. c6.formula = '=A1';
  13. a1.textFormat = { bold: true };
  14. c6.note = 'This is a note!';
  15. await sheet.saveUpdatedCells(); // save all updates in one call
  16. ```
More info:



Why?

This module provides an intuitive wrapper around Google's API to simplify common interactions


While Google's v4 sheets api is much easier to use than v3 was, the official googleapis npm module is a giant meta-tool that handles _every Google product_. The module and the API itself are awkward and the docs are pretty terrible, at least to get started.

**In what situation should you use Google's API directly?**
This module makes trade-offs for simplicity of the interface.
Google's API provides a mechanism to make many requests in parallel, so if speed and efficiency is extremely important to your use case, you may want to use their API directly. There are also several features of their API that are not implemented here yet.


Support & Contributions


This module was written and is actively maintained by Theo Ephraim.

**Are you actively using this module for a commercial project? Want to help support it?**

Sponsors


None yet - get in touch!

Contributing


Contributions are welcome, but please follow the existing conventions, use the linter, add relevant tests, add relevant documentation.

The docs site is generated using docsify. To preview and run locally so you can make edits, runnpm run docs:preview and head to http://localhost:3000
The content lives in markdown files in the docs folder.

License

This is free and unencumbered public domain software. For more info, see https://unlicense.org.