Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

excel2json

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

excel2json - npm Package Compare versions

Comparing version 0.1.0 to 0.1.1

test/data/test.json

95

lib/index.js

@@ -0,1 +1,2 @@

var _ = require('lodash');
var async = require('async');

@@ -15,2 +16,4 @@

this._dataLine = 3;
// refKey
this.refKey = '_id';

@@ -55,2 +58,3 @@ return this;

this.dataLine = options.dataLine || this.dataLine;
this.refKey = options.refKey || this.refKey;

@@ -268,1 +272,92 @@ this._optionCel = this.celname2index(this.optionCel);

};
/**
* sheetDatas to json
* @param {Array} sheetDatas
* @param {Function} callback
*/
Excel2Json.prototype.toJson = function(sheetDatas, callback) {
function findOrigin(_dataMap, option, content) {
var origin = _dataMap[content.__ref];
if (!origin || !option.key) {
console.error('not found origin.', JSON.stringify(content));
return;
}
var keys = option.key.split('.');
var __in = content.__in ? content.__in.split('.') : [];
for (var i = 0; i < keys.length; i++) {
if (/^#/.test(keys[i])) {
var key = keys[i].replace(/^#/, '');
var index = __in[i] && __in[i].replace(/^#.+:(\d+)$/, '$1');
if (!index) {
console.error('not found index.', JSON.stringify(content));
return;
}
origin[key] = origin[key] || [];
origin = origin[key];
origin[index] = origin[index] || {};
origin = origin[index];
} else if (keys[i] === '$') {
origin = origin[__in[i]];
} else if (i + 1 === keys.length) {
origin[keys[i]] = origin[keys[i]] || (option.type === 'array' ? [] : {});
origin = origin[keys[i]];
} else {
origin = origin[keys[i]];
}
if (!origin) {
console.error('not found origin parts.', JSON.stringify(content));
return;
}
}
if (option.type === 'array') {
if (!Array.isArray(origin)) {
console.error('is not Array.', JSON.stringify(content));
return;
}
origin.push({});
origin = origin[origin.length - 1];
} else if (option.type === 'map') {
if (!content.__key) {
console.error('not found __key.', JSON.stringify(content));
return;
}
origin = origin[content.__key] = {};
} else {
console.error(option);
return;
}
return origin;
}
var collectionMap = {};
var errors = {};
for (var i = 0; i < sheetDatas.length; i++) {
var sheetData = sheetDatas[i];
var name = sheetData.option.name || sheetData.name;
var refKey = sheetData.option.refKey || this.refKey;
var dataMap = collectionMap[name] = collectionMap[name] || {};
for (var j = 0; j < sheetData.contents.length; j++) {
var content = sheetData.contents[j];
if (!sheetData.option.type || sheetData.option.type === 'origin') {
dataMap[content[refKey]] = content;
} else {
var origin = findOrigin(dataMap, sheetData.option, content);
if (origin) {
delete content.__ref;
delete content.__in;
delete content.__key;
_.extend(origin, content);
} else {
errors[name] = errors[name] || [];
errors[name].push(content);
}
}
}
}
callback(Object.keys(errors).length ? errors : null, collectionMap);
};

2

package.json
{
"name": "excel2json",
"version": "0.1.0",
"version": "0.1.1",
"description": "excel2json",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -45,10 +45,10 @@ Excel2Json

| | A | B | C | D |
|:-:|:-------|:---------|:-----------------|---|
| 1 | {} | | | |
| 2 | _id | obj.code | obj.value:number | |
| 3 | | | | |
| 4 | first | one | 1 | |
| 5 | second | two | 2 | |
| 6 | | | | |
| | A | B | C | D |
|:-:|:---------------|:---------|:-----------------|---|
| 1 | {name: 'Test'} | | | |
| 2 | _id | obj.code | obj.value:number | |
| 3 | | | | |
| 4 | first | one | 1 | |
| 5 | second | two | 2 | |
| 6 | | | | |
Sheet1

@@ -63,9 +63,25 @@ ```

// [{
num: 1, // sheet number
name: 'Sheet1', // sheet name
option: {}, // sheet option (A1)
contents: [
{ _id: 'first', obj: { code: 'one', value: 1 } }, { _id: 'second', obj: { code: 'two', value: 2 } }
]
}]
// num: 1, // sheet number
// name: 'Sheet1', // sheet name
// option: { name: 'Test' }, // sheet option (A1)
// contents: [
// { _id: 'first', obj: { code: 'one', value: 1 } }, { _id: 'second', obj: { code: 'two', value: 2 } }
// ]
// }]
excel2json.toJson(data, function(err, json) {
console.log(json);
// {
// Test: {
// first: {
// _id: 'first',
// obj: { code: 'one', value: 1 }
// },
// second: {
// _id: 'second',
// obj: { code: 'two', value: 2 }
// }
// }
// }
});
});

@@ -82,6 +98,17 @@ ```

attrLine: '2', // Line with a data attribute. (default: '2'
dataLine: '4' // Line with a data. (default: '4'
dataLine: '4', // Line with a data. (default: '4'
refKey: '_id' // ref key. (default: '_id'
});
```
### Sheet option
sheet option. setting with optionCell (default: 'A1'
* `name`
* `type`
* `key`
* `attrLine`
* `dataLine`
* `refKey`
### Attribute

@@ -97,76 +124,18 @@ Specify the key name.

### An example of a complex format
Data to be expected.
### Example
An example of a complex format.
[test.xlsx](https://github.com/yuhei-a/excel2json/raw/master/test/data/test.xlsx) > [test.json](https://github.com/yuhei-a/excel2json/blob/master/test/data/test.json)
```
[
{
_id: 'test1',
arr: [
{
code: 'test1_1',
list: [ [ 1, 2 ], [ 3, 4 ] ],
arr: [
{ code: 'test1_1_1', is: true },
{ code: 'test1_1_2', is: false }
]
},
{
code: 'test1_2',
list: [ [ 5, 6, 7 ], [ 8 ] ],
arr: [
{ code: 'test1_2_1', is: true },
{ code: 'test1_2_2', is: false }
]
}
]
},
{
_id: 'test2',
arr: [
{
code: 'test2_1',
list: [ [ 1 ], [ 2 ], [ 3 ], [ 4 ] ],
arr: [
{ code: 'test2_1_1', is: true },
{ code: 'test2_1_2', is: false }
]
},
{
code: 'test2_2',
list: [ [ 5, 6, 7 ], [ 8 ] ],
arr: [
{ code: 'test2_2_1', is: true },
{ code: 'test2_2_2', is: false }
]
}
]
}
]
var excel2json = require('excel2json');
excel2json.parse('test.xlsx', [], function(err, sheetDatas) {
excel2json.toJson(sheetDatas, function(err, result) {
fs.writeFileSync('test.json', JSON.stringifi(result, null, 4));
});
});
```
Necessary Excel data.
| | A | B | C | D | E | F | G |
|:--:|:-------|:-----------|:----------|:-----------------|:--------------------|:---------------|:---------------------|
| 1 | {} | | | | | | |
| 2 | _id | #arr:index | #arr.code | #arr.#list:index | #arr.#list.#:number | #arr.#arr.code | #arr.#arr.is:boolean |
| 3 | | | | | | | |
| 4 | test1 | 0 | test1_1 | 0 | 1 | test1_1_1 | true |
| 5 | | 0 | | 0 | 2 | test1_1_2 | false |
| 6 | | 0 | | 1 | 3 | | |
| 7 | | 0 | | 1 | 4 | | |
| 8 | | 1 | test1_2 | 0 | 5 | test1_2_1 | true |
| 9 | | 1 | | 0 | 6 | test1_2_2 | false |
| 10 | | 1 | | 0 | 7 | | |
| 11 | | 1 | | 1 | 8 | | |
| 12 | test2 | 0 | test2_1 | 0 | 1 | test2_1_1 | true |
| 13 | | 0 | | 1 | 2 | test2_1_2 | false |
| 14 | | 0 | | 2 | 3 | | |
| 15 | | 0 | | 3 | 4 | | |
| 16 | | 1 | test2_2 | 0 | 5 | test2_2_1 | true |
| 17 | | 1 | | 0 | 6 | test2_2_2 | false |
| 18 | | 1 | | 0 | 7 | | |
| 19 | | 1 | | 1 | 8 | | |
## Test
Run `npm test` and `npm run-script jshint`

@@ -5,3 +5,2 @@ var should = require('should');

var filepath = 'test/data/index.xlsx';

@@ -50,3 +49,3 @@ describe('#celname2index', function() {

result.should.eql(expected);
isOrigin.should.be.true;
isOrigin.should.equal(true);
done();

@@ -75,3 +74,3 @@ });

result.should.eql(expected);
isFirst.should.be.false;
isFirst.should.equal(false);
done();

@@ -457,4 +456,4 @@ });

describe('#parse', function() {
var filepath = 'test/data/index.xlsx';
it(filepath, function(done) {

@@ -507,1 +506,18 @@ excel2json.parse(filepath, [], function(err, result) {

});
describe('#toJson', function() {
var filepath = 'test/data/test.xlsx';
it(filepath, function(done) {
excel2json.parse(filepath, [], function(err, result) {
should.not.exist(err);
should.exist(result);
excel2json.toJson(result, function(_err, _result) {
should.not.exist(_err);
should.exist(_result);
_result.should.eql(require('./data/test.json'));
done();
});
});
});
});
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