Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Fix XLSX exports dropping the currency symbol for non-currency columns (#75026)

Ngoc Khuat0c1e95acdd0b
diff --git a/src/metabase/formatter/impl.clj b/src/metabase/formatter/impl.clj
index e850397a6b2..20d26bb58eb 100644
--- a/src/metabase/formatter/impl.clj
+++ b/src/metabase/formatter/impl.clj
@@ -133,12 +133,10 @@
(get column-settings {::mb.viz/column-name col-id-or-name}))
(qualify-keys col-settings))
global-settings (streaming.common/viz-settings-for-col col viz-settings)
- currency? (boolean (or (= (::mb.viz/number-style column-settings) "currency")
- (= (::mb.viz/number-style viz-settings) "currency")
- (and (nil? (::mb.viz/number-style column-settings))
- (or
- (::mb.viz/currency-style column-settings)
- (::mb.viz/currency column-settings)))))
+ ;; Shared with the XLSX export path so both agree on what counts as currency (GDGT-2398). The extra
+ ;; viz-settings check covers a number-style set at the global/viz level rather than on the column itself.
+ currency? (or (streaming.common/currency-settings? column-settings)
+ (= (::mb.viz/number-style viz-settings) "currency"))
{::mb.viz/keys [number-separators decimals scale number-style
prefix suffix currency-style currency]} global-settings
diff --git a/src/metabase/query_processor/streaming/common.clj b/src/metabase/query_processor/streaming/common.clj
index 58bf2e8024d..aa29fb0a849 100644
--- a/src/metabase/query_processor/streaming/common.clj
+++ b/src/metabase/query_processor/streaming/common.clj
@@ -175,13 +175,33 @@
(defmethod global-type-settings :default [_ _viz-settings]
{})
+(defn currency-settings?
+ "Whether a column's viz `settings` indicate it should be formatted as currency.
+
+ True when `number-style` is explicitly \"currency\", or when a currency / currency label style is set without any
+ `number-style`. The latter case matters because the column-formatting UI hides the style dropdown for
+ currency-semantic columns, so `number-style` is frequently never persisted -- the currency options are then the only
+ signal. Both the CSV (`metabase.formatter.impl`) and XLSX export paths share this predicate so they agree on what
+ counts as currency; when they diverged, CSV showed the symbol while XLSX dropped it (GDGT-2398)."
+ [settings]
+ (let [number-style (::mb.viz/number-style settings)]
+ (boolean
+ (or (= number-style "currency")
+ ;; No explicit number-style, but the user picked a currency or a currency label style -- treat as currency.
+ (and (nil? number-style)
+ (or (::mb.viz/currency-style settings)
+ (::mb.viz/currency settings)))))))
+
(defn- column-setting-defaults
"Look up the setting defaults based on any information in the column-settings. This is the case when a column has no
special type (e.g. a number) but the user has specified that the type is currency. We prefer the currency defaults to
the number defaults."
[global-column-settings column-settings]
- (case (::mb.viz/number-style column-settings)
- "currency" (:type/Currency global-column-settings)
+ (if (currency-settings? column-settings)
+ ;; Inject number-style "currency" so consumers that key off it (e.g. the XLSX writer) treat the column as currency
+ ;; even when the user never persisted an explicit number-style.
+ (merge {::mb.viz/number-style "currency"}
+ (:type/Currency global-column-settings))
{}))
(defn- ensure-global-viz-settings
diff --git a/test/metabase/query_processor/streaming/xlsx_test.clj b/test/metabase/query_processor/streaming/xlsx_test.clj
index 7e9417aab40..d575ffbf452 100644
--- a/test/metabase/query_processor/streaming/xlsx_test.clj
+++ b/test/metabase/query_processor/streaming/xlsx_test.clj
@@ -183,6 +183,19 @@
(testing "Default currency formatting is dollar sign"
(is (= "[$$]#,##0.00" (format-string {::mb.viz/currency-in-header false} price-col))))))))
+(deftest format-settings->format-string-currency-without-number-style-test
+ (mt/with-temporary-setting-values [custom-formatting {}]
+ (testing "Currency settings without an explicit number-style still produce a currency cell format (GDGT-2398)"
+ ;; A column that has currency formatting (currency / currency-style / in-cell) but is NOT currency-semantic and
+ ;; never had number-style "currency" persisted -- e.g. an aggregation, native, or model column that dropped its
+ ;; :type/Currency semantic type. CSV emits "$" here, so XLSX must too.
+ (let [non-currency-col {:base_type :type/Float, :effective_type :type/Float}]
+ (is (= "[$$]#,##0.00"
+ (format-string {::mb.viz/currency-in-header false
+ ::mb.viz/currency-style "symbol"
+ ::mb.viz/currency "USD"}
+ non-currency-col)))))))
+
(deftest format-settings->format-string-test-3b
(mt/with-temporary-setting-values [custom-formatting {}]
(testing "Currency formatting"