Commit 343e2a0f authored by Heddi Heryadi's avatar Heddi Heryadi 💪

update lib

parent 8fa552db
......@@ -12,10 +12,10 @@ Converting xlsx file to json files using nodejs (js-xlsx)
## Usage
```javascript
xlsxj = require("am-xlsx2json");
xlsxj({
input: "sample.xlsx",
output: "output.json"
am_xlsx2json = require("am-xlsx2json");
am_xlsx2json({
input: "sample.xlsx", // path of excel file / uploaded file
output: "output.json" // if set it will write to file
}, function(err, result) {
if(err) {
console.error(err);
......@@ -30,10 +30,10 @@ Converting xlsx file to json files using nodejs (js-xlsx)
You can optionally provide a sheet name to extract from that sheet
```javascript
xlsxj = require("am-xlsx2json");
xlsxj({
input: "sample.xlsx",
output: "output.json",
am_xlsx2json = require("am-xlsx2json");
am_xlsx2json({
input: "sample.xlsx", // path of excel file / uploaded file
output: "output.json" // if set it will write to file
sheet: "tags"
}, function(err, result) {
if(err) {
......
var am_xlsx2json = require('../')
am_xlsx2json({
input: __dirname + '/testExcel.xlsx',
output: __dirname + '/testExcel.json'
}, function(err, result) {
if(err) {
console.error(err);
}else {
console.log(result);
}
});
[{"Harga":"3000","Nama":"Jus Mangga"},{"Harga":"4000","Nama":"Jus Strawberry"}]
\ No newline at end of file
index.js
var am_xlsx2json = require('./lib');
module.exports = am_xlsx2json;
\ No newline at end of file
var fs = require('fs');
var xlsx = require('xlsx');
var cvcsv = require('csv');
var mime = require('mime');
exports = module.exports = XLSX_json;
var excelMimeType = ['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/vnd.ms-excel']
function XLSX_json(config, callback) {
var extInput = mime.lookup(config.input);
if (excelMimeType.indexOf(extInput) > -1) {
if (!config.input) {
console.error("You miss a input file");
process.exit(1);
}
var cv = new CV(config, callback);
} else {
console.error("Input file not supported");
process.exit(1);
}
}
function CV(config, callback) {
var wb = this.load_xlsx(config.input)
var ws = this.ws(config, wb);
var csv = this.csv(ws)
this.cvjson(csv, config.output, callback)
}
CV.prototype.load_xlsx = function(input) {
return xlsx.readFile(input);
}
CV.prototype.ws = function(config, wb) {
var target_sheet = config.sheet;
if (target_sheet == null)
target_sheet = wb.SheetNames[0];
ws = wb.Sheets[target_sheet];
return ws;
}
CV.prototype.csv = function(ws) {
return csv_file = xlsx.utils.make_csv(ws)
}
CV.prototype.cvjson = function(csv, output, callback) {
var record = []
var header = []
cvcsv()
.from.string(csv)
.transform(function(row) {
row.unshift(row.pop());
return row;
})
.on('record', function(row, index) {
if (index === 0) {
header = row;
} else {
var obj = {};
header.forEach(function(column, index) {
obj[column.trim()] = row[index].trim();
})
record.push(obj);
}
})
.on('end', function(count) {
if (output !== null) {
var stream = fs.createWriteStream(output, {
flags: 'w'
});
stream.write(JSON.stringify(record));
callback(null, record);
} else {
callback(null, record);
}
})
.on('error', function(error) {
console.error(error.message);
});
}
......@@ -9,7 +9,8 @@
"author": "Aksimaya Dev",
"license": "ISC",
"dependencies": {
"csv": "^0.4.6",
"csv": "^0.3.6",
"mime": "^1.3.4",
"xlsx": "^0.8.0"
},
"devDependencies": {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment