上财课程点名册导出

将上海财经大学课程点名册导出为Excel表格

当前为 2025-03-09 提交的版本,查看 最新版本

// ==UserScript==
// @name         上财课程点名册导出
// @namespace    
// @version      1.0.0
// @author       wyih
// @description  将上海财经大学课程点名册导出为Excel表格
// @license      ISC
// @match        https://eams.sufe.edu.cn/eams/teacherTask!printAttendanceCheckList.action*
// @require      https://cdn.jsdelivr.net/npm/[email protected]/dist/xlsx.full.min.js
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    
    // 添加导出按钮
    function addExportButton() {
        const toolbar = document.getElementById('toolbar14832979851');
        if(!toolbar) return;

        const exportBtn = document.createElement('button');
        exportBtn.textContent = '导出Excel';
        exportBtn.style.marginLeft = '10px';
        exportBtn.style.padding = '3px 8px';
        exportBtn.style.backgroundColor = '#4CAF50';
        exportBtn.style.color = 'white';
        exportBtn.style.border = 'none';
        exportBtn.style.borderRadius = '4px';
        exportBtn.style.cursor = 'pointer';
        exportBtn.onclick = exportToExcel;
        
        toolbar.appendChild(exportBtn);
    }

    // 从表格中提取数据
    function getTableData() {
        const tables = document.getElementsByClassName('listTable');
        let data = [];
        
        // 获取表头列名
        let headerNames = [];
        if(tables.length > 0) {
            const headerRow = tables[0].getElementsByTagName('tr')[0];
            const headerCells = headerRow.getElementsByTagName('td');
            for(let i = 0; i < headerCells.length; i++) {
                headerNames.push(headerCells[i].textContent.trim());
            }
        }
        
        // 遍历所有表格
        for(let table of tables) {
            const rows = table.getElementsByTagName('tr');
            
            // 跳过表头
            for(let i = 1; i < rows.length; i++) {
                const cells = rows[i].getElementsByTagName('td');
                
                // 确保有足够的单元格
                if(cells.length >= 6) {
                    let rowData = {};
                    
                    // 获取前6列数据(序号、学号、姓名、性别、修读类别、班级)
                    for(let j = 0; j < 6; j++) {
                        const headerName = headerNames[j] || `列${j+1}`;
                        rowData[headerName] = cells[j].textContent.trim();
                    }
                    
                    // 检查是否有红色标记(毕业班学生)
                    const rowStyle = rows[i].getAttribute('style');
                    if(rowStyle && rowStyle.includes('color:red')) {
                        rowData['备注'] = '毕业班学生';
                    }
                    
                    data.push(rowData);
                }
            }
        }
        
        return data;
    }

    // 获取课程信息
    function getCourseInfo() {
        const courseInfo = {
            semester: '',
            courseNo: '',
            courseName: '',
            teacher: '',
            schedule: ''
        };
        
        // 获取学期信息
        const semesterElem = document.querySelector('.contentTableTitleTextStyle b');
        if(semesterElem) {
            courseInfo.semester = semesterElem.textContent.trim();
        }
        
        // 获取课程信息
        const infoRows = document.querySelectorAll('.infoTitle');
        for(let row of infoRows) {
            const text = row.textContent;
            
            if(text.includes('课程序号')) {
                const match = text.match(/课程序号:(\d+)/);
                if(match) courseInfo.courseNo = match[1];
                
                const nameMatch = text.match(/课程名称:(.+)/);
                if(nameMatch) courseInfo.courseName = nameMatch[1].trim();
            }
            
            if(text.includes('授课教师')) {
                const teacherMatch = text.match(/授课教师:([^课]+)/);
                if(teacherMatch) courseInfo.teacher = teacherMatch[1].trim();
                
                const scheduleMatch = text.match(/课程安排:(.+)/);
                if(scheduleMatch) courseInfo.schedule = scheduleMatch[1].trim();
            }
        }
        
        return courseInfo;
    }

    // 导出到Excel
    function exportToExcel() {
        const data = getTableData();
        const courseInfo = getCourseInfo();
        
        // 创建工作簿
        const wb = XLSX.utils.book_new();
        
        // 创建工作表
        const ws = XLSX.utils.json_to_sheet(data);
        
        // 添加课程信息作为标题行
        const titleData = [
            ['上海财经大学课程点名册'],
            [`学期: ${courseInfo.semester}`],
            [`课程: ${courseInfo.courseName} (${courseInfo.courseNo})`],
            [`教师: ${courseInfo.teacher}`],
            [`课程安排: ${courseInfo.schedule}`],
            ['']  // 空行
        ];
        
        // 创建标题工作表
        const titleWs = XLSX.utils.aoa_to_sheet(titleData);
        
        // 合并工作表
        const finalWs = XLSX.utils.sheet_add_json(titleWs, data, {origin: 'A7'});
        
        // 添加工作表到工作簿
        XLSX.utils.book_append_sheet(wb, finalWs, "学生名单");
        
        // 生成文件名
        const fileName = `${courseInfo.courseName}_${courseInfo.courseNo}_学生名单.xlsx`;
        
        // 导出文件
        XLSX.writeFile(wb, fileName);
        
        // 显示成功消息
        alert(`已成功导出 ${data.length} 名学生的名单到Excel文件`);
    }

    // 页面加载完成后添加导出按钮
    window.addEventListener('load', function() {
        // 延迟一点执行,确保页面元素都已加载
        setTimeout(addExportButton, 500);
    });

})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址