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

v1.1.2

Published

An easy to use synchronous google sheets reader without the use of callbacks.

Downloads

35

Readme

Google Sheets JS

Easy to use, read-only wrapper for googleapis for both asynchronous & synchronous read access to Google Sheets.

Installation

npm install --save google-sheets-js

Usage

This package can be used for reading data of Google Sheets.

Reading Data

const GoogleSheets = require("google-sheets-js");

const googleSheet = new GoogleSheets(SAMPLE_SPREADSHEET_ID);

googleSheet.fetchData("Sheet1!A1:C4")
    .then (data => {
        console.log(data);
    });

The data will be read as:

[
  [
    "colA",
    "colB",
    "colC"
  ],
  [
    "row2ColA",
    "row2ColB",
    "row2ColC"
  ],
  [
    "row3ColA",
    "row3ColB",
    "row3ColC"
  ],
  [
    "row4ColA",
    "row4ColB",
    "row4ColC"
  ]
]

Refresh tokens

Each access token has an expiry date & time. After this, the token will need to be refreshed periodically. to get a new token, call the getNewToken function.

const GoogleSheets = require("google-sheets-js");

const googleSheet = new GoogleSheets();
googleSheet.getNewToken();

Enable Google APIs and getting credentials

1. Enable Google Sheets API

  1. Visit Google Developers quickstart guide.
  2. Follow the instructions to enable Google API with your Google account.
  3. Download the credentials and save them in credentials-sample.json.

2. Generate new token

The token is stored in the root directory in the file token-sample.json. To generate a new token, ts-node must be installed.

  1. Execute the TokenGenerator by executing ts-node src/TokenGenerator.ts
  2. Visit the URL which will open up a Google authentication interface.
  3. Login to the Google account or select the Google account from the list.
  4. Allow permission to proceed.
  5. Copy the token shown on the screen. Paste it in the terminal and hit Enter.
  6. A new file should be created called token-sample.json.

This temporary token has an expiry date. If the token has expired, generate a new token in this way.

3. Get the Sheet ID of the spreadsheet to read

The sheet ID will need to be set in the .env file for SAMPLE_SHEET_ID variable.

  1. Open the sheet from a web browser.
  2. Select the Sheet ID from the URL. https://docs.google.com/spreadsheets/d/{{SHEET_ID}}/edit#gid=0000000000
  3. Save the sheet id in the .env file. SAMPLE_SHEET_ID=12312398ISADJAKSLHDASJHDASJDHAJSDHJASDASD

Setup

Setting up the required Credentials & Tokens to GoogleSheets via this package can happen in two different ways. In the Firstly, the credentials and tokens can be provided in JSON files for which the location is set via .env file or environment variables. Secondly, the credentials and the token can be passed directly into the constructor as the second and third parameters.

Setup via .env file

  1. Create a .env file in the root directory of your project.
  2. Set the relative location of your credentials file related to the root directory of your project. CREDENTIALS_FILE="credentials-sample.json"
  3. Set the relative location of your token file related to the root directory of your project. TOKEN_FILE="token-sample.json"

Sample .env file

CREDENTIALS_FILE="credentials-sample.json"
TOKEN_FILE="token-sample.json"

SAMPLE_SHEET_ID="1ENxACcSTa6QY0qg0qmmy1Yr808Nc6ar5RpCnmF5yUXM"

Code

The SheetID can be supplied via the .env file, or it can be passed via the first parameter in the constructor.

const GoogleSheets = require("google-sheets-js");

const googleSheet = new GoogleSheets();
const data = await googleSheet.fetchData("Sheet1!A1:C4");

Setup via passing credentials & token via the constructor

The credentials and token can be stored in two different variables in code. Just pass them as constructor parameters.

const GoogleSheets = require("google-sheets-js");

const SAMPLE_SHEET_ID = "";
const CREDENTIALS = "";
const TOKEN = "";

const googleSheet = new GoogleSheets(SAMPLE_SHEET_ID, CREDENTIALS, TOKEN);

googleSheet.fetchData("Sheet1!A1:C4")
    .then (data => {
        console.log(data);
    });