-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileProcessor.js
35 lines (35 loc) · 1.15 KB
/
fileProcessor.js
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
import fs from 'node:fs';
import XLSX from 'xlsx';
XLSX.set_fs(fs);
const cellNameRegularExpression = /^A\d+$/;
const accountNumberRegularExpression = /^\d+$/;
export function loadAccountNumbers(inputFile) {
var _a;
const accountNumbers = [];
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 = (_a = cellData.v) !== null && _a !== void 0 ? _a : '';
if (accountNumberRegularExpression.test(potentialAccountNumber)) {
accountNumbers.push(potentialAccountNumber);
}
}
return accountNumbers;
}
export const writeCSVFile = (outputFile, outputData) => {
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'
});
};