_model.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. const fs = require('fs');
  2. const path = require('path');
  3. const XLSX = require('xlsx'); // 通过 npm 安装
  4. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5. //Initialize codes.
  6. AutoLoadPlugins();
  7. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  8. //You can load the plugins in global field, if the next line codes are uncommented.
  9. //AutoLoadGlobalPlugins();
  10. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  11. //Sample codes.
  12. exports.Test = function() {
  13. console.log("Hi, I'm a model test funciton");
  14. };
  15. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  16. //Put you codes here
  17. let absentStudents = []; //初始化未到学生列表,
  18. // 添加未到学生
  19. exports.AddAbsentStudent = function(name) {
  20. if (absentStudents.indexOf(name) === -1) {
  21. absentStudents.push(name);
  22. }
  23. };
  24. // 清空名单
  25. exports.ClearAbsentList = function() {
  26. absentStudents = [];
  27. };
  28. // 导出未到学生名单为 Excel 文件
  29. exports.ExportAbsentStudents = function() {
  30. // 如果没有未到学生,直接返回 null,不导出
  31. if (absentStudents.length === 0) {
  32. return null;
  33. }
  34. // 构造二维数组,第一行为表头“姓名”,后续每行是一个未到学生的名字
  35. const data = [['姓名'], ...absentStudents.map(n => [n])];
  36. // 将二维数组转换为 worksheet(工作表)
  37. const worksheet = XLSX.utils.aoa_to_sheet(data);
  38. // 创建一个新的 workbook(工作簿)
  39. const workbook = XLSX.utils.book_new();
  40. // 将 worksheet 添加到 workbook 中,命名为“未到学生”
  41. XLSX.utils.book_append_sheet(workbook, worksheet, '未到学生');
  42. // 获取当前时间,用于生成文件名
  43. const now = new Date();
  44. const filename = `absent_${now.getFullYear()}${now.getMonth()+1}${now.getDate()}_${now.getHours()}${now.getMinutes()}.xlsx`;
  45. const outputDir = path.join(unit.dir, 'output');
  46. // 如果文件夹不存在,则创建
  47. if (!fs.existsSync(outputDir)) {
  48. fs.mkdirSync(outputDir, { recursive: true }); // recursive: true 可创建多级目录
  49. }
  50. // 构造文件完整路径
  51. const filepath = path.join(outputDir, filename);
  52. // 写入 Excel 文件到磁盘
  53. XLSX.writeFile(workbook, filepath);
  54. // 返回文件路径,用于后续提示或打开文件
  55. return filepath;
  56. };
  57. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  58. // AutoLoadPlugins Function Implement Start.
  59. function AutoLoadPlugins() {
  60. var plugin_dir = (__dirname + '/../addon/');
  61. if (!fs.existsSync(plugin_dir)) {
  62. return;
  63. }
  64. var files = fs.readdirSync(plugin_dir);
  65. files.forEach(function(filename) {
  66. var filedir = path.join(plugin_dir, filename);
  67. var stats = fs.statSync(filedir);
  68. if (!stats.isDirectory()) {
  69. if (filedir.indexOf('-linux.node') !== -1 && require('os').platform() === 'linux') {
  70. require(filedir);
  71. }
  72. if (filedir.indexOf('-win.node') !== -1 && require('os').platform() === 'win32') {
  73. require(filedir);
  74. }
  75. }
  76. });
  77. }
  78. function AutoLoadGlobalPlugins() {
  79. var plugin_dir = (process.env.COMX_SDK + 'addon/');
  80. if (!fs.existsSync(plugin_dir)) {
  81. return;
  82. }
  83. var files = fs.readdirSync(plugin_dir);
  84. files.forEach(function(filename) {
  85. var filedir = path.join(plugin_dir, filename);
  86. var stats = fs.statSync(filedir);
  87. if (!stats.isDirectory()) {
  88. if (filedir.indexOf('-linux.node') !== -1 && require('os').platform() === 'linux') {
  89. require(filedir);
  90. }
  91. if (filedir.indexOf('-win.node') !== -1 && require('os').platform() === 'win32') {
  92. require(filedir);
  93. }
  94. }
  95. });
  96. }
  97. //AutoLoadPlugins Function Implement End.
  98. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  99. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  100. // ide_info Function Implement Start.
  101. exports.ide_info = (msg) => {
  102. if (process.send) {
  103. process.send({
  104. type: 'debug',
  105. info: msg
  106. });
  107. }
  108. };
  109. //ide_info Function Implement End.
  110. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////