New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@cag-group/google-api-tools

Package Overview
Dependencies
Maintainers
3
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@cag-group/google-api-tools

Helpers related to Google APIs

  • 0.3.6
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
29
increased by70.59%
Maintainers
3
Weekly downloads
 
Created
Source

Google Cloud API tools for Node.js

Various helpers for Google Calendar, Drive and Sheets.

Authorization

This module authorizes with a service account only, as opposite to google user delegation in most Google's examples. Create a service account in cloud console and save it as service-account.json. Enable the APIs (Calendar, Drive and/or Sheets).

const key = require('./service-account-key.json')

Google Drive

API reference.

The drive client is a helper that can list meta data about files in a folder:

const {DriveClient} = require('@cag-group/google-api-tools')
const client = new DriveClient(key.client_email, key.private_key)
const response = await client.getFilesInFolder(folderId)

Get meta data for a named file in a specific folder:

const file = await client.getFileMetaData(folderId, 'Walt Disney characters')

Google Calendar

API reference.

Read calendar items within a time interval.

The calendar shall be public or shared with the service account.

const {CalendarClient} = require('@cag-group/google-api-tools')
const client = new CalendarClient(key.client_email, key.private_key)
const items = await client.getAllItems(calendarId, new Date('2017-09-15T00:00'), new Date('2017-09-16T00:00'))
console.log(items)

Google Sheets

API reference.

Read cell values
const {SheetsClient} = require('@cag-group/google-api-tools')
const client = new SheetsClient(key.client_email, key.private_key)

Read cell values given a cell range:

const rows = await client.readSheetCellRange(sheetId, 'A1:K')

It returns a two-dimensional array of values.

Object creation from spreadsheet cells

Given an example spreadsheet:

ID      Förnamn	    Efternamn	Unused	Förmögenhet     Först sedd
1       Kalle       Anka        foo     1               1934-06-09
2       Alexander   Lukas       bar     1000000         1948-12-01
3       Joakim      von Anka    zoo     1×10^+30        1947-12-01 

the SheetsTableReader is used to read each row into objects.

A field mapping is specified, mapping column headers to object property names and optional cell value conversions:

const {SheetsAPIUtil} = require('@cag-group/google-api-tools')
const fieldMapping = [
    {property: 'id', header: 'ID'},
    {property: 'firstName', header: 'Förnamn'},
    {property: 'lastName', header: 'efternamn'},
    {property: 'balance', header: 'förmögenhet', convert: v => v / 100},
    {property: 'firstSeen', header: 'Först sedd'}
]

Specifying this columns can change order and place in the spreadsheet without affecting the code reading it. New columns can also be added - they will be ignored.

In order to match column headers with field mapping, common human errors are addressed by relaxed string comparisions: Headers in code and in sheet are converted to lower case and all spaces are removed before comparision.

example usage:

const {SheetsTableReader} = require('@cag-group/google-api-tools')
const reader = new SheetsTableReader(rows[0], fieldMapping)
const rowObjects = rows.slice(1).map(r => reader.createObject(r))

Outputted rowObjects:

[ { id: 1,
    firstName: 'Kalle',
    lastName: 'Anka',
    balance: 0.01,
    firstSeen: 1934-06-09T00:00:00.000Z },
  { id: 2,
    firstName: 'Alexander',
    lastName: 'Lukas',
    balance: 10000,
    firstSeen: 1948-12-01T00:00:00.000Z },
  { id: 3,
    firstName: 'Joakim',
    lastName: 'von Anka',
    balance: 1e+28,
    firstSeen: 1947-12-01T00:00:00.000Z } ]

FAQs

Package last updated on 16 Dec 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc