From 356d45fda1c6f356b1d11285bddd686b0ddb4499 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A9=E6=B5=B7=E8=92=BC=E7=81=86?= Date: Mon, 13 Apr 2026 20:54:57 +0800 Subject: [PATCH] Feat: add cell type coercion for Excel export (#13808) ### What problem does this PR solve? - Implemented a helper function to convert markdown cell text to native numeric types for Excel output. - Ensured that leading zeros are preserved and handled various numeric formats, including those with thousand separators and scientific notation. ### Type of change - [x] New Feature (non-breaking change which adds functionality) --- agent/component/message.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/agent/component/message.py b/agent/component/message.py index 7f9a9d328c..c589a55373 100644 --- a/agent/component/message.py +++ b/agent/component/message.py @@ -226,6 +226,38 @@ class Message(ComponentBase): rows = [] headers = None + + def _coerce_excel_cell_type(cell: str): + # Convert markdown cell text to native numeric types when safe,so Excel writes numeric cells instead of text. + if not isinstance(cell, str): + return cell + + value = cell.strip() + if value == "": + return "" + + # Keep values like "00123" as text to avoid losing leading zeros. + if re.match(r"^[+-]?0\d+$", value): + return cell + + # Support thousand separators like 1,234 or 1,234.56 + numeric_candidate = value + if re.match(r"^[+-]?\d{1,3}(,\d{3})+(\.\d+)?$", value): + numeric_candidate = value.replace(",", "") + + if re.match(r"^[+-]?\d+$", numeric_candidate): + try: + return int(numeric_candidate) + except ValueError: + return cell + + if re.match(r"^[+-]?(\d+\.\d+|\d+\.|\.\d+)([eE][+-]?\d+)?$", numeric_candidate) or re.match(r"^[+-]?\d+[eE][+-]?\d+$", numeric_candidate): + try: + return float(numeric_candidate) + except ValueError: + return cell + + return cell for line in table_lines: # Split by | and clean up @@ -236,6 +268,7 @@ class Message(ComponentBase): if headers is None: headers = cells else: + cells = [_coerce_excel_cell_type(c) for c in cells] rows.append(cells) if headers and rows: