-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathexcel-processor.ts
51 lines (46 loc) · 1.84 KB
/
excel-processor.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 path from 'path';
import { getWorksheets, getXlsxStream } from 'xlstream';
import { IExcelProcessorConfig, processStream, sheetGetter } from '../types';
import { DirectoryProcessor } from './base/directory-processor';
export class ExcelProcessor extends DirectoryProcessor {
protected hasHeader: boolean | number | undefined;
protected ignoreEmpty: boolean;
protected encoding?: string;
protected fillMergedCells: boolean;
protected sheets: (string | number)[] | sheetGetter;
constructor(config: IExcelProcessorConfig) {
super(config);
this.encoding = config.encoding;
this.hasHeader = config.hasHeader;
this.ignoreEmpty = config.ignoreEmpty === false ? false : true;
this.fillMergedCells = !!config.fillMergedCells;
if (config.sheets) {
this.sheets = config.sheets;
} else {
this.sheets = [0];
}
}
async process(processStream: processStream) {
for (let file of this.files) {
const filePath = path.join(this.path, file);
let sheets: (number | string)[] = [];
if (this.sheets instanceof Function) {
const allSheets = await getWorksheets({ filePath });
sheets = await this.sheets(allSheets);
} else {
sheets = this.sheets;
}
for (let i = 0; i < sheets.length; i++) {
const readStream = await getXlsxStream({
filePath,
sheet: sheets[i],
withHeader: this.hasHeader,
ignoreEmpty: this.ignoreEmpty,
fillMergedCells: this.fillMergedCells,
encoding: this.encoding,
});
await processStream(readStream, file, filePath, sheets[i]);
}
}
}
}