-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
50 lines (38 loc) · 1.32 KB
/
index.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
const xlsx = require('node-xlsx');
const fs = require('fs');
const path = require('path');
const inputFolder = path.join(__dirname, '/input/');
const outputFolder = path.join(__dirname, '/output/');
let csvString = '';
let filename = '';
fs.readdir(inputFolder, (err, files) => {
if (err) throw err;
const xlsxFiles = files.filter((file) => {
// ~ prefix is used by Excel to store temporary files
return file.endsWith('.xlsx') && !file.startsWith('~');
});
xlsxFiles.forEach((file) => {
filename = file.split('.').slice(0, -1).join('.');
const parsedXlsx = xlsx.parse(inputFolder + `/${file}`, { defval: '' });
// convert only the first sheet of the xlsx file since right now I need only the first sheet
const sheet1 = parsedXlsx[0];
const rows = sheet1.data;
rows.forEach((row, index) => {
let rowString = '';
row.forEach((cell) => {
const stringCell = String(cell);
if (stringCell.includes(',') || stringCell.includes('"')) {
rowString += `"${stringCell}"` + ',';
} else {
rowString += stringCell + ',';
}
});
rowString += '\n';
csvString += rowString;
});
});
fs.writeFile(outputFolder + `/${filename}.csv`, csvString, (err) => {
if (err) throw err;
console.log(`Created: ${filename}.csv`);
});
});