-
Notifications
You must be signed in to change notification settings - Fork 0
/
googleDocsWrapper.ts
78 lines (59 loc) · 2.38 KB
/
googleDocsWrapper.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
import { GoogleSpreadsheet, GoogleSpreadsheetWorksheet } from "google-spreadsheet";
export type CurrentReportWorkSheet = 'Meta Data' | 'Content Overview' | 'Pages Per Category' | 'Pages Per Org Unit' | 'Content Graph' | 'Connection List';
export type DataOverTimeWorkSheet = 'Content Types';
class GoogleDoc {
public doc: GoogleSpreadsheet;
protected _initialised = false;
public get initialised(): boolean {
return this._initialised;
}
constructor(id: string) {
this.doc = new GoogleSpreadsheet(id);
}
public async initialise(): Promise<void> {
if (!process.env.GOOGLE_SERVICE_ACCOUNT_EMAIL || !process.env.GOOGLE_SERVICE_ACCOUNT_PRIVATE_KEY)
throw Error("Could not find credentials");
await this.doc.useServiceAccountAuth({
client_email: process.env.GOOGLE_SERVICE_ACCOUNT_EMAIL,
private_key: process.env.GOOGLE_SERVICE_ACCOUNT_PRIVATE_KEY.replace(/\\n/gm, '\n'),
});
await this.doc.loadInfo();
this._initialised = true;
}
}
export class CurrentReportDoc extends GoogleDoc {
private static _instance: CurrentReportDoc;
public static get instance(): CurrentReportDoc {
return this._instance || (this._instance = new this());
}
private constructor() {
if (!process.env.CURRENT_REPORT_SPREADSHEET_ID) {
throw ('Could not find current-report sheet id');
}
super(process.env.CURRENT_REPORT_SPREADSHEET_ID);
}
public async getSheet(sheetTitle: CurrentReportWorkSheet): Promise<GoogleSpreadsheetWorksheet> {
if (!this._initialised) {
await this.initialise();
}
return this.doc.sheetsByTitle[sheetTitle];
}
}
export class DataOverTimeDoc extends GoogleDoc {
private static _instance: DataOverTimeDoc;
public static get instance(): DataOverTimeDoc {
return this._instance || (this._instance = new this());
}
private constructor() {
if (!process.env.DATA_OVER_TIME_SPREADSHEET_ID) {
throw ('Could not find data-over-time sheet id')
}
super(process.env.DATA_OVER_TIME_SPREADSHEET_ID);
}
public async getSheet(sheetTitle: DataOverTimeWorkSheet): Promise<GoogleSpreadsheetWorksheet> {
if (!this._initialised) {
await this.initialise();
}
return this.doc.sheetsByTitle[sheetTitle];
}
}