// 全局变量用于存储 Excel 数据 let workbook = null; // 切换模式的逻辑 document.addEventListener('DOMContentLoaded', () => { const numericOptions = document.getElementById('numeric-options'); const excelOptions = document.getElementById('excel-options'); const modeRadios = document.querySelectorAll('input[name="mode"]'); modeRadios.forEach(radio => { radio.addEventListener('change', (event) => { if (event.target.value === 'numeric') { numericOptions.style.display = 'block'; excelOptions.style.display = 'none'; } else { numericOptions.style.display = 'none'; excelOptions.style.display = 'block'; } }); }); // 处理文件上传事件 document.getElementById('excel-file').addEventListener('change', handleFileUpload, false); }); function handleFileUpload(event) { const file = event.target.files[0]; if (!file) return; const reader = new FileReader(); reader.onload = (e) => { const data = new Uint8Array(e.target.result); workbook = XLSX.read(data, {type: 'array'}); populateSheetNames(workbook.SheetNames); document.getElementById('sheet-name-select').disabled = false; }; reader.readAsArrayBuffer(file); } function populateSheetNames(sheetNames) { const select = document.getElementById('sheet-name-select'); select.innerHTML = ''; sheetNames.forEach(name => { const option = document.createElement('option'); option.text = name; option.value = name; select.add(option); }); } function generateRandom() { const mode = document.querySelector('input[name="mode"]:checked').value; const resultsDiv = document.getElementById('results'); resultsDiv.innerHTML = ''; if (mode === 'numeric') { const n = parseInt(document.getElementById('n-numeric').value); const a = parseInt(document.getElementById('a').value); const b = parseInt(document.getElementById('b').value); if (n > (b - a + 1)) { resultsDiv.textContent = '错误:抽取数量 n 不能大于抽取范围的数字总数!'; return; } const numbers = Array.from({length: b - a + 1}, (_, i) => a + i); const shuffled = shuffle(numbers); const result = shuffled.slice(0, n).sort((x, y) => x - y); resultsDiv.innerHTML = `数字序列:
${result.join(', ')}`; } else if (mode === 'excel') { if (!workbook) { resultsDiv.textContent = '错误:请先上传一个 Excel 文件!'; return; } const sheetName = document.getElementById('sheet-name-select').value; const columnLetter = document.getElementById('column-letter').value.toUpperCase(); const startRow = parseInt(document.getElementById('start-row').value); const endRow = parseInt(document.getElementById('end-row').value); const n = parseInt(document.getElementById('n-excel').value); const worksheet = workbook.Sheets[sheetName]; if (!worksheet) { resultsDiv.textContent = '错误:找不到指定的工作表!'; return; } const dataArray = []; for (let i = startRow; i <= endRow; i++) { const cellAddress = columnLetter + i; const cell = worksheet[cellAddress]; if (cell && cell.v !== undefined) { dataArray.push(cell.v); } } if (n > dataArray.length) { resultsDiv.textContent = '错误:抽取数量 n 不能大于指定范围的有效数据总数!'; return; } const shuffled = shuffle(dataArray); const result = shuffled.slice(0, n); resultsDiv.innerHTML = `抽取内容:
${result.join(', ')}`; } } // Fisher-Yates 洗牌算法 function shuffle(array) { let currentIndex = array.length, randomIndex; while (currentIndex !== 0) { randomIndex = Math.floor(Math.random() * currentIndex); currentIndex--; [array[currentIndex], array[randomIndex]] = [array[randomIndex], array[currentIndex]]; } return array; }
Author

Zero'F_Fa

Posted on

2025-09-25

Updated on

2025-09-25

Licensed under

Comments