Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Gumball12 committed Mar 1, 2024
1 parent 358f039 commit ecb5322
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
coverage
private-key.json

# Logs
logs
Expand Down
35 changes: 35 additions & 0 deletions src/__tests__/parser.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { describe, it, expect } from 'vitest';
import { publicGoogleSheetsParser } from '../parser';
import { JWT } from 'google-auth-library';
import { googleSpreadsheet } from '../parser/googleSpreadsheet';

Check failure on line 4 in src/__tests__/parser.test.ts

View workflow job for this annotation

GitHub Actions / ci

Cannot find module '../parser/googleSpreadsheet' or its corresponding type declarations.
import PublicGoogleSheetsParser from 'public-google-sheets-parser';
import { GoogleSpreadsheet } from 'google-spreadsheet';

// https://docs.google.com/spreadsheets/d/1j23zhzHcPd_LzDQ7uPrXgMJfPoZYs289boUKoKnAjUo/edit#gid=0
const SHEET_NAME = 'ParserTest';
Expand Down Expand Up @@ -54,3 +57,35 @@ describe('publicGoogleSheetsParser', () => {
expect(parsed).toEqual(expected);
});
});

const PRIVATE_SHEETS_SCOPES = [
'https://www.googleapis.com/auth/spreadsheets.readonly',
];

/**
* 테스트 방법
* (테스트에서 사용하는 private-key.json 파일을 공개된 저장소에 업로드하지 마세요!)
* -
*/
describe('GoogleSpreadsheet', () => {
it('Common forms', async () => {
// 테스트 시 private-key.json 파일을 공개된 저장소에 업로드하지 마세요!
const privateKey = await import('./private-key.json');

Check failure on line 73 in src/__tests__/parser.test.ts

View workflow job for this annotation

GitHub Actions / ci

Cannot find module './private-key.json' or its corresponding type declarations.

const jwt = new JWT({
email: privateKey.client_email,
key: privateKey.private_key,
scopes: PRIVATE_SHEETS_SCOPES,
});

const doc = new GoogleSpreadsheet(SPREADSHEET_ID, jwt);
await doc.loadInfo();

const sheetInstance = doc.sheetsByIndex[0];

await googleSpreadsheet(sheetInstance, {
path: ['Key', 'Property'],
typeName: 'Type',
})();
});
});
8 changes: 6 additions & 2 deletions src/parser/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,13 @@ If `path` is `['Key', 'Property']` and `typeName` is `Type`, the return will loo
}
```

## google-spreadsheet-parser
## google-spreadsheet (node-google-spreadsheet)

WIP
A parser that parses **private** Google Sheets. This parser uses [google-spreadsheet](https://github.com/theoephraim/node-google-spreadsheet).

### Usage

### API

## Writing a custom parser

Expand Down
45 changes: 32 additions & 13 deletions src/parser/googleSpreadSheet.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,35 @@
import { JWT } from 'google-auth-library';
import { GoogleSpreadsheet } from 'google-spreadsheet';
import { type GoogleSpreadsheetWorksheet } from 'google-spreadsheet';

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,

Check failure on line 11 in src/parser/googleSpreadSheet.ts

View workflow job for this annotation

GitHub Actions / ci

'typeName' is declared but its value is never read.
) =>
async (): Promise<object> => {

Check failure on line 13 in src/parser/googleSpreadSheet.ts

View workflow job for this annotation

GitHub Actions / ci

A function whose declared type is neither 'undefined', 'void', nor 'any' must return a value.
const rows = await sheetInstance.getRows();
// console.log(rows);

const rows = await sheet.getRows();
console.log(rows, sheet.headerValues);
};
// rows.forEach(row => {
// const data = row.get('Page');
// console.log({ data });
// });

const filledData = rows.map((row, index, rows) => {

Check failure on line 22 in src/parser/googleSpreadSheet.ts

View workflow job for this annotation

GitHub Actions / ci

'filledData' is declared but its value is never read.

Check failure on line 22 in src/parser/googleSpreadSheet.ts

View workflow job for this annotation

GitHub Actions / ci

'index' is declared but its value is never read.

Check failure on line 22 in src/parser/googleSpreadSheet.ts

View workflow job for this annotation

GitHub Actions / ci

'rows' is declared but its value is never read.
const data = path.reduce<object>((acc, p) => {

Check failure on line 23 in src/parser/googleSpreadSheet.ts

View workflow job for this annotation

GitHub Actions / ci

'data' is declared but its value is never read.

Check failure on line 23 in src/parser/googleSpreadSheet.ts

View workflow job for this annotation

GitHub Actions / ci

Argument of type '(acc: object, p: string) => void' is not assignable to parameter of type '(previousValue: object, currentValue: string, currentIndex: number, array: string[]) => object'.

Check failure on line 23 in src/parser/googleSpreadSheet.ts

View workflow job for this annotation

GitHub Actions / ci

'acc' is declared but its value is never read.
const item = row.get(p);
// const item = row.get(p);

// if (!item) {
// const prev = rows[index - 1];
// return prev.get(p);
// }

// return item;
}, {});
});
};

0 comments on commit ecb5322

Please sign in to comment.