-
Notifications
You must be signed in to change notification settings - Fork 4
/
google-sheet.ts
352 lines (320 loc) · 10.5 KB
/
google-sheet.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import { google, sheets_v4 } from 'googleapis';
import get from 'lodash.get';
import { colToA, getLongestArray, getRange, parseRange } from './utils';
export namespace GoogleSheetCli {
export interface Credentials {
client_email: string;
private_key: string;
}
export type RawData = (string | number | boolean | null)[][];
export enum ValueInputOption {
USER_ENTERED = 'USER_ENTERED',
RAW = 'RAW',
}
export interface QueryOptions {
minCol?: number;
maxCol?: number;
minRow?: number;
maxRow?: number;
range?: string;
valueInputOption?: ValueInputOption;
worksheetTitle?: string | null;
hasHeaderRow?: boolean;
}
export interface FormattedData {
[name: string]: string;
}
export interface SheetData {
rawData: RawData;
formatted: FormattedData[];
header: string[];
range?: string | null;
}
}
const GOOGLE_FEED_URL = 'https://spreadsheets.google.com/feeds/';
/**
* GoogleSheet helper class for CRUD operations
*
* @export
* @class GoogleSheet
*/
export default class GoogleSheet {
private sheets!: sheets_v4.Sheets;
/**
* Creates an instance of GoogleSheet.
* @param {string} [spreadsheetId]
* @param {string} [worksheetTitle]
* @memberof GoogleSheet
*/
constructor(private spreadsheetId?: string, private worksheetTitle?: string | null) {}
/**
* Authorize with credentials
*
* @param {GoogleSheetCli.Credentials} credentials
* @returns {Promise<void>}
* @memberof GoogleSheet
*/
async authorize(credentials: GoogleSheetCli.Credentials): Promise<void> {
credentials.private_key = credentials.private_key.replace(/\\n/g, '\n');
// Create the JWT client
const auth = await google.auth.getClient({ credentials, scopes: [GOOGLE_FEED_URL] });
this.sheets = google.sheets({ version: 'v4', auth });
}
/**
* Get information about the spreadsheet.
*
* @param {string} [spreadsheetId]
* @returns {Promise<sheets_v4.Schema$Spreadsheet>}
* @memberof GoogleSheet
*/
async getSpreadsheet(spreadsheetId?: string): Promise<sheets_v4.Schema$Spreadsheet> {
const { data: sheet } = await this.sheets.spreadsheets.get({
spreadsheetId: spreadsheetId || this.spreadsheetId,
});
this.sheets.spreadsheets.sheets;
if (!sheet) throw `Spreadsheet "${spreadsheetId || this.spreadsheetId}" not found`;
return sheet;
}
/**
* Get information about the worksheet.
*
* @param {string} title
* @param {string} [spreadsheetId]
* @returns {Promise<sheets_v4.Schema$Sheet>}
* @memberof GoogleSheet
*/
async getWorksheet(title: string, spreadsheetId?: string): Promise<sheets_v4.Schema$Sheet> {
const { sheets = [], properties: { title: ssTitle = '' } = {} } = await this.getSpreadsheet(spreadsheetId);
const sheet = sheets.find(({ properties: { title: ws } = {} }) => ws === title);
if (!sheet) throw `Sheet "${title}" not found in "${ssTitle}"`;
this.worksheetTitle = get(sheet, 'properties.title');
return sheet;
}
/**
* Get the data of the specified cells (or every available cell data)
*
* @param {GoogleSheetCli.QueryOptions} [options={}]
* @param {string} [spreadsheetId]
* @returns {Promise<GoogleSheetCli.SheetData>}
* @memberof GoogleSheet
*/
async getData(options: GoogleSheetCli.QueryOptions = {}, spreadsheetId?: string): Promise<GoogleSheetCli.SheetData> {
options.worksheetTitle = options.worksheetTitle || this.worksheetTitle;
if (options.range) {
const parsedOptions = parseRange(options.range);
if (parsedOptions.worksheetTitle) {
options.worksheetTitle = parsedOptions.worksheetTitle;
}
if (parsedOptions.minCol) {
options.minCol = parsedOptions.minCol;
}
if (parsedOptions.maxCol) {
options.maxCol = parsedOptions.maxCol;
}
if (parsedOptions.minRow) {
options.minRow = parsedOptions.minRow;
}
if (parsedOptions.maxRow) {
options.maxRow = parsedOptions.maxRow;
}
}
if (!options.worksheetTitle) {
throw 'Option property "worksheetTitle" is required';
}
const sheet = await this.getWorksheet(options.worksheetTitle, spreadsheetId);
const { rowCount = 0, columnCount = 0 } = sheet?.properties?.gridProperties || {};
const sanitizedOptions: GoogleSheetCli.QueryOptions = {
...options,
maxCol: options.maxCol || columnCount || 0,
maxRow: options.maxRow || rowCount || 0,
};
const res = await this.sheets.spreadsheets.values.get({
spreadsheetId: spreadsheetId || this.spreadsheetId,
range: getRange(sanitizedOptions),
});
const range = get(res, 'data.range');
let values = get(res, 'data.values');
let header: string[] = [];
if (sanitizedOptions.hasHeaderRow) {
if (!sanitizedOptions.minRow || sanitizedOptions.minRow <= 1) {
[header, ...values] = values || [];
} else {
const res = await this.sheets.spreadsheets.values.get({
spreadsheetId: spreadsheetId || this.spreadsheetId,
range: getRange({
...sanitizedOptions,
worksheetTitle: options.worksheetTitle,
minRow: 1,
maxRow: 1,
range: undefined,
}),
});
[header] = get(res, 'data.values', [[]]);
if (!header.length) throw 'No header row exists';
}
}
let maxCol = (sanitizedOptions.maxCol ? sanitizedOptions.maxCol + 1 : 0) - (sanitizedOptions.minCol || 0);
let maxRow = 0;
if (values) {
maxCol = getLongestArray(values).length;
maxRow = values.length;
}
// fill missing headings
for (let c = 0; c < maxCol; c++) {
header[c] = header[c] || `(${colToA(c + (sanitizedOptions.minCol || 0))})`;
}
let formatted: GoogleSheetCli.FormattedData[] = [];
let rawData: GoogleSheetCli.RawData = [];
for (let r = 0; r < maxRow; r++) {
const row = values?.[r] || [];
const rawRow = [];
let set = {};
for (let c = 0; c < maxCol; c++) {
const heading = header[c];
const cell = row[c] || '';
rawRow[c] = cell;
set = { ...set, [heading]: cell };
}
formatted = [...formatted, set];
rawData = [...rawData, rawRow];
}
return { rawData, formatted, header, range };
}
/**
* Append row data to a worksheet, starting after the last row in a specific column
*
* @param {GoogleSheetCli.RawData} data
* @param {GoogleSheetCli.QueryOptions} options
* @param {string} [spreadsheetId]
* @returns {Promise<void>}
* @memberof GoogleSheet
*/
async appendData(data: GoogleSheetCli.RawData, options: GoogleSheetCli.QueryOptions, spreadsheetId?: string): Promise<void> {
const { rawData }: GoogleSheetCli.SheetData = await this.getData(options, spreadsheetId);
options.minRow = rawData.length + 1;
await this.updateData(data, options, spreadsheetId);
}
/**
* Update the data starting at a specific row and column
*
* @param {GoogleSheetCli.RawData} data [['A1', 'A2', 'A3', 'A4', 'A5'], ['B1', 'B2', 'B3', 'B4', 'B5', 'B6']]
* @param {GoogleSheetCli.QueryOptions} options
* @param {string} [spreadsheetId]
* @returns {Promise<void>}
* @memberof GoogleSheet
*/
async updateData(data: GoogleSheetCli.RawData, options: GoogleSheetCli.QueryOptions, spreadsheetId?: string): Promise<void> {
options.worksheetTitle = options.worksheetTitle || this.worksheetTitle;
if (!options.worksheetTitle) throw 'Specify worksheetTitle';
if (!Array.isArray(data) || !data.every(Array.isArray)) throw 'Check "data" property - has to be supplied as nested array ([["1", "2"], ["3", "4"]])';
const range = getRange(options);
await this.sheets.spreadsheets.values.update({
spreadsheetId: spreadsheetId || this.spreadsheetId,
valueInputOption: options.valueInputOption || GoogleSheetCli.ValueInputOption.RAW,
range,
requestBody: {
values: data,
},
});
}
/**
* Add a worksheet with title
*
* @param {string} title
* @param {string} [spreadsheetId]
* @returns {Promise<sheets_v4.Schema$Sheet>}
* @memberof GoogleSheet
*/
async addWorksheet(title: string, spreadsheetId?: string): Promise<sheets_v4.Schema$Sheet | undefined> {
const response = await this.sheets.spreadsheets.batchUpdate({
spreadsheetId: spreadsheetId || this.spreadsheetId,
requestBody: {
requests: [
{
addSheet: {
properties: {
title,
},
},
},
],
},
});
const sheet = get(response, 'data.replies[0].addSheet');
this.worksheetTitle = get(sheet, 'properties.title');
return sheet;
}
/**
* Remove a worksheet by title
*
* @param {string} title
* @param {string} [spreadsheetId]
* @returns {Promise<void>}
* @memberof GoogleSheet
*/
async removeWorksheet(title: string, spreadsheetId?: string): Promise<void> {
const sheet = await this.getWorksheet(title, spreadsheetId);
await this.sheets.spreadsheets.batchUpdate({
spreadsheetId: spreadsheetId || this.spreadsheetId,
requestBody: {
requests: [
{
deleteSheet: {
sheetId: sheet.properties?.sheetId || -1,
},
},
],
},
});
this.worksheetTitle = '';
}
/**
* Rename a worksheet by title
*
* @param {string} title
* @param {string} newTitle
* @param {string} [spreadsheetId]
* @returns {Promise<void>}
* @memberof GoogleSheet
*/
async renameWorksheet(title: string, newTitle: string, spreadsheetId?: string): Promise<void> {
const worksheet = await this.getWorksheet(title, spreadsheetId);
await this.sheets.spreadsheets.batchUpdate({
spreadsheetId: spreadsheetId || this.spreadsheetId,
requestBody: {
requests: [
{
updateSheetProperties: {
properties: {
sheetId: worksheet.properties?.sheetId || -1,
title: newTitle,
},
fields: 'title',
},
},
],
},
});
this.worksheetTitle = newTitle;
}
/**
* Add a spreadsheet with title
*
* @param {string} title
* @returns {Promise<sheets_v4.Schema$Spreadsheet>}
* @memberof GoogleSheet
*/
async addSpreadsheet(title: string): Promise<sheets_v4.Schema$Spreadsheet> {
const { data: sheet } = await this.sheets.spreadsheets.create({
requestBody: {
properties: {
title,
},
},
});
if (sheet.spreadsheetId) {
this.spreadsheetId = sheet.spreadsheetId;
}
return sheet;
}
}