From 677f2eca297236be608047e47e3d8041e873205e Mon Sep 17 00:00:00 2001 From: github-automation-metabase <166700802+github-automation-metabase@users.noreply.github.com> Date: Fri, 15 Nov 2024 16:33:19 -0500 Subject: [PATCH] XLSX Pivot Exports Now Use The Sort Settings from Viz Settings (#50099) (#50103) * XLSX Pivot Exports Now Use The Sort Settings from Viz Settings Prior to this PR, the xlsx pivot exports worked correctly except for the fact that no sorting (ascending or descending) was applied to the pivot rows or columns. Now, those settings are used when they exist. * only try to set sort when a correct setting exists * add ooxml-full dep for correct classes * Update src/metabase/query_processor/streaming/xlsx.clj --------- Co-authored-by: adam-james <21064735+adam-james-v@users.noreply.github.com> Co-authored-by: Noah Moss <32746338+noahmoss@users.noreply.github.com> --- deps.edn | 1 + src/metabase/query_processor/streaming/xlsx.clj | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/deps.edn b/deps.edn index 0d68f671a9b..666471da1ba 100644 --- a/deps.edn +++ b/deps.edn @@ -125,6 +125,7 @@ org.apache.poi/poi-ooxml {:mvn/version "5.2.5" :exclusions [org.bouncycastle/bcpkix-jdk15on org.bouncycastle/bcprov-jdk15on]} + org.apache.poi/poi-ooxml-full {:mvn/version "5.2.5"} org.apache.sshd/sshd-core {:mvn/version "2.12.1" ; ssh tunneling and test server :exclusions [org.slf4j/slf4j-api org.slf4j/jcl-over-slf4j]} diff --git a/src/metabase/query_processor/streaming/xlsx.clj b/src/metabase/query_processor/streaming/xlsx.clj index 23acb6c64f8..8e81cd6b385 100644 --- a/src/metabase/query_processor/streaming/xlsx.clj +++ b/src/metabase/query_processor/streaming/xlsx.clj @@ -25,7 +25,8 @@ (org.apache.poi.ss.usermodel Cell DataConsolidateFunction DataFormat DateUtil Workbook) (org.apache.poi.ss.util AreaReference CellRangeAddress CellReference) (org.apache.poi.xssf.streaming SXSSFRow SXSSFSheet SXSSFWorkbook) - (org.apache.poi.xssf.usermodel XSSFPivotTable XSSFRow XSSFSheet XSSFWorkbook))) + (org.apache.poi.xssf.usermodel XSSFPivotTable XSSFRow XSSFSheet XSSFWorkbook) + (org.openxmlformats.schemas.spreadsheetml.x2006.main STFieldSortType))) (set! *warn-on-reflection* true) @@ -576,7 +577,7 @@ ;; Since we're the ones creating the file, we can lower the ratio to get what we want. (ZipSecureFile/setMinInflateRatio 0.001) (defn- init-native-pivot - [{:keys [pivot-grouping-key] :as pivot-spec} + [{:keys [pivot-grouping-key column-sort-order] :as pivot-spec} {:keys [ordered-cols col-settings viz-settings format-rows?]}] (let [idx-shift (fn [indices] (map (fn [idx] @@ -612,6 +613,16 @@ (.addColLabel pivot-table idx)) (doseq [idx pivot-measures] (.addColumnLabel pivot-table DataConsolidateFunction/SUM #_(get aggregation-functions idx DataConsolidateFunction/SUM) idx)) + (doseq [[idx sort-setting] column-sort-order] + (let [setting (case sort-setting + :ascending STFieldSortType/ASCENDING + :descending STFieldSortType/DESCENDING)] + (when setting + (-> pivot-table + .getCTPivotTableDefinition + .getPivotFields + (.getPivotFieldArray idx) + (.setSortType setting))))) (let [swb (-> (SXSSFWorkbook. ^XSSFWorkbook wb) (doto (.setCompressTempFiles true))) sheet (spreadsheet/select-sheet "data" swb)] -- GitLab