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-spreadsheet-report

v1.0.1

Published

A simple way to create a report in google spreadsheet

Downloads

105

Readme

google-spreadsheet-report

A simple library to append data to a google spreadsheet.

npm install google-spreadsheet-report

Logging data to spreadsheet

The appendData function appends data to the bottom of a google spreadsheet. Like a log. To keep the document from getting too big, rows with dates older than the retention limit will be purged on each update.

const dayjs = require('dayjs')
const gsr = require('../google-spreadsheet-report')

const options = {
  email: '[email protected]',
  key: `-----BEGIN PRIVATE KEY-----
Private key here
-----END PRIVATE KEY-----`,
  spreadsheetId: '<spreadsheetId>',
  sheet: '<name of sheet>', // Optional. Defaults to the first sheet.
  retention: 14, // Retention in days. Defaults to 14.
}

const data = {
  date: dayjs().format('YYYY-MM-DD'),
  val1: Math.floor(Math.random() * 50),
  val2: Math.floor(Math.random() * 1000),
}

const run = async () => {
  try {
    await gsr.appendData(data, options)
  } catch (e) {
    console.error(e)
  }
}

run()

This would produce a spreadsheet looking something like this:

date | val1 | val2 -----|------|----- 2019-06-02 | 34 | 759

If you later add an extra attribute like this

const data = {
  date: dayjs().format('YYYY-MM-DD'),
  val1: Math.floor(Math.random() * 50),
  val2: Math.floor(Math.random() * 1000),
  val3: Math.floor(Math.random() * 1000),
}

await gsr.appendData(data, options)

a new column would be added to the spreadsheet:

date | val1 | val2 | val3 -----|------|-----|----- 2019-06-02 | 34 | 759 | 2019-06-03 | 12 | 846 | 594

The worksheet is created if it doesn't exist. Any missing column headers are also added.

Updating key values

The setKeyValues finds the row with a matching key and updates all the values on that row. The row is created if it doesn´t exist

const dayjs = require('dayjs')
const gsr = require('../google-spreadsheet-report')

const options = {
  email: '[email protected]',
  key: `-----BEGIN PRIVATE KEY-----
Private key here
-----END PRIVATE KEY-----`,
  spreadsheetId: '<spreadsheetId>',
  sheet: '<name of sheet>', // Optional. Defaults to the first sheet.
  keyName: 'job', // Name of the column to update. Defaults to "name".
}

const data = {
  job: 'Nightly report',
  'last run': dayjs().format('YYYY-MM-DD HH:mm'),
  status: 'OK',
  error: ''
}

const run = async () => {
  try {
    await gsr.setKeyValues(data, options)
  } catch (e) {
    console.error(e)
  }
}

run()

This would output the following data.

job | last run | status | error -----|------|-----|----- Nightly report | 2019-12-22 21:44 | OK |

If you run the same code again, only the value of last run on that same line would be updated.

Generating credentials

  1. Log in to the Google Developer Console
  2. Create a project new project och select an existing one
  3. Open "Library" tab and enable the "Google Drive API"
  4. Go back to the Google Developer Console and open the "Credentials" tab
  5. Create a "Service account key"
  6. Copy the service account id (Someting like "[email protected]")
  7. Select "P12" and click "Create" and then "Create without role"
  8. The p12-file should now be downloaded to your computer
  9. Convert the p12 file into pem format
    openssl pkcs12 -in <filename.p12> -nodes -nocerts > key.pem
    when prompted for password, enter notasecret
  10. Create a new spreadsheet and share it (using the Share button) with the service email from step 6
  11. Get the spreadsheet id from the url. For example if the url is
    https://docs.google.com/spreadsheets/d/1IeEaLOGLuIcy5oPN-OZlxzYwPYRuzVnlrpDlqkzWtOk/edit#gid=0
    the id is 1IeEaLOGLuIcy5oPN-OZlxzYwPYRuzVnlrpDlqkzWtOk
  12. Now you have everything you need. Create the options object wiht the email, key and spreadsheet id
const options = {
  email: '[email protected]',
  key: `-----BEGIN PRIVATE KEY-----
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDDVa....
-----END PRIVATE KEY-----`,
  spreadsheetId: '1IeEaLOGLuIcy5oPN-OZlxzYwPYRuzVnlrpDlqkzWtOk',
}