添加数据验证逻辑,抛出自定义异常以处理验证失败情况;更新错误处理逻辑以显示详细错误信息
This commit is contained in:
@@ -160,8 +160,9 @@ class DocxWriter:
|
||||
cell_end = table.cell(row, col_span + non_none_count - 1)
|
||||
cell_start.merge(cell_end)
|
||||
except IndexError:
|
||||
self.signal(f"单元格合并失败:({row}, {col_span}),需要自行检查表格准确性",
|
||||
LOGLEVEL.WARNING)
|
||||
pass
|
||||
# self.signal(f"单元格合并失败:({row}, {col_span}),需要自行检查表格准确性",
|
||||
# LOGLEVEL.WARNING)
|
||||
col_span += non_none_count
|
||||
|
||||
start = rows - X + 3 + self.excel_reader.kpi_number
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
# Copyright (c) 2025 Jeffrey Hsu - JITToolBox
|
||||
# Copyright (c) 2025-2026 Jeffrey Hsu - JITToolBox
|
||||
# #
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
@@ -73,6 +73,13 @@ class ExcelReader:
|
||||
image = io.BytesIO(self._images[cell]())
|
||||
return Image.open(image)
|
||||
|
||||
class ValidError(Exception):
|
||||
def __init__(self, message):
|
||||
self.message = message
|
||||
|
||||
def __str__(self):
|
||||
return self.message
|
||||
|
||||
def __init__(self, file_path: str, version_check: bool = False,
|
||||
signal: Callable[[str, str], None] = lambda x, y: print(x)):
|
||||
super().__init__()
|
||||
@@ -271,9 +278,15 @@ class ExcelReader:
|
||||
for i in range(len(self.suggestion_template_list), 5):
|
||||
self.suggestion_template_list.append(None)
|
||||
|
||||
self.validate_data()
|
||||
if vd_lst := self.validate_data():
|
||||
raise self.ValidError("\n\n".join(vd_lst))
|
||||
|
||||
self.gen_picture()
|
||||
|
||||
except self.ValidError as ve:
|
||||
raise Exception(f"""
|
||||
数据验证失败:\n\n{str(ve)}
|
||||
""")
|
||||
except Exception as e:
|
||||
error_message = traceback.format_exc()
|
||||
raise Exception(f"""
|
||||
@@ -284,9 +297,18 @@ class ExcelReader:
|
||||
def set_file_path(self, file_path: str):
|
||||
self.file_path = file_path
|
||||
|
||||
def validate_data(self):
|
||||
def validate_data(self) -> list[str]:
|
||||
lst: list[str] = []
|
||||
self.signal("正在验证数据", LOGLEVEL.INFO)
|
||||
return 0
|
||||
if len(self.kpi_list) != self.kpi_number:
|
||||
self.signal("\"课程目标\"或\"目标支撑的毕业要求指标点\"数量与期望目标数量不符", LOGLEVEL.ERROR)
|
||||
lst.append(
|
||||
f"\"课程目标\"或\"目标支撑的毕业要求指标点\"数量与期望目标数量不符,请检查Excel表格中的\"课程目标\"和\"目标支撑的毕业要求指标点\"列是否填写完整。"
|
||||
f"期望得到 {self.kpi_number} 个,实际检测到 {len(self.kpi_list)} 个。"
|
||||
f"(如想暂时不填,请在Excel表格对应的位置添加一个空格)"
|
||||
)
|
||||
|
||||
return lst
|
||||
|
||||
def run(self):
|
||||
self.parse_excel()
|
||||
@@ -655,7 +677,7 @@ class ExcelReader:
|
||||
yield "专业负责人/系主任(签字)"
|
||||
yield ("整改意见:\n"
|
||||
"\n\n\n"
|
||||
f"{" "*8}{self.suggestion_template_list[3] if self.suggestion_template_list[3] is not None else '\n\n\n'}\n\n\n")
|
||||
f"{" " * 8}{self.suggestion_template_list[3] if self.suggestion_template_list[3] is not None else '\n\n\n'}\n\n\n")
|
||||
yield ""
|
||||
yield "签字:"
|
||||
yield ""
|
||||
@@ -664,7 +686,7 @@ class ExcelReader:
|
||||
yield "课程负责人(签字)"
|
||||
yield ("拟整改计划与措施:\n"
|
||||
"\n\n\n"
|
||||
f"{" "*8}{self.suggestion_template_list[4] if self.suggestion_template_list[4] is not None else '\n\n\n'}\n\n\n")
|
||||
f"{" " * 8}{self.suggestion_template_list[4] if self.suggestion_template_list[4] is not None else '\n\n\n'}\n\n\n")
|
||||
yield ""
|
||||
yield "签字:"
|
||||
yield ""
|
||||
|
||||
@@ -269,3 +269,13 @@ class AchievementWidget(Widget):
|
||||
duration=5000,
|
||||
parent=self
|
||||
)
|
||||
elif level == LOGLEVEL.ERROR:
|
||||
InfoBar.error(
|
||||
title='错误',
|
||||
content=content,
|
||||
orient=Qt.Horizontal,
|
||||
isClosable=True,
|
||||
position=InfoBarPosition.TOP_RIGHT,
|
||||
duration=-1,
|
||||
parent=self
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user