Skip to content

Commit

Permalink
bugfix:number 0 does not output
Browse files Browse the repository at this point in the history
  • Loading branch information
zk-luke committed Nov 4, 2017
1 parent bb1ea53 commit e9a20ae
Showing 1 changed file with 73 additions and 64 deletions.
137 changes: 73 additions & 64 deletions lib/xlsx-to-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ function parseHead(sheet, headIndex) {
//表类型 普通表 主表 引用表
sheetType: SheetType.NORMAL,

getIdKey:function(){
getIdKey: function() {
let id_col_index = this.types.indexOf(DataType.ID);
if(id_col_index < 0){
if (id_col_index < 0) {
throw '获取不到id列的名字';
}
return this.names[id_col_index];
Expand Down Expand Up @@ -195,75 +195,83 @@ function parseRow(row, rowIndex, head) {
let result = {};
let id;

console.log('parsing row', row);

row.forEach((cell, index) => {

if (cell) {
// if (cell) {

let name = head.names[index];
let type = head.types[index];
let name = head.names[index];
let type = head.types[index];

switch (type) {
case DataType.ID: // number string boolean
if (isNumber(cell)) {
id = Number(cell);
} else {
id = cell;
}
result[name] = id;
break;
case DataType.UNKOWN: // number string boolean
if (isNumber(cell)) {
result[name] = Number(cell);
} else if (isBoolean(cell)) {
result[name] = toBoolean(cell);
} else {
switch (type) {
case DataType.ID: // number string boolean
if (isNumber(cell)) {
id = Number(cell);
} else {
id = cell;
}
result[name] = id;
break;
case DataType.UNKOWN: // number string boolean
if (isNumber(cell)) {
result[name] = Number(cell);
} else if (isBoolean(cell)) {
result[name] = toBoolean(cell);
} else {
if (cell) {
result[name] = cell;
}
break;
case DataType.DATE:
if (isNumber(cell)) {
//xlsx's bug!!!
result[name] = numdate(cell);
} else {
}
break;
case DataType.DATE:
if (isNumber(cell)) {
//xlsx's bug!!!
result[name] = numdate(cell);
} else {
if (cell) {
result[name] = cell.toString();
}
break;
case DataType.STRING:
result[name] = cell.toString();
break;
case DataType.NUMBER:
//+xxx.toString() '+' means convert it to number
if (isNumber(cell)) {
result[name] = Number(cell);
} else {
console.warn("type error at [" + rowIndex + "," + index + "]," + cell + " is not a number");
}
break;
case DataType.BOOL:
result[name] = toBoolean(cell);
break;
case DataType.OBJECT: //support {number boolean string date} property type
}
break;
case DataType.STRING:
result[name] = cell.toString();
break;
case DataType.NUMBER:
//+xxx.toString() '+' means convert it to number
if (isNumber(cell)) {
result[name] = Number(cell);
} else {
console.warn("type error at [" + rowIndex + "," + index + "]," + cell + " is not a number");
}
break;
case DataType.BOOL:
result[name] = toBoolean(cell);
break;
case DataType.OBJECT: //support {number boolean string date} property type
if (cell) {
result[name] = array2object(cell.split(';'));
break;
case DataType.ARRAY: //[number] [boolean] [string] todo:support [date] type
result[name] = parseBasicArrayField(cell, arraySeparator);
break;
case DataType.OBJECT_ARRAY:
result[name] = parseObjectArrayField(cell);
break;
default:
// foo#[]| 处理自定义数组分隔符
if (type.indexOf(DataType.ARRAY) !== -1) {
// if (!type.endsWith(DataType.ARRAY)) {
let separator = type.substr(-1, 1); //get the last character
result[name] = parseBasicArrayField(cell, separator);
// }
} else {
console.log('unrecognized type', '[' + rowIndex + ',' + index + ']', cell, typeof(cell));
}
break;
}
}
break;
case DataType.ARRAY: //[number] [boolean] [string] todo:support [date] type
result[name] = parseBasicArrayField(cell, arraySeparator);
break;
case DataType.OBJECT_ARRAY:
result[name] = parseObjectArrayField(cell);
break;
default:
// foo#[]| 处理自定义数组分隔符
if (type.indexOf(DataType.ARRAY) !== -1) {
// if (!type.endsWith(DataType.ARRAY)) {
let separator = type.substr(-1, 1); //get the last character
result[name] = parseBasicArrayField(cell, separator);
// }
} else {
console.log('unrecognized type', '[' + rowIndex + ',' + index + ']', cell, typeof(cell));
}
break;
}
// }
});

return result;
Expand Down Expand Up @@ -397,10 +405,11 @@ function isNumberArray(arr) {
*/
function isNumber(value) {

if (typeof value === 'number') {
return true;
}

if (value) {
if (typeof value === 'number') {
return true;
}
return !isNaN(+value.toString());
}

Expand Down

0 comments on commit e9a20ae

Please sign in to comment.