diff --git a/.gitignore b/.gitignore index 97c1739..efeb07a 100755 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ .nyc_output/ node_modules/ test/assets/debug.js +test/assets/debug.json package-lock.json diff --git a/lib/index.js b/lib/index.js index d5ad347..c88f3b1 100644 --- a/lib/index.js +++ b/lib/index.js @@ -52,7 +52,7 @@ class XLSX2JSON { // 自定义解析后的缓存 this.parse2jsonDataCache = []; // 解析过程,记录一些覆盖赋值的情况。 - this.parse2jsonCover = []; + this.parse2jsonCover = new Set(); } /** * Rotate each'sheet'structure. Fill in the blank'cell' @@ -118,7 +118,7 @@ class XLSX2JSON { } return keyPartDescription; } - createJsonEnumCol(columnObject, key, value, colIndex) { + createJsonEnumCol(columnObject, key, value, colIndex, rowIndex, sheetIndex) { if (key === '') return ; if (typeof key !== 'object') { key = key.split('.'); @@ -136,7 +136,7 @@ class XLSX2JSON { let requireSheetResult = this.parsedXlsxData[requireSheetIndex].data; requireSheetResult = this.convertProcess(requireSheetResult, requireSheetIndex)[colIndex]; - return this.createJsonEnumCol(columnObject, keyPart, requireSheetResult, colIndex); + return this.createJsonEnumCol(columnObject, keyPart, requireSheetResult, colIndex, rowIndex, requireSheetIndex); } const keyFirstPartDesc = this.analysisAttrDesc(keyPart); const keyPartIsArr = keyFirstPartDesc[0]; @@ -148,32 +148,32 @@ class XLSX2JSON { if (columnObject[keyPartKeyName] === undefined) { columnObject[keyPartKeyName] = []; } else if (!Array.isArray(columnObject[keyPartKeyName])) { - this.parse2jsonCover.push(`${XLSX2JSON.getJsonCoverKey(keyPart, key)}`); + this.collectCoverKey(sheetIndex, rowIndex); columnObject[keyPartKeyName] = []; } if (keyPartKeyIndex === null) { columnObject[keyPartKeyName].push(isKeyPartEnd ? value : {}); } else { if (columnObject[keyPartKeyName][keyPartKeyIndex] !== undefined) { - this.parse2jsonCover.push(`${XLSX2JSON.getJsonCoverKey(keyPart, key)}`); + this.collectCoverKey(sheetIndex, rowIndex); } columnObject[keyPartKeyName][keyPartKeyIndex] = isKeyPartEnd ? value : {}; } const quoteIndex = keyPartKeyIndex || columnObject[keyPartKeyName].length - 1; - return this.createJsonEnumCol(columnObject[keyPartKeyName][quoteIndex], key, value, colIndex); + return this.createJsonEnumCol(columnObject[keyPartKeyName][quoteIndex], key, value, colIndex, rowIndex, sheetIndex); } else { if (columnObject[keyPartKeyName] === undefined) { columnObject[keyPartKeyName] = isKeyPartEnd ? value : {}; } else if (Object.prototype.toString.call(columnObject[keyPartKeyName]) !== '[object Object]') { - this.parse2jsonCover.push(`${XLSX2JSON.getJsonCoverKey(keyPart, key)}`); + this.collectCoverKey(sheetIndex, rowIndex); columnObject[keyPartKeyName] = isKeyPartEnd ? value : {}; }else { if (isKeyPartEnd) { - this.parse2jsonCover.push(`${XLSX2JSON.getJsonCoverKey(keyPart, key)}`); + this.collectCoverKey(sheetIndex, rowIndex); columnObject[keyPartKeyName] = value; } } - return this.createJsonEnumCol(columnObject[keyPartKeyName], key, value, colIndex); + return this.createJsonEnumCol(columnObject[keyPartKeyName], key, value, colIndex, rowIndex, sheetIndex); } } convertProcess(sheetData, sheetIndex = 0) { @@ -191,7 +191,7 @@ class XLSX2JSON { for (let j = 0, lenJ = colValueArr.length; j < lenJ; j += 1) { // 返回此次tab[].c 数显转换后的深度结构的属性引用。 - this.createJsonEnumCol(colValueJson, attrDescArr[j], colValueArr[j], i - 1); + this.createJsonEnumCol(colValueJson, attrDescArr[j], colValueArr[j], i - 1, j, sheetIndex); } parsedJson.push(colValueJson); } @@ -219,8 +219,11 @@ class XLSX2JSON { } return index; } - static getJsonCoverKey(keyPart, key) { - return `${keyPart}${key.length ? '.' + key.join('.') : ''}`; + collectCoverKey(sheetIndex, rowIndex) { + const sheetName = this.parsedXlsxData[sheetIndex].sheetName; + const keyDescName = this.parsedXlsxData[sheetIndex].data[0][rowIndex]; + const msg = `sheet name "${sheetName}", row ${rowIndex}, value "${keyDescName}"`; + this.parse2jsonCover.add(msg); } } diff --git a/package.json b/package.json index 63e312c..37cf2d3 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xlsx-json-js", - "version": "0.0.2", + "version": "0.0.3", "description": "Parse the'xlsx'file as JSON according to a set of attribute description syntax.", "main": "lib/index.js", "scripts": {