rusackas commented on code in PR #42601:
URL: https://github.com/apache/superset/pull/42601#discussion_r3685644719
##########
superset-frontend/src/utils/downloadAsPivotExcel.ts:
##########
@@ -17,12 +17,47 @@
* under the License.
*/
import { utils, writeFile } from 'xlsx';
+import type { WorkSheet } from 'xlsx';
+
+// `raw: true` (used below) keeps every table cell as text, so ordinary
+// numbers lose their native Excel numeric type along with the
+// locale-formatted ones. A cell's text is only restored to a real number
+// when it round-trips losslessly through Number() (e.g. "42" or "-3.5"):
+// that guarantees it's a plain, unambiguous number under any locale, so
+// restoring it can't reintroduce the misparsing raw: true guards against.
+// Anything that doesn't round-trip (grouped thousands, percent suffixes,
+// trailing zero padding, other D3_FORMAT output, etc.) stays as text,
+// exactly as rendered.
+function restoreUnambiguousNumbers(sheet: WorkSheet): void {
+ Object.keys(sheet).forEach(cellRef => {
+ if (cellRef.startsWith('!')) {
+ return;
+ }
+ const cell = sheet[cellRef];
+ if (!cell || cell.t !== 's' || typeof cell.v !== 'string') {
+ return;
+ }
+ const value = Number(cell.v);
+ if (cell.v !== '' && Number.isFinite(value) && String(value) === cell.v) {
+ cell.t = 'n';
+ cell.v = value;
+ }
+ });
+}
export default function exportPivotExcel(
tableSelector: string,
fileName: string,
) {
const table = document.querySelector(tableSelector);
- const workbook = utils.table_to_book(table);
+ // `raw: true` keeps every cell as the literal text rendered in the DOM.
+ // Without it, SheetJS tries to infer numbers/dates from the displayed
+ // string, which mangles values that were formatted using a non-US
+ // D3_FORMAT (e.g. "1.234,56" gets misread as a date or truncated number).
+ const workbook = utils.table_to_book(table, { raw: true });
Review Comment:
Good catch, thanks. Same fix as the numeric case: ISO date/datetime strings
round-trip unambiguously under any locale, so I restore those to native date
cells the same way, everything else stays text.
--
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]