From 3d713b0fc519ae6fbf329269232d92a03355cafc Mon Sep 17 00:00:00 2001 From: admin <572701190@qq.com> Date: Fri, 8 May 2026 23:54:03 +0800 Subject: [PATCH] move empty not-find rows to summary --- app/processor.py | 92 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 68 insertions(+), 24 deletions(-) diff --git a/app/processor.py b/app/processor.py index b70a138..26d1b1b 100644 --- a/app/processor.py +++ b/app/processor.py @@ -462,8 +462,9 @@ def _postprocess_workbook( workbook = load_workbook(path) try: _normalize_unmatched_columns(workbook) - _remove_empty_not_find_rows(workbook) summary_records = _collect_summary_records(workbook) + _remove_not_found_rows(workbook) + _remove_empty_unmatched_columns(workbook) if not include_unmatched_items: _remove_unmatched_columns(workbook) @@ -509,13 +510,14 @@ def _normalize_unmatched_columns(workbook) -> None: if item_name not in item_names: item_names.append(item_name) + if not item_names: + if sheet.max_column >= marker_col: + sheet.delete_cols(marker_col, sheet.max_column - marker_col + 1) + continue + if sheet.max_column >= marker_col: sheet.delete_cols(marker_col, sheet.max_column - marker_col + 1) - if not item_names: - sheet.delete_cols(marker_col, 1) - continue - sheet.cell(1, marker_col).value = UNMATCHED_HEADER sheet.cell(1, marker_col).font = Font(bold=True) sheet.cell(1, marker_col).fill = UNMATCHED_FILL @@ -534,7 +536,7 @@ def _normalize_unmatched_columns(workbook) -> None: cell.fill = UNMATCHED_FILL -def _remove_empty_not_find_rows(workbook) -> None: +def _remove_not_found_rows(workbook) -> None: for sheet in workbook.worksheets: if sheet.title == SUMMARY_SHEET_NAME or sheet.max_row < 2: continue @@ -547,13 +549,7 @@ def _remove_empty_not_find_rows(workbook) -> None: _cell_text(sheet.cell(row_index, col).value) for col in range(5, standard_end + 1) ] - unmatched_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): + if _all_standard_values_missing(standard_values): sheet.delete_rows(row_index, 1) @@ -564,8 +560,7 @@ def _collect_summary_records(workbook) -> list[dict[str, object]]: 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 + standard_end = unmatched_col if unmatched_col is not None else sheet.max_column 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)] @@ -573,18 +568,13 @@ def _collect_summary_records(workbook) -> list[dict[str, object]]: continue standard_values = [ _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): continue + item_values = _summary_item_values(sheet, row_index, header, unmatched_col, standard_end) + if not item_values: + continue records.append( { "name": base_values[0], @@ -598,6 +588,31 @@ def _collect_summary_records(workbook) -> list[dict[str, object]]: 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: if SUMMARY_SHEET_NAME in workbook.sheetnames: 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) +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: 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"