Skip to content

Commit

Permalink
Feature: Parsing entries can be specified.
Browse files Browse the repository at this point in the history
  • Loading branch information
jobyrao committed Sep 28, 2019
1 parent 8ed4958 commit b8ab5f3
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
23 changes: 16 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,23 @@ class XLSX2JSON {
/**
*
* @param source filepath or buffer
* @param options
* @param options {object} entry: sheetName, sheets: ['sheetName']
* @returns {[{sheetName,data}]} sheet list
*/
parse(source, options = {}) {
const readMethod = typeof source === 'string' ? 'readFile' : 'read';
const workbook = XLSX[readMethod](source, options);
const result = [];
for (let i in workbook.Sheets) {
const workbookSheets = Object.keys(workbook.Sheets);
if (!options.entry) {
options.entry = workbookSheets[0];
}
let parsingSheets = null;
if (options.sheets instanceof Array) {
options.sheets.unshift(options.entry);
parsingSheets = [...new Set(options.sheets)];
}
for (let i of (parsingSheets || workbookSheets)) {
const sheetData = workbook.Sheets[i];
result.push({
sheetName: i,
Expand All @@ -40,7 +49,6 @@ class XLSX2JSON {
parse2json(source, options = {}) {
// 解析前,对原先数据校零处理。
this.zeroCorrection();
const opts = Object.assign({isColOriented: true}, options);
this.parsedXlsxData = this.parse(source, options);

XLSX2JSON.switch2customStructure(this.parsedXlsxData);
Expand Down Expand Up @@ -90,10 +98,11 @@ class XLSX2JSON {
for (let i = 0, len = firstCol.length; i < len; i += 1) {
if (firstCol[i] === undefined || (firstCol[i].trim && firstCol[i].trim() === '')) {
if (i === 0) {
console.error('The first line of the\'json\'attribute description value is forbidden to be null.');
process.exit(0);
}
firstCol[i] = firstCol[i - 1];
console.warn('The first line of the\'json\'attribute description value is forbidden to be null.');
firstCol[i] = 'firstLineAttrDesc';
} else {
firstCol[i] = firstCol[i - 1];
}
} else {
firstCol[i] = firstCol[i].toString().replace(/\s/g, '');
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "xlsx-json-js",
"version": "0.0.4",
"version": "0.0.5",
"description": "Parse the'xlsx'file as JSON according to a set of attribute description syntax.",
"main": "lib/index.js",
"scripts": {
Expand Down
Binary file modified test/assets/excel.xlsx
Binary file not shown.
14 changes: 14 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const testXlsxPath = path.join(__dirname, './assets/excel.xlsx');

describe('原生数据结构parse解析测试', function() {
const parsedNativeData = xlsx2json.parse(testXlsxPath);
const parsedNativeData2 = xlsx2json.parse(testXlsxPath, {entry: 'company', sheets: ['address']});
it('原生解析:sheet个数完整', function() {
expect(parsedNativeData.length).to.be.equal(4);
});
Expand All @@ -17,10 +18,19 @@ describe('原生数据结构parse解析测试', function() {
expect(parsedNativeData[0].data[0][0]).to.be.equal('filename');
expect(parsedNativeData[3].data[0][0]).to.be.equal('city[]');
});
it('原生解析:指定解析入口', function() {
expect(parsedNativeData2[0].sheetName).to.be.equal('company');
});
it('原生解析:指定解析sheets', function() {
expect(parsedNativeData2.length).to.be.equal(2);
expect(parsedNativeData2[0].sheetName).to.be.equal('company');
expect(parsedNativeData2[1].sheetName).to.be.equal('address');
});
});

describe('自定义数据结构parse2json解析测试', function() {
const parsedCustomData = xlsx2json.parse2json(testXlsxPath);
const parsedCustomData2 = xlsx2json.parse2json(testXlsxPath, {entry: 'company', sheets: ['address']});
it('自定义解析:语言码个数完整', function() {
expect(parsedCustomData.length).to.be.equal(2);
});
Expand All @@ -44,4 +54,8 @@ describe('自定义数据结构parse2json解析测试', function() {
expect(parsedCustomData[0].companyInfo.address.city[0]).to.be.equal('广州市');
expect(parsedCustomData[1].companyInfo.address.city[0]).to.be.equal('guangzhou');
});
it('自定义解析:指定入口和sheets', function() {
expect(parsedCustomData2[0].address.city[0]).to.be.equal('广州市');
expect(parsedCustomData2[1].address.city[0]).to.be.equal('guangzhou');
});
});

0 comments on commit b8ab5f3

Please sign in to comment.