
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
google-api-wrapper
Advanced tools
A simpler way to work with Google Sheets and Google Drive files. 😊
Simple Wrapper around Google Sheets & Drive APIs. It installs googleapis as dependency. It also allows easy access to underlying googleapis objects.
npm install google-api-wrapper
This will add googleapis as dependency.
const Google = require('google-api-wrapper');
async function main() {
Google.loadCredFile('/path/to/credentails.json');
Google.loadTokenFile('/path/to/token.json');
const Sheet = Google.getSheet();
const rows = await Sheet.read('1nZqgw5otHxvg7by-qnYmjkyNdHAPQYgduv7Tbf5aKlw');
console.log(rows);
}
main();
If you already have auth object
Googe.setAuth(authObject);
Or, set credential file followed by token
Google.loadCredFile('/path/to/credentails.json');
Google.loadTokenFile('/path/to/token.json');
Or, set them directly from json objects
Google.setCred(cred);
Google.setToken(token);
Or, exchange token provided from OAuth
Google.setCred(cred);
let token = await Google.exchangeCode(code);
Or, set refresh token
Google.setCred(cred);
Google.setRefreshToken(refresh_token);
Sheet object maintains an internal sheetId and range.
const Sheet = Google.getSheet();
await Sheet.read(id, range = 'Sheet1');
It two-dimensional array of rows and column values of sheet data. Alternatively, you can set id and range separately.
Sheet.set(id, range)
await Sheet.read();
To assume first column as header and read documents:
await Sheet.readDocs(id, range = 'Sheet1');
await Sheet.readDocs(sheet_name, range, { byName: false, slugify: true });
Returns an array of objects by using first row as field names. "slugify" will convert field names to snake case (eg: "Min. Qty" to "min_qty")
const Sheet = Google.getSheet();
Sheet.set(id, range); // range defaults to 'Sheet1', if not provided
await Sheet.clear(); // clears the range / sheet tab
await Sheet.write(row1);
await Sheet.write(row2);
await Sheet.endWrite();
Batches up multiple rows and then appends at once at interval of 500 rows, or when endWrite() is called. You must make a final call to endWrite to complete writing to sheet.
const Sheet = Google.getSheet();
const rows = await Sheet.read(sheetId, 'Sheet1');
rows[0][0] = 'Updated!';
await Sheet.overwrite(rows);
Overwrite rows at last set Sheet Id and Range. (set by read operation in previous example)
const Sheet = Google.getSheet();
await Sheet.create(name);
await Sheet.write([ 'hello', 'there' ]);
await Sheet.create(name, folderId);
const Drive = Google.getDrive();
const file = Drive.byId(fileId);
It returns a file object with { id, name, mimeType }
attributes.
Drive.byName(name, type = null, folderId = null);
Example:
const Drive = Google.getDrive();
let file;
file = Drive.byName('My Document');
file = Drive.byName('example.csv', 'type/csv', parentFolderId);
file = Drive.byName('Example', null, parentFolderId);
It returns a file object with { id, name, mimeType }
attributes.
Drive.list(folderId);
Example: to list all files under a folder named "My Folder"
const Drive = Google.getDrive();
const folder = Drive.byName('My Folder');
const files = Drive.list(folder.id);
It returns an array of file objects with { id, name, mimeType }
attributes.
const Drive = Google.getDrive();
await Drive.readFile(id);
Returns string of file content.
const Drive = Google.getDrive();
await Drive.copy(fileId, newName);
const Drive = Google.getDrive();
await Drive.move(fileId, folderId);
const Drive = Google.getDrive();
const file = await Drive.create(name, mimeType, body, parentFolderId);
const file = await Drive.create(name, 'folder');
See mime-type mappings below for simplified mime type inputs.
To create a sub-folder under another parent folder, provide null body and parent folder id.
const file = await Drive.create(subFolderName, 'folder', null, parentFolderId);
Quick mimetype mappings:
Input Mime Type | Translated Mime Type |
---|---|
'folder' | 'application/vnd.google-apps.folder' |
'doc' | 'application/vnd.google-apps.document' |
'document' | 'application/vnd.google-apps.document' |
'sheet' | 'application/vnd.google-apps.spreadsheet' |
'spreadsheet' | 'application/vnd.google-apps.spreadsheet' |
Drive object holds reference to Google API's drive object as a property 'drive', which you can use to call the methods of Google Drive API.
For example, to list comments on a document named 'My Document', follow below
const Google = require('google-api-wrapper');
const Drive = Google.getDrive();
const file = Drive.byName('My Document');
// Below is call to underlying Google Drive API's method directly
const comments = Drive.drive.comments.list({ fileId: file.id });
Similarly, Google Sheet object is stored as 'sheet' property of wrapper's sheet object. methods on Sheet.sheet can be made as per Google Sheet API.
const Google = require('google-api-wrapper');
const Sheet = Google.getSheet();
// Calls underlying Google Sheet API
Sheet.sheet.spreadsheets.batchGet(options);
To test the package,
# .env file
CRED_PATH=/path/to/credentials.json
TOKEN_PATH=/path/to/token.json
npm test
FAQs
Google API Wrapper
The npm package google-api-wrapper receives a total of 25 weekly downloads. As such, google-api-wrapper popularity was classified as not popular.
We found that google-api-wrapper demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.