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-bulk-writer

v1.0.2

Published

JavaScript library for queueing data to be bulk written to the Google Sheets API at timed intervals to avoid violating the rate limit

Downloads

68

Readme

Google Sheets Bulk Writer

JavaScript library for queueing data to be bulk written to the Google Sheets API on timed intervals to avoid violating the rate limit

Getting Started

Installation

npm install google-sheets-bulk-writer --save

Requirements

  • Google API v4 Authentication

Initialization

const BulkWriter = require('google-sheets-bulk-writer');
const { google } = require('googleapis');

const auth = new google.auth.GoogleAuth({
    keyFile: process.env.GOOGLE_CONFIG,
    scopes: ['https://www.googleapis.com/auth/spreadsheets'],
});

const bulkWriter = new BulkWriter({
    auth,
    spreadsheetId: process.env.SPREADSHEET_ID,
});

Options

| Option | Type | Required | Default | Description | | ---------------- | ---------------------- | -------- | ------------- | ------------------------ | | auth | google.auth.GoogleAuth | Yes | | Required to use the Google Sheets API documentation. | | spreadsheetId | String | Yes | | Google Sheets identifier | | interval | Number | No | 30000ms | The interval at which to append to the spreadsheet | | valueInputOption | String | No | USER_ENTERED | See Google Sheets API documentation | | insertDataOption | String | No | INSERT_ROWS | See Google Sheets API documentation | | errorHandler | Function | No | console.error | Handler to catch errors returned by the Google Sheets API |

Use

bulkWriter.setErrorHandler((err) => {
    console.log(`Failed to write values to spreadsheet`);
    console.log(JSON.stringify(bulkWriter.getErrorValues(), null, 4));

    bulkWriter.clearErrorValues();
});

bulkWriter.start();

bulkWriter.append(`A:H`, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']);
bulkWriter.append(`A:H`, ['b', 'b', 'c', 'd', 'e', 'f', 'g', 'h']);
bulkWriter.append(`Sheet2!A:H`, ['c', 'b', 'c', 'd', 'e', 'f', 'g', 'h']);

setTimeout(() => {
    bulkWriter.append(`A:H`, ['d', 'b', 'c', 'd', 'e', 'f', 'g', 'h']);
    bulkWriter.append(`Sheet2!A:H`, ['e', 'b', 'c', 'd', 'e', 'f', 'g', 'h']);
}, 25000);

API

start()

Required to start the recurring bulk write.

stop()

If the bulk write needs to be stopped for whatever reason.

restart()

Calls stop() and then start(). This needs to be called if a new interval is set.

append(range, values)

Adds a row to be appended to the spreadsheet. range is expected to be a string. The sheetId is specified in the range. See Google Sheets API documentation for more information. values is a two-dimensional array of values to be appended to the spreadsheet.

flush()

This gets automatically called every interval amount of time, but it is available to be used manually as well.

write(range, values)

This is a wrapper function for google.sheets.spreadsheets.values.append(). It gets automatically called by the flush() functin.

getInterval()

Retrieve the interval option value.

setInterval(interval)

Set a new interval value. Expects an integer as the parameter.

getCache()

Retrieve the values set to be posted to a spreadsheet. cache is an object with key values of different ranges. Allows for posting to multiple tabs.

clearRangeQueue(range)

Clears a single range field in the cache object. Expects a string as the parameter.

clearCache()

Resets the cache object.

getErrorValues()

clearErrorValues()

setErrorHandler(errorHandler)

Allows for custom handling of Google Sheets API errors. Expects a function as the parameter.