eugeneo17 commented on code in PR #45817:
URL: https://github.com/apache/superset/pull/45817#discussion_r4445328929


##########
superset/charts/client_processing.py:
##########
@@ -1222,6 +1223,87 @@ def merge_currency_formats(
     return currency_formats
 
 
+def apply_export_number_formats(
+    df: pd.DataFrame,
+    coltypes: list[GenericDataType],
+    form_data: dict[str, Any],
+    datasource: Optional[Union["BaseDatasource", "Query"]] = None,
+) -> pd.DataFrame:
+    """Format numeric columns as display text for a chart-data export.
+
+    Each numeric column (by ``coltypes``, aligned with ``df.columns``) is
+    resolved to its configured d3 format, currency and small-number format the
+    same way the live Table plugin does -- ``column_config`` overrides first,
+    then the datasource's saved per-metric formats -- and handed to
+    ``format_column``. A numeric column with nothing configured takes the
+    fixed-point fallback so its cells are never serialized as raw floats.
+    Non-numeric columns, nulls, NaN and infinity are left to the existing
+    helpers and pass through unchanged. The passed frame is mutated in place 
and
+    returned; the helper runs no query and performs no I/O.
+    """
+    saved_formats, verbose_map = get_datasource_column_formats(datasource)
+    saved_currency_formats, _ = get_datasource_currency_formats(datasource)
+    column_config = form_data.get("column_config") or {}
+
+    def label_of(name: str) -> str:
+        """Return the column label as it appears in ``df`` 
(verbose-renamed)."""
+        return name if name in df.columns else verbose_map.get(name, name)
+
+    # Index the per-column overrides by the label present in ``df`` so the
+    # lookups line up with the (possibly verbose-renamed) frame columns.
+    number_format_by_label = {
+        label_of(name): fmt for name, fmt in saved_formats.items()
+    }
+    currency_by_label = {
+        label_of(name): currency for name, currency in 
saved_currency_formats.items()
+    }
+    config_by_label = {label_of(name): config for name, config in 
column_config.items()}
+
+    numeric_columns = {
+        column
+        for column, coltype in zip(df.columns, coltypes, strict=True)
+        if coltype == GenericDataType.NUMERIC
+    }
+
+    for column in df.columns:
+        if column not in numeric_columns:
+            continue
+
+        config = config_by_label.get(column) or {}
+        configured_currency = config.get("currencyFormat") or {}
+        # The small-number formatter is built from this object whether or not a
+        # symbol survived in it, so an absent one has to stay distinguishable
+        # from a cleared one.
+        small_currency = config.get("currencyFormat")
+        number_format = config.get("d3NumberFormat") or 
number_format_by_label.get(
+            column
+        )
+        currency = (
+            configured_currency
+            if configured_currency.get("symbol")
+            else currency_by_label.get(column) or {}
+        )
+        # A cleared Select leaves a blank string behind, which the browser
+        # treats as unset.
+        small_number_format = (config.get("d3SmallNumberFormat") or 
"").strip() or None
+
+        if number_format or currency.get("symbol") or small_number_format:
+            format_column(
+                df,
+                column,
+                number_format,
+                currency,
+                small_d3_format=small_number_format,
+                small_currency=small_currency,
+            )

Review Comment:
   Both the query-wide `detected_currency` and the per-row 
`row_currency_context` are passed to `format_column` in the configured branch, 
so an AUTO currency resolves against the row's context first and falls back to 
the detected currency. When neither is available the live chart also renders no 
symbol, so leaving the amount bare is the correct parity rather than a dropped 
symbol.
   



##########
superset/common/query_context_processor.py:
##########
@@ -644,6 +646,14 @@ def get_data(
             if verbose_map:
                 df.columns = [verbose_map.get(column, column) for column in 
columns]
 
+            if is_feature_enabled("EXPORT_FORMATTED_NUMBERS"):
+                apply_export_number_formats(
+                    df,
+                    coltypes,
+                    self._query_context.form_data or {},
+                    self._qc_datasource,
+                )

Review Comment:
   This is already handled: when `EXPORT_FORMATTED_NUMBERS` is on, numeric 
coltypes are mapped to `STRING` before `excel.apply_column_types` (the 
`export_coltypes` list in `query_context_processor.py`), precisely so a 
formatted `3.50` is not coerced back to the number `3.5`. The new 
`test_xlsx_export_keeps_configured_decimals_as_text` pins this by reading the 
raw cell type.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to