xls-to-json
Advanced tools
Comparing version 0.2.2 to 0.3.0
@@ -1,80 +0,40 @@ | ||
var fs = require('fs'); | ||
var xlsjs = require('xlsjs'); | ||
var cvcsv = require('csv'); | ||
var XLS = require('xlsjs'); | ||
exports = module.exports = XLS_json; | ||
exports = module.exports = xls_json; | ||
// exports.XLS_json = XLS_json; | ||
function xls_json (file, options, callback) { | ||
function XLS_json (config, callback) { | ||
if(!config.input) { | ||
console.error("You miss a input file"); | ||
process.exit(1); | ||
} | ||
if(arguments.length > 3) { | ||
console.error("Argument should be below 3"); | ||
process.exit(1); | ||
}else if(arguments.length == 2) { | ||
callback = options; | ||
options = null; | ||
}else if(arguments.length < 2) { | ||
console.error("Argument should be greater than 2"); | ||
process.exit(2); | ||
} | ||
var cv = new CV(config, callback); | ||
} | ||
// workbook arguments (fileName, options) | ||
if(options) { | ||
// have options | ||
var workbook = XLS.readFile(file, options) | ||
}else { | ||
// no options | ||
var workbook = XLS.readFile(file); | ||
} | ||
function CV(config, callback) { | ||
var wb = this.load_xls(config.input) | ||
var ws = this.ws(wb); | ||
var csv = this.csv(ws) | ||
this.cvjson(csv, config.output, callback) | ||
} | ||
var sheet_name_list = workbook.SheetNames; | ||
var list_length = sheet_name_list.length; | ||
// saving sheets, data to this array | ||
var sheet_arr = []; | ||
CV.prototype.load_xls = function(input) { | ||
return xlsjs.readFile(input); | ||
} | ||
// workbook sheet name | ||
for(i = 0; i < list_length; i++) { | ||
var sheet = workbook.Sheets[sheet_name_list[i]]; | ||
sheet.sheetName = sheet_name_list[i]; | ||
sheet_arr.push(sheet); | ||
} | ||
CV.prototype.ws = function(wb) { | ||
var target_sheet = ''; | ||
if(target_sheet === '') | ||
target_sheet = wb.SheetNames[0]; | ||
ws = wb.Sheets[target_sheet]; | ||
return ws; | ||
} | ||
CV.prototype.csv = function(ws) { | ||
return csv_file = xlsjs.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){ | ||
// when writing to a file, use the 'close' event | ||
// the 'end' event may fire before the file has been written | ||
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.log(error.message); | ||
}); | ||
} | ||
callback(null, sheet_arr); | ||
} |
{ | ||
"name": "xls-to-json", | ||
"version": "0.2.2", | ||
"version": "0.3.0", | ||
"description": "Converting xls file to json files using nodejs", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# node-xls-json | ||
[![Build Status](https://travis-ci.org/DataGarage/node-xls-json.png?branch=master)](https://travis-ci.org/DataGarage/node-xls-json) | ||
[![Build Status](https://travis-ci.org/DataGarage/node-xls-json.svg?branch=master)](https://travis-ci.org/DataGarage/node-xls-json) | ||
Converting xls file to json files using nodejs | ||
`node-xlsx-json` is a simple module that convert xls files into json format, using an awesome library https://github.com/SheetJS/js-xls (see converting setting details and options in the repo). | ||
@@ -15,8 +15,5 @@ ## Install | ||
``` | ||
node_xj = require("xls-to-json"); | ||
node_xj({ | ||
input: "sample.xls", | ||
output: "output.json" | ||
}, function(err, result) { | ||
```javascript | ||
var node_xls = require("xls-to-json"); | ||
node_xls('cancer.xls', function(err, result) { | ||
if(err) { | ||
@@ -26,2 +23,17 @@ console.error(err); | ||
console.log(result); | ||
// result output: | ||
// example: | ||
// | ||
// [{ A1: { ixfe: 63, XF: [Object], v: 1, t: 'n' }, | ||
// A2: { ixfe: 63, XF: [Object], v: 1, t: 'n' }, | ||
// A3: { ixfe: 63, XF: [Object], v: 10, t: 'n' }, | ||
// A4: { ixfe: 63, XF: [Object], v: 100, t: 'n' }, | ||
// A5: { ixfe: 63, XF: [Object], v: 1000, t: 'n' }, | ||
// A6: { ixfe: 63, XF: [Object], v: 10000, t: 'n' }, | ||
// A7: { ixfe: 63, XF: [Object], v: 100000, t: 'n' }, | ||
// A8: { ixfe: 63, XF: [Object], v: 1000000, t: 'n' }, | ||
// A9: { ixfe: 63, XF: [Object], v: 10000000, t: 'n' }, | ||
// '!range': { s: [Object], e: [Object] }, | ||
// '!ref': 'A1:B15', | ||
// sheetName: 'Miscellany' } ] | ||
} | ||
@@ -31,4 +43,21 @@ }); | ||
In config object, you have to enter an input path. But If you don't want to output any file you can set to `null`. | ||
## Input Type | ||
- **File**: the `xls` file path | ||
## Output Format | ||
The output is an array, contains with objects of multipule sheets in a excel file. Each object is a sheet with data structures in the sheets. see details in https://github.com/SheetJS/js-xls | ||
## Options | ||
https://github.com/SheetJS/js-xls#options | ||
## API | ||
- **node_xls(fileName, options, callback)** | ||
* fileName : a xls file path | ||
* options : options of xls modules see options section above. | ||
* callback : callback funciton with two arguments (error, result) | ||
## License | ||
@@ -35,0 +64,0 @@ |
@@ -8,8 +8,6 @@ var should = require('should'); | ||
it('should convert xls to json', function() { | ||
xls2json({ | ||
input: './sample/sample-xls.xls', | ||
output: null | ||
}, function(err, result) { | ||
xls2json('./sample/sample-xls.xls' | ||
, function(err, result) { | ||
should.not.exist(err) | ||
result.should.be.an.instanceOf(Object) | ||
result.should.be.an.instanceOf(Array) | ||
}) | ||
@@ -19,8 +17,7 @@ }) | ||
it('should convert xls to json file', function() { | ||
xls2json({ | ||
input: './sample/sample-xls.xls', | ||
output: './sample/test.json' | ||
}, function(err, result) { | ||
xls2json('./sample/sample-xls.xls' | ||
, {cellFormula: false, cellNF: true} | ||
, function(err, result) { | ||
should.not.exist(err) | ||
result.should.be.an.instanceOf(Object) | ||
result.should.be.an.instanceOf(Array) | ||
}) | ||
@@ -30,21 +27,2 @@ | ||
it('should read file in test.json', function() { | ||
var exist = fs.existsSync('./sample/test.json') | ||
exist.should.be.true; | ||
}) | ||
it('should trim', function() { | ||
xls2json({ | ||
input: './sample/testtrim.xls', | ||
output: './sample/test.json' | ||
}, function(err, result) { | ||
should.not.exist(err) | ||
result.should.be.an.instanceOf(Object) | ||
//test any space | ||
var re= /\s/; | ||
re.test(result[0].name).should.be.false; | ||
}) | ||
}) | ||
}) |
64
1
124247
10
64