0.1.1 • Published 5 months ago

google-sheet-manager v0.1.1

Weekly downloads
-
License
ISC
Repository
-
Last release
5 months ago

GOOGLE SHEET MANAGER

This package is an abstraction of googleapis that lets you simply use a Google Sheet to read and write content.

You can create, read and write sub-sheets within a Google Sheet.

Requirements

  • Create a google sheet inside your Google drive
  • Create a service account and get its credentials (Guide)
  • Retrieve your sheet identifier within the url (https://docs.google.com/spreadsheets/d/<your identifier>/*)

How it works

Google-sheet-manager provides a class named SheetManager, you need to instanciate it with the same auth params you would provide to GoogleAuth (e.g: an array of scopes and the credentials JSON file of an account service) plus the Google Sheet identifier you want to interact with.

Example:

import { SheetManager } from  "google-sheet-manager";
import  credentials  from  "./google_credentials.json"  assert { type: "json" };
import { SHEET_ID } from  "../config/index.js";

const  scopes  = [
"https://www.googleapis.com/auth/drive",
"https://www.googleapis.com/auth/drive.file",
"https://www.googleapis.com/auth/spreadsheets",
];
const  authParams  = { scopes, credentials };
const  Manager  =  new  SheetManager(authParams, SHEET_ID);

With the following code, you are able to create, write and read any sub-sheets inside the sheet corresponding to SHEET_ID through Manager.

Methods

The SheetManager instance has access to the following methods:

  • writeToSheetWithCustomRange(sheetName, values, range, checkIfSheetExists?)
ParameterTypeDefaultValueDescription
sheetNamestringNoneThe sub sheet name you are writting in
valuesstring[]NoneEach index of the array correspond to a cell
rangestringNoneExample: "A2:C3"
checkIfSheetExistsbooleantrueWill check before inserting the values if the sheet exists, if not, it will create it first

Returns nothing.

  • readFromSheet(sheetName, range?)
ParameterTypeDefaultValueDescription
sheetNamestringNoneThe sub sheet name you want to get content
rangestring"A2:Z1000"Example: "A2:C3"

Returns an array of string arrays

Example of usage

NodeJS project with the following structure:

plot

./config/index.js plot

./lib/sheet.js plot

./index.js plot

It's a basic usage and of course the sheet name can be dynamic, so you can use others with the same instance of SheetManager.

0.1.1

5 months ago

0.1.0

5 months ago