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 🙏

© 2024 – Pkg Stats / Ryan Hefner

google-sheets-wrapper

v1.0.4

Published

Lightweight wrapper around the official Google Sheets API that makes it easy to read/write rows

Downloads

6

Readme

google-sheets-node

Current version Downloads on npm License Build status Dependencies

A lightweight wrapper around the official Google Sheets API that makes it easy to read and write rows. It's written in TypeScript and uses async/await to handle requests to Google's API.

Usage

The library only supports interacting with rows in Google Sheets. Not with columns or individual cells.

When fetching rows, the library will map your data to Javascript objects. When inserting new rows, you can also use objects.

Authentication

Follow step 1 of the official "Node.js Quickstart": https://developers.google.com/sheets/api/quickstart/nodejs. This will walk you through enabling the Sheets API and creating credentials (JSON file).

Afterwards set the environment variable GOOGLE_APPLICATION_CREDENTIALS so that it contains the path to your credentials file. Could be something like this:

export GOOGLE_APPLICATION_CREDENTIALS=/path/to/my/credentials.json

Other methods of authenticating are currently not supported.

Header row

This library assumes that the first row in your spreadsheet is used as a header.

The header row is used to transform your rows to Javascript objects. Try to keep the values in the header simple (no spaces, no special characters, ...) The library will convert your titles to camelCase, so be aware of this. For example: Time posted will be converted to timePosted.

Getting rows

This will map your rows to objects, using the first row of your spreadsheet as header (see "Header row"):

// Open spreadsheet with ID XXXX-XXXX-XXXX and work with columns A to C in worksheet "Sheet 1"
let sheet = new GoogleSheet({
    sheetId: "XXXXXXXXXX-XXXXXXXXX-XXXXXXXX",
    range: "'Sheet 1'!A:C"
});

// Get the data
let data = await sheet.getRows();

// Show it
console.log(data);

For example, this spreadsheet:

Will be mapped to this:

[ 
    { timestamp: '1488806320466', message: 'Hi there!', user: 'Xavier' },
    { timestamp: '1488806320467', message: 'We meet again.', user: 'Xavier' } 
]

Writing new rows

To write new rows you have to construct an array of objects, much like the output of getRows(). Each object will be inserted as a row:

// Open spreadsheet with ID XXXX-XXXX-XXXX and work with columns A to C in worksheet "Sheet 1"
let sheet = new GoogleSheet({
    sheetId: "XXXXXXXXXX-XXXXXXXXX-XXXXXXXX",
    range: "'Sheet 1'!A:C"
});

// The data that we want to add to the spreadsheet
let data = [
    {
        timestamp: Date.now(),
        message: 'Another message',
        user: 'Peter'
    },
    {
        timestamp: Date.now(),
        message: 'Awesome work!',
        user: 'Simon'
    }
]

await sheet.appendRows(data);

Note: the order of the fields doesn't matter. The library will match your data with the header row and insert it in the correct columns.

Warning: the library will throw an error if your data contain a property that isn't in the header row. However, you can omit rows (they will be empty).

Contributing & License

Feel free to fork this library, improve it or create issues and pull requests.