Commit 649d15fa authored by Administrator's avatar Administrator

recode ... new version

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