Skip to content

Commit

Permalink
Choose XLSX or XLSB when converting csv to excel
Browse files Browse the repository at this point in the history
  • Loading branch information
erssebaggala committed Oct 10, 2019
1 parent a0c1fcf commit 8cb92bc
Show file tree
Hide file tree
Showing 8 changed files with 488 additions and 21 deletions.
2 changes: 1 addition & 1 deletion background/background-process.html
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,7 @@ <h1>Background process</h1>
//
try{
if(task === 'combine_csv_to_excel'){
const result = await utils.combinedCSVsIntoExcel(options.csvDirectory);
const result = await utils.combinedCSVsIntoExcel(options.csvDirectory, options.excelFormat, options.combine, options.outputFolder);
sendLogToUI(task, result.status, result.message);
}
}catch(err){
Expand Down
40 changes: 36 additions & 4 deletions background/background-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const { VENDOR_CM_FORMATS, VENDOR_PM_FORMATS, VENDOR_FM_FORMATS,
VENDOR_CM_PARSERS, VENDOR_PM_PARSERS, VENDOR_FM_PARSERS } = window.require('./vendor-formats');
const tems = window.require('./tems');
const csvToExcelCombiner = window.require('./csv-to-excel-combiner');
const EXCEL = window.require('./excel');

//Fix PATH env variable on Mac OSX
if(process.platform === 'darwin'){
Expand Down Expand Up @@ -1576,11 +1577,42 @@ async function clearBaselineReference(){
}
}

async function combinedCSVsIntoExcel(csvDirectory){
/*
*
* @param string csvDirectory
* @param string excelFormat XLSX, XLSB
* @param string combined Whether to combined into one workbook or separate workbooks. Values are: true or false
* @param string outputFolder Output folder for combined === true
*/
async function combinedCSVsIntoExcel(csvDirectory, excelFormat, combined, outputFolder){
try{
const combinedExcelFile = path.join(app.getPath('downloads'), 'combined_csv.xlsx');
await csvToExcelCombiner.combine(csvDirectory, combinedExcelFile);
return {status: 'success', message: combinedExcelFile };
const format = excelFormat || "XLSX";

if(format === 'XLSX' && combined){
const combinedExcelFile = path.join(app.getPath('downloads'), 'combined_csv.xlsx');
await csvToExcelCombiner.combine(csvDirectory, combinedExcelFile);
return {status: 'success', message: combinedExcelFile };
}

if(format === 'XLSX' && !combined){
const outFolder = outputFolder || app.getPath('downloads');
await EXCEL.csvToXLSX(csvDirectory, outFolder);
return {status: 'success', message: outFolder };
}

if(format === 'XLSB' && combined){
const combinedExcelFile = path.join(app.getPath('downloads'), 'combined_csv.xlsb');
await EXCEL.combineCSVToXLSB(csvDirectory, combinedExcelFile);
return {status: 'success', message: combinedExcelFile };
}

if(format === 'XLSB' && !combined){
const outFolder = outputFolder || app.getPath('downloads');
await EXCEL.csvToXLSB(csvDirectory, outFolder);
return {status: 'success', message: outFolder };
}


}catch(e){
log.error(e);
return {status: 'error', message: `Error occured while combining csv files. Check logs for details.`};
Expand Down
208 changes: 208 additions & 0 deletions background/excel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
const XLSX = window.require('xlsx');
const csv = window.require('csvtojson');
const Excel = window.require('exceljs');

/*
* Combine CSV files into Excel XLSB Workbook
*/
async function combineCSVToXLSB(inputFolder, outputFile){

files = fs.readdirSync(inputFolder, { withFileTypes: true }).filter(dirent => !dirent.isDirectory()).map(dirent => dirent.name);

var wb = XLSX.utils.book_new();

for(let i=0; i< files.length; i++) {
const fileName = files[i];
const filePath = path.join(inputFolder, files[i]);
const sheetName = fileName.replace(".csv", "");

let wbData = [];
await new Promise((resolve, reject) => {
try{
csv({noheader:true, output: "csv"})
.fromFile(filePath)
.subscribe( (csvRow)=>{
wbData.push(csvRow);
},(err) => {//onError
log.error(`combineCSVtoXLSB.csvJoJson.onError: ${err.toString()}`);
reject();
},
()=>{//onComplete
resolve(undefined);
});
}catch(e){
log.error(e);
}

});
var ws = XLSX.utils.aoa_to_sheet(wbData);
XLSX.utils.book_append_sheet(wb, ws, sheetName);
}

//XLSX.writeFile(wb, outputFile, {Props:{Author:"Bodastage Solutions", bookType: "xlsb"}});
XLSX.writeFile(wb, outputFile, {
raw: true,
bookType: "xlsb",
type: "xlsb",
compression: true,
Props:{
Author:"Boda-Lite"
}
});
}


/*
* Combine csv files in a folder
*
* @param string csvFolder
*/
async function combineCSVToXLSX(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 = 'Boda-Lite';

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();

}

async function csvToXLSX(csvFolder, outputFolder){
files = fs.readdirSync(csvFolder, { withFileTypes: true }).filter(dirent => !dirent.isDirectory()).map(dirent => dirent.name);

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 combineExcelFile = path.join(outputFolder, `${sheetName}.xlsx`);
var excelOptions = {
filename: combineExcelFile
};

//const workbook = new Excel.Workbook();
var workbook = new Excel.stream.xlsx.WorkbookWriter(excelOptions);

workbook.creator = 'Boda-Lite';

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();
await workbook.commit();

}//eo-for

}

/*
* Convert each CSV file into Excel XLSB Workbook
*/
async function csvToXLSB(inputFolder, outputFolder){

files = fs.readdirSync(inputFolder, { withFileTypes: true }).filter(dirent => !dirent.isDirectory()).map(dirent => dirent.name);

for(let i=0; i< files.length; i++) {
const fileName = files[i];
const filePath = path.join(inputFolder, files[i]);
const sheetName = fileName.replace(".csv", "");
const outputFile = path.join(outputFolder, sheetName + ".xlsb");

var wb = XLSX.utils.book_new();

let wbData = [];
await new Promise((resolve, reject) => {
try{
csv({noheader:true, output: "csv"})
.fromFile(filePath)
.subscribe( (csvRow)=>{
wbData.push(csvRow);
},(err) => {//onError
log.error(`combineCSVtoXLSB.csvJoJson.onError: ${err.toString()}`);
reject();
},
()=>{//onComplete
resolve(undefined);
});
}catch(e){
log.error(e);
}

});
var ws = XLSX.utils.aoa_to_sheet(wbData);
XLSX.utils.book_append_sheet(wb, ws, sheetName);
XLSX.writeFile(wb, outputFile, {
raw: true,
bookType: "xlsb",
type: "xlsb",
compression: true,
Props:{
Author:"Boda-Lite"
}
});
}


}


exports.csvToXLSX = csvToXLSX;
exports.csvToXLSB = csvToXLSB;
exports.combineCSVToXLSX = combineCSVToXLSX;
exports.combineCSVToXLSB = combineCSVToXLSB;
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Boda-Lite",
"version": "0.3.0",
"version": "0.3.1",
"description": "Boda-Lite is a telecommunication network management application",
"private": true,
"homepage": "./",
Expand Down Expand Up @@ -90,6 +90,7 @@
"hex-to-rgba": "^2.0.1",
"i": "^0.3.6",
"immutable": "^3.8.2",
"js-xlsx": "^0.8.22",
"leaflet": "^1.3.3",
"leaflet-contextmenu": "^1.4.0",
"leaflet-fullscreen": "^1.0.2",
Expand Down Expand Up @@ -148,6 +149,7 @@
"whatwg-fetch": "^2.0.4",
"winston-electron": "^0.2.0",
"workbox-webpack-plugin": "3.6.3",
"xlsx": "^0.15.1",
"xmldom": "^0.1.27",
"xpath": "^0.0.27"
},
Expand Down
Loading

0 comments on commit 8cb92bc

Please sign in to comment.