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

gsweet

v2.0.9

Published

Help with writing scripts to run against gSuite

Downloads

108

Readme

gsweet

Summary

A project for gathering the core methods and tools for making it easier to write scripts across all the products in the gSuite.

Installation

npm i gsweet --save

Basic Use

Once you have your authentication JSON file set up in a file named .env.json you require the package and call the auth() function

const GSweet = require("gsweet");
const gsweet = new Gsweet();
const { driveOps } = gsweet;
const { sheetOps } = gsweet;

In the above example the lack of a parameter to the new GSweet constructor is telling the package to look for .env.json at the root of the project. If your credentials file lives somewhere else you can just pass in a relative or absolute path like this

const gsweet = new Gsweet({pathOrVarName:"/Users/your-user-name/dev/ENV_VARS/gsweet.env.json", useExistingEnvVar:false);
const { driveOps } = gsweet;
const { sheetOps } = gsweet;

The second parameter supports using an environment variable that already contains the proper credential string. For example, if you're using Heroku and you have a config var named "GSWEET" the call would be

const gsweet = new GSweet({ pathOrVarName: "GSWEET", useExistingEnvVar: true });

The code that runs loads the JSON file and parses the top level objects into environment variables needed by this package.

A full example usage of the package might look like this

cconst gsweet = new Gsweet({pathOrVarName:"/Users/your-user-name/dev/ENV_VARS/gsweet.env.json", useExistingEnvVar:false);
const { driveOps, sheetOps } = gsweet;

const main = async () => {
  // Drive Examples
  const TEST_FILE = "<name of sheet in your drive>";
  const result = await driveOps.getFiles({
    withName: TEST_FILE,
    exactMatch: true
  });
  console.log(result);

  // Sheet Examples
  const sheetRange = {
    id: "<google id of a sheet in your drive>",
    range: "Sheet1!A1",
    data: [["Test1"], ["Test2"]]
  };
  const result2 = await sheetOps.setRangeData(sheetRange);
  console.log(result2.config.data.values); // just showing the values passed in
  console.log("Num Cells Updated:", result2.data.updatedCells);
};
main();

Authentication

If you clone this repo it will not contain the needed authorization pieces. You will need to create a JSON file with the following structure:

"gsweet":{
   "client_secrets": {
    },
    "drive_credentials": {
    },
    "sheet_credentials": {
    }
  }
}

If you want to store all of this in an .env file you will need to turn it into a properly escaped string. A partial example looks like this. Note that all but the starting and ending quote marks need to be escaped. JSON wants double quotes so you can't substitute single quotes.

"gsweet":"{\"client_secrets\":{\"installed\":{\"client_id\"..."

You will need to fill in those objects with the expected json that Google requires. Google has a quick-start on how to create all of these credentials. You can see a full example of the JSOn file in the Reference section of this Readme.

Testing

This package contains both unit tests and integration tests. The integration tests are fragile since they require access to specific files and folders in google drive. The constants for these files are stored in the test-data/integration.json. Modify that document to contain the names and ids for your files. The expected structure of the test data is test-folder

---node-test-subfolder
  |---doc-in-subfolder
  |---sheet-in-subfolder
  node-test-sheet
  node-test-doc

The top of the integration test files also uses create-env to load credentials. You will need to change that path to point to your credentials json file.

This project is set up such that unit tests will be written with a test.js suffix and integration tests will end with testi.js. You can run unit tests with npm test -- the unit tests
npm run test:int -- the integration tests
npm run test:all -- both unit and integration tests

Reference on Using the Google Apis

This is information for anyone contributing to (rather than using) this project.

GoogleApis npm package

Check out some Drive Samples.

The Drive API

Additional Documentation

This project uses JSDoc comments. If you want to generate the HTML documentation in an /out folder at the root of the project use:
npm run doc

Reference

Full .json.env example

{
  "gsweet":{
    "client_secrets": {
        "installed": {
          "client_id": "your id goes here",
          "project_id": "your project id",
          "auth_uri": "https://accounts.google.com/o/oauth2/auth",
          "token_uri": "https://accounts.google.com/o/oauth2/token",
          "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
          "client_secret": "your client secret",
          "redirect_uris": [
            "urn:ietf:wg:oauth:2.0:oob",
            "http://localhost"
          ]
        }
      },
      "drive_credentials": {
        "access_token": "a really long token",
        "refresh_token": "a shorter token",
        "token_type": "Bearer",
        "expiry_date": 123
      },
      "sheet_credentials": {
        "access_token": "your token",
        "refresh_token": "the refresh toeken",
        "scope": "https://www.googleapis.com/auth/spreadsheets",
        "token_type": "Bearer",
        "expiry_date": 123
      }
  }
}

Documentation on GitHub

See the GitHub documentation for the list of available modules, functions, function signatures, and usage examples.