import re UNMATCHED_HEADER = "未匹配检测内容" UNASSIGNED_SHEET_NAME = "未归属检测内容" def match_re(value, pattern): return re.match(str(pattern), str(value or "")) is not None def clean_result(value): if value in (None, "", "."): return "None" return str(value) def detail_value(row): for key in ("result_str", "result_ref", "result_txt", "result1"): value = row.get(key, "") if value not in (None, ""): return clean_result(value) return "" def route_detail_rows(detail_rows, all_tests): sheet_results = {} unassigned_items = [] for detail_row in detail_rows: item_name = detail_row.get("rpt_itemname", "") matched_any = False for test in all_tests: test_result_col_name = test["test_result_col_name"] test_check_list = test["test_check_list"] test_check_list_all = test["test_check_list_all"] for index, checks in enumerate(test_check_list_all): if isinstance(checks, str): checks = [checks] if any(match_re(item_name, pattern) for pattern in checks): sheet_name = test["test_check_name"] result_name = test_check_list[index] sheet_results.setdefault(sheet_name, {})[result_name] = clean_result( detail_row.get(test_result_col_name, "") ) matched_any = True break if not matched_any: unassigned_items.append(f"{item_name}:{detail_value(detail_row)}") return sheet_results, unassigned_items def append_routed_report( add_content_to_excel, result_save_path, all_tests, excel_head, excel_basic, sheet_results, unassigned_items, show_not_match, ): for test in all_tests: sheet_name = test["test_check_name"] if sheet_name not in sheet_results: continue result_values = sheet_results[sheet_name] row = excel_head + excel_basic + [ result_values.get(test_item, "Not_Find") for test_item in test["test_check_list"] ] if show_not_match: row += unassigned_items add_content_to_excel(result_save_path, sheet_name, row) if unassigned_items and not sheet_results: add_content_to_excel(result_save_path, UNASSIGNED_SHEET_NAME, excel_head + excel_basic + unassigned_items)