forked from behroozk/excel2csv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
83 lines (66 loc) · 2.19 KB
/
index.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env node
import commander from 'commander';
import csvStringify from 'csv-stringify';
import fs from 'fs';
import path from 'path';
import { promisify } from 'util';
import xlsx from 'xlsx';
import { IConvertOptions } from './src/types/covnert_options.interface';
const csvStringifyAsync = promisify<csvStringify.Input, csvStringify.Options, string>(csvStringify);
const writeFileAsync = promisify(fs.writeFile);
export async function convert(
excelPath: string,
partialOptions: Partial<IConvertOptions> = {},
): Promise<string | boolean> {
try {
const options: IConvertOptions = formOptions(excelPath, partialOptions);
const items: any[] = await excelToJson(excelPath);
const csv = await csvStringifyAsync(items, { header: true });
if (options.writeCsv) {
await writeFileAsync(options.csvPath, csv);
return true;
}
return csv;
} catch (error) {
throw error;
}
}
function formOptions(excelPath: string, partialOptions: Partial<IConvertOptions>): IConvertOptions {
const excelFilename: string = path.parse(excelPath).name;
const options: IConvertOptions = {
...{
csvPath: `${excelFilename}.csv`,
writeCsv: false,
},
...partialOptions,
};
return options;
}
async function excelToJson(excelPath: string): Promise<any[]> {
const workbook: xlsx.WorkBook = xlsx.readFile(excelPath);
const sheetName: string = workbook.SheetNames[0];
const sheet: xlsx.WorkSheet = workbook.Sheets[sheetName];
const items = xlsx.utils.sheet_to_json(sheet);
return items;
}
function main(): void {
if (require.main !== module) { // required as module
return;
}
commander
.version('0.1.0', '-v, --version')
.arguments('<excel_file>')
.option('-o, --output <csv_file>', 'Output CSV file path')
.action((excelFile?: string) => {
if (excelFile) {
convert(excelFile, {
csvPath: commander.output,
writeCsv: true,
});
}
})
.parse(process.argv);
return;
}
main();
export { IConvertOptions };