let originalData = []; let updatedData = []; let workbook; let sheetName; let skippedStudents = []; // 导入学生名单 document.getElementById('excelFileInput').addEventListener('change', function (e) { var file = e.target.files[0]; if (!file) { return; } var reader = new FileReader(); reader.onload = function (e) { var data = new Uint8Array(e.target.result); workbook = XLSX.read(data, { type: 'array' }); sheetName = workbook.SheetNames[0]; var worksheet = workbook.Sheets[sheetName]; originalData = XLSX.utils.sheet_to_json(worksheet); updatedData = JSON.parse(JSON.stringify(originalData)); var displayDiv = document.getElementById('excelDataDisplay'); // 添加表头 displayDiv.innerHTML = '
学号姓名积分
'; updatedData.forEach(function (row) { var rowDiv = document.createElement('div'); Object.keys(row).forEach(key => { var cellDiv = document.createElement('span'); cellDiv.textContent = row[key] + ' '; rowDiv.appendChild(cellDiv); }); displayDiv.appendChild(rowDiv); }); console.log("导入学生名单成功。"); }; reader.readAsArrayBuffer(file); }); // 点名按钮 document.getElementById('callButton').addEventListener('click', function () { console.log("点名按钮被点击。"); // 去除上一次的高亮 var displayDiv = document.getElementById('excelDataDisplay'); Array.from(displayDiv.children).forEach(child => child.style.backgroundColor = ''); // 过滤掉因上次回答问题得分超过 2.5 而跳过的学生 let eligibleStudents = updatedData.filter(student => !skippedStudents.includes(student)); // 计算总积分 let totalPoints = eligibleStudents.reduce((acc, student) => { let pointsKey = Object.keys(student).find(key => key.includes('积分')); return acc + (pointsKey ? parseInt(student[pointsKey]) : 0); }, 0); // 确保总积分不为零 if (totalPoints === 0) { // 如果总积分为零,可以随机选择一个学生或者采取其他处理方式 let randomIndex = Math.floor(Math.random() * eligibleStudents.length); let selectedStudent = eligibleStudents[randomIndex]; var displayDiv = document.getElementById('excelDataDisplay'); var rows = Array.from(displayDiv.children).slice(1); var selectedRow = rows.find(row => row.querySelector('span').textContent.trim() === selectedStudent[Object.keys(selectedStudent)[0]]); selectedRow.style.backgroundColor = 'lightblue'; // 获取选中行的学号和姓名并显示在主页面 var studentInfo = selectedRow.querySelectorAll('span'); var studentId = studentInfo[0].textContent.trim(); var studentName = studentInfo[1].textContent.trim(); var selectedStudentDiv = document.getElementById('selectedStudentInfo'); selectedStudentDiv.innerHTML = `

点到的学生学号:${studentId},姓名:${studentName}

`; console.log(`随机选择了学生,学号:${studentId},姓名:${studentName}`); return; } // 计算每个学生被选中的概率(总积分越高,被点到的概率越低) let probabilities = eligibleStudents.map(student => { let pointsKey = Object.keys(student).find(key => key.includes('积分')); let studentPoints = pointsKey ? parseInt(student[pointsKey]) : 0; return 1 / (studentPoints + 1) / totalPoints; }); // 使用 Math.random 和 Date.now 作为种子 let randomValue = Math.random() + Date.now() % 1000 / 1000; let cumulativeProbability = 0; let selectedStudent; for (let i = 0; i < eligibleStudents.length; i++) { cumulativeProbability += probabilities[i]; if (randomValue < cumulativeProbability) { selectedStudent = eligibleStudents[i]; break; } } if (selectedStudent) { var displayDiv = document.getElementById('excelDataDisplay'); var rows = Array.from(displayDiv.children).slice(1); var selectedRow = rows.find(row => row.querySelector('span').textContent.trim() === selectedStudent[Object.keys(selectedStudent)[0]]); selectedRow.style.backgroundColor = 'lightblue'; // 获取选中行的学号和姓名并显示在主页面 var studentInfo = selectedRow.querySelectorAll('span'); var studentId = studentInfo[0].textContent.trim(); var studentName = studentInfo[1].textContent.trim(); var selectedStudentDiv = document.getElementById('selectedStudentInfo'); selectedStudentDiv.innerHTML = `

点到的学生学号:${studentId},姓名:${studentName}

