npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

google-sheets-automation

v1.0.0

Published

Automate Google Sheets from Node.js: add, update, and read rows with simple API. Supports service accounts, header mapping, and batch operations.

Readme


Overview

google-sheets-automation is a Node.js package for programmatically managing Google Sheets. Easily add, update, and read rows using service account credentials. Designed for backend, CLI, and serverless environments.

Features

  • Add rows with optional header mapping
  • Update cells in bulk (A1 notation)
  • Read sheet data as objects
  • Batch operations
  • Promise-based API
  • Secure authentication via Google service accounts

Installation

npm install google-sheets-automation

Quick Start

import { GoogleSheetProvider } from 'google-sheets-automation';

const provider = new GoogleSheetProvider({
  client_email: '[email protected]',
  private_key: '-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n',
  sheetId: 'your-google-sheet-id',
});

const headerMap = {
  Name: 'name',
  Email: 'email',
  Message: 'message',
};

const rows = [
  { name: 'Alice', email: '[email protected]', message: 'Hello!' },
  { name: 'Bob', email: '[email protected]', message: 'Hi!' },
];

await provider.addRows({ spreadsheetId: 'your-google-sheet-id', sheetName: 'Sheet1', headerMap }, rows);

React/NextJS Serverside form example

import type { NextApiRequest, NextApiResponse } from 'next';
import { GoogleSheetProvider } from 'google-sheets-automation';

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
  if (req.method !== 'POST') return res.status(405).end();

  const { name, email, message } = req.body;
  if (!name || !email || !message) {
    return res.status(400).json({ error: 'Missing required fields' });
  }

  const credentials = {
    private_key: process.env.GOOGLE_PRIVATE_KEY?.replace(/\\n/g, '\n'),
    client_email: process.env.GOOGLE_CLIENT_EMAIL,
  };

  const client = new GoogleSheetProvider(credentials);

  const options = {
    spreadsheetId: process.env.GOOGLE_SHEET_ID,
    sheetName: 'Sheet1',
    headerMap: { name: 'Name', email: 'Email', message: 'Message' }
  };
  const rows = [{ name, email, message }];

  try {
    await client.addRows(options, rows);
    res.status(200).json({ success: true });
  } catch (err: any) {
    res.status(500).json({ error: err.message });
  }
}

Environment Setup

  1. Create a Google Cloud project and enable the Google Sheets API.
  2. Create a service account and download the JSON key.
  3. Share your target Google Sheet with the service account email.
  4. Store credentials in your .env:
    GOOGLE_SERVICE_ACCOUNT_EMAIL=your-service-account-email@project.iam.gserviceaccount.com
    GOOGLE_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n"
    GOOGLE_SHEET_ID=your-google-sheet-id

API Reference

GoogleSheetProvider

addRows(options, rows)

Add rows to a sheet. If headerMap is provided and the sheet is empty, headers are added automatically.

updateCells(options)

Update cells in a sheet using A1 notation and a 2D array of values.

readData(options)

Read all data from a sheet, returned as an array of objects (first row is used as keys).

deleteRows(options)

Delete rows by index (not yet implemented).

createSheet(options)

Create a new sheet (not yet implemented).

Types

  • AddRowsOptions: { spreadsheetId, sheetName, headerMap? }
  • UpdateCellsOptions: { spreadsheetId, sheetName, range?, values }
  • ReadDataOptions: { spreadsheetId, sheetName, range? }
  • DeleteRowsOptions: { spreadsheetId, sheetName, rowIndexes }
  • CreateSheetOptions: { spreadsheetId, sheetName }

Advanced Usage

Update Cells Example

await provider.updateCells({
  spreadsheetId: 'your-google-sheet-id',
  sheetName: 'Sheet1',
  range: 'Sheet1!A2:B2',
  values: [['Alice', '[email protected]']]
});

Read Data Example

const data = await provider.readData({
  spreadsheetId: 'your-google-sheet-id',
  sheetName: 'Sheet1'
});
console.log(data);

Testing

Unit Tests

Run all unit tests (mocks Google API calls):

npm run test

Integration Tests

Run integration tests against the real Google Sheets API (requires valid .env and sheet setup):

npm run test test/providers/googleProvider.integration.test.ts

Contributing

Pull requests and issues are welcome! Please open an issue for feature requests or bugs.

License

MIT

Testing

Unit tests are written with Jest and mock all Google API calls, so no credentials are required:

npm run test

Integration Testing

To run integration tests against the real Google Sheets API, you must:

  • Set up a .env file with valid GOOGLE_SERVICE_ACCOUNT_EMAIL, GOOGLE_PRIVATE_KEY, and GOOGLE_SHEET_ID.
  • Ensure the target sheet (e.g. IntegrationTestSheet) exists in your Google Spreadsheet before running the test.
  • Run:
npm run test test/providers/googleProvider.integration.test.ts

Integration tests will use your credentials and make real API calls. Do not run these against production data.

Providers

  • GoogleSheetProvider (currently available)
    • Usage: new GoogleSheetProvider({ ...credentials }) -(Planned) ExcelSheetProvider, AirtableSheetProvider, etc.

API Functions (ISheetProvider)

All providers implement the following methods:

addRows(options, rows) — Add rows to the sheet, optionally mapping headers. updateCells(options) — Update cells in the sheet (A1 range, 2D values). readData(options) — Read data from the sheet (returns array of objects). deleteRows(options) — Delete rows by index (not yet implemented). createSheet(options) — Create a new sheet (not yet implemented).

Types

AddRowsOptions: { spreadsheetId, sheetName, headerMap? } UpdateCellsOptions: { spreadsheetId, sheetName, range?, values } ReadDataOptions: { spreadsheetId, sheetName, range? } DeleteRowsOptions: { spreadsheetId, sheetName, rowIndexes } CreateSheetOptions: { spreadsheetId, sheetName }

Roadmap

  • Support for additional sheet providers (Excel, Airtable, etc.) planned for future releases.

License

MIT