google-spreadsheet
Advanced tools
Comparing version 3.0.8 to 3.0.9
@@ -22,2 +22,3 @@ const _ = require('lodash'); | ||
API_KEY: 'API_KEY', | ||
RAW_ACCESS_TOKEN: 'RAW_ACCESS_TOKEN', | ||
}; | ||
@@ -52,2 +53,8 @@ | ||
// token must be created and managed (refreshed) elsewhere | ||
async useRawAccessToken(token) { | ||
this.authMode = AUTH_MODES.RAW_ACCESS_TOKEN; | ||
this.accessToken = token; | ||
} | ||
// creds should be an object obtained by loading the json file google gives you | ||
@@ -88,2 +95,5 @@ async useServiceAccountAuth(creds) { | ||
config.headers.Authorization = `Bearer ${this.jwtClient.credentials.access_token}`; | ||
} else if (this.authMode === AUTH_MODES.RAW_ACCESS_TOKEN) { | ||
if (!this.accessToken) throw new Error('Invalid access token'); | ||
config.headers.Authorization = `Bearer ${this.accessToken}`; | ||
} else if (this.authMode === AUTH_MODES.API_KEY) { | ||
@@ -245,3 +255,3 @@ if (!this.apiKey) throw new Error('Please set API key'); | ||
const response = await this._makeSingleUpdateRequest('addSheet', { | ||
properties: _.omit(properties, 'headers'), | ||
properties: _.omit(properties, 'headers', 'headerValues'), | ||
}); | ||
@@ -253,4 +263,5 @@ // _makeSingleUpdateRequest already adds the sheet | ||
// allow it to work with `.headers` but `.headerValues` is the real prop | ||
if (properties.headers) { await newSheet.setHeaderRow(properties.headers); } | ||
if (properties.headerValues) { await newSheet.setHeaderRow(properties.headerValues); } | ||
if (properties.headerValues || properties.headers) { | ||
await newSheet.setHeaderRow(properties.headerValues || properties.headers); | ||
} | ||
@@ -257,0 +268,0 @@ return newSheet; |
@@ -180,2 +180,4 @@ const _ = require('lodash'); | ||
_getUpdateRequest() { | ||
// this logic should match the _isDirty logic above | ||
// but we need it broken up to build the request below | ||
const isValueUpdated = this._draftData.value !== undefined; | ||
@@ -186,2 +188,7 @@ const isNoteUpdated = this._draftData.note !== undefined; | ||
// if no updates, we return null, which we can filter out later before sending requests | ||
if (!_.some([isValueUpdated, isNoteUpdated, isFormatUpdated, isFormatCleared])) { | ||
return null; | ||
} | ||
return { | ||
@@ -188,0 +195,0 @@ updateCells: { |
@@ -22,2 +22,4 @@ const { columnToLetter } = require('./utils'); | ||
get rowNumber() { return this._rowNumber; } | ||
// TODO: deprecate rowIndex - the name implies it should be zero indexed :( | ||
get rowIndex() { return this._rowNumber; } | ||
@@ -34,3 +36,3 @@ get a1Range() { | ||
async save() { | ||
async save(options = {}) { | ||
if (this._deleted) throw new Error('This row has been deleted - call getRows again before making updates.'); | ||
@@ -42,3 +44,3 @@ | ||
params: { | ||
valueInputOption: 'USER_ENTERED', // other option is RAW | ||
valueInputOption: options.raw ? 'RAW' : 'USER_ENTERED', | ||
includeValuesInResponse: true, | ||
@@ -45,0 +47,0 @@ }, |
@@ -141,3 +141,4 @@ const _ = require('lodash'); | ||
get a1SheetName() { return `'${this.title}'`; } | ||
get a1SheetName() { return `'${this.title.replace(/'/g, "''")}'`; } | ||
get encodedA1SheetName() { return encodeURIComponent(this.a1SheetName); } | ||
get lastColumnLetter() { return columnToLetter(this.columnCount); } | ||
@@ -220,2 +221,9 @@ | ||
const responseRanges = _.map(cellsToUpdate, (c) => `${this.a1SheetName}!${c.a1Address}`); | ||
// if nothing is being updated the request returned is just `null` | ||
// so we make sure at least 1 request is valid - otherwise google throws a 400 | ||
if (!_.compact(requests).length) { | ||
throw new Error('At least one cell must have something to update'); | ||
} | ||
await this._spreadsheet._makeBatchUpdateRequest(requests, responseRanges); | ||
@@ -309,3 +317,3 @@ } | ||
method: 'put', | ||
url: `/values/${encodeURIComponent(this.a1SheetName)}!A1`, | ||
url: `/values/${this.encodedA1SheetName}!1:1`, | ||
params: { | ||
@@ -316,3 +324,3 @@ valueInputOption: 'USER_ENTERED', // other option is RAW | ||
data: { | ||
range: `${this.a1SheetName}!A1`, | ||
range: `${this.a1SheetName}!1:1`, | ||
majorDimension: 'ROWS', | ||
@@ -329,3 +337,3 @@ values: [[ | ||
async addRows(rows) { | ||
async addRows(rows, options = {}) { | ||
// adds multiple rows in one API interaction using the append endpoint | ||
@@ -339,2 +347,8 @@ | ||
// google bug that does not handle colons in names | ||
// see https://issuetracker.google.com/issues/150373119 | ||
if (this.title.includes(':')) { | ||
throw new Error('Please remove the ":" from your sheet title. There is a bug with the google API which breaks appending rows if any colons are in the sheet title.'); | ||
} | ||
if (!_.isArray(rows)) throw new Error('You must pass in an array of row values to append'); | ||
@@ -364,6 +378,6 @@ | ||
method: 'post', | ||
url: `/values/${encodeURIComponent(this.a1SheetName)}:append`, | ||
url: `/values/${this.encodedA1SheetName}:append`, | ||
params: { | ||
valueInputOption: 'USER_ENTERED', // RAW | ||
insertDataOption: 'OVERWRITE', // INSERT_ROWS | ||
valueInputOption: options.raw ? 'RAW' : 'USER_ENTERED', | ||
insertDataOption: options.insert ? 'INSERT_ROWS' : 'OVERWRITE', | ||
includeValuesInResponse: true, | ||
@@ -382,2 +396,10 @@ }, | ||
// if new rows were added, we need update sheet.rowRount | ||
if (options.insert) { | ||
this._rawProperties.gridProperties.rowCount += rows.length; | ||
} else if (rowNumber + rows.length > this.rowCount) { | ||
// have to subtract 1 since one row was inserted at rowNumber | ||
this._rawProperties.gridProperties.rowCount = rowNumber + rows.length - 1; | ||
} | ||
return _.map(response.data.updates.updatedData.values, (rowValues) => { | ||
@@ -389,4 +411,4 @@ const row = new GoogleSpreadsheetRow(this, rowNumber++, rowValues); | ||
async addRow(rowValues) { | ||
const rows = await this.addRows([rowValues]); | ||
async addRow(rowValues, options) { | ||
const rows = await this.addRows([rowValues], options); | ||
return rows[0]; | ||
@@ -502,3 +524,3 @@ } | ||
async getCellsInRange(a1Range, options) { | ||
const response = await this._spreadsheet.axios.get(`/values/${encodeURIComponent(this.a1SheetName)}!${a1Range}`, { | ||
const response = await this._spreadsheet.axios.get(`/values/${this.encodedA1SheetName}!${a1Range}`, { | ||
params: options, | ||
@@ -795,3 +817,3 @@ }); | ||
// sheet name without ie 'sheet1' rather than 'sheet1'!A1:B5 is all cells | ||
await this._spreadsheet.axios.post(`/values/${encodeURIComponent(this.a1SheetName)}:clear`); | ||
await this._spreadsheet.axios.post(`/values/${this.encodedA1SheetName}:clear`); | ||
this.resetLocalCache(true); | ||
@@ -798,0 +820,0 @@ } |
@@ -5,3 +5,3 @@ { | ||
"description": "Google Sheets API (v4) -- simple interface to read/write data and manage sheets", | ||
"version": "3.0.8", | ||
"version": "3.0.9", | ||
"license": "Unlicense", | ||
@@ -35,2 +35,3 @@ "keywords": [ | ||
"devDependencies": { | ||
"delay": "^4.3.0", | ||
"docsify-cli": "^4.4.0", | ||
@@ -37,0 +38,0 @@ "eslint": "^6.8.0", |
# google-spreadsheet | ||
> The most popular [Google Sheets API](https://developers.google.com/sheets/api/reference/rest) wrapper for javascript | ||
[![NPM version](https://badge.fury.io/js/google-spreadsheet.png)](http://badge.fury.io/js/google-spreadsheet) | ||
[![CircleCI](https://circleci.com/gh/theoephraim/node-google-spreadsheet.svg?style=svg)](https://circleci.com/gh/theoephraim/node-google-spreadsheet) | ||
[![NPM version](https://img.shields.io/npm/v/google-spreadsheet)](https://www.npmjs.com/package/google-spreadsheet) | ||
[![CircleCI](https://circleci.com/gh/theoephraim/node-google-spreadsheet.svg?style=shield)](https://circleci.com/gh/theoephraim/node-google-spreadsheet) | ||
[![Known Vulnerabilities](https://snyk.io/test/github/theoephraim/node-google-spreadsheet/badge.svg?targetFile=package.json)](https://snyk.io/test/github/theoephraim/node-google-spreadsheet?targetFile=package.json) | ||
[![NPM](https://img.shields.io/npm/dw/google-spreadsheet)](https://www.npmjs.com/package/google-spreadsheet) | ||
[![NPM](https://img.shields.io/npm/dw/google-spreadsheet)](https://www.npmtrends.com/google-spreadsheet) | ||
@@ -14,5 +14,12 @@ - multiple auth options - API key, service account, oauth | ||
## Documentation | ||
## Documentation Site | ||
Full docs are available at [https://theoephraim.github.io/node-google-spreadsheet](https://theoephraim.github.io/node-google-spreadsheet) | ||
> **🚨 Google Deprecation Warning - affects older version (v2) of this module 🚨** | ||
> | ||
> Google is [phasing out their old v3 api](https://cloud.google.com/blog/products/g-suite/migrate-your-apps-use-latest-sheets-api), which the older version of this module used to use. Originally they were going to shut it down on March 3rd 2020, but have pushed that date back to September 30th. | ||
**Regardless, please upgrade to the latest version of this module (v3) which uses the newer sheets v4 API** | ||
------------- | ||
@@ -64,3 +71,3 @@ | ||
// create a sheet and set the header row | ||
const sheet = await doc.addSheet({ headers: ['name', 'email'] }); | ||
const sheet = await doc.addSheet({ headerValues: ['name', 'email'] }); | ||
@@ -67,0 +74,0 @@ // append rows |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
65275
1320
147
10