`; console.log(`通过概率选择点到了学生,学号:${studentId},姓名:${studentName}`); } }); // 到达教室按钮 document.getElementById('arriveClassroom').addEventListener('click', function () { console.log("到达教室按钮被点击。"); var displayDiv = document.getElementById('excelDataDisplay'); var rows = Array.from(displayDiv.children).slice(1); if (rows.length > 0) { var selectedRow = rows.find(row => row.style.backgroundColor === 'lightblue'); if (selectedRow) { var studentInfo = selectedRow.querySelectorAll('span'); var studentId = studentInfo[0].textContent.trim(); var studentName = studentInfo[1].textContent.trim(); // 查找对应的学生并增加积分 var student = updatedData.find(student => student[Object.keys(student)[0]] === studentId && student[Object.keys(student)[1]] === studentName); if (student) { let pointsKey = Object.keys(student).find(key => key.includes('积分')); if (pointsKey) { let originalPoints = parseFloat(student[pointsKey]); student[pointsKey] = originalPoints + 1; // 更新显示 var rowSpans = selectedRow.querySelectorAll('span'); rowSpans[rowSpans.length - 1].textContent = student[pointsKey]; console.log(`学生${studentName}(学号:${studentId})到达教室,积分从${originalPoints}增加到${student[pointsKey]}`); } } } } }); // 准确重复所提问的问题 document.getElementById('repeatedQuestionYes').addEventListener('click', function () { console.log("准确重复问题按钮被点击。"); var displayDiv = document.getElementById('excelDataDisplay'); var rows = Array.from(displayDiv.children).slice(1); if (rows.length > 0) { var selectedRow = rows.find(row => row.style.backgroundColor === 'lightblue'); if (selectedRow) { var studentInfo = selectedRow.querySelectorAll('span'); var studentId = studentInfo[0].textContent.trim(); var studentName = studentInfo[1].textContent.trim(); // 查找对应的学生并增加积分 var student = updatedData.find(student => student[Object.keys(student)[0]] === studentId && student[Object.keys(student)[1]] === studentName); if (student) { let pointsKey = Object.keys(student).find(key => key.includes('积分')); if (pointsKey) { let originalPoints = parseFloat(student[pointsKey]); student[pointsKey] = originalPoints + 0.5; // 更新显示 var rowSpans = selectedRow.querySelectorAll('span'); rowSpans[rowSpans.length - 1].textContent = student[pointsKey]; console.log(`学生${studentName}(学号:${studentId})准确重复问题,积分从${originalPoints}增加到${student[pointsKey]}`); } } } } }); // 不能准确重复所提问的问题 document.getElementById('repeatedQuestionNo').addEventListener('click', function () { console.log("不能准确重复问题按钮被点击。"); var displayDiv = document.getElementById('excelDataDisplay'); var rows = Array.from(displayDiv.children).slice(1); if (rows.length > 0) { var selectedRow = rows.find(row => row.style.backgroundColor === 'lightblue'); if (selectedRow) { var studentInfo = selectedRow.querySelectorAll('span'); var studentId = studentInfo[0].textContent.trim(); var studentName = studentInfo[1].textContent.trim(); // 查找对应的学生并减少积分 var student = updatedData.find(student => student[Object.keys(student)[0]] === studentId && student[Object.keys(student)[1]] === studentName); if (student) { let pointsKey = Object.keys(student).find(key => key.includes('积分')); if (pointsKey) { let originalPoints = parseFloat(student[pointsKey]); student[pointsKey] = originalPoints - 1; // 更新显示 var rowSpans = selectedRow.querySelectorAll('span'); rowSpans[rowSpans.length - 1].textContent = student[pointsKey]; console.log(`学生${studentName}(学号:${studentId})不能准确重复问题,积分从${originalPoints}减少到${student[pointsKey]}`); } } } } }); // 增加自定义分数 document.getElementById('addScoreButton').addEventListener('click', function () { console.log("增加自定义分数按钮被点击。"); var inputScore = parseFloat(document.getElementById('addScoreInput').value); if (inputScore >= 0.5 && inputScore <= 3 && !isNaN(inputScore)) { var displayDiv = document.getElementById('excelDataDisplay'); var rows = Array.from(displayDiv.children).slice(1); if (rows.length > 0) { var selectedRow = rows.find(row => row.style.backgroundColor === 'lightblue'); if (selectedRow) { var studentInfo = selectedRow.querySelectorAll('span'); var studentId = studentInfo[0].textContent.trim(); var studentName = studentInfo[1].textContent.trim(); // 查找对应的学生并增加积分 var student = updatedData.find(student => student[Object.keys(student)[0]] === studentId && student[Object.keys(student)[1]] === studentName); if (student) { let pointsKey = Object.keys(student).find(key => key.includes('积分')); if (pointsKey) { let originalPoints = parseFloat(student[pointsKey]); student[pointsKey] = originalPoints + inputScore; // 更新显示 var rowSpans = selectedRow.querySelectorAll('span'); rowSpans[rowSpans.length - 1].textContent = student[pointsKey]; // 如果得分超过 2.5 分,将该学生加入跳过列表 if (student[pointsKey] > 2.5) { skippedStudents.push(student); } console.log(`学生${studentName}(学号:${studentId})增加自定义分数${inputScore},积分从${originalPoints}增加到${student[pointsKey]}`); } } } } } else { alert('输入的分数应在 0.5 到 3 之间且为有效数字。'); } }); // 按积分从高到低排序 document.getElementById('sortByPointsButton').addEventListener('click', function () { updatedData.sort((a, b) => { let pointsKeyA = Object.keys(a).find(key => key.includes('积分')); let pointsKeyB = Object.keys(b).find(key => key.includes('积分')); return (pointsKeyB ? parseInt(b[pointsKeyB]) : 0) - (pointsKeyA ? parseInt(a[pointsKeyA]) : 0); }); var displayDiv = document.getElementById('excelDataDisplay'); displayDiv.innerHTML = '
学号姓名积分
'; updatedData.forEach(function (row) { var rowDiv = document.createElement('div'); Object.keys(row).forEach(key => { var cellDiv = document.createElement('span'); cellDiv.textContent = row[key] + ' '; rowDiv.appendChild(cellDiv); }); displayDiv.appendChild(rowDiv); }); }); // 按学号从小到大排序 document.getElementById('sortByStudentIdButton').addEventListener('click', function () { updatedData.sort((a, b) => { return a[Object.keys(a)[0]] - b[Object.keys(b)[0]]; }); var displayDiv = document.getElementById('excelDataDisplay'); displayDiv.innerHTML = '
学号姓名积分
'; updatedData.forEach(function (row) { var rowDiv = document.createElement('div'); Object.keys(row).forEach(key => { var cellDiv = document.createElement('span'); cellDiv.textContent = row[key] + ' '; rowDiv.appendChild(cellDiv); }); displayDiv.appendChild(rowDiv); }); });