-
Notifications
You must be signed in to change notification settings - Fork 0
/
publicGoogleSheetsParser.ts
53 lines (45 loc) 路 1.51 KB
/
publicGoogleSheetsParser.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
import PublicGoogleSheetsParser from 'public-google-sheets-parser';
import { FilledData } from './types/data';
import { filledDataToObject } from './utils/filledDataToObject';
type PublicGoogleSheetsParserParams = {
path: string[];
typeName: string;
};
type PublicGoogleSheetsParserOptions = {
spreadsheetId: ConstructorParameters<typeof PublicGoogleSheetsParser>[0];
sheetInfo: ConstructorParameters<typeof PublicGoogleSheetsParser>[1];
};
export const publicGoogleSheetsParser =
(
instanceOrOptions:
| PublicGoogleSheetsParser
| PublicGoogleSheetsParserOptions,
{ path, typeName }: PublicGoogleSheetsParserParams,
) =>
async (): Promise<object> => {
const instance = getPublicGoogleSheetsParserInstance(instanceOrOptions);
const parsedData = await instance.parse();
const filledData = parsedData.map<FilledData[number]>(
(item, index, data) => {
if (index === 0) {
return item;
}
for (const p of path) {
if (!item[p]) {
item[p] = data[index - 1][p];
}
}
return item;
},
);
return filledDataToObject(filledData, path, typeName);
};
const getPublicGoogleSheetsParserInstance = (
instanceOrOptions: PublicGoogleSheetsParser | PublicGoogleSheetsParserOptions,
) => {
if (instanceOrOptions instanceof PublicGoogleSheetsParser) {
return instanceOrOptions;
}
const { spreadsheetId, sheetInfo } = instanceOrOptions;
return new PublicGoogleSheetsParser(spreadsheetId, sheetInfo);
};