move empty not-find rows to summary

This commit is contained in:
2026-05-08 23:54:03 +08:00
parent 0c07cd1290
commit 3d713b0fc5

View File

@@ -462,8 +462,9 @@ def _postprocess_workbook(
workbook = load_workbook(path) workbook = load_workbook(path)
try: try:
_normalize_unmatched_columns(workbook) _normalize_unmatched_columns(workbook)
_remove_empty_not_find_rows(workbook)
summary_records = _collect_summary_records(workbook) summary_records = _collect_summary_records(workbook)
_remove_not_found_rows(workbook)
_remove_empty_unmatched_columns(workbook)
if not include_unmatched_items: if not include_unmatched_items:
_remove_unmatched_columns(workbook) _remove_unmatched_columns(workbook)
@@ -509,13 +510,14 @@ def _normalize_unmatched_columns(workbook) -> None:
if item_name not in item_names: if item_name not in item_names:
item_names.append(item_name) item_names.append(item_name)
if not item_names:
if sheet.max_column >= marker_col: if sheet.max_column >= marker_col:
sheet.delete_cols(marker_col, sheet.max_column - marker_col + 1) sheet.delete_cols(marker_col, sheet.max_column - marker_col + 1)
if not item_names:
sheet.delete_cols(marker_col, 1)
continue continue
if sheet.max_column >= marker_col:
sheet.delete_cols(marker_col, sheet.max_column - marker_col + 1)
sheet.cell(1, marker_col).value = UNMATCHED_HEADER sheet.cell(1, marker_col).value = UNMATCHED_HEADER
sheet.cell(1, marker_col).font = Font(bold=True) sheet.cell(1, marker_col).font = Font(bold=True)
sheet.cell(1, marker_col).fill = UNMATCHED_FILL sheet.cell(1, marker_col).fill = UNMATCHED_FILL
@@ -534,7 +536,7 @@ def _normalize_unmatched_columns(workbook) -> None:
cell.fill = UNMATCHED_FILL cell.fill = UNMATCHED_FILL
def _remove_empty_not_find_rows(workbook) -> None: def _remove_not_found_rows(workbook) -> None:
for sheet in workbook.worksheets: for sheet in workbook.worksheets:
if sheet.title == SUMMARY_SHEET_NAME or sheet.max_row < 2: if sheet.title == SUMMARY_SHEET_NAME or sheet.max_row < 2:
continue continue
@@ -547,13 +549,7 @@ def _remove_empty_not_find_rows(workbook) -> None:
_cell_text(sheet.cell(row_index, col).value) _cell_text(sheet.cell(row_index, col).value)
for col in range(5, standard_end + 1) for col in range(5, standard_end + 1)
] ]
unmatched_values = [] if _all_standard_values_missing(standard_values):
if unmatched_index is not None:
unmatched_values = [
_cell_text(sheet.cell(row_index, col).value)
for col in range(unmatched_index + 2, sheet.max_column + 1)
]
if _all_standard_values_missing(standard_values) and not any(unmatched_values):
sheet.delete_rows(row_index, 1) sheet.delete_rows(row_index, 1)
@@ -564,8 +560,7 @@ def _collect_summary_records(workbook) -> list[dict[str, object]]:
continue continue
header = [_cell_text(sheet.cell(1, col).value) for col in range(1, sheet.max_column + 1)] header = [_cell_text(sheet.cell(1, col).value) for col in range(1, sheet.max_column + 1)]
unmatched_col = _find_header_index(header, UNMATCHED_HEADER) unmatched_col = _find_header_index(header, UNMATCHED_HEADER)
if unmatched_col is None: standard_end = unmatched_col if unmatched_col is not None else sheet.max_column
continue
for row_index in range(2, sheet.max_row + 1): for row_index in range(2, sheet.max_row + 1):
base_values = [_cell_text(sheet.cell(row_index, col).value) for col in range(1, 5)] base_values = [_cell_text(sheet.cell(row_index, col).value) for col in range(1, 5)]
@@ -573,18 +568,13 @@ def _collect_summary_records(workbook) -> list[dict[str, object]]:
continue continue
standard_values = [ standard_values = [
_cell_text(sheet.cell(row_index, col).value) _cell_text(sheet.cell(row_index, col).value)
for col in range(5, unmatched_col + 1) for col in range(5, standard_end + 1)
] ]
item_values = {
_cell_text(sheet.cell(1, col).value): _cell_text(sheet.cell(row_index, col).value)
for col in range(unmatched_col + 2, sheet.max_column + 1)
if _cell_text(sheet.cell(1, col).value)
}
item_values = {name: value for name, value in item_values.items() if value}
if not item_values:
continue
if not _all_standard_values_missing(standard_values): if not _all_standard_values_missing(standard_values):
continue continue
item_values = _summary_item_values(sheet, row_index, header, unmatched_col, standard_end)
if not item_values:
continue
records.append( records.append(
{ {
"name": base_values[0], "name": base_values[0],
@@ -598,6 +588,31 @@ def _collect_summary_records(workbook) -> list[dict[str, object]]:
return records return records
def _summary_item_values(
sheet,
row_index: int,
header: list[str],
unmatched_col: int | None,
standard_end: int,
) -> dict[str, str]:
item_values: dict[str, str] = {}
if unmatched_col is not None:
for col in range(unmatched_col + 2, sheet.max_column + 1):
item_name = _cell_text(sheet.cell(1, col).value)
item_value = _cell_text(sheet.cell(row_index, col).value)
if item_name and item_value:
item_values[item_name] = item_value
if item_values:
return item_values
for col in range(5, standard_end + 1):
item_name = header[col - 1] if col - 1 < len(header) else ""
item_value = _cell_text(sheet.cell(row_index, col).value)
if item_name and item_value:
item_values[item_name] = item_value
return item_values
def _replace_summary_sheet(workbook, records: list[dict[str, object]]) -> None: def _replace_summary_sheet(workbook, records: list[dict[str, object]]) -> None:
if SUMMARY_SHEET_NAME in workbook.sheetnames: if SUMMARY_SHEET_NAME in workbook.sheetnames:
workbook.remove(workbook[SUMMARY_SHEET_NAME]) workbook.remove(workbook[SUMMARY_SHEET_NAME])
@@ -656,6 +671,35 @@ def _remove_unmatched_columns(workbook) -> None:
sheet.delete_cols(unmatched_col + 1, sheet.max_column - unmatched_col) sheet.delete_cols(unmatched_col + 1, sheet.max_column - unmatched_col)
def _remove_empty_unmatched_columns(workbook) -> None:
for sheet in workbook.worksheets:
if sheet.title == SUMMARY_SHEET_NAME or sheet.max_row < 1:
continue
header = [_cell_text(sheet.cell(1, col).value) for col in range(1, sheet.max_column + 1)]
unmatched_col = _find_header_index(header, UNMATCHED_HEADER)
if unmatched_col is None:
continue
first_col = unmatched_col + 1
item_cols = [
col
for col in range(first_col + 1, sheet.max_column + 1)
if _cell_text(sheet.cell(1, col).value)
]
used_item_cols = [
col
for col in item_cols
if any(_cell_text(sheet.cell(row, col).value) for row in range(2, sheet.max_row + 1))
]
if not used_item_cols:
sheet.delete_cols(first_col, sheet.max_column - unmatched_col)
continue
for col in reversed(item_cols):
if col not in used_item_cols:
sheet.delete_cols(col, 1)
def _sort_workbook(workbook, sort_by: str, sort_order: str) -> None: def _sort_workbook(workbook, sort_by: str, sort_order: str) -> None:
sort_by = sort_by if sort_by in SORT_FIELDS else "sample_time" sort_by = sort_by if sort_by in SORT_FIELDS else "sample_time"
sort_order = sort_order if sort_order in SORT_ORDERS else "asc" sort_order = sort_order if sort_order in SORT_ORDERS else "asc"