|
|
@@ -300,31 +300,70 @@ export default {
|
|
|
if (allData.length < 2) return this.$message.error("Excel 数据行数不足");
|
|
|
|
|
|
// 读取第一行作为标题,动态匹配列索引
|
|
|
- const headers = allData[0];
|
|
|
- const getColumnIndex = (searchKeywords) => {
|
|
|
- const index = headers.findIndex(header =>
|
|
|
- searchKeywords.some(keyword =>
|
|
|
- String(header).trim().includes(keyword)
|
|
|
- )
|
|
|
- );
|
|
|
- return index >= 0 ? index : -1;
|
|
|
- };
|
|
|
+ const headers = allData[0].map(h => String(h).trim());
|
|
|
+
|
|
|
+ // 优先精确匹配,然后模糊匹配,且优先匹配更长的关键词
|
|
|
+ const getColumnIndex = (searchKeywords, excludeIndices = []) => {
|
|
|
+ // 先按长度降序排序,优先匹配更长的关键词(更具体的匹配)
|
|
|
+ const sortedKeywords = [...searchKeywords].sort((a, b) => b.length - a.length);
|
|
|
+
|
|
|
+ // 先尝试精确匹配
|
|
|
+ for (const keyword of sortedKeywords) {
|
|
|
+ const exactIndex = headers.findIndex((header, idx) =>
|
|
|
+ !excludeIndices.includes(idx) && header === keyword
|
|
|
+ );
|
|
|
+ if (exactIndex >= 0) return exactIndex;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 再尝试模糊匹配(includes),但排除已匹配的列
|
|
|
+ for (const keyword of sortedKeywords) {
|
|
|
+ const fuzzyIndex = headers.findIndex((header, idx) =>
|
|
|
+ !excludeIndices.includes(idx) && header.includes(keyword)
|
|
|
+ );
|
|
|
+ if (fuzzyIndex >= 0) return fuzzyIndex;
|
|
|
+ }
|
|
|
|
|
|
- // 动态获取列索引
|
|
|
- const colIndices = {
|
|
|
- topic_type: getColumnIndex(['题目类型', '题型']),
|
|
|
- topic_name: getColumnIndex(['题目', '题目内容']),
|
|
|
- answer_A: getColumnIndex(['选项A', '答案A', '选项 A', '答案 A']),
|
|
|
- answer_B: getColumnIndex(['选项B', '答案B', '选项 B', '答案 B']),
|
|
|
- answer_C: getColumnIndex(['选项C', '答案C', '选项 C', '答案 C']),
|
|
|
- answer_D: getColumnIndex(['选项D', '答案D', '选项 D', '答案 D']),
|
|
|
- answer_E: getColumnIndex(['选项E', '答案E', '选项 E', '答案 E']),
|
|
|
- answer_F: getColumnIndex(['选项F', '答案F', '选项 F', '答案 F']),
|
|
|
- correct_answer: getColumnIndex(['正确答案', '标准答案', '答案']),
|
|
|
- topic_analysis: getColumnIndex(['解析', '题目解析', '答案解析']),
|
|
|
- score: getColumnIndex(['分数', '分值', '得分'])
|
|
|
+ return -1;
|
|
|
};
|
|
|
|
|
|
+ // 动态获取列索引,按优先级顺序匹配,避免冲突
|
|
|
+ const colIndices = {};
|
|
|
+ const usedIndices = [];
|
|
|
+
|
|
|
+ // 先匹配更具体的字段(避免被包含在其他字段中)
|
|
|
+ colIndices.topic_type = getColumnIndex(['题目类型', '题型'], usedIndices);
|
|
|
+ if (colIndices.topic_type >= 0) usedIndices.push(colIndices.topic_type);
|
|
|
+
|
|
|
+ colIndices.topic_name = getColumnIndex(['题目内容', '题目'], usedIndices);
|
|
|
+ if (colIndices.topic_name >= 0) usedIndices.push(colIndices.topic_name);
|
|
|
+
|
|
|
+ colIndices.correct_answer = getColumnIndex(['正确答案', '标准答案', '答案'], usedIndices);
|
|
|
+ if (colIndices.correct_answer >= 0) usedIndices.push(colIndices.correct_answer);
|
|
|
+
|
|
|
+ colIndices.topic_analysis = getColumnIndex(['题目解析', '答案解析', '解析'], usedIndices);
|
|
|
+ if (colIndices.topic_analysis >= 0) usedIndices.push(colIndices.topic_analysis);
|
|
|
+
|
|
|
+ colIndices.answer_A = getColumnIndex(['选项A', '答案A', '选项 A', '答案 A'], usedIndices);
|
|
|
+ if (colIndices.answer_A >= 0) usedIndices.push(colIndices.answer_A);
|
|
|
+
|
|
|
+ colIndices.answer_B = getColumnIndex(['选项B', '答案B', '选项 B', '答案 B'], usedIndices);
|
|
|
+ if (colIndices.answer_B >= 0) usedIndices.push(colIndices.answer_B);
|
|
|
+
|
|
|
+ colIndices.answer_C = getColumnIndex(['选项C', '答案C', '选项 C', '答案 C'], usedIndices);
|
|
|
+ if (colIndices.answer_C >= 0) usedIndices.push(colIndices.answer_C);
|
|
|
+
|
|
|
+ colIndices.answer_D = getColumnIndex(['选项D', '答案D', '选项 D', '答案 D'], usedIndices);
|
|
|
+ if (colIndices.answer_D >= 0) usedIndices.push(colIndices.answer_D);
|
|
|
+
|
|
|
+ colIndices.answer_E = getColumnIndex(['选项E', '答案E', '选项 E', '答案 E'], usedIndices);
|
|
|
+ if (colIndices.answer_E >= 0) usedIndices.push(colIndices.answer_E);
|
|
|
+
|
|
|
+ colIndices.answer_F = getColumnIndex(['选项F', '答案F', '选项 F', '答案 F'], usedIndices);
|
|
|
+ if (colIndices.answer_F >= 0) usedIndices.push(colIndices.answer_F);
|
|
|
+
|
|
|
+ colIndices.score = getColumnIndex(['分数', '分值', '得分'], usedIndices);
|
|
|
+ if (colIndices.score >= 0) usedIndices.push(colIndices.score);
|
|
|
+
|
|
|
// 验证必要的列是否存在
|
|
|
const requiredColumns = ['topic_type', 'topic_name', 'correct_answer'];
|
|
|
const missingColumns = requiredColumns.filter(key => colIndices[key] === -1);
|