Skip to content

Commit 6ca2641

Browse files
committed
fix: excel helper
1 parent 2085f18 commit 6ca2641

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

src/helpers/Excel.ts

+29-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1+
import fs from 'fs'
12
import * as ExcelJS from 'exceljs'
3+
import excelToJson from 'convert-excel-to-json'
24

3-
class ExcelHelper {
5+
interface OptionConvert {
6+
header?: any
7+
columnToKey?: any
8+
}
9+
10+
class Excel {
411
/**
512
*
613
* @param headers
@@ -35,6 +42,26 @@ class ExcelHelper {
3542
})
3643
})
3744
}
45+
46+
/**
47+
*
48+
* @param path
49+
* @param options
50+
* options is used when there is only 1 sheet
51+
*/
52+
public static convertToJson(path: string | Buffer, options?: OptionConvert) {
53+
const resultConvert = excelToJson({
54+
source: fs.readFileSync(path), // fs.readFileSync return a Buffer
55+
header: options?.header || {
56+
rows: 1,
57+
},
58+
columnToKey: options?.columnToKey || {
59+
'*': '{{columnHeader}}',
60+
},
61+
})
62+
63+
return resultConvert
64+
}
3865
}
3966

40-
export default ExcelHelper
67+
export default Excel

0 commit comments

Comments
 (0)