-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileProcessor.ts
51 lines (35 loc) · 1.15 KB
/
fileProcessor.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
import fs from 'node:fs'
import XLSX from 'xlsx'
XLSX.set_fs(fs)
const cellNameRegularExpression = /^A\d+$/
const accountNumberRegularExpression = /^\d+$/
export function loadAccountNumbers(inputFile: string): string[] {
const accountNumbers: string[] = []
const workbook = XLSX.readFile(inputFile)
if (!workbook) {
return accountNumbers
}
const sheet = workbook.Sheets[workbook.SheetNames[0]]
if (!sheet) {
return accountNumbers
}
for (const [cellName, cellData] of Object.entries(sheet)) {
if (!cellNameRegularExpression.test(cellName)) {
continue
}
const potentialAccountNumber: string = cellData.v ?? ''
if (accountNumberRegularExpression.test(potentialAccountNumber)) {
accountNumbers.push(potentialAccountNumber)
}
}
return accountNumbers
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const writeCSVFile = (outputFile: string, outputData: any[]): void => {
const sheet = XLSX.utils.json_to_sheet(outputData)
const workbook = XLSX.utils.book_new()
XLSX.utils.book_append_sheet(workbook, sheet)
XLSX.writeFile(workbook, outputFile, {
bookType: 'csv'
})
}