What is google-spreadsheet?
The google-spreadsheet npm package allows you to interact with Google Sheets through the Google Sheets API. It provides a simple and intuitive way to read, write, and manage Google Sheets data programmatically.
What are google-spreadsheet's main functionalities?
Authentication
This feature allows you to authenticate with the Google Sheets API using either service account credentials or OAuth2 tokens.
const { GoogleSpreadsheet } = require('google-spreadsheet');
const doc = new GoogleSpreadsheet('<spreadsheet-id>');
// Using service account credentials
await doc.useServiceAccountAuth(require('./path/to/credentials.json'));
// OR using OAuth2 tokens
await doc.useOAuth2Client(oAuth2Client);
Loading a Spreadsheet
This feature allows you to load the spreadsheet's metadata and worksheets, making it possible to interact with the spreadsheet's structure and data.
await doc.loadInfo(); // loads document properties and worksheets
console.log(doc.title);
Reading Rows
This feature allows you to read rows from a specific sheet within the spreadsheet. You can access individual cell values and iterate through the rows.
const sheet = doc.sheetsByIndex[0]; // or use doc.sheetsById[id]
const rows = await sheet.getRows(); // can pass in { limit, offset }
console.log(rows[0].name);
Adding Rows
This feature allows you to add new rows to a sheet within the spreadsheet. You can specify the values for each column in the new row.
await sheet.addRow({ name: 'John Doe', email: 'john.doe@example.com' });
Updating Cells
This feature allows you to update the values of specific cells in a row and save the changes back to the spreadsheet.
const row = rows[0];
row.name = 'Jane Doe';
await row.save();
Other packages similar to google-spreadsheet
googleapis
The googleapis package is a comprehensive library for interacting with various Google APIs, including the Google Sheets API. It provides more extensive functionality beyond just Google Sheets, but it requires more setup and configuration compared to google-spreadsheet.
gsheets
The gsheets package is a lightweight library for accessing Google Sheets data. It offers basic functionalities for reading and writing data but lacks some of the advanced features and ease of use provided by google-spreadsheet.
g-sheets-api
The g-sheets-api package is designed for simple and quick access to Google Sheets data. It is less feature-rich compared to google-spreadsheet but can be a good choice for straightforward use cases where minimal setup is desired.
google-spreadsheet
The most popular Google Sheets API wrapper for javascript
- multiple auth options - API key, service account, oauth
- 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
Documentation
Full docs are available at https://theoephraim.github.io/node-google-spreadsheet
🌈 Installation - npm i google-spreadsheet --save
Examples
the following examples are meant to give you an idea of just some of the things you can do
The Basics
const { GoogleSpreadsheet } = require('google-spreadsheet');
const doc = new GoogleSpreadsheet('<the sheet ID from the url>');
await doc.useServiceAccountAuth({
client_email: process.env.GOOGLE_SERVICE_ACCOUNT_EMAIL,
private_key: process.env.GOOGLE_PRIVATE_KEY,
});
await doc.useServiceAccountAuth(require('./creds-from-google.json'));
doc.useApiKey('YOUR-API-KEY');
await doc.loadInfo();
console.log(doc.title);
await doc.updateProperties({ title: 'renamed doc' });
const sheet = doc.sheetsByIndex[0];
console.log(sheet.title);
console.log(sheet.rowCount);
const newSheet = await doc.addSheet({ title: 'hot new sheet!' });
await newSheet.delete();
More info:
Working with rows
const sheet = await doc.addSheet({ headers: ['name', 'email'] });
const larryRow = await sheet.addRow({ name: 'Larry Page', email: 'larry@google.com' });
const moreRows = await sheet.addRows([
{ name: 'Sergey Brin', email: 'sergey@google.com' },
{ name: 'Eric Schmidt', email: 'eric@google.com' },
]);
const rows = await sheet.getRows();
console.log(rows[0].name);
rows[1].email = 'sergey@abc.xyz';
await rows[1].save();
await rows[1].delete();
More info:
Working with cells
await sheet.loadCells('A1:E10');
console.log(sheet.cellStats);
const a1 = sheet.getCell(0, 0);
const c6 = sheet.getCellByA1('C6');
console.log(a1.value);
console.log(a1.formula);
console.log(a1.formattedValue);
a1.value = 123.456;
c6.formula = '=A1';
a1.textFormat = { bold: true };
c6.note = 'This is a note!';
await sheet.saveUpdatedCells();
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?
Buy Theo a beer
None yet - get in touch!
Contributing
Contributions are welcome, but please follow the existing conventions, use the linter, add relevant tests, add relevant documentation.
These docs are generated using docsify. To preview and run locally so you can make edits, run npm 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.