Git Product home page Git Product logo

google-sheets-bulk-writer's Introduction

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.

google-sheets-bulk-writer's People

Contributors

dorfman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.