New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

xls-to-json

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

xls-to-json - npm Package Compare versions

Comparing version 0.3.0 to 0.3.1

sample/test.json

106

libs/index.js

@@ -1,40 +0,80 @@

var XLS = require('xlsjs');
var fs = require('fs');
var xlsjs = require('xlsjs');
var cvcsv = require('csv');
exports = module.exports = xls_json;
exports = module.exports = XLS_json;
function xls_json (file, options, callback) {
// exports.XLS_json = XLS_json;
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);
}
function XLS_json (config, callback) {
if(!config.input) {
console.error("You miss a input file");
process.exit(1);
}
// workbook arguments (fileName, options)
if(options) {
// have options
var workbook = XLS.readFile(file, options)
}else {
// no options
var workbook = XLS.readFile(file);
}
var cv = new CV(config, callback);
}
var sheet_name_list = workbook.SheetNames;
var list_length = sheet_name_list.length;
// saving sheets, data to this array
var sheet_arr = [];
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)
}
// 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.load_xls = function(input) {
return xlsjs.readFile(input);
}
callback(null, sheet_arr);
}
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);
});
}
{
"name": "xls-to-json",
"version": "0.3.0",
"version": "0.3.1",
"description": "Converting xls file to json files using nodejs",

@@ -24,3 +24,3 @@ "main": "index.js",

"dependencies": {
"xlsjs": "0.6.20",
"xlsjs": "0.7.0",
"csv": "~0.3.6"

@@ -27,0 +27,0 @@ },

@@ -5,3 +5,3 @@ # node-xls-json

`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).
Converting xls file to json files using nodejs

@@ -16,5 +16,8 @@ ## Install

```javascript
var node_xls = require("xls-to-json");
node_xls('cancer.xls', function(err, result) {
``` javascript
node_xj = require("xls-to-json");
node_xj({
input: "sample.xls",
output: "output.json"
}, function(err, result) {
if(err) {

@@ -24,17 +27,2 @@ 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' } ]
}

@@ -44,21 +32,4 @@ });

## Input Type
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`.
- **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

@@ -65,0 +36,0 @@

@@ -8,6 +8,8 @@ var should = require('should');

it('should convert xls to json', function() {
xls2json('./sample/sample-xls.xls'
, function(err, result) {
xls2json({
input: './sample/sample-xls.xls',
output: null
}, function(err, result) {
should.not.exist(err)
result.should.be.an.instanceOf(Array)
result.should.be.an.instanceOf(Object)
})

@@ -17,7 +19,8 @@ })

it('should convert xls to json file', function() {
xls2json('./sample/sample-xls.xls'
, {cellFormula: false, cellNF: true}
, function(err, result) {
xls2json({
input: './sample/sample-xls.xls',
output: './sample/test.json'
}, function(err, result) {
should.not.exist(err)
result.should.be.an.instanceOf(Array)
result.should.be.an.instanceOf(Object)
})

@@ -27,2 +30,21 @@

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;
})
})
})
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc