fix(bom): reject lossy non-text part numbers

This commit is contained in:
阿白叔 committed 2026-10-19 08:56:09 +08:00
1 parent bc36289059
commit ba4c328b2f
3 files changed
+149 -5

No files matched your search

@@ -70,8 +70,16 @@ def write_bom_template(path: Path, records=()) -> Path:
'H': 28, 'I': 12, 'J': 24, 'K': 12}.items():
sheet.column_dimensions[column].width = width
sheet.column_dimensions[column].alignment = wrapped
writable_last_row = max(2, sheet.max_row)
for column in ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J'):
sheet.column_dimensions[column].number_format = '@'
# Existing cell styles override column defaults in Excel. Format the
# scaffold (or first editing row) explicitly to preserve zeroes without
# expanding the workbook's used area with empty formatted rows.
for row_number in range(2, writable_last_row + 1):
cell = sheet[f'{column}{row_number}']
cell.number_format = '@'
cell.alignment = wrapped
path = Path(path)
workbook.save(path)
return path
@@ -110,8 +118,11 @@ def read_bom_records(path: Path) -> tuple[BomRecord, ...]:
raise ValueError('BOM 必须包含旧料号、新料号和位号列')
positions = {name: index for index, name in enumerate(headers) if name}
result = []
for cells in records[1:]:
record = parse_bom_row(cells, positions)
for number, cells in enumerate(records[1:], 2):
try:
record = parse_bom_row(cells, positions)
except (ValueError, TypeError) as error:
raise ValueError(f'Excel 第 {number} 行:{error}') from error
if record is not None:
result.append(record)
return tuple(result)
@@ -130,8 +141,8 @@ def parse_bom_row(cells, positions):
return None
if any(cell.data_type == 'f' for cell in cells):
raise ValueError('BOM 不接受公式,请填写实际值')
old = _text(values, positions, 'old_part_number')
new = _text(values, positions, 'new_part_number')
old = _part_number_text(cells, positions, 'old_part_number', '旧料号')
new = _part_number_text(cells, positions, 'new_part_number', '新料号')
refs = _split_refdes(_text(values, positions, 'refdes'))
if not old and not new and not refs and not _text(values, positions, 'custom_value'):
return None # An untouched project row is a scaffold, not a rule.
@@ -224,6 +235,19 @@ def _text(values, positions, name):
return '' if value is None else str(value).strip()
def _part_number_text(cells, positions, name, label):
index = positions.get(name)
if index is None or index >= len(cells):
return ''
cell = cells[index]
value = cell.value
if value is None:
return ''
if not isinstance(value, str) or cell.data_type not in {'s', 'inlineStr'}:
raise ValueError(f'BOM {label}必须按文本填写,不能使用数值、日期、布尔或错误单元格')
return value.strip()
def _split_refdes(value):
return tuple(item for item in _REFDES_SPLIT.split(value.strip()) if item)