google-spreadsheet 3.1.15 → 3.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.js +1 -0
- package/Changelog.md +10 -0
- package/README.md +3 -1
- package/TODO +14 -0
- package/lib/GoogleSpreadsheet.js +4 -3
- package/lib/GoogleSpreadsheetCell.js +0 -1
- package/lib/GoogleSpreadsheetWorksheet.js +40 -10
- package/package.json +3 -3
package/.eslintrc.js
CHANGED
package/Changelog.md
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
TODO: set this up to be automated and pulled from git commits... for now just going to leave some notes manually!
|
|
4
|
+
|
|
5
|
+
### 3.2.0 (2021-11-07)
|
|
6
|
+
|
|
7
|
+
- Added `insertDimension` functionality
|
|
8
|
+
- Added custom header row index for row-based API
|
|
9
|
+
- Bumped dependency versions
|
|
10
|
+
- Readme/docs cleanup
|
package/README.md
CHANGED
|
@@ -44,8 +44,10 @@ const { GoogleSpreadsheet } = require('google-spreadsheet');
|
|
|
44
44
|
// Initialize the sheet - doc ID is the long id in the sheets URL
|
|
45
45
|
const doc = new GoogleSpreadsheet('<the sheet ID from the url>');
|
|
46
46
|
|
|
47
|
-
// Initialize Auth - see
|
|
47
|
+
// Initialize Auth - see https://theoephraim.github.io/node-google-spreadsheet/#/getting-started/authentication
|
|
48
48
|
await doc.useServiceAccountAuth({
|
|
49
|
+
// env var values are copied from service account credentials generated by google
|
|
50
|
+
// see "Authentication" section in docs for more info
|
|
49
51
|
client_email: process.env.GOOGLE_SERVICE_ACCOUNT_EMAIL,
|
|
50
52
|
private_key: process.env.GOOGLE_PRIVATE_KEY,
|
|
51
53
|
});
|
package/TODO
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
- support more cell functionality
|
|
2
|
+
- data validation rules - https://github.com/theoephraim/node-google-spreadsheet/issues/487
|
|
3
|
+
|
|
4
|
+
- dont explode if sheet contains charts - https://github.com/theoephraim/node-google-spreadsheet/issues/444
|
|
1
5
|
- add row.toJSON()
|
|
2
6
|
https://github.com/theoephraim/node-google-spreadsheet/issues/247
|
|
3
7
|
- google auth - Application Default Creds
|
|
@@ -16,6 +20,16 @@
|
|
|
16
20
|
https://github.com/theoephraim/node-google-spreadsheet/issues/404
|
|
17
21
|
|
|
18
22
|
|
|
23
|
+
- nice tips here - https://github.com/mikro-orm/mikro-orm/issues/12
|
|
24
|
+
- start using semantic release? or similar https://semantic-release.gitbook.io/semantic-release/
|
|
25
|
+
- better commit messages to autogenerate changelog
|
|
26
|
+
- commitizen - https://github.com/commitizen/cz-cli
|
|
27
|
+
- https://gist.github.com/brianclements/841ea7bffdb01346392c
|
|
28
|
+
- commitlint checks - https://github.com/conventional-changelog/commitlint
|
|
29
|
+
- https://github.com/conventional-changelog/conventional-changelog
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
19
33
|
|
|
20
34
|
|
|
21
35
|
BUGS
|
package/lib/GoogleSpreadsheet.js
CHANGED
|
@@ -299,15 +299,16 @@ class GoogleSpreadsheet {
|
|
|
299
299
|
// https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/request#AddSheetRequest
|
|
300
300
|
|
|
301
301
|
const response = await this._makeSingleUpdateRequest('addSheet', {
|
|
302
|
-
properties: _.omit(properties, 'headers', 'headerValues'),
|
|
302
|
+
properties: _.omit(properties, 'headers', 'headerValues', 'headerRowIndex'),
|
|
303
303
|
});
|
|
304
304
|
// _makeSingleUpdateRequest already adds the sheet
|
|
305
305
|
const newSheetId = response.properties.sheetId;
|
|
306
306
|
const newSheet = this.sheetsById[newSheetId];
|
|
307
307
|
|
|
308
308
|
// allow it to work with `.headers` but `.headerValues` is the real prop
|
|
309
|
-
|
|
310
|
-
|
|
309
|
+
const headers = properties.headerValues || properties.headers;
|
|
310
|
+
if (headers) {
|
|
311
|
+
await newSheet.setHeaderRow(headers, properties.headerRowIndex);
|
|
311
312
|
}
|
|
312
313
|
|
|
313
314
|
return newSheet;
|
|
@@ -20,6 +20,8 @@ class GoogleSpreadsheetWorksheet {
|
|
|
20
20
|
constructor(parentSpreadsheet, { properties, data }) {
|
|
21
21
|
this._spreadsheet = parentSpreadsheet; // the parent GoogleSpreadsheet instance
|
|
22
22
|
|
|
23
|
+
this._headerRowIndex = 1; // assume "header row" (for row-based calls) is in first row
|
|
24
|
+
|
|
23
25
|
// basic properties
|
|
24
26
|
this._rawProperties = properties;
|
|
25
27
|
|
|
@@ -51,6 +53,7 @@ class GoogleSpreadsheetWorksheet {
|
|
|
51
53
|
resetLocalCache(dataOnly) {
|
|
52
54
|
if (!dataOnly) this._rawProperties = null;
|
|
53
55
|
this.headerValues = null;
|
|
56
|
+
this._headerRowIndex = 1;
|
|
54
57
|
this._cells = [];
|
|
55
58
|
}
|
|
56
59
|
|
|
@@ -174,7 +177,6 @@ class GoogleSpreadsheetWorksheet {
|
|
|
174
177
|
return this._cells[rowIndex][columnIndex];
|
|
175
178
|
}
|
|
176
179
|
|
|
177
|
-
|
|
178
180
|
async loadCells(sheetFilters) {
|
|
179
181
|
// load the whole sheet
|
|
180
182
|
if (!sheetFilters) return this._spreadsheet.loadCells(this.a1SheetName);
|
|
@@ -283,11 +285,11 @@ class GoogleSpreadsheetWorksheet {
|
|
|
283
285
|
// });
|
|
284
286
|
// }
|
|
285
287
|
|
|
286
|
-
|
|
287
288
|
// ROW BASED FUNCTIONS ///////////////////////////////////////////////////////////////////////////
|
|
288
289
|
|
|
289
|
-
async loadHeaderRow() {
|
|
290
|
-
|
|
290
|
+
async loadHeaderRow(headerRowIndex) {
|
|
291
|
+
if (headerRowIndex !== undefined) this._headerRowIndex = headerRowIndex;
|
|
292
|
+
const rows = await this.getCellsInRange(`A${this._headerRowIndex}:${this.lastColumnLetter}${this._headerRowIndex}`);
|
|
291
293
|
if (!rows) {
|
|
292
294
|
throw new Error('No values in the header row - fill the first row with header values before trying to interact with rows');
|
|
293
295
|
}
|
|
@@ -298,7 +300,7 @@ class GoogleSpreadsheetWorksheet {
|
|
|
298
300
|
checkForDuplicateHeaders(this.headerValues);
|
|
299
301
|
}
|
|
300
302
|
|
|
301
|
-
async setHeaderRow(headerValues) {
|
|
303
|
+
async setHeaderRow(headerValues, headerRowIndex) {
|
|
302
304
|
if (!headerValues) return;
|
|
303
305
|
if (headerValues.length > this.columnCount) {
|
|
304
306
|
throw new Error(`Sheet is not large enough to fit ${headerValues.length} columns. Resize the sheet first.`);
|
|
@@ -310,15 +312,17 @@ class GoogleSpreadsheetWorksheet {
|
|
|
310
312
|
throw new Error('All your header cells are blank -');
|
|
311
313
|
}
|
|
312
314
|
|
|
315
|
+
if (headerRowIndex) this._headerRowIndex = headerRowIndex;
|
|
316
|
+
|
|
313
317
|
const response = await this._spreadsheet.axios.request({
|
|
314
318
|
method: 'put',
|
|
315
|
-
url: `/values/${this.encodedA1SheetName}
|
|
319
|
+
url: `/values/${this.encodedA1SheetName}!${this._headerRowIndex}:${this._headerRowIndex}`,
|
|
316
320
|
params: {
|
|
317
321
|
valueInputOption: 'USER_ENTERED', // other option is RAW
|
|
318
322
|
includeValuesInResponse: true,
|
|
319
323
|
},
|
|
320
324
|
data: {
|
|
321
|
-
range: `${this.a1SheetName}
|
|
325
|
+
range: `${this.a1SheetName}!${this._headerRowIndex}:${this._headerRowIndex}`,
|
|
322
326
|
majorDimension: 'ROWS',
|
|
323
327
|
values: [[
|
|
324
328
|
...trimmedHeaderValues,
|
|
@@ -369,7 +373,7 @@ class GoogleSpreadsheetWorksheet {
|
|
|
369
373
|
|
|
370
374
|
const response = await this._spreadsheet.axios.request({
|
|
371
375
|
method: 'post',
|
|
372
|
-
url: `/values/${this.encodedA1SheetName}!
|
|
376
|
+
url: `/values/${this.encodedA1SheetName}!A${this._headerRowIndex}:append`,
|
|
373
377
|
params: {
|
|
374
378
|
valueInputOption: options.raw ? 'RAW' : 'USER_ENTERED',
|
|
375
379
|
insertDataOption: options.insert ? 'INSERT_ROWS' : 'OVERWRITE',
|
|
@@ -423,7 +427,7 @@ class GoogleSpreadsheetWorksheet {
|
|
|
423
427
|
|
|
424
428
|
if (!this.headerValues) await this.loadHeaderRow();
|
|
425
429
|
|
|
426
|
-
const firstRow =
|
|
430
|
+
const firstRow = 1 + this._headerRowIndex + options.offset;
|
|
427
431
|
const lastRow = firstRow + options.limit - 1; // inclusive so we subtract 1
|
|
428
432
|
const lastColumn = columnToLetter(this.headerValues.length);
|
|
429
433
|
const rawRows = await this.getCellsInRange(
|
|
@@ -632,9 +636,35 @@ class GoogleSpreadsheetWorksheet {
|
|
|
632
636
|
// https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/request#FindReplaceRequest
|
|
633
637
|
}
|
|
634
638
|
|
|
635
|
-
async insertDimension() {
|
|
639
|
+
async insertDimension(columnsOrRows, range, inheritFromBefore = null) {
|
|
636
640
|
// Request type = `insertDimension`
|
|
637
641
|
// https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/request#InsertDimensionRequest
|
|
642
|
+
|
|
643
|
+
if (!columnsOrRows) throw new Error('You need to specify a dimension. i.e. COLUMNS|ROWS');
|
|
644
|
+
if (!_.isObject(range)) throw new Error('`range` must be an object containing `startIndex` and `endIndex`');
|
|
645
|
+
if (!_.isInteger(range.startIndex) || range.startIndex < 0) throw new Error('range.startIndex must be an integer >=0');
|
|
646
|
+
if (!_.isInteger(range.endIndex) || range.endIndex < 0) throw new Error('range.endIndex must be an integer >=0');
|
|
647
|
+
if (range.endIndex <= range.startIndex) throw new Error('range.endIndex must be greater than range.startIndex');
|
|
648
|
+
|
|
649
|
+
// default inheritFromBefore to true - unless inserting in the first row/column
|
|
650
|
+
if (inheritFromBefore === null) {
|
|
651
|
+
inheritFromBefore = range.startIndex > 0;
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
// do not allow inheritFromBefore if inserting at first row/column
|
|
655
|
+
if (inheritFromBefore && range.startIndex === 0) {
|
|
656
|
+
throw new Error('Cannot set inheritFromBefore to true if inserting in first row/column');
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
return this._makeSingleUpdateRequest('insertDimension', {
|
|
660
|
+
range: {
|
|
661
|
+
sheetId: this.sheetId,
|
|
662
|
+
dimension: columnsOrRows,
|
|
663
|
+
startIndex: range.startIndex,
|
|
664
|
+
endIndex: range.endIndex,
|
|
665
|
+
},
|
|
666
|
+
inheritFromBefore,
|
|
667
|
+
});
|
|
638
668
|
}
|
|
639
669
|
|
|
640
670
|
async insertRange() {
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"author": "Theo Ephraim <theozero@gmail.com> (https://theoephraim.com)",
|
|
3
3
|
"name": "google-spreadsheet",
|
|
4
4
|
"description": "Google Sheets API (v4) -- simple interface to read/write data and manage sheets",
|
|
5
|
-
"version": "3.
|
|
5
|
+
"version": "3.2.0",
|
|
6
6
|
"license": "Unlicense",
|
|
7
7
|
"keywords": [
|
|
8
8
|
"google spreadsheets",
|
|
@@ -26,9 +26,9 @@
|
|
|
26
26
|
"node": ">=0.8.0"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"axios": "^0.21.
|
|
29
|
+
"axios": "^0.21.4",
|
|
30
30
|
"google-auth-library": "^6.1.3",
|
|
31
|
-
"lodash": "^4.17.
|
|
31
|
+
"lodash": "^4.17.21"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
34
|
"delay": "^4.3.0",
|