-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoogleSheetsUtils.ts
More file actions
103 lines (93 loc) · 2.81 KB
/
Copy pathgoogleSheetsUtils.ts
File metadata and controls
103 lines (93 loc) · 2.81 KB
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
import { google, sheets_v4 } from 'googleapis';
import { FeedbackRecord, Feedback } from '../types';
const SHEETS_COLUMN_MAP: { [K in Feedback]: 'A' | 'B' | 'C' | 'D' | 'E' } = {
[Feedback.Timestamp]: 'A',
[Feedback.PageURL]: 'B',
[Feedback.Rating]: 'C',
[Feedback.Comment]: 'D',
[Feedback.Email]: 'E'
};
const SHEET_NAME = 'Sheet1';
export async function getAuthClient(clientEmail: string, privateKey: string) {
try {
const auth = new google.auth.JWT(
clientEmail,
undefined,
privateKey.replace(/\\n/g, '\n'),
['https://www.googleapis.com/auth/spreadsheets']
);
await auth.authorize();
return google.sheets({ version: 'v4', auth });
} catch (e) {
const message = e instanceof Error ? e.message : 'No further details';
throw new Error(`Google Sheets API failed to authorize: ${message}`);
}
}
export async function createFeedback(
sheetsClient: sheets_v4.Sheets,
sheetId: string,
pageURL: string,
rating: boolean,
comment?: string,
email?: string
): Promise<number> {
const newRecord: FeedbackRecord = {
date: Date.now(),
pageUrl: pageURL,
rating: rating
};
if (comment != null) {
newRecord.comment = comment;
}
if (email != null) {
newRecord.comment = email;
}
try {
const result = await sheetsClient.spreadsheets.values.append({
spreadsheetId: sheetId,
range: SHEET_NAME,
valueInputOption: 'RAW',
requestBody: {
values: [Object.values(newRecord)]
}
});
if (result.data.updates?.updatedRange == null) {
throw new Error('No updated range');
}
return getRowFromAppendRange(result.data.updates.updatedRange);
} catch (e) {
const message = e instanceof Error ? e.message : 'No further details';
throw Error(`Google Sheets API failed to create feedback row: ${message}`);
}
}
export async function updateFeedback(
sheetsClient: sheets_v4.Sheets,
sheetId: string,
feedbackId: number,
valueType: Feedback,
value: string
): Promise<number> {
try {
const result = await sheetsClient.spreadsheets.values.update({
spreadsheetId: sheetId,
range: SHEET_NAME + '!' + SHEETS_COLUMN_MAP[valueType] + feedbackId,
valueInputOption: 'RAW',
requestBody: {
values: [[value]]
}
});
if (result.data.updatedRange == null) {
throw new Error('No updated range');
}
return getRowFromUpdateRange(result.data.updatedRange);
} catch (e) {
const message = e instanceof Error ? e.message : 'No further details';
throw Error(`Google Sheets API failed to update feedback row: ${message}`);
}
}
export function getRowFromAppendRange(sheetsRange: string) {
return parseInt(sheetsRange.split(':')[1].slice(1));
}
export function getRowFromUpdateRange(sheetsRange: string) {
return parseInt(sheetsRange.split('!')[1].slice(1));
}