1- import { GoogleSpreadsheet , GoogleSpreadsheetWorksheet } from 'google-spreadsheet' // version: ^3.3.0
2- import credentials from './gs_credentials'
1+ import { GoogleSpreadsheet , GoogleSpreadsheetWorksheet , ServiceAccountCredentials } from 'google-spreadsheet' // version: ^3.3.0
32import fs from 'node:fs'
43import path from 'node:path'
54
@@ -19,16 +18,14 @@ const compose = (...fns: { (str: string): string; (str: string): string }[]) =>
1918 }
2019}
2120
22- const validCredentials = ( credentials : Record < string , string > ) => {
23- const { private_key, client_email} = credentials
24- if ( private_key === 'YOUR_PRIVATE_KEY' || client_email === 'YOUR_CLIENT_EMAIL' ) {
25- throw new Error ( 'Please set your credentials in src/runs/gen/gs_credentials.ts' )
21+ const sheetInit = async ( sheetId : string , sheetIndex : number , credentials : ServiceAccountCredentials ) => {
22+ const doc = new GoogleSpreadsheet ( sheetId )
23+ try {
24+ await doc . useServiceAccountAuth ( credentials )
25+ } catch ( error ) {
26+ console . log ( 'auth-error:' , error )
2627 }
27- }
2828
29- const sheetInit = async ( sheetId : string , sheetIndex : number ) => {
30- const doc = new GoogleSpreadsheet ( sheetId )
31- await doc . useServiceAccountAuth ( credentials )
3229 await doc . loadInfo ( )
3330 const sheet = doc . sheetsByIndex [ sheetIndex ]
3431 await sheet . loadCells ( )
@@ -78,28 +75,29 @@ const getKeys = async (sheet: GoogleSpreadsheetWorksheet, cell: {x: number, y: n
7875 return keys
7976}
8077
81- /**
82- *
83- * @param {string } sheetId google sheet id
84- * @param {number[] } sheetIndexs google worksheet index
85- * @param {{x: number, y: number} } hStartCell horizontal start cell
86- * @param {{x: number, y: number} } vStartCell vertical start cell
87- * @returns {Promise<void> } void
88- */
89- const main = async (
78+ type MainArgs = {
9079 sheetId : string ,
9180 sheetIndexs : number [ ] ,
92- hStartCell :{ x : number , y : number } ,
93- vStartCell :{ x : number , y : number } ,
94- ) : Promise < void > => {
95- validCredentials ( credentials )
96-
81+ hStartCell : { x : number , y : number } ,
82+ vStartCell : { x : number , y : number } ,
83+ credentialsPath : string
84+ }
85+ const main = async ( {
86+ sheetId,
87+ sheetIndexs,
88+ hStartCell,
89+ vStartCell,
90+ credentialsPath,
91+ } : MainArgs ) : Promise < void > => {
9792 const localesJsonMap : Record < string , Record < string , string > > = { }
98- const firstSheet = await sheetInit ( sheetId , sheetIndexs [ 0 ] )
93+ const resultPath = path . join ( process . cwd ( ) , credentialsPath )
94+ console . log ( 'resultPath:' , resultPath )
95+ const credentials = JSON . parse ( fs . readFileSync ( resultPath , 'utf-8' ) )
96+ const firstSheet = await sheetInit ( sheetId , sheetIndexs [ 0 ] , credentials )
9997 const locales = await getLocales ( firstSheet , hStartCell )
10098
10199 sheetIndexs . forEach ( async sheetIndex => {
102- const sheet = await sheetInit ( sheetId , sheetIndex )
100+ const sheet = await sheetInit ( sheetId , sheetIndex , credentials )
103101 const keys = await getKeys ( sheet , vStartCell )
104102
105103 for ( let i = 0 ; i < locales . length ; i ++ ) {
0 commit comments