google-spreadsheet
Google Sheets API (v4) wrapper for Node.js
README
google-spreadsheet
The most popular Google Sheets API wrapper for javascript
🚨 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.
🌈 Installation - npm i google-spreadsheet --save or yarn add google-spreadsheet
Examples
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:
- ``` js
- (async function() {
- await someAsyncFunction();
- }());
- ```
The Basics
- ``` js
- const { GoogleSpreadsheet } = require('google-spreadsheet');
- // Initialize the sheet - doc ID is the long id in the sheets URL
- const doc = new GoogleSpreadsheet('<the sheet ID from the url>');
- // Initialize Auth - see https://theoephraim.github.io/node-google-spreadsheet/#/getting-started/authentication
- await doc.useServiceAccountAuth({
- // env var values are copied from service account credentials generated by google
- // see "Authentication" section in docs for more info
- client_email: process.env.GOOGLE_SERVICE_ACCOUNT_EMAIL,
- private_key: process.env.GOOGLE_PRIVATE_KEY,
- });
- await doc.loadInfo(); // loads document properties and worksheets
- console.log(doc.title);
- await doc.updateProperties({ title: 'renamed doc' });
- const sheet = doc.sheetsByIndex[0]; // or use doc.sheetsById[id] or doc.sheetsByTitle[title]
- console.log(sheet.title);
- console.log(sheet.rowCount);
- // adding / removing sheets
- const newSheet = await doc.addSheet({ title: 'hot new sheet!' });
- await newSheet.delete();
- ```
Working with rows
- ``` js
- // create a sheet and set the header row
- const sheet = await doc.addSheet({ headerValues: ['name', 'email'] });
- // append rows
- 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' },
- ]);
- // read rows
- const rows = await sheet.getRows(); // can pass in { limit, offset }
- // read/write row values
- console.log(rows[0].name); // 'Larry Page'
- rows[1].email = 'sergey@abc.xyz'; // update a value
- await rows[1].save(); // save updates
- await rows[1].delete(); // delete a row
- ```
Working with cells
- ``` js
- await sheet.loadCells('A1:E10'); // loads range of cells into local cache - DOES NOT RETURN THE CELLS
- console.log(sheet.cellStats); // total cells, loaded, how many non-empty
- const a1 = sheet.getCell(0, 0); // access cells using a zero-based index
- const c6 = sheet.getCellByA1('C6'); // or A1 style notation
- // access everything about the cell
- console.log(a1.value);
- console.log(a1.formula);
- console.log(a1.formattedValue);
- // update the cell contents and formatting
- a1.value = 123.456;
- c6.formula = '=A1';
- a1.textFormat = { bold: true };
- c6.note = 'This is a note!';
- await sheet.saveUpdatedCells(); // save all updates in one call
- ```
Why?
This module provides an intuitive wrapper around Google's API to simplify common interactions