aardio 日程管理系统

文周周的 2天前 86

 

一、系统概述

aardio开发的日程管理系统,具备日程的添加、删除、修改、筛选、导出及本地存储功能。系统采用模块化设计,各模块职责清晰,便于维护与扩展。

二、完整模块代码及说明

1. 界面模块(UI组件与窗口管理)

import win.ui;
import win.inputBox;

/*DSG{{*/
var winform = win.form(text="日程管理系统";right=850;bottom=500)
winform.add(
button_add={cls="button";text="添加日程";left=25;top=20;right=135;bottom=50;z=1};
button_delete={cls="button";text="删除日程";left=150;top=20;right=260;bottom=50;z=2};
button_export={cls="button";text="导出";left=800;top=52;right=845;bottom=72;z=8};
button_filter={cls="button";text="按日期筛选";left=400;top=20;right=530;bottom=50;z=4};
button_modify={cls="button";text="修改日程";left=275;top=20;right=385;bottom=50;z=3};
button_reset={cls="button";text="重置";left=800;top=22;right=845;bottom=47;z=7};
datetimepick={cls="datetimepick";left=650;top=22;right=780;bottom=47;edge=1;z=5};
edit_date={cls="edit";left=650;top=52;right=780;bottom=72;edge=1;hint="输入筛选日期";z=6};
listbox_schedule={cls="listbox";left=25;top=70;right=580;bottom=450;edge=1;items={};z=9}
)
/*}}*/

// 窗口显示方法:负责显示主窗口并启动消息循环
function showMainWindow() {
    winform.show()
    win.loopMessage()
}

模块功能:定义系统所有UI组件(按钮、列表框、日期选择器等)的布局与属性,提供窗口启动入口。
核心作用:构建用户交互的视觉界面,为其他功能模块提供操作载体。

2. 数据管理模块(数据存储与操作)

// 存储所有日程的全局数组
var schedules = {}

// 数据操作管理器:封装所有数据处理方法
var DataManager = {
    // 获取全部日程
    getAll = function() {
        return schedules;
    },
    // 添加日程
    add = function(schedule) {
        table.push(schedules, schedule);
        return true;
    },
    // 删除指定索引日程
    del = function(index) {
        if (index !== 0 && index <= #schedules) {
            table.remove(schedules, index);
            return true;
        }
        return false;
    },
    // 更新指定索引日程
    update = function(index, newSchedule) {
        if (index !== 0 && index <= #schedules) {
            schedules[index] = newSchedule;
            return true;
        }
        return false;
    },
    // 按日期筛选日程
    filterByDate = function(date) {
        var res = {};
        for (i, v in schedules) {
            if (string.find(v, date)) table.push(res, v);
        }
        return res;
    }
};

模块功能:管理日程数据的存储与核心操作(增删改查、筛选)。
核心作用:通过统一接口处理数据,确保数据操作的一致性,隔离数据层与UI层。

3. 日程操作模块(添加/删除/修改功能)

// 添加日程按钮事件
winform.button_add.oncommand = function(id, event) {
    var schedule = winform.inputBox("请输入新的日程内容:", "输入框", "")
    if (schedule) {
        var datetime = winform.datetimepick.text;
        if (datetime) schedule = datetime + " " + schedule;
        DataManager.add(schedule);
        winform.listbox_schedule.add(schedule);
    }
}

// 删除日程按钮事件
winform.button_delete.oncommand = function(id, event) {
    var index = winform.listbox_schedule.selIndex;
    if (index !== 0) {
        if (DataManager.del(index)) {
            var delRes = winform.listbox_schedule.delete(index);
            if (delRes !== -1) win.msgbox('删除成功');
        }
    } else {
        win.msgbox('请先选中要删除的日程项');
    }
}

// 修改日程按钮事件
winform.button_modify.oncommand = function(id, event) {
    var selIndex = winform.listbox_schedule.selIndex;
    var index = winform.listbox_schedule.getSelected(1, false);
    
    if (index !== -1) {
        var newSchedule = winform.inputBox("请输入新的日程内容:", "输入框", "")
        if (newSchedule) {
            var datetime = winform.datetimepick.text;
            if (datetime) newSchedule = datetime + " " + newSchedule;
            
            if (DataManager.update(selIndex, newSchedule)) {
                winform.listbox_schedule.delete(index);
                winform.listbox_schedule.add(newSchedule, selIndex);
                win.msgbox('修改日程成功');
            }
        }
    }  
};

模块功能:绑定添加、删除、修改按钮的交互事件,实现日程的核心操作。
核心作用:将用户操作转化为数据操作,同步更新数据层与UI显示。

4. 筛选与重置模块(日期筛选与列表重置)

// 按日期筛选按钮事件
winform.button_filter.oncommand = function(id, event) {
    var date = winform.edit_date.text;
    winform.listbox_schedule.clear();
    var hasMatch = false; 
    
    var filtered = DataManager.filterByDate(date);
    for (i, v in filtered) {
        winform.listbox_schedule.add(v);
        hasMatch = true;
    }
    if (!hasMatch) winform.listbox_schedule.add('暂无数据');
}

// 日期选择器联动输入框
winform.datetimepick.onnotify = function(id, code, ptr) {
    winform.edit_date.text = winform.datetimepick.text;
}

// 重置按钮事件
winform.button_reset.oncommand = function(id, event) {
    winform.listbox_schedule.clear();
    var allSchedules = DataManager.getAll();
    for (k, v in allSchedules) {
        winform.listbox_schedule.add(v);
    }    
}

模块功能:实现按日期筛选日程、日期选择器与输入框联动、列表重置为全部数据。
核心作用:提供便捷的数据查询功能,提升用户查找特定日程的效率。

5. 数据导出模块(CSV导出与Excel转换)

winform.button_export.oncommand = function(id, event) {
    import py3;
    import web.json;
    import console;
    import string.csv;
    
    // 生成CSV
    var csv = string.csv(','); 
    var headers = {"日期", "内容"};
    csv.push(headers);
    
    var splitResult_list = {}; 
    var allSchedules = DataManager.getAll();
    for (index, content in allSchedules) {
        var splitResult = string.split(content, " ");
        var date = splitResult[1];
        var contentText = splitResult[2];
        table.push(splitResult_list, {
            '内容' : contentText,
            '日期' : date
        });    
    }
    
    // 写入CSV数据
    for (i=1; #splitResult_list; 1) {
        var row = {};
        for (k, v in headers) {
            table.push(row, splitResult_list[i][v]);
        }
        csv.push(row);
    }
    
    // 保存CSV
    var save = csv.save('测试.csv');
    if (save !== 0) {
        win.msgbox('保存为csv文件成功');
    } else {
        win.msgbox('保存失败'); 
    }
    
    // CSV转Excel
    function py_method() {
        var pyCode = /** 
import csv
from openpyxl import Workbook
from openpyxl.styles import Alignment
from openpyxl.utils import get_column_letter

def format_csv_to_excel():
    wb = Workbook()
    ws = wb.active
    input_csv = "测试.csv"
    output_excel = "formatted_data.xlsx"

    with open(input_csv, 'r', encoding='utf-8') as f:
        reader = csv.reader(f)
        for row in reader:
            ws.append(row)

    # 单元格格式
    for row in ws.iter_rows():
        for cell in row:
            cell.alignment = Alignment(
                horizontal='center',
                vertical='center',
                wrapText=True,
                indent=3
            )

    # 列宽计算
    for col in ws.columns:
        col_idx = col[0].column
        col_letter = get_column_letter(col_idx)
        max_width = 0
        for cell in col:
            if cell.value:
                text = str(cell.value)
                width = 0
                for char in text:
                    if '\u4e00' <= char <= '\u9fff':
                        width += 2
                    else:
                        width += 1
                if width > max_width:
                    max_width = width
        ws.column_dimensions[col_letter].width = max_width + 4

    # 行高计算
    for row in ws.iter_rows():
        max_height = 30
        for cell in row:
            if cell.value:
                lines = str(cell.value).count('\n') + 1
                row_height = lines * 30
                if row_height > max_height:
                    max_height = row_height
        ws.row_dimensions[cell.row].height = max_height

    wb.save(output_excel)
    return f"转换完成!文件已保存至:{output_excel}"
**/;
        py3.exec(pyCode);
        var result = py3.main.format_csv_to_excel();
        if (result) win.msgbox(result);
        return result ? 1 : 0;
    }
    
    if (py_method() !== 0) {
        win.msgbox('调整csv内容成功,转换为Excel文件');
    } else {
        win.msgbox('调整csv内容失败');
    }
}

模块功能:将日程数据导出为CSV文件,并通过Python脚本转换为带格式的Excel文件(含单元格对齐、自适应列宽行高)。
核心作用:支持数据备份与分享,满足用户离线查看日程的需求。

6. 数据持久化模块(本地存储与加载)

import fsys;
import fsys.file;
var filepath = "schedules.txt";

// 加载本地存储的日程数据
if (fsys.isFile(filepath)) {
  var file = fsys.file(filepath, "r");
  if (file) {
      var content = file.read(-1);
      file.close();

      if (content && #content > 0) {
          var lines = string.split(content, '\r\n');
          for (i, line in lines) {
              line = string.trim(line);
              if (line && #line > 0) {
                  table.push(schedules, line);
                  winform.listbox_schedule.add(line);
              }
          }
      }
  }
}

// 程序退出时保存日程数据
winform.onClose = function(){
    var content = "";
   
    for (i, schedule in schedules) {
        content = content + schedule + '\r\n';
    }
    string.save("schedules.txt", content);
}

模块功能:程序启动时加载本地文件中的日程数据,关闭时将当前日程保存到本地文件。
核心作用:确保数据持久化,避免程序关闭后日程丢失。

7. 程序启动模块

// 启动主窗口,开始程序运行
showMainWindow();

模块功能:调用窗口显示方法,启动整个应用程序。
核心作用:作为程序入口,触发整个系统的运行流程。

界面展示

三、总结

本系统通过模块化设计实现了功能的解耦,每个模块专注于特定职责,既保证了代码的完整性,又便于后续维护和功能扩展。核心亮点包括:

  • 清晰的模块划分,降低了代码复杂度

  • 数据与UI分离,提高了代码的可维护性

  • 完善的数据持久化机制,确保用户数据安全

  • 丰富的导出功能,满足多样化的数据使用需求

aardio 新手交流学习群,一起学习的进

qq群号:697197055
微信公众号: 非专业学编程

 


最新回复 (1)
  • mfk 2天前
    0 引用 2

返回