-
Notifications
You must be signed in to change notification settings - Fork 26
/
csv-to-excel-combiner.js
58 lines (43 loc) · 1.32 KB
/
csv-to-excel-combiner.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const csv = window.require('csvtojson');
const Excel = window.require('exceljs');
/*
* Combine csv files in a folder
*
* @param string csvFolder
*/
async function combine(csvFolder, combineExcelFile){
files = fs.readdirSync(csvFolder, { withFileTypes: true }).filter(dirent => !dirent.isDirectory()).map(dirent => dirent.name);
var excelOptions = {
filename: combineExcelFile
};
//const workbook = new Excel.Workbook();
var workbook = new Excel.stream.xlsx.WorkbookWriter(excelOptions);
workbook.creator = 'Bodastage Solutions';
for(let i=0; i< files.length; i++) {
const fileName = files[i];
const filePath = path.join(csvFolder, files[i]);
const sheetName = fileName.replace(".csv", "");
const worksheet = workbook.addWorksheet(sheetName);
await new Promise((resolve, reject) => {
try{
csv({noheader:true, output: "csv"})
.fromFile(filePath)
.subscribe( (csvRow)=>{
worksheet.addRow(csvRow).commit();
},(err) => {//onError
log.error(`CSVToExcelCombiner.csvJoJson.onError: ${err.toString()}`);
reject();
},
()=>{//onComplete
resolve(undefined);
});
}catch(e){
log.error(e);
}
});
await worksheet.commit();
}//eo-for
await workbook.commit();
//const res = await workbook.xlsx.writeFile(combineExcelFile);
}
exports.combine = combine;