@yuanqing/google-sheets
v0.0.2
Published
An easier interface to read from and write to Google Sheets
Maintainers
Readme
@yuanqing/google-sheets

An easier interface to read from and write to Google Sheets
Quick start
$ yarn add @yuanqing/google-sheetsconst { getSpreadsheet } = require('@yuanqing/google-sheets')
async function main () {
const serviceAccountCredentials = {
clientEmail: '<client_email>',
priateKey: '<private_key>'
}
const spreadsheetId = '<spreadsheet_id>'
const sheetName = '<sheet_name>'
const spreadsheet = await getSpreadsheet(
serviceAccountCredentials,
spreadsheetId
)
const sheet = await spreadsheet.getSheet(sheetName)
const rows = await sheet.getAllRows()
console.log(rows)
}
await main()<client_email>and<private_key>are credentials for a Service Account with edit access to your spreadsheet. See Initial setup.<spreadsheet_id>is the value between/d/and/editin your spreadsheet URL.<sheet_name>is the name of the sheet that you want to read from or write to.
Initial setup
- Navigate to the Google API Console
- Select a project from the drop-down box in the top bar.
- Click
Credentials(the Key icon) on the left navigation bar. - Click the
Create credentialsdrop-down box, and selectService account key. - Click the
Select…drop-down box, and selectNew service account. Enter aService account name. ForRole, selectProject › Editor. - For
Key type, selectJSON. - Click the
Createbutton. A JSON file with the Service Account credentials will be generated. Note theclient_emailandprivate_keyvalues in the generated JSON file.
- Navigate to your spreadsheet.
- Click the
Sharebutton on the top-right corner of the page. - In the
Enter names or email addresses…text box, enter theclient_emailof the Service Account, then click theSendbutton.
Assumptions
- Data is row-based. Each field is stored on a column.
- Row 1 of the sheet contains the headers for the data. “Actual” data starts from Row 2.
API
const { createSpreadsheet, getSpreadsheet } = require('@yuanqing/google-sheets')Spreadsheet
const spreadsheet = await createSpreadsheet(serviceAccountCredentials)
Creates a new spreadsheet and returns a Promise for it.
serviceAccountCredentialsis an object literal with the following keys:Key | Description :-|:-
clientEmail| Email address of the Service Account that has edit access to the spreadsheet.privateKey| Private key of the Service Account.
const spreadsheet = await getSpreadsheet(serviceAccountCredentials, spreadsheetId)
Returns a Promise for an existing spreadsheet.
spreadsheetIdis the value between/d/and/editin the spreadsheet URL.
const sheet = await spreadsheet.createSheet(spreadsheetName, headers)
Creates a new sheet and returns a Promise for it.
spreadsheetNameis the name of the new sheet.headersis an array of headers for the new sheet.
const sheet = await spreadsheet.getSheet(spreadsheetName)
Returns a Promise for an existing sheet.
Sheet
const rows = await sheet.getAllRows()
Returns a Promise for an array containing all the rows from the sheet.
const rows = await sheet.getRowsByRange(m, n)
Returns a Promise for an array containing rows from the row m to row n of the sheet.
await sheet.addRows(rows)
Appends the given array of rows to the sheet, and returns a Promise that resolves.
Installation
$ yarn add @yuanqing/google-sheets
