google-spreadsheet 3.2.0 → 3.3.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/README.md +1 -1
- package/lib/GoogleSpreadsheet.js +39 -0
- package/lib/GoogleSpreadsheetWorksheet.js +29 -4
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -100,7 +100,7 @@ More info:
|
|
|
100
100
|
|
|
101
101
|
### Working with cells
|
|
102
102
|
```javascript
|
|
103
|
-
await sheet.loadCells('A1:E10'); // loads
|
|
103
|
+
await sheet.loadCells('A1:E10'); // loads range of cells into local cache - DOES NOT RETURN THE CELLS
|
|
104
104
|
console.log(sheet.cellStats); // total cells, loaded, how many non-empty
|
|
105
105
|
const a1 = sheet.getCell(0, 0); // access cells using a zero-based index
|
|
106
106
|
const c6 = sheet.getCellByA1('C6'); // or A1 style notation
|
package/lib/GoogleSpreadsheet.js
CHANGED
|
@@ -29,6 +29,7 @@ class GoogleSpreadsheet {
|
|
|
29
29
|
this.authMode = null;
|
|
30
30
|
this._rawSheets = {};
|
|
31
31
|
this._rawProperties = null;
|
|
32
|
+
this._spreadsheetUrl = null;
|
|
32
33
|
|
|
33
34
|
// create an axios instance with sheet root URL and interceptors to handle auth
|
|
34
35
|
this.axios = Axios.create({
|
|
@@ -263,6 +264,7 @@ class GoogleSpreadsheet {
|
|
|
263
264
|
...includeCells && { includeGridData: true },
|
|
264
265
|
},
|
|
265
266
|
});
|
|
267
|
+
this._spreadsheetUrl = response.data.spreadsheetUrl;
|
|
266
268
|
this._rawProperties = response.data.properties;
|
|
267
269
|
_.each(response.data.sheets, (s) => this._updateOrCreateSheet(s));
|
|
268
270
|
}
|
|
@@ -382,6 +384,43 @@ class GoogleSpreadsheet {
|
|
|
382
384
|
const { sheets } = result.data;
|
|
383
385
|
_.each(sheets, (sheet) => { this._updateOrCreateSheet(sheet); });
|
|
384
386
|
}
|
|
387
|
+
|
|
388
|
+
// EXPORTING /////////////////////////////////////////////////////////////
|
|
389
|
+
async _downloadAs(fileType, worksheetId, returnStreamInsteadOfBuffer) {
|
|
390
|
+
// see https://stackoverflow.com/questions/11619805/using-the-google-drive-api-to-download-a-spreadsheet-in-csv-format/51235960#51235960
|
|
391
|
+
|
|
392
|
+
if (['html', 'xlsx', 'ods'].includes(fileType)) {
|
|
393
|
+
if (worksheetId) throw new Error(`Cannot specify worksheetId when exporting as ${fileType}`);
|
|
394
|
+
} else if (['csv', 'tsv', 'pdf'].includes(fileType)) {
|
|
395
|
+
if (worksheetId === undefined) throw new Error(`Must specify worksheetId when exporting as ${fileType}`);
|
|
396
|
+
} else {
|
|
397
|
+
throw new Error(`unsupported export fileType - ${fileType}`);
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
// google UI shows "html" but passes through "zip"
|
|
401
|
+
if (fileType === 'html') fileType = 'zip';
|
|
402
|
+
|
|
403
|
+
const exportUrl = this._spreadsheetUrl.replace('/edit', '/export');
|
|
404
|
+
const response = await this.axios.get(exportUrl, {
|
|
405
|
+
baseUrl: '', // unset baseUrl since we're not hitting the normal sheets API
|
|
406
|
+
params: {
|
|
407
|
+
id: this.spreadsheetId,
|
|
408
|
+
format: fileType,
|
|
409
|
+
...worksheetId && { gid: worksheetId },
|
|
410
|
+
},
|
|
411
|
+
responseType: returnStreamInsteadOfBuffer ? 'stream' : 'arraybuffer',
|
|
412
|
+
});
|
|
413
|
+
return response.data;
|
|
414
|
+
}
|
|
415
|
+
async downloadAsHTML(returnStreamInsteadOfBuffer = false) {
|
|
416
|
+
return this._downloadAs('html', null, returnStreamInsteadOfBuffer);
|
|
417
|
+
}
|
|
418
|
+
async downloadAsXLSX(returnStreamInsteadOfBuffer = false) {
|
|
419
|
+
return this._downloadAs('xlsx', null, returnStreamInsteadOfBuffer);
|
|
420
|
+
}
|
|
421
|
+
async downloadAsODS(returnStreamInsteadOfBuffer = false) {
|
|
422
|
+
return this._downloadAs('ods', null, returnStreamInsteadOfBuffer);
|
|
423
|
+
}
|
|
385
424
|
}
|
|
386
425
|
|
|
387
426
|
module.exports = GoogleSpreadsheet;
|
|
@@ -444,6 +444,13 @@ class GoogleSpreadsheetWorksheet {
|
|
|
444
444
|
return rows;
|
|
445
445
|
}
|
|
446
446
|
|
|
447
|
+
async clearRows(options = {}) {
|
|
448
|
+
// default to first row after header
|
|
449
|
+
const startRowIndex = options.start || this._headerRowIndex + 1;
|
|
450
|
+
const endRowIndex = options.end || this.rowCount;
|
|
451
|
+
await this._spreadsheet.axios.post(`/values/${this.encodedA1SheetName}!${startRowIndex}:${endRowIndex}:clear`);
|
|
452
|
+
}
|
|
453
|
+
|
|
447
454
|
// BASIC PROPS ///////////////////////////////////////////////////////////////////////////////////
|
|
448
455
|
async updateProperties(properties) {
|
|
449
456
|
// Request type = `updateSheetProperties`
|
|
@@ -626,9 +633,17 @@ class GoogleSpreadsheetWorksheet {
|
|
|
626
633
|
// https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/request#DuplicateFilterViewRequest
|
|
627
634
|
}
|
|
628
635
|
|
|
629
|
-
async
|
|
636
|
+
async duplicate(options = {}) {
|
|
630
637
|
// Request type = `duplicateSheet`
|
|
631
638
|
// https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/request#DuplicateSheetRequest
|
|
639
|
+
const response = await this._makeSingleUpdateRequest('duplicateSheet', {
|
|
640
|
+
sourceSheetId: this.sheetId,
|
|
641
|
+
...options.index !== undefined && { insertSheetIndex: options.index },
|
|
642
|
+
...options.id && { newSheetId: options.id },
|
|
643
|
+
...options.title && { newSheetName: options.title },
|
|
644
|
+
});
|
|
645
|
+
const newSheetId = response.properties.sheetId;
|
|
646
|
+
return this._spreadsheet.sheetsById[newSheetId];
|
|
632
647
|
}
|
|
633
648
|
|
|
634
649
|
async findReplace() {
|
|
@@ -850,12 +865,22 @@ class GoogleSpreadsheetWorksheet {
|
|
|
850
865
|
});
|
|
851
866
|
}
|
|
852
867
|
|
|
853
|
-
async clear() {
|
|
854
|
-
// clears
|
|
868
|
+
async clear(a1Range) {
|
|
869
|
+
// clears data in the sheet - defaults to entire sheet
|
|
870
|
+
const range = a1Range ? `!${a1Range}` : '';
|
|
855
871
|
// sheet name without ie 'sheet1' rather than 'sheet1'!A1:B5 is all cells
|
|
856
|
-
await this._spreadsheet.axios.post(`/values/${this.encodedA1SheetName}:clear`);
|
|
872
|
+
await this._spreadsheet.axios.post(`/values/${this.encodedA1SheetName}${range}:clear`);
|
|
857
873
|
this.resetLocalCache(true);
|
|
858
874
|
}
|
|
875
|
+
async downloadAsCSV(returnStreamInsteadOfBuffer = false) {
|
|
876
|
+
return this._spreadsheet._downloadAs('csv', this.sheetId, returnStreamInsteadOfBuffer);
|
|
877
|
+
}
|
|
878
|
+
async downloadAsTSV(returnStreamInsteadOfBuffer = false) {
|
|
879
|
+
return this._spreadsheet._downloadAs('tsv', this.sheetId, returnStreamInsteadOfBuffer);
|
|
880
|
+
}
|
|
881
|
+
async downloadAsPDF(returnStreamInsteadOfBuffer = false) {
|
|
882
|
+
return this._spreadsheet._downloadAs('pdf', this.sheetId, returnStreamInsteadOfBuffer);
|
|
883
|
+
}
|
|
859
884
|
}
|
|
860
885
|
|
|
861
886
|
module.exports = GoogleSpreadsheetWorksheet;
|
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.3.0",
|
|
6
6
|
"license": "Unlicense",
|
|
7
7
|
"keywords": [
|
|
8
8
|
"google spreadsheets",
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"api",
|
|
17
17
|
"googleapis"
|
|
18
18
|
],
|
|
19
|
-
"homepage": "https://github.
|
|
19
|
+
"homepage": "https://theoephraim.github.io/node-google-spreadsheet",
|
|
20
20
|
"repository": {
|
|
21
21
|
"type": "git",
|
|
22
22
|
"url": "git://github.com/theoephraim/node-google-spreadsheet.git"
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
34
|
"delay": "^4.3.0",
|
|
35
|
-
"docsify-cli": "^4.4.
|
|
35
|
+
"docsify-cli": "^4.4.3",
|
|
36
36
|
"eslint": "^6.8.0",
|
|
37
37
|
"eslint-config-airbnb-base": "^14.0.0",
|
|
38
38
|
"eslint-plugin-async-await": "0.0.0",
|