Files
37duAutomation/plugins/xq0033/logic/sheet_utils.py
T
puddy 843ff244a6 feat: 新增多插件 UI 支持及运行日志集中回传功能
本次提交包含多项功能更新:
1. 为多个插件(xq0002/0003/0004/0008/0010/0011/0020/0031_xq0032/0033/0035/0037/0045_xq0046/0047/0048)新增 UI 界面及配置支持
2. 新增运行日志集中回传功能,客户端自动上传运行日志到中心机集中存储
3. 新增插件分类字段支持,用于场景库分组管理
4. 优化 xq0034/0037/0048 等插件的功能及描述
5. 修复控制面响应兼容问题,支持新旧版本数据格式
6. 新增 xq0033 插件的状态存储模块,用于历史记录管理
7. 优化 CRM 登录窗口模式,支持全屏置顶
2026-10-26 15:42:50 +08:00

139 lines
4.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""XQ0033 插件 - openpyxl 工作表通用操作(清洗 / 日期解析 / 行块写删)。
内存约束:openpyxl 的 ws.cell(row=r, column=c) 对不存在的单元格会**创建** Cell 对象,
百万行模板上逐行扫描会凭空造出几十万个 Cell(单个约 220 字节,且 save 时还要序列化),
是本插件出现 MemoryError 的主因。因此扫描类操作一律改为遍历 ws._cells(只读已存在的
单元格),绝不使用 "for r in range(start, ws.max_row + 1): ws.cell(...)" 的写法。
"""
from datetime import date, datetime
def clean(v):
"""None → "",其余转字符串并去首尾空白(与人工粘贴一致)。"""
if v is None:
return ""
return str(v).strip()
def to_date(v):
"""积分日期单元格 → date(datetime/date 直取,字符串尝试 ISO 解析),失败返回 None。"""
if isinstance(v, datetime):
return v.date()
if isinstance(v, date):
return v
if v is None:
return None
try:
return date.fromisoformat(str(v).strip()[:10])
except ValueError:
return None
def cell_map(ws):
"""可写工作表内部单元格字典 {(row, col): Cell};read_only 工作表返回 None。"""
return getattr(ws, "_cells", None)
def existing_value(ws, row, col):
"""读取单元格值但不创建 Cell(模板上大量空白格不落地)。"""
cells = cell_map(ws)
if cells is None:
return ws.cell(row=row, column=col).value
cell = cells.get((row, col))
return None if cell is None else cell.value
def delete_sheet_rows(ws, row_nums):
"""按行号集合删除行:合并连续段后从后往前 delete_rows,避免逐行移动。"""
if not row_nums:
return 0
nums = sorted(set(row_nums))
removed = 0
start = prev = nums[0]
segments = []
for n in nums[1:]:
if n == prev + 1:
prev = n
continue
segments.append((start, prev - start + 1))
start = prev = n
segments.append((start, prev - start + 1))
for seg_start, seg_len in sorted(segments, reverse=True):
ws.delete_rows(seg_start, seg_len)
removed += seg_len
return removed
def scan_last_data_row(ws, col, start_row):
"""扫描某列非空值的最后一行(只遍历已存在单元格,不创建空白 Cell)。"""
last = start_row - 1
cells = cell_map(ws)
if cells is None:
for r in range(start_row, ws.max_row + 1):
if ws.cell(row=r, column=col).value not in (None, ""):
last = r
return last
for (r, c), cell in cells.items():
if c != col or r < start_row:
continue
if cell.value not in (None, "") and r > last:
last = r
return last
def scan_last_formula_row(ws, col, start_row):
"""扫描某列最后一个公式单元格(值以 = 开头),同样不创建 Cell。"""
last = start_row - 1
cells = cell_map(ws)
if cells is None:
for r in range(start_row, ws.max_row + 1):
v = ws.cell(row=r, column=col).value
if isinstance(v, str) and v.startswith("="):
last = r
return last
for (r, c), cell in cells.items():
if c != col or r < start_row:
continue
v = cell.value
if isinstance(v, str) and v.startswith("=") and r > last:
last = r
return last
def iter_rows_with_value(ws, col, start_row):
"""按行号升序产出 (行号, 该列值),只遍历已存在单元格,不创建 Cell。"""
cells = cell_map(ws)
if cells is None:
for r in range(start_row, ws.max_row + 1):
v = ws.cell(row=r, column=col).value
if v not in (None, ""):
yield r, v
return
row_nums = sorted(r for (r, c) in cells if c == col and r >= start_row)
for r in row_nums:
v = cells[(r, col)].value
if v not in (None, ""):
yield r, v
def write_block(ws, start_row, rows_values, col_start, old_last_row, col_count=None):
"""写入行块并清理旧尾部:新行写入 [start_row, start_row+n),其后旧数据行清 None。"""
n = len(rows_values)
if col_count is None:
col_count = len(rows_values[0]) if rows_values else 0
for i, row_vals in enumerate(rows_values):
r = start_row + i
for j, v in enumerate(row_vals):
ws.cell(row=r, column=col_start + j, value=v)
# 清尾只清理真实存在的单元格,避免在空白行上凭空创建 Cell
tail_start = start_row + n
if old_last_row >= tail_start:
cells = cell_map(ws)
for r in range(tail_start, old_last_row + 1):
for c in range(col_start, col_start + col_count):
cell = cells.get((r, c)) if cells is not None else None
if cell is not None:
cell.value = None
return n