This commit is contained in:
fan 2020-12-25 11:18:09 +08:00
commit ada7d199c8
2 changed files with 185 additions and 66 deletions

View File

@ -15,85 +15,204 @@ export default {
beginRow: 1, beginRow: 1,
beginCol: 0, beginCol: 0,
fieldNum: 6, fieldNum: 6,
sepField: '车次号', trainId: '车次号',
columns: { ignoreColumns: ['备注', '车次号', '运行方向']
'停车站名称': { key: 'stationName', formatter: (val) => { return val; } }, // sepField: '下行线 / 往金海路方向',
'到达时间': { key: 'arriveTime', formatter: (val) => { return val; } }, // columns: {
'出发时间': { key: 'departureTime', formatter: (val) => { return val; } } // '停车站名称': { key: 'stationName', formatter: (val) => { return val; } },
} // '到达时间': { key: 'arriveTime', formatter: (val) => { return val; } },
// '出发时间': { key: 'departureTime', formatter: (val) => { return val; } }
// }
}, },
/** 解析exal数据转换为Json后台数据*/ /** 解析exal数据转换为Json后台数据*/
importData(Sheet, JsonData) { importData(Sheet, JsonData) {
const dataList = convertSheetToList(Sheet, true); const dataList = convertSheetToList(Sheet, true);
const needList = Object.keys(this.ExcelConfig.columns); // const needList = Object.keys(this.ExcelConfig.columns);
if (dataList && dataList.length) { if (dataList && dataList.length) {
for (let colIndex = this.ExcelConfig.beginCol; colIndex < dataList.length; colIndex += this.ExcelConfig.fieldNum + 1) { if (dataList[1] && dataList[0]) {
let isContinue = true; const tIndexStart = 5; // 设置不用过滤开始行数
let tripObj = { code: '', directionCode: '', arrivalList: [] }; const tIndexEnd = 7; // 设置不用过滤结束行数
for (let rowIndex = this.ExcelConfig.beginRow; isContinue; rowIndex += 1) { /** 解析二维数组为json对象*/
isContinue = false; const reg3 = /^(\d+:\d+:\d+|)/; // 06:12:00
dataList.forEach((elem, i) => {
const stationObj = {}; var begin = -1;
for (let index = 0; index < this.ExcelConfig.fieldNum; index += 1) { /** 跳过名称所在的行*/
if (dataList[colIndex + index]) { if ((i < tIndexStart || i > tIndexEnd) && elem && elem.length > 0) {
const title = dataList[colIndex + index][0]; let count = 0;
const value = dataList[colIndex + index][rowIndex]; let param = { begTime: '', endTime: '' };
let stationName = '';
if (title && value) { if (i > tIndexEnd) { elem.reverse(); }
// 数据列解析 // debugger;
isContinue = true; elem.forEach((item, j) => {
const titleStr = `${title}`.trim(); /** 过滤空值*/
let valueStr = `${value}`.trim(); if (item) {
let title = '';
if (titleStr == this.ExcelConfig.sepField) { let nextTitle = '';
valueStr = `${dataList[colIndex + index - 1][rowIndex]}${valueStr}`; var value = `${item}`.trim();
if (tripObj.code) { if (i > tIndexEnd) { // 上行线
const length = tripObj.arrivalList.length; title = `${dataList[tIndexStart][dataList[tIndexStart].length - j - 1]}`.replace(/\s*/g, '');
if (length == 1) { if (title == '到点' || title == '发点') {
tripObj.arrivalList[0]['flag'] = true; title = `${dataList[tIndexEnd][dataList[tIndexEnd].length - j - 1]}`.replace(/\s*/g, '');
} }
if (valueStr != tripObj.code) { nextTitle = dataList[tIndexEnd - 1][dataList[tIndexEnd - 1].length - j - 1];
JsonData.push(tripObj); if (title == 'undefined') {
const strVal = `${value}`.trim(); title = `${dataList[tIndexStart][dataList[tIndexStart].length - j - 2]}`.replace(/\s*/g, '');
tripObj = { code: valueStr, directionCode: strVal[0], arrivalList: [] }; if (title == '到点' || title == '发点') {
title = `${dataList[tIndexEnd][dataList[tIndexEnd].length - j - 2]}`.replace(/\s*/g, '');
} }
nextTitle = dataList[tIndexEnd - 1][dataList[tIndexEnd - 1].length - j - 2];
}
} else if (i < tIndexStart) { // 下行线
title = `${dataList[tIndexStart][j]}`.replace(/\s*/g, '');
nextTitle = dataList[tIndexStart + 1][j];
if (title == 'undefined') {
title = `${dataList[tIndexStart][j - 1]}`.replace(/\s*/g, '');
nextTitle = dataList[tIndexStart + 1][j - 1];
}
}
/** 匹配到开始位置*/
if (title == this.ExcelConfig.trainId) {
if (!value.includes('Y') && value != '金备') {
begin = value; // 设置初始索引
JsonData.push({
code: value,
destinationCode: '',
arrivalList: []
});
} else { } else {
const strVal = `${value}`.trim(); begin = -1; // 设置初始索引
tripObj.code = valueStr; }
tripObj.directionCode = strVal[0]; } else if (begin !== -1) {
if (this.ExcelConfig.ignoreColumns.indexOf(title) == -1 ) {
let need = false;
if (nextTitle) { stationName = nextTitle; }
if (value == '…') {
param.endTime = '…';
} else if (value == '▼' || value == '▲') {
value = '不填';
if (count == 0) {
param.endTime = value;
count = 1;
} else {
param.begTime = value;
count = 0;
}
} else if (reg3.test(value)) {
if (title == '到点') {
param.endTime = value;
} else if (title == '发点') {
param.begTime = value;
} else {
if (count == 0) {
param.endTime = value;
count = 1;
} else {
param.begTime = value;
count = 0;
} }
} }
}
if (title != '发点' && title != '到点') {
stationName = title;
}
if (param.begTime && param.endTime) {
if (param.endTime == '…') {
param.endTime = param.begTime;
}
if (param.endTime == '不填') {
param.endTime = '';
} if (param.begTime == '不填') {
param.begTime = '';
}
need = true;
}
if (need) { // 储存非空 数据
var stationObj = {
stationName: stationName
};
stationObj['arriveTime'] = prefixTime(param.endTime);
stationObj['departureTime'] = prefixTime(param.begTime);
JsonData[JsonData.length - 1].arrivalList.push(stationObj);
param = { begTime: '', endTime: '' };
}
}
}
}
});
}
});
debugger;
console.log(JsonData);
}
// for (let colIndex = this.ExcelConfig.beginCol; colIndex < dataList.length; colIndex += this.ExcelConfig.fieldNum + 1) {
// let isContinue = true;
// const tripObj = { code: '', directionCode: '', arrivalList: [] };
// for (let rowIndex = this.ExcelConfig.beginRow; isContinue; rowIndex += 1) {
// isContinue = false;
// 取需要的字段 // const stationObj = {};
if (needList.findIndex(elem => { return elem == titleStr; }) >= 0) { // for (let index = 0; index < this.ExcelConfig.fieldNum; index += 1) {
stationObj[this.ExcelConfig.columns[titleStr].key] = this.ExcelConfig.columns[titleStr].formatter(valueStr);
if (this.ExcelConfig.columns[titleStr].key == 'arriveTime' || this.ExcelConfig.columns[titleStr].key == 'departureTime') {
stationObj[this.ExcelConfig.columns[titleStr].key] = this.ExcelConfig.columns[titleStr].formatter(prefixTime(valueStr));
}
}
}
}
}
// 添加字段值 // }
if (Object.keys(stationObj).length) { // // for (let index = 0; index < this.ExcelConfig.fieldNum; index += 1) {
if (stationObj.arriveTime) { // // if (dataList[colIndex + index]) {
tripObj.arrivalList.push(stationObj); // // const title = dataList[colIndex + index][0];
} // // const value = dataList[colIndex + index][rowIndex];
}
}
// 添加最后那条没有车次的记录 // // // 数据列解析
if (tripObj.code) { // // debugger;
const length = tripObj.arrivalList.length; // // isContinue = true;
if (length) { // // const titleStr = `${title}`.trim();
tripObj.arrivalList[length - 1]['flag'] = true; // // let valueStr = `${value}`.trim();
}
JsonData.push(tripObj); // // if (titleStr == this.ExcelConfig.sepField) {
} // // valueStr = `${dataList[colIndex + index - 1][rowIndex]}${valueStr}`;
} // // if (tripObj.code) {
// // const length = tripObj.arrivalList.length;
// // if (length == 1) {
// // tripObj.arrivalList[0]['flag'] = true;
// // }
// // if (valueStr != tripObj.code) {
// // JsonData.push(tripObj);
// // const strVal = `${value}`.trim();
// // tripObj = { code: valueStr, directionCode: strVal[0], arrivalList: [] };
// // }
// // } else {
// // const strVal = `${value}`.trim();
// // tripObj.code = valueStr;
// // tripObj.directionCode = strVal[0];
// // }
// // }
// // // 取需要的字段
// // if (needList.findIndex(elem => { return elem == titleStr; }) >= 0) {
// // stationObj[this.ExcelConfig.columns[titleStr].key] = this.ExcelConfig.columns[titleStr].formatter(valueStr);
// // if (this.ExcelConfig.columns[titleStr].key == 'arriveTime' || this.ExcelConfig.columns[titleStr].key == 'departureTime') {
// // stationObj[this.ExcelConfig.columns[titleStr].key] = this.ExcelConfig.columns[titleStr].formatter(prefixTime(valueStr));
// // }
// // }
// // }
// // }
// // }
// // 添加字段值
// if (Object.keys(stationObj).length) {
// if (stationObj.arriveTime) {
// tripObj.arrivalList.push(stationObj);
// }
// }
// }
// // 添加最后那条没有车次的记录
// if (tripObj.code) {
// const length = tripObj.arrivalList.length;
// if (length) {
// tripObj.arrivalList[length - 1]['flag'] = true;
// }
// JsonData.push(tripObj);
// }
// }
} }
return JsonData; return JsonData;