Commit 649d15fa authored by Administrator's avatar Administrator

recode ... new version

parent 3cc48dc9
...@@ -24,59 +24,20 @@ function XLSX_json(config, callback) { ...@@ -24,59 +24,20 @@ function XLSX_json(config, callback) {
function CV(config, callback) { function CV(config, callback) {
var wb = this.load_xlsx(config.input) var wb = this.load_xlsx(config.input)
var ws = this.ws(config, wb); var csv = this.to_json(wb);
var csv = this.csv(ws) return callback(null,csv);
this.cvjson(csv, config.output, callback)
} }
CV.prototype.load_xlsx = function(input) { CV.prototype.load_xlsx = function(input) {
return xlsx.readFile(input); return xlsx.readFile(input);
} }
CV.prototype.to_json = function(workbook) {
CV.prototype.ws = function(config, wb) { var result = {};
var target_sheet = config.sheet; workbook.SheetNames.forEach(function(sheetName) {
if (target_sheet == null) var roa = xlsx.utils.sheet_to_row_object_array(workbook.Sheets[sheetName]);
target_sheet = wb.SheetNames[0]; if(roa.length > 0){
ws = wb.Sheets[target_sheet]; result[sheetName] = roa;
return ws; }
} });
return result;
CV.prototype.csv = function(ws) { }
return csv_file = xlsx.utils.make_csv(ws) \ No newline at end of file
}
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);
});
}
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