| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- const fs = require('fs');
- const path = require('path');
- const XLSX = require('xlsx'); // 通过 npm 安装
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //Initialize codes.
- AutoLoadPlugins();
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //You can load the plugins in global field, if the next line codes are uncommented.
- //AutoLoadGlobalPlugins();
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //Sample codes.
- exports.Test = function() {
- console.log("Hi, I'm a model test funciton");
- };
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //Put you codes here
- let absentStudents = []; //初始化未到学生列表,
- // 添加未到学生
- exports.AddAbsentStudent = function(name) {
- if (absentStudents.indexOf(name) === -1) {
- absentStudents.push(name);
- }
- };
- // 清空名单
- exports.ClearAbsentList = function() {
- absentStudents = [];
- };
- // 导出未到学生名单为 Excel 文件
- exports.ExportAbsentStudents = function() {
- // 如果没有未到学生,直接返回 null,不导出
- if (absentStudents.length === 0) {
- return null;
- }
- // 构造二维数组,第一行为表头“姓名”,后续每行是一个未到学生的名字
- const data = [['姓名'], ...absentStudents.map(n => [n])];
- // 将二维数组转换为 worksheet(工作表)
- const worksheet = XLSX.utils.aoa_to_sheet(data);
- // 创建一个新的 workbook(工作簿)
- const workbook = XLSX.utils.book_new();
- // 将 worksheet 添加到 workbook 中,命名为“未到学生”
- XLSX.utils.book_append_sheet(workbook, worksheet, '未到学生');
- // 获取当前时间,用于生成文件名
- const now = new Date();
- const filename = `absent_${now.getFullYear()}${now.getMonth()+1}${now.getDate()}_${now.getHours()}${now.getMinutes()}.xlsx`;
- const outputDir = path.join(unit.dir, 'output');
- // 如果文件夹不存在,则创建
- if (!fs.existsSync(outputDir)) {
- fs.mkdirSync(outputDir, { recursive: true }); // recursive: true 可创建多级目录
- }
- // 构造文件完整路径
- const filepath = path.join(outputDir, filename);
- // 写入 Excel 文件到磁盘
- XLSX.writeFile(workbook, filepath);
- // 返回文件路径,用于后续提示或打开文件
- return filepath;
- };
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // AutoLoadPlugins Function Implement Start.
- function AutoLoadPlugins() {
- var plugin_dir = (__dirname + '/../addon/');
- if (!fs.existsSync(plugin_dir)) {
- return;
- }
- var files = fs.readdirSync(plugin_dir);
- files.forEach(function(filename) {
- var filedir = path.join(plugin_dir, filename);
- var stats = fs.statSync(filedir);
- if (!stats.isDirectory()) {
- if (filedir.indexOf('-linux.node') !== -1 && require('os').platform() === 'linux') {
- require(filedir);
- }
- if (filedir.indexOf('-win.node') !== -1 && require('os').platform() === 'win32') {
- require(filedir);
- }
- }
- });
- }
- function AutoLoadGlobalPlugins() {
- var plugin_dir = (process.env.COMX_SDK + 'addon/');
- if (!fs.existsSync(plugin_dir)) {
- return;
- }
- var files = fs.readdirSync(plugin_dir);
- files.forEach(function(filename) {
- var filedir = path.join(plugin_dir, filename);
- var stats = fs.statSync(filedir);
- if (!stats.isDirectory()) {
- if (filedir.indexOf('-linux.node') !== -1 && require('os').platform() === 'linux') {
- require(filedir);
- }
- if (filedir.indexOf('-win.node') !== -1 && require('os').platform() === 'win32') {
- require(filedir);
- }
- }
- });
- }
- //AutoLoadPlugins Function Implement End.
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // ide_info Function Implement Start.
- exports.ide_info = (msg) => {
- if (process.send) {
- process.send({
- type: 'debug',
- info: msg
- });
- }
- };
- //ide_info Function Implement End.
- /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|