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

v1.0.4

Published

Google Sheets API v4 abstracted TypeScript modules for reading and manipulating Google Spreadsheets.

Downloads

12

Readme

Google Sheets Manager (TypeScript)

Google Sheets API v4 abstracted TypeScript modules for reading and manipulating Google Spreadsheets using service account access.

Modules (WIKI)

(google-sheets-manager wiki)

Modules and code components breakdown and documentation on the wiki

Installation

NPM

Basic Usage

Simple example to show some of the things you can do.

var googlesheets = require("google-sheets-manager");
var creds = require("./client_secret.json");

var ServiceAccount = googlesheets.ServiceAccount;
var GoogleSheet = googlesheets.GoogleSheet;

const GOOGLE_SPREADSHEETID = ''; // update placeholder value
const GOOGLE_SHEETID = 0; // update placeholder value

var authClass = new ServiceAccount(creds);
var sheetAPI = new GoogleSheet(authClass, GOOGLE_SPREADSHEETID, GOOGLE_SHEETID);

var defaultCallback = (err, res) => {
	if (err) {
		throw err;
	}
	console.log(res);
};

sheetAPI.getData(defaultCallback);

sheetAPI.getData({
	range: {
		startRow: 2,
		startCol: 1,
		endRow: 3,
		endCol: 2,
	},
	majorDimension: "COLUMNS",
}, defaultCallback);

sheetAPI.getBatchData(defaultCallback);

sheetAPI.getBatchData({
	ranges: [{
		startRow: 2,
		startCol: 1,
		endRow: 3,
		endCol: 2,
	}, {
		startRow:2,
		endCol: 1,
	}],
	majorDimension: "COLUMNS",
}, defaultCallback);

sheetAPI.setData([
	['1', '2'],
	['3', '4'],
], defaultCallback);

sheetAPI.setData([
	['1', '2'],
	['3', '4'],
], {
	range: {
		startRow: 2,
		startCol: 1,
		endRow: 3,
		endCol: 2,
	},
	majorDimension: "COLUMNS",
}, defaultCallback);

Authentication

Service Account (recommended method)

This is a 2-legged oauth method and designed to be "an account that belongs to your application instead of to an individual end user". Use this for an app that needs to access a set of documents that you have full access to. (read more)

Setup Instructions

  1. Go to the Google Developers Console
  2. Select your project or create a new one (and then select it)
  3. Enable the Sheets API for your project
    • In the sidebar on the left, expand APIs & auth > APIs
    • Search for "sheets"
    • Click on "Sheets API"
    • click the blue "Enable API" button
  4. Create a service account for your project
    • In the sidebar on the left, expand APIs & auth > Credentials
    • Click blue "Add credentials" button
    • Select the "Service account" option
    • Select "Furnish a new private key" checkbox
    • Select the "JSON" key type option
    • Click blue "Create" button
    • your JSON key file is generated and downloaded to your machine (it is the only copy!)
    • note your service account's email address (also available in the JSON key file)
  5. Share the doc (or docs) with your service account using the email noted above

API

This module follows "normal" node callback conventions:

  • Every method that takes a callback takes it as its last param
  • Every callback will be called with the error (or null) as first param
  • Some methods have optional params

GoogleSpreadsheet

TODO


ServiceAccount

Wrapper class for simplifing authentication process using service account method

new ServiceAccount(creds, [googleAuthScopes])

  • creds object - service account credentials loaded as json object (see setup instruction above)
  • googleAuthScopes string[] - google scopes which you want to give the api access on, default value is scopes (["https://www.googleapis.com/auth/spreadsheets"])

GoogleSheet

Represents a single "sheet" from the spreadsheet. These are the different tabs/pages visible at the bottom of the Google Sheets interface.

sheets info returned as property sheets when calling GoogleSheet.getInfo.

Properties:

  • spreadsheetId number - the ID of the main spreadsheet, could be extracted from sheet's url using this regex /spreadsheets/d/([a-zA-Z0-9-_]+)
  • sheetId number - the ID of the single sheet tab, could be extracted from sheet's url using this regex [#&]gid=([0-9]+)

new GoogleSheet(authClass, spreadsheetId, [sheetId])

  • `authClass AuthClass - instance of AuthClass inhereted class, like ServiceAccount
  • spreadsheetId number - the ID of the main spreadsheet
  • sheetId number - the ID of the single sheet tab, default value is 0

SpreadsheetWorksheet.getData([options], callback)

Loads data from google sheet.

  • options object (optional)
    • range (optional) - The range of the data to be fetched, if not provided then the function returns all the data in the sheet.
      • startRow number (optional) - default: 1
      • startColnumber (optional) - default: 1
      • endRow number (optional) - default: MAX_ROW
      • endCol number (optional) - default: MAX_COL
    • majorDimension string - enum('ROWS' || 'COLUMNS') how the loaded data should be represented, either array of rows or array of columns, default values is 'ROWS'
  • callback (err, res)
    • res two dimentional array containing the range data

SpreadsheetWorksheet.getBatchData([options], callback)

Loads data from multiple ranges in a single request from google sheet.

  • options object (optional)
    • ranges object Array (optional) - The ranges of the data to be fetched, if not provided then the function returns all the data in the sheet.
      • startRow number (optional) - default: 1
      • startColnumber (optional) - default: 1
      • endRow number (optional) - default: MAX_ROW
      • endCol number (optional) - default: MAX_COL
    • majorDimension string - enum('ROWS' || 'COLUMNS') how the loaded data should be represented, either array of rows or array of columns, default values is 'ROWS'
  • callback (err, res)
    • res two dimentional array containing the range data

SpreadsheetWorksheet.setData(data, [options], callback)

set data in google sheet.

  • data (string | null | undefined) 2D array - representing data to be updated to the google sheet
  • options object (optional)
    • range object (optional) - The range of the data to be updated, if not provided then the function apply the data to the first range that fits in the sheet.
      • startRow number (optional) - default: 1
      • startColnumber (optional) - default: 1
      • endRow number (optional) - default: MAX_ROW
      • endCol number (optional) - default: MAX_COL
    • majorDimension string - enum('ROWS' || 'COLUMNS') how the provided data are represented, either array of rows or array of columns, default values is 'ROWS'
  • callback (err, res)
    • res contains google reponse with the updated cells info

SpreadsheetWorksheet.setBatchData(data, [options], callback)

set data in multiple ranges in a single request from google sheet.

  • data (string | null | undefined) 3D array - representing array of data to be updated to the google sheet
  • options object (optional)
    • ranges object Array (optional) - The ranges of the data to be updated, if not provided then the function apply the data to the first range that fits in the sheet, If ranges provided then it must be of the same size as data array first dimension.
      • startRow number (optional) - default: 1
      • startColnumber (optional) - default: 1
      • endRow number (optional) - default: MAX_ROW
      • endCol number (optional) - default: MAX_COL
    • majorDimension string - enum('ROWS' || 'COLUMNS') how the provided data are represented, either array of rows or array of columns, default values is 'ROWS'
  • callback (err, res)
    • res contains google reponse with the updated cells info

GoogleRow

TODO


GoogleCol

TODO


GoogleCell

TODO


Modules