Simple Sheets
Reads and writes Google Sheets row data, perfect for sheets populated by Google Forms. This is a wrapper for the very-powerful-yet-overwhelming official Sheets API.
Authenticates via a Google Service Account by passing in the client_email
and private_key
values provided by the .json
file that Google Service Accounts generate. See service-account-credentials.json for an example.
Now includes tools for clearing and seeding sheets with data, which is useful for writing automated tests against spreadsheets.
API
getRows
;
It returns a promise that resolves to an object that has arrays with the requested mappings.
rows
is an array of objects that have the following properties:
label: "people" // This will be the label of the array range: "A2:B" // In A1 format mapping: "firstName" "lastName" // These are what the columns will be labeled label: "locations" range: "'Cities'!C2:C" mapping: "city"
options
include:
spreadsheetId
(required): The ID of the Google sheet, which is the long string in the URL of the pageclientEmail
(required): The authorizedclient_email
for your service account (remember to add permissions for this email to your sheet!)privateKey
(required): the authorizedprivate_key
for your service accountdateTimeFormat
: An optional override of the date format that will return
Usage
const getRows = ;const PRIVATE_KEY = "-----BEGIN PRIVATE KEY-----\nMIIEAoIBAQCwmz3cj...ee+Z81xUH4QTo18s=\n-----END PRIVATE KEY-----\n";
updateRows
;
data
is an array of objects, following the following format:
range: "A2:A" values: "A" "B"
options
include:
spreadsheetId
(required): The ID of the Google sheet, which is the long string in the URL of the pageclientEmail
(required): The authorizedclient_email
for your service account (remember to add permissions for this email to your sheet!)privateKey
(required): the authorizedprivate_key
for your service accountvalueInputOption
: Whether input should be taken literally ("RAW"
), or as if a user entered them ("USER_ENTERED"
, default)
It returns a count of modified rows.
Usage
const updateRows = ;const PRIVATE_KEY = "-----BEGIN PRIVATE KEY-----\nMIIEAoIBAQCwmz3cj...ee+Z81xUH4QTo18s=\n-----END PRIVATE KEY-----\n"; ;
addRows
;
range
is an A1 range (eg "A2:A") that will be searched to find something table-like to append to the end of.data
is an array of arrays of values to add:
"column 1" "column 2" "column 1" "column 2"
options
include:
spreadsheetId
(required): The ID of the Google sheet, which is the long string in the URL of the pageclientEmail
(required): The authorizedclient_email
for your service account (remember to add permissions for this email to your sheet!)privateKey
(required): the authorizedprivate_key
for your service accountvalueInputOption
: Whether input should be taken literally ("RAW"
), or as if a user entered them ("USER_ENTERED"
, default)
It returns an object with the count of modified rows:
Usage
const addRows = ;const PRIVATE_KEY = "-----BEGIN PRIVATE KEY-----\nMIIEAoIBAQCwmz3cj...ee+Z81xUH4QTo18s=\n-----END PRIVATE KEY-----\n"; ;
clearSheets
;
sheets
is an array of sheet names, following the following format:
"First Sheet Name" "Second"
options
include:
spreadsheetId
(required): The ID of the Google sheet, which is the long string in the URL of the pageclientEmail
(required): The authorizedclient_email
for your service account (remember to add permissions for this email to your sheet!)privateKey
(required): the authorizedprivate_key
for your service account
It returns an array containing the counts of modified rows in each sheet.
Usage
const clearSheets = ;const PRIVATE_KEY = "-----BEGIN PRIVATE KEY-----\nMIIEAoIBAQCwmz3cj...ee+Z81xUH4QTo18s=\n-----END PRIVATE KEY-----\n"; ;
seedSheets
;
sheets
is an array of sheet objects, following the following format:
sheetName: "Sheet 1" mapping: "column1" "column2" seedData: column1: "a" column2: "b" column1: "c" column2: "d" sheetName: "Sheet 2" mapping: "column1" "optionalColumn" seedData: column1: "e" optionalColumn: "f" column1: "g"
Please note the following:
- This method assumes the first row is headers
- The mapping must include all column labels, in order
- The seed columns can be entered in any order (and can even be omitted), but must have a matching mapping value
options
include:
spreadsheetId
(required): The ID of the Google sheet, which is the long string in the URL of the pageclientEmail
(required): The authorizedclient_email
for your service account (remember to add permissions for this email to your sheet!)privateKey
(required): the authorizedprivate_key
for your service account
It returns an array containing the counts of modified rows in each sheet.
Usage
const clearSheets = ;const PRIVATE_KEY = "-----BEGIN PRIVATE KEY-----\nMIIEAoIBAQCwmz3cj...ee+Z81xUH4QTo18s=\n-----END PRIVATE KEY-----\n"; ;
Look at the Google Sheets batchGet
API Docs, the Google Sheets batchUpdate
API Docs and the Google Sheets append
API Docs for more information.
Testing
npm test