0.1.4 • Published 4 years ago

simplegooglesheetsjs v0.1.4

Weekly downloads
19
License
MIT
Repository
github
Last release
4 years ago

SimpleGoogleSheetsJS

Create, read, and write Google Sheets through a simplified interface for the Google Sheets API

Installation

npm install simplegooglesheetsjs

Usage

const simplegooglesheets = require("./index.js");


let spreadsheet = new simplegooglesheets(); //Initialize the spreadsheet object

//Next, authorize the spreadsheet access
spreadsheet.authorizeAPIKey("MY_API_KEY");
//Or, authorize with a service account
spreadsheet.authorizeServiceAccount("CLIENT_EMAIL", "PRIVATE_KEY");
//Or, authorize with a JSON key file (containing fields client_email and private_key)
spreadsheet.authorizeServiceAccountFile("/path/to/key/file.json");


//Use an existing spreadsheet
const spreadsheetId = "MY_SHEET_ID";
spreadsheet.setSpreadsheet(spreadsheetId).then(() => {
    spreadsheet.setSheet("MY_SHEET_NAME").then(() => {
        //Print out the headers
        console.log(spreadsheet.getHeaders().Headers);
        
        //Get and print a row by header names
        spreadsheet.getRow(5).then((row) => console.log(row));
    });
});

//Or create a new one
spreadsheet.createSpreadsheet("My new spreadsheet");

//Get rows
spreadsheet.getRowArray(2).then((res) => console.log(res))
spreadsheet.getRows(2,10).then((rows) => console.log(rows));
spreadsheet.getLastRow().then((i) => { //Get the last row index, then get the last row
    spreadsheet.getRow(i).then((data) => {
        console.log(data);
    });
});

//Add a row to the end
spreadsheet.addRow({"HEADER1":"DATA1"});
spreadsheet.addRowArray(["DATA1","DATA2","DATA3"]);

//Set a row based on index
spreadsheet.setRow(2, {"HEADER1":"DATA1"});
spreadsheet.setRowArray(2, ["DATA1","DATA2","DATA3"]);

//Delete a row
spreadsheet.deleteRow(2);

Documentation

simplegooglesheetsjs

Requires: module:googleapis

simplegooglesheetsjs.SimpleGoogleSheets

SimpleGoogleSheets is a simplified wrapper for the Google Sheets API

Kind: static class of simplegooglesheetsjs

new SimpleGoogleSheets()

SimpleGoogleSheets default constructor

SimpleGoogleSheets~authorizeServiceAccount(client_email, private_key)

Authorize with a service account from an email and a private key

Kind: inner method of SimpleGoogleSheets

ParamTypeDescription
client_emailstringThe service account email
private_keystringThe service account private key

SimpleGoogleSheets~authorizeServiceAccountFile(keyFile)

Authorize with a service account from a file

Kind: inner method of SimpleGoogleSheets

ParamTypeDescription
keyFilestringName of the file to get the client_email and private_key from

SimpleGoogleSheets~authorizeAPIKey(key)

Authorize with an API Key

Kind: inner method of SimpleGoogleSheets

ParamTypeDescription
keystringAPI Key

SimpleGoogleSheets~refreshHeaders() ⇒ Promise

Refresh the current spreadsheet headers

Kind: inner method of SimpleGoogleSheets
Returns: Promise - Promise of status

SimpleGoogleSheets~setRowArray(rowIndex, dataArray) ⇒ Promise

Set the row with the data in the array. Warning: will overwrite any headers (on row 0)

Kind: inner method of SimpleGoogleSheets
Returns: Promise - Status of function

ParamTypeDescription
rowIndexnumberIndex of row
dataArrayArray.<string>Data to set in cells (index=column number)

SimpleGoogleSheets~setRow(rowIndex, data) ⇒ Promise

Set the row with the data in the format {"HEADER_1":"data1", "HEADER_N":"datan"}

Kind: inner method of SimpleGoogleSheets
Returns: Promise - Status of function

ParamTypeDescription
rowIndexnumberIndex of row
dataHeaderDataData to set in cells (in HEADER_NAME:VALUE pairs)

SimpleGoogleSheets~addRow(rowIndex, data) ⇒ Promise

Add the row with the data in the format {"HEADER_1":"data1", "HEADER_N":"datan"}

Kind: inner method of SimpleGoogleSheets
Returns: Promise - Status of function

ParamTypeDescription
rowIndexnumberIndex of row
dataHeaderDataData to set in cells (in HEADER_NAME:VALUE pairs)

SimpleGoogleSheets~addRowArray(rowIndex, dataArray) ⇒ Promise

Add the row with the data in the array. Warning: will overwrite any headers (on row 0)

Kind: inner method of SimpleGoogleSheets
Returns: Promise - Status of function

ParamTypeDescription
rowIndexnumberIndex of row
dataArrayArray.<string>Data to set in cells (index=column number)

SimpleGoogleSheets~getRowArray(rowIndex) ⇒ Promise.<Array>

Get the row and return the data in the array

Kind: inner method of SimpleGoogleSheets
Returns: Promise.<Array> - Array of data

ParamTypeDescription
rowIndexnumberIndex of row

SimpleGoogleSheets~getRow(rowIndex) ⇒ Promise.<Object>

Get the row and return the data in the format {"HEADER_1":"data1", "HEADER_N":"datan"}

Kind: inner method of SimpleGoogleSheets
Returns: Promise.<Object> - Data

ParamTypeDescription
rowIndexnumberIndex of row

SimpleGoogleSheets~getLastRowIndex() ⇒ Promise.<number>

Get the index of the last row

Kind: inner method of SimpleGoogleSheets
Returns: Promise.<number> - Index of the last row with data

SimpleGoogleSheets~getRows(rowIndexStart, rowIndexEnd) ⇒ Promise.<Array.<Object>>

Get an array of rows and return the data in the format {"HEADER_1":"data1", "HEADER_N":"datan"}

Kind: inner method of SimpleGoogleSheets
Returns: Promise.<Array.<Object>> - Data

ParamTypeDescription
rowIndexStartnumberIndex of row start
rowIndexEndnumberIndex of row start

SimpleGoogleSheets~setHeaders(headers) ⇒ Promise

Set the spreadsheet headers

Kind: inner method of SimpleGoogleSheets
Returns: Promise - Promise of update status

ParamTypeDescription
headersHeadersHeaders to set

SimpleGoogleSheets~createAndSetSpreadsheet(name) ⇒ Promise

Create a new spreadsheet and set it as current

Kind: inner method of SimpleGoogleSheets
Returns: Promise - Undefined on success

ParamType
namestring

SimpleGoogleSheets~createSheet(name) ⇒ Promise

Create and set as current sheet

Kind: inner method of SimpleGoogleSheets
Returns: Promise - Undefined on success

ParamTypeDescription
namestringString name of sheet

SimpleGoogleSheets~deleteSheet(nameOrId) ⇒ Promise

Delete the sheet referenced by the name or the ID

Kind: inner method of SimpleGoogleSheets
Returns: Promise - Undefined on success

ParamTypeDescription
nameOrIdstring | numberInteger ID or string name of sheet

SimpleGoogleSheets~deleteRow(rowIndex) ⇒ Promise

Delete the row in the spreadsheet. Warning: deleting row 0 will remove the spreadsheet headers (if any)

Kind: inner method of SimpleGoogleSheets
Returns: Promise - Undefined on success

ParamTypeDescription
rowIndexnumberIndex of row to delete

SimpleGoogleSheets~deleteCol(colIndex) ⇒ Promise

Delete the column in the spreadsheet

Kind: inner method of SimpleGoogleSheets
Returns: Promise - Undefined on success

ParamTypeDescription
colIndexstringIndex of column to delete

SimpleGoogleSheets~findAndReplace(find, replacement, allSheets, matchCase, entireCell, useRegex) ⇒ Promise

Find and replace the data in the spreadsheet

Kind: inner method of SimpleGoogleSheets
Returns: Promise - Promise of number of values replaced

ParamTypeDescription
findstringThe string to search for
replacementstringReplacement string for find and replace
allSheetsbooleanSearch all sheets or just the current one
matchCasebooleanMatch the case
entireCellbooleanMatch the entire cell
useRegexbooleanUse regex to search the cell (find must be a regex string)

SimpleGoogleSheets~setSheet(nameOrId) ⇒ Promise

Set the current sheet, referenced by the name or the id. Also updates the headers

Kind: inner method of SimpleGoogleSheets
Returns: Promise - Undefined on success

ParamTypeDescription
nameOrIdstring | numberInteger id or string name of sheet

SimpleGoogleSheets~setSpreadsheet(spreadsheetId) ⇒ Promise

Set the current spreadsheet

Kind: inner method of SimpleGoogleSheets
Returns: Promise - Undefined on success

ParamTypeDescription
spreadsheetIdstringID of spreadsheet to use

SimpleGoogleSheets~metadata() ⇒ Object

Get the current spreadsheet meta data

Kind: inner method of SimpleGoogleSheets
Returns: Object - Metadata of current spreadsheet

SimpleGoogleSheets~getColumnName(columnIndex) ⇒

Get the name of the column (such as A, ABZ, or ZAD) from the index

Kind: inner method of SimpleGoogleSheets
Returns: Column name (such as ABZ)
Throws:

  • Error if invalid index provided
ParamTypeDescription
columnIndexnumberGet the column name from the index

SimpleGoogleSheets~getRangeName(rowStart, columnStart, rowEnd, columnEnd) ⇒ string

Get the name of the range (in sheets format) from indexes

Kind: inner method of SimpleGoogleSheets
Returns: string - Range
Throws:

  • Error if invalid values provided
ParamTypeDescription
rowStartnumberStarting row (and/or columnStart)
columnStartnumberStarting column
rowEndnumberEnding row (and/or columnEnd)
columnEndnumberEnding column

License

Copyright 2020 Alex Mous

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.