google-spreadsheet 5.1.0 → 5.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/README.md +2 -0
- package/dist/index.cjs +27 -6
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +25 -7
- package/dist/index.d.ts +25 -7
- package/dist/index.js +27 -6
- package/dist/index.js.map +1 -1
- package/package.json +3 -2
- package/src/lib/GoogleSpreadsheet.ts +47 -11
- package/src/lib/GoogleSpreadsheetCell.ts +2 -4
- package/src/lib/GoogleSpreadsheetRow.ts +10 -1
- package/src/lib/GoogleSpreadsheetWorksheet.ts +5 -2
- package/src/lib/types/sheets-types.ts +2 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "google-spreadsheet",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.2.0",
|
|
4
4
|
"description": "Google Sheets API -- simple interface to read/write data and manage sheets",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"google spreadsheets",
|
|
@@ -66,6 +66,7 @@
|
|
|
66
66
|
"eslint-plugin-import": "^2.27.5",
|
|
67
67
|
"eslint-plugin-no-floating-promise": "^1.0.2",
|
|
68
68
|
"google-auth-library": "^10.5.0",
|
|
69
|
+
"nock": "^14.0.11",
|
|
69
70
|
"tsdown": "^0.20.3",
|
|
70
71
|
"typescript": "^5.9.3",
|
|
71
72
|
"varlock": "^0.2.2",
|
|
@@ -79,4 +80,4 @@
|
|
|
79
80
|
"optional": true
|
|
80
81
|
}
|
|
81
82
|
}
|
|
82
|
-
}
|
|
83
|
+
}
|
|
@@ -1,10 +1,18 @@
|
|
|
1
|
-
import ky, { HTTPError, KyInstance } from 'ky'; // eslint-disable-line import/no-extraneous-dependencies
|
|
1
|
+
import ky, { HTTPError, KyInstance, RetryOptions } from 'ky'; // eslint-disable-line import/no-extraneous-dependencies
|
|
2
2
|
import * as _ from './toolkit';
|
|
3
3
|
import { GoogleSpreadsheetWorksheet } from './GoogleSpreadsheetWorksheet';
|
|
4
4
|
import { getFieldMask } from './utils';
|
|
5
5
|
import {
|
|
6
|
-
DataFilter,
|
|
7
|
-
|
|
6
|
+
DataFilter,
|
|
7
|
+
DataFilterObject,
|
|
8
|
+
DeveloperMetadata,
|
|
9
|
+
GridRange,
|
|
10
|
+
NamedRangeId,
|
|
11
|
+
ProtectedRange,
|
|
12
|
+
SpreadsheetId,
|
|
13
|
+
SpreadsheetProperties,
|
|
14
|
+
WorksheetId,
|
|
15
|
+
WorksheetProperties,
|
|
8
16
|
} from './types/sheets-types';
|
|
9
17
|
import { PermissionRoles, PermissionsList, PublicPermissionRoles } from './types/drive-types';
|
|
10
18
|
import { RecursivePartial } from './types/util-types';
|
|
@@ -112,11 +120,20 @@ export class GoogleSpreadsheet {
|
|
|
112
120
|
* @category Initialization
|
|
113
121
|
* */
|
|
114
122
|
constructor(
|
|
115
|
-
/** id of
|
|
123
|
+
/** id of Google spreadsheet doc */
|
|
116
124
|
spreadsheetId: SpreadsheetId,
|
|
117
125
|
/** authentication to use with Google Sheets API */
|
|
118
|
-
auth: GoogleApiAuth
|
|
126
|
+
auth: GoogleApiAuth,
|
|
127
|
+
/** Additional options */
|
|
128
|
+
options?: {
|
|
129
|
+
/**
|
|
130
|
+
* customize retry behavior --
|
|
131
|
+
* see the [ky documentation](https://github.com/sindresorhus/ky#retry) for details of the available options and defaults.
|
|
132
|
+
* */
|
|
133
|
+
retryConfig?: RetryOptions | number
|
|
134
|
+
}
|
|
119
135
|
) {
|
|
136
|
+
const { retryConfig } = options || {};
|
|
120
137
|
this.spreadsheetId = spreadsheetId;
|
|
121
138
|
this.auth = auth;
|
|
122
139
|
|
|
@@ -131,6 +148,7 @@ export class GoogleSpreadsheet {
|
|
|
131
148
|
beforeRequest: [(r) => this._setAuthRequestHook(r)],
|
|
132
149
|
beforeError: [(e) => this._errorHook(e)],
|
|
133
150
|
},
|
|
151
|
+
retry: retryConfig,
|
|
134
152
|
});
|
|
135
153
|
this.driveApi = ky.create({
|
|
136
154
|
prefixUrl: `${DRIVE_API_BASE_URL}/${spreadsheetId}`,
|
|
@@ -138,6 +156,7 @@ export class GoogleSpreadsheet {
|
|
|
138
156
|
beforeRequest: [(r) => this._setAuthRequestHook(r)],
|
|
139
157
|
beforeError: [(e) => this._errorHook(e)],
|
|
140
158
|
},
|
|
159
|
+
retry: retryConfig,
|
|
141
160
|
});
|
|
142
161
|
}
|
|
143
162
|
|
|
@@ -395,15 +414,12 @@ export class GoogleSpreadsheet {
|
|
|
395
414
|
async loadCells(
|
|
396
415
|
/**
|
|
397
416
|
* single filter or array of filters
|
|
398
|
-
* strings are treated as A1 ranges, objects are treated as GridRange objects
|
|
417
|
+
* strings are treated as A1 ranges, objects are treated as GridRange objects,
|
|
418
|
+
* objects with a `developerMetadataLookup` key are treated as DeveloperMetadataLookup filters
|
|
399
419
|
* pass nothing to fetch all cells
|
|
400
420
|
* */
|
|
401
421
|
filters?: DataFilter | DataFilter[]
|
|
402
422
|
) {
|
|
403
|
-
// TODO: make it support DeveloperMetadataLookup objects
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
423
|
// TODO: switch to this mode if using a read-only auth token?
|
|
408
424
|
const readOnlyMode = this.authMode === AUTH_MODES.API_KEY;
|
|
409
425
|
|
|
@@ -416,7 +432,9 @@ export class GoogleSpreadsheet {
|
|
|
416
432
|
if (readOnlyMode) {
|
|
417
433
|
throw new Error('Only A1 ranges are supported when fetching cells with read-only access (using only an API key)');
|
|
418
434
|
}
|
|
419
|
-
|
|
435
|
+
if ('developerMetadataLookup' in filter) {
|
|
436
|
+
return { developerMetadataLookup: filter.developerMetadataLookup };
|
|
437
|
+
}
|
|
420
438
|
return { gridRange: filter };
|
|
421
439
|
}
|
|
422
440
|
throw new Error('Each filter must be an A1 range string or a gridrange object');
|
|
@@ -631,6 +649,24 @@ export class GoogleSpreadsheet {
|
|
|
631
649
|
await this.driveApi.delete(`permissions/${permissionId}`);
|
|
632
650
|
}
|
|
633
651
|
|
|
652
|
+
// DEVELOPER METADATA ///////////////////////////////////////////////////////////////////////////
|
|
653
|
+
|
|
654
|
+
/**
|
|
655
|
+
* search for developer metadata entries matching the given filters
|
|
656
|
+
* @see https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.developerMetadata/search
|
|
657
|
+
*/
|
|
658
|
+
async searchDeveloperMetadata(
|
|
659
|
+
/** array of DataFilter objects to match against */
|
|
660
|
+
filters: DataFilterObject[]
|
|
661
|
+
): Promise<DeveloperMetadata[]> {
|
|
662
|
+
const response = await this.sheetsApi.post('developerMetadata:search', {
|
|
663
|
+
json: { dataFilters: filters },
|
|
664
|
+
});
|
|
665
|
+
const data = await response.json<any>();
|
|
666
|
+
if (!data.matchedDeveloperMetadata) return [];
|
|
667
|
+
return data.matchedDeveloperMetadata.map((m: any) => m.developerMetadata);
|
|
668
|
+
}
|
|
669
|
+
|
|
634
670
|
//
|
|
635
671
|
// CREATE NEW DOC ////////////////////////////////////////////////////////////////////////////////
|
|
636
672
|
static async createNewSpreadsheetDocument(auth: GoogleApiAuth, properties?: Partial<SpreadsheetProperties>) {
|
|
@@ -152,10 +152,8 @@ export class GoogleSpreadsheetCell {
|
|
|
152
152
|
return this.value as string;
|
|
153
153
|
}
|
|
154
154
|
set stringValue(val: string | undefined) {
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
}
|
|
158
|
-
this.value = val;
|
|
155
|
+
this._draftData.valueType = 'stringValue';
|
|
156
|
+
this._draftData.value = val || '';
|
|
159
157
|
}
|
|
160
158
|
|
|
161
159
|
/**
|
|
@@ -13,7 +13,15 @@ export class GoogleSpreadsheetRow<T extends Record<string, any> = Record<string,
|
|
|
13
13
|
/** raw underlying data for row */
|
|
14
14
|
private _rawData: any[]
|
|
15
15
|
) {
|
|
16
|
+
this._padRawData();
|
|
17
|
+
}
|
|
16
18
|
|
|
19
|
+
/** pad _rawData with empty strings so it always matches header length */
|
|
20
|
+
private _padRawData() {
|
|
21
|
+
const headerLength = this._worksheet.headerValues.length;
|
|
22
|
+
while (this._rawData.length < headerLength) {
|
|
23
|
+
this._rawData.push('');
|
|
24
|
+
}
|
|
17
25
|
}
|
|
18
26
|
|
|
19
27
|
private _deleted = false;
|
|
@@ -92,7 +100,8 @@ export class GoogleSpreadsheetRow<T extends Record<string, any> = Record<string,
|
|
|
92
100
|
},
|
|
93
101
|
});
|
|
94
102
|
const data = await response.json<any>();
|
|
95
|
-
this._rawData = data.updatedData.values[0];
|
|
103
|
+
this._rawData = data.updatedData.values?.[0] || [];
|
|
104
|
+
this._padRawData();
|
|
96
105
|
}
|
|
97
106
|
|
|
98
107
|
/** delete this row */
|
|
@@ -250,7 +250,10 @@ export class GoogleSpreadsheetWorksheet {
|
|
|
250
250
|
return `${this.a1SheetName}!${filter}`;
|
|
251
251
|
}
|
|
252
252
|
if (_.isObject(filter)) {
|
|
253
|
-
//
|
|
253
|
+
// pass through developer metadata filters without adding sheetId
|
|
254
|
+
if ('developerMetadataLookup' in filter) {
|
|
255
|
+
return filter;
|
|
256
|
+
}
|
|
254
257
|
|
|
255
258
|
// check if the user passed in a sheet id
|
|
256
259
|
const filterAny = filter as any;
|
|
@@ -714,7 +717,7 @@ export class GoogleSpreadsheetWorksheet {
|
|
|
714
717
|
*/
|
|
715
718
|
async updateDimensionProperties(
|
|
716
719
|
columnsOrRows: WorksheetDimension,
|
|
717
|
-
properties: WorksheetDimensionProperties
|
|
720
|
+
properties: Partial<WorksheetDimensionProperties>,
|
|
718
721
|
bounds: Partial<DimensionRangeIndexes>
|
|
719
722
|
) {
|
|
720
723
|
// Request type = `updateDimensionProperties`
|
|
@@ -410,8 +410,8 @@ export type GridRange = {
|
|
|
410
410
|
};
|
|
411
411
|
export type GridRangeWithoutWorksheetId = Omit<GridRange, 'sheetId'>;
|
|
412
412
|
export type GridRangeWithOptionalWorksheetId = MakeOptional<GridRange, 'sheetId'>;
|
|
413
|
-
export type DataFilter = A1Range | GridRange;
|
|
414
|
-
export type DataFilterWithoutWorksheetId = A1Range | GridRangeWithoutWorksheetId;
|
|
413
|
+
export type DataFilter = A1Range | GridRange | DataFilterObject;
|
|
414
|
+
export type DataFilterWithoutWorksheetId = A1Range | GridRangeWithoutWorksheetId | DataFilterObject;
|
|
415
415
|
|
|
416
416
|
/**
|
|
417
417
|
* A coordinate in a sheet. All indexes are zero-based.
|