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

json2sheets

v1.1.3

Published

A library built with Bun and Google API client to import data into Google Sheets.

Downloads

25

Readme

json2sheets

A library built with Bun and Google API client to import data into Google Sheets.

Installation

To install json2sheets, run the following command:

npm install json2sheets

Usage

To use json2sheets, you need to create a new instance of the SheetsClient class, passing the spreadsheetId, sheetId, and an optional header as arguments.

const { SheetsClient } = require('json2sheets')

const client = new SheetsClient('spreadsheetId', 'sheetId', ['header1', 'header2', 'header3'])

SheetsClient

The SheetsClient class uses a JWT authentication strategy, you will need to provide the GOOGLE_ACCOUNT_EMAIL and GOOGLE_ACCOUNT_PRIVATE_KEY as environment variables.

The class is initialized with the spreadsheetId, the sheetId and an optional header.

Retrieving data

You can get the header and rows data using the getHeader and getRows methods.

const client = new SheetsClient('spreadsheetId', 'sheetId')

client.getHeader()
// Returns the header row from the sheet
// Example output : ['Name', 'Date', 'Status', 'Link']

client.getRows()
// Returns all rows from the sheet as an array of objects with keys corresponding to the header row
// Example output :
// [
//     {
//         Name: 'Send email',
//         Date: '2023-03-02T13:07:00.000Z',
//         Status: 'Done',
//         Link: { text: "[email protected]", url: "mailto:[email protected]" }
//     }
// ]

Writing data

All batchUpdate requests are queued and need to be sent using commit()

const client = new SheetsClient('spreadsheetId', 'sheetId')

// Sets the sheet header row and uses it for data keys
// Compares provided header with existing and adds, deletes and updates columns
client.setHeader(['Name', 'Date', 'Status', 'Link'])

// Updates data in the specified row with the provided data
client.updateRow(2, { Name: 'Send email', Date: '2023-03-02T13:07:00.000Z', Status: 'Done' })

// Adds new rows with provided data at the end of the sheet
client.addRows([{ Name: 'Send email', Date: '2023-03-02T13:07:00.000Z', Status: 'Done' }])

// Adds the provided number of columns at the end of the sheet
client.addColumns(2)

// Deletes the specified row or column from the sheet
client.deleteRow(2)
client.deleteColumn(2)

// Deletes the specified rows or columns from the sheet
// Multiple rows or columns are deleted in descending index order
client.deleteRows([2, 4])
client.deleteColumns([2, 4])

// Sends request
client.commit()

Clearing data

const client = new SheetsClient('spreadsheetId', 'sheetId')

// Clears all the data from the sheet
client.clearSheet()

Note

This package uses the googleapis library to interact with the Google Sheets API, so you will need to make sure that you have properly set up a project in the Google Cloud Console and enabled the Google Sheets API for your project. You will also need to create a service account and provide the GOOGLE_ACCOUNT_EMAIL and GOOGLE_ACCOUNT_PRIVATE_KEY as environment variables.

You can find more information about how to set up a project and enable the Google Sheets API in the Google Sheets API documentation.