-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement a parser for private google sheets
- Loading branch information
Showing
9 changed files
with
194 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
coverage | ||
private-key.json | ||
|
||
# Logs | ||
logs | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,30 @@ | ||
import { JWT } from 'google-auth-library'; | ||
import { GoogleSpreadsheet } from 'google-spreadsheet'; | ||
import { type GoogleSpreadsheetWorksheet } from 'google-spreadsheet'; | ||
import { filledDataToObject } from './utils/filledDataToObject'; | ||
import { FilledData } from './types/data'; | ||
|
||
export const googleSpreadsheetParser = async ( | ||
spreadsheetId: string, | ||
serviceAccountAuth: JWT, | ||
) => { | ||
const doc = new GoogleSpreadsheet(spreadsheetId, serviceAccountAuth); | ||
await doc.loadInfo(); | ||
type GoogleSpreadsheetParams = { | ||
path: string[]; | ||
typeName: string; | ||
}; | ||
|
||
const sheet = doc.sheetsByIndex[0]; | ||
await sheet.loadHeaderRow(); | ||
export const googleSpreadsheet = | ||
( | ||
sheetInstance: GoogleSpreadsheetWorksheet, | ||
{ path, typeName }: GoogleSpreadsheetParams, | ||
) => | ||
async (): Promise<object> => { | ||
const rows = await sheetInstance.getRows(); | ||
const filledData = rows.reduce<FilledData>((filledData, row, index) => { | ||
const prevData = filledData[index - 1]; | ||
const data = [...path, typeName].reduce<FilledData[number]>((acc, p) => { | ||
const item = row.get(p) || prevData[p]; | ||
acc[p] = item; | ||
return acc; | ||
}, {}); | ||
|
||
const rows = await sheet.getRows(); | ||
console.log(rows, sheet.headerValues); | ||
}; | ||
filledData.push(data); | ||
return filledData; | ||
}, []); | ||
|
||
return filledDataToObject(filledData, path, typeName); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { publicGoogleSheetsParser } from './publicGoogleSheetsParser'; | ||
export { googleSpreadsheet } from './googleSpreadsheet'; | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type FilledData = Record<string, string>[]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { FilledData } from '../types/data'; | ||
|
||
type Result = { | ||
[key: string]: string | Result; | ||
}; | ||
|
||
export const filledDataToObject = ( | ||
filledData: FilledData, | ||
path: string[], | ||
typeName: string, | ||
): Result => | ||
filledData.reduce<Result>((filledData, item) => { | ||
let current = filledData; | ||
for (let i = 0; i < path.length - 1; i++) { | ||
const p = path[i]; | ||
const name = item[p]; | ||
|
||
if (!current[name]) { | ||
current[name] = {}; | ||
} | ||
|
||
// @ts-expect-error TS7053 | ||
current = current[name]; | ||
} | ||
|
||
const last = path[path.length - 1]; | ||
const name = item[last]; | ||
current[name] = item[typeName]; | ||
|
||
return filledData; | ||
}, {}); |