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-simple

v1.1.2

Published

A simple way to access Google Sheets with named and unnamed ranges

Downloads

17

Readme

google-sheets-simple

npm package Build Status Coverage Status

A simple way to access Google Sheets with named and unnamed ranges

Breaking changes

Version v0.3.0 onwards use a different syntax for the require:

Version v1.0.0 is first release version.

const { Sheet, Range } = require('google-sheets-simple');

Please note

This library is a simplified interface to the Google APIs Node.js Client and currently only uses the Service to Service Authentication method.

The full API can be found here. This library currently only supports the basic essentials.

Usage

The design behind this module is that you wish to get or put a particular range of cells. It works best with a named range but equally will work with a range described by Sheet1!A1:D15. You can get all the cells in the range or set some of the cells, clearing out the rest of the range.

All methods return promises.

Installation

npm install google-sheets-simple

Example

// This assumes you have set the environment variable
//  GOOGLE_APPLICATION_CREDENTIALS to point at a service account file

const { Sheet } = require('google-sheets-simple');

const sheet = new Sheet(<sheet_id>);
sheet.initialise()
.then(() => {
  const data = <some two-dimensional array>;
  sheet.save(<named range>, data);
  })
.then(() => {
  sheet.get(<another range>);
  })
.then((array) => {
  // array contains a two-dimensional array of the cell values
  });

Constructor

This is simply called with the id of the spreadsheet. The id can be obtained from the URL in a browser. It is the part between /d/ and /edit.

This library assumes that you have set up GOOGLE_APPLICATION_CREDENTIALS correctly and that the service account has access to the spreadsheet. The scope used is https://www.googleapis.com/auth/spreadsheets.

const { Sheet } = require('google-sheets-simple');

const sheet = new Sheet(<sheet_id>);

Methods

initialise (or initialize)

This is required before any other method is called. It sets up the authentication and populates spreadsheet information into the class. It returns a promise and you should wait for the promise to resolve before making any other calls. The promise resolves with the full spreadsheet data from the API. This can usually be ignored.

await sheet.initialise();

get

This should be passed a range. All ranges may be passed either as a named range (Data ⇒ Named ranges) or in the format Sheet1!A1:C20. Please note that when using A1 ranges, the sheet name is currently required, even if there is only one sheet. Using named ranges is highly recommended.

The second parameter is optional and defaults to ROWS. The alternative is COLUMNS and describes how the data should be retreived. The data returned will be a two-dimensional array.

data = await sheet.get('named_range', 'ROWS');

save

This should be passed a range, as in get() above. The second parameter is the data as a two-dimensional array and the optional third defaults to 'ROWS' (see above).

If the data does not fill the range given, the remaining rows will be cleared. If ROWS is passed, the cleared space is everything from the last row provided to the bottom of the range. If COLUMNS is provided, each column is cleared from the bottom of the data in that column to the bottom of the range.

The response is the raw response data from the API.

await sheet.save('named_range', dataArray, 'ROWS');

clear

This should be passed a range, as in get() above. The range will be cleared.

The response is the raw response data from the API.

await sheet.clear('Sheet2!A3:J50');

append

This should be passed a range, a two-dimensional array is in save() above and a third parameter defaulting to 'ROWS'. The range should describe the top part of a table.

The data passed will be appended to the table, even though the table extends below the range. It is usual with append() only to pass a single row of data to be appended but please remember that it still needs to be a two-dimensional array.

await sheet.append('named_range', [ rowDataArray ]);

Any issues

Please report any issues or comments at Github issues. PRs and suggestions are very welcome.