From ce5217bd23956c3d2599beaff9e85d44c2e9b0ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=5FMr=E4=B8=B6s=20=E3=82=9B=F0=9F=98=81=C2=AE?= <31917567+ma123456mai@users.noreply.github.com> Date: Wed, 30 Sep 2026 15:05:59 +0800 Subject: [PATCH] fix(csv): stop wrapping exported dates in equals-prefixed formulas --- .../src/components/objects/ObjectBrowser.vue | 3 +-- .../__tests__/useDataGridExport.spec.ts | 15 ++++++++++++ .../src/composables/useDataGridExport.ts | 15 ++++-------- .../useSidebarTreeExportRuntime.ts | 3 +-- .../dataGrid/columnFormatter.spec.ts | 24 +------------------ .../src/lib/dataGrid/columnFormatter.ts | 18 -------------- packages/app-tests/useDataGridExport.test.ts | 6 ++--- 7 files changed, 25 insertions(+), 59 deletions(-) diff --git a/apps/desktop/src/components/objects/ObjectBrowser.vue b/apps/desktop/src/components/objects/ObjectBrowser.vue index db43210536..bdd70e3a57 100644 --- a/apps/desktop/src/components/objects/ObjectBrowser.vue +++ b/apps/desktop/src/components/objects/ObjectBrowser.vue @@ -118,7 +118,6 @@ import { } from "@/lib/table/tableClipboard"; import { buildSingleDdlExportFileContent } from "@/lib/export/ddlExport"; import { fetchTableDataForExport } from "@/lib/table/tableDataExport"; -import { forceCsvTextForTemporalColumns } from "@/lib/dataGrid/columnFormatter"; import { useConnectionStore } from "@/stores/connectionStore"; import { treeNodePinIdentity, type PinnedTreeNodeIdentity } from "@/lib/app/pinnedItems"; import { useExportTracker, type ExportTask } from "@/composables/useExportTracker"; @@ -2423,7 +2422,7 @@ async function exportTableData(row: ObjectBrowserRow, format: "csv" | "xlsx" | " executePage: (sql) => api.executeQuery(props.connection.id, props.database, sql), }); if (format === "csv") { - await api.exportQueryResultCsv(filePath, result.columns, forceCsvTextForTemporalColumns(result.rows, result.column_types ?? []), settingsStore.editorSettings.csvQuoteMode, csvNullLiteralForMode(settingsStore.editorSettings.csvNullMode)); + await api.exportQueryResultCsv(filePath, result.columns, result.rows, settingsStore.editorSettings.csvQuoteMode, csvNullLiteralForMode(settingsStore.editorSettings.csvNullMode)); } else { const comments = result.columns.map((name) => columnInfos?.find((column) => column.name.toLocaleLowerCase() === name.toLocaleLowerCase())?.comment); const headerOverrides = buildXlsxHeaderOverrides(result.columns, comments, headerMode); diff --git a/apps/desktop/src/composables/__tests__/useDataGridExport.spec.ts b/apps/desktop/src/composables/__tests__/useDataGridExport.spec.ts index c563a403a4..39ded51c4d 100644 --- a/apps/desktop/src/composables/__tests__/useDataGridExport.spec.ts +++ b/apps/desktop/src/composables/__tests__/useDataGridExport.spec.ts @@ -1704,6 +1704,21 @@ describe("useDataGridExport prepared row statements", () => { expect(exportQueryResultCsv).toHaveBeenLastCalledWith(expect.any(String), ["_id", "value"], [["1", reservedString]], expect.anything(), expect.anything()); }); + it("exports temporal CSV values without an Excel formula wrapper (#10694)", async () => { + setActivePinia(createPinia()); + const timestamp = "2026-09-30 12:34:56.789"; + const table: DataGridTableMeta = { + tableName: "events", + primaryKeys: [], + columns: [{ name: "created_at", data_type: "timestamp" }], + }; + const state = createExportState(table, ["created_at"], undefined, [timestamp]); + + await state.exportCurrentPageCsv(); + + expect(exportQueryResultCsv).toHaveBeenLastCalledWith(expect.any(String), ["created_at"], [[timestamp]], expect.anything(), expect.anything()); + }); + it("exports only visible Mongo columns from the full result set", async () => { setActivePinia(createPinia()); const state = createMongoExportState({ diff --git a/apps/desktop/src/composables/useDataGridExport.ts b/apps/desktop/src/composables/useDataGridExport.ts index 8bc4b8af6b..5a602443ba 100644 --- a/apps/desktop/src/composables/useDataGridExport.ts +++ b/apps/desktop/src/composables/useDataGridExport.ts @@ -30,7 +30,7 @@ import type { DatabaseType, QueryResult } from "@/types/database"; import type { QueryResultExportRequest } from "@/lib/backend/api"; import { usesSyntheticRowIdKey } from "@/lib/table/tableEditing"; import { buildXlsxSqlWorksheet } from "@/lib/export/xlsxSqlSheet"; -import { forceCsvTextForTemporalColumns, formatTemporalRowsForExport } from "@/lib/dataGrid/columnFormatter"; +import { formatTemporalRowsForExport } from "@/lib/dataGrid/columnFormatter"; import { translateBackendError } from "@/i18n/backend-errors"; import XlsxHeaderDialog from "@/components/export/XlsxHeaderDialog.vue"; import i18n from "@/i18n"; @@ -885,13 +885,7 @@ export function useDataGridExport(options: UseDataGridExportOptions) { }; } }); - // Hand the raw rows straight to the Rust command. Formatting (NULL→"", - // bool/number→text, etc.) happens there on a spawn_blocking thread, so - // we avoid mapping every cell synchronously on the UI thread. Temporal - // columns are the one exception: force-text them here (see - // forceCsvTextForTemporalColumns) so WPS/Excel-style CSV import can't - // re-guess and truncate/reformat a date-looking string. - const rows = forceCsvTextForTemporalColumns(result.rows, result.columnTypes); + // Let the Rust command format and write the raw values off the UI thread. if (needsFullExport && exportProgressState) { exportProgressState.value = { ...exportProgressState.value, @@ -913,7 +907,7 @@ export function useDataGridExport(options: UseDataGridExportOptions) { } outputPath = path as string; } - await api.exportQueryResultCsv(outputPath, result.columns, rows, useSettingsStore().editorSettings.csvQuoteMode, csvNullLiteralForMode(useSettingsStore().editorSettings.csvNullMode)); + await api.exportQueryResultCsv(outputPath, result.columns, result.rows, useSettingsStore().editorSettings.csvQuoteMode, csvNullLiteralForMode(useSettingsStore().editorSettings.csvNullMode)); if (needsFullExport && exportProgressState) { exportProgressState.value = { ...exportProgressState.value, @@ -952,8 +946,7 @@ export function useDataGridExport(options: UseDataGridExportOptions) { outputPath = path as string; } const result = await resultToExport(undefined, undefined, false); - const rows = forceCsvTextForTemporalColumns(result.rows, result.columnTypes); - await api.exportQueryResultCsv(outputPath, result.columns, rows, useSettingsStore().editorSettings.csvQuoteMode, csvNullLiteralForMode(useSettingsStore().editorSettings.csvNullMode)); + await api.exportQueryResultCsv(outputPath, result.columns, result.rows, useSettingsStore().editorSettings.csvQuoteMode, csvNullLiteralForMode(useSettingsStore().editorSettings.csvNullMode)); toast(t("grid.exported")); } catch (e: any) { toast(t("grid.exportFailed", { message: translateBackendError(t, e) }), 5000); diff --git a/apps/desktop/src/composables/useSidebarTreeExportRuntime.ts b/apps/desktop/src/composables/useSidebarTreeExportRuntime.ts index 02b2c1ad83..253e76434f 100644 --- a/apps/desktop/src/composables/useSidebarTreeExportRuntime.ts +++ b/apps/desktop/src/composables/useSidebarTreeExportRuntime.ts @@ -16,7 +16,6 @@ import { joinExportedDdls } from "@/lib/export/ddlExport"; import { translateBackendError } from "@/i18n/backend-errors"; import { sidebarStructureExportTargets, sidebarTableDataExportTargets } from "@/lib/sidebar/sidebarExportRuntime"; import { fetchTableDataForExport } from "@/lib/table/tableDataExport"; -import { forceCsvTextForTemporalColumns } from "@/lib/dataGrid/columnFormatter"; import XlsxHeaderDialog from "@/components/export/XlsxHeaderDialog.vue"; import { buildXlsxHeaderOverrides, hasXlsxHeaderComments, type XlsxExportOptions, type XlsxHeaderMode } from "@/lib/export/xlsxHeader"; import { @@ -476,7 +475,7 @@ export function useSidebarTreeExportRuntime(options: SidebarTreeExportRuntimeOpt executePage: (sql) => api.executeQuery(connectionId, database, sql), }); if (format === "csv") { - await api.exportQueryResultCsv(outputPath, result.columns, forceCsvTextForTemporalColumns(result.rows, result.column_types ?? []), target.csvQuoteMode, target.nullLiteral); + await api.exportQueryResultCsv(outputPath, result.columns, result.rows, target.csvQuoteMode, target.nullLiteral); } else { const comments = result.columns.map((name) => exportColumnInfos?.find((column) => column.name.toLocaleLowerCase() === name.toLocaleLowerCase())?.comment); const headerOverrides = buildXlsxHeaderOverrides(result.columns, comments, headerMode); diff --git a/apps/desktop/src/lib/__tests__/dataGrid/columnFormatter.spec.ts b/apps/desktop/src/lib/__tests__/dataGrid/columnFormatter.spec.ts index 0abcfa274e..ee9ba3d7dc 100644 --- a/apps/desktop/src/lib/__tests__/dataGrid/columnFormatter.spec.ts +++ b/apps/desktop/src/lib/__tests__/dataGrid/columnFormatter.spec.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from "vitest"; -import { applyColumnFormatter, columnFormatterKeys, defaultIoTDBTimestampFormatter, forceCsvTextForTemporalColumns, formatIoTDBTimestampEditorValue, normalizeColumnFormatter, normalizeSupportedDateTimePattern, parseIoTDBTimestampEditorValue } from "@/lib/dataGrid/columnFormatter"; +import { applyColumnFormatter, columnFormatterKeys, defaultIoTDBTimestampFormatter, formatIoTDBTimestampEditorValue, normalizeColumnFormatter, normalizeSupportedDateTimePattern, parseIoTDBTimestampEditorValue } from "@/lib/dataGrid/columnFormatter"; describe("columnFormatterKeys", () => { // MySQL query metadata mirrors the database into the schema slot, while the @@ -199,25 +199,3 @@ describe("defaultIoTDBTimestampFormatter", () => { expect(parseIoTDBTimestampEditorValue("2026-08-17 10:00:00", "iotdb", "TIMESTAMP", "time_zone=Asia%2FShanghai")).toBeUndefined(); }); }); - -describe("forceCsvTextForTemporalColumns", () => { - it("wraps only string values in temporal columns as force-text formulas", () => { - const rows = [["2026-07-25 00:00:00.000", 48.962002, "root.a.b"]]; - const columnTypes = ["TIMESTAMP", "DOUBLE", "TEXT"]; - expect(forceCsvTextForTemporalColumns(rows, columnTypes)).toEqual([['="2026-07-25 00:00:00.000"', 48.962002, "root.a.b"]]); - }); - - it("leaves null and non-string temporal cells untouched", () => { - const rows = [[null, 1700000000000]]; - const columnTypes = ["DATETIME", "DATETIME"]; - expect(forceCsvTextForTemporalColumns(rows, columnTypes)).toEqual([[null, 1700000000000]]); - }); - - it("returns shallow copies of rows when no column is temporal", () => { - const rows = [["plain", 1]]; - const columnTypes = ["VARCHAR", "INT"]; - const result = forceCsvTextForTemporalColumns(rows, columnTypes); - expect(result).toEqual(rows); - expect(result[0]).not.toBe(rows[0]); - }); -}); diff --git a/apps/desktop/src/lib/dataGrid/columnFormatter.ts b/apps/desktop/src/lib/dataGrid/columnFormatter.ts index 2ce9e0d0d7..295065d0fc 100644 --- a/apps/desktop/src/lib/dataGrid/columnFormatter.ts +++ b/apps/desktop/src/lib/dataGrid/columnFormatter.ts @@ -452,24 +452,6 @@ export function formatTemporalRowsForExport(rows: readonly ); } -// CSV is untyped text: spreadsheet apps (WPS/Excel/OnlyOffice) re-guess a -// quoted date-looking cell's type on open regardless of RFC4180 quoting, -// which silently truncates/reformats it and can drop the milliseconds or -// the date part entirely. Wrapping the value as an `="..."` formula is the -// standard cross-app way to force text interpretation; the surrounding CSV -// quoting/escaping (which already doubles embedded `"`) makes it round-trip -// correctly. Only applied to already-string temporal cells so numeric/plain -// columns keep exporting as-is. -export function forceCsvTextForTemporalColumns(rows: readonly (readonly T[])[], columnTypes: readonly (string | null | undefined)[]): T[][] { - if (!columnTypes.some((type) => isTemporalColumnType(type))) return rows.map((row) => [...row]); - return rows.map((row) => - row.map((value, index) => { - if (typeof value !== "string" || !isTemporalColumnType(columnTypes[index])) return value; - return `="${value}"` as T; - }), - ); -} - function formatTemporalValueForExport(value: CellValue, pattern: string): string { if (typeof value === "string") { const match = value.match(ISO_OFFSET_DATETIME_PATTERN); diff --git a/packages/app-tests/useDataGridExport.test.ts b/packages/app-tests/useDataGridExport.test.ts index 2ffab4f5a7..313e42ea34 100644 --- a/packages/app-tests/useDataGridExport.test.ts +++ b/packages/app-tests/useDataGridExport.test.ts @@ -913,7 +913,7 @@ test("selected query result CSV export keeps the existing in-memory path", async assert.equal(apiMock.exportQueryResultCsv.mock.calls[0][3], "necessary"); }); -test("selected query result CSV export formats only typed temporal columns", async () => { +test("selected query result CSV export formats typed temporal columns without a formula wrapper", async () => { useSettingsStore().updateEditorSettings({ globalDateTimeExportFormat: "YYYY/M/D HH:mm:ss" }); const rawDateTime = "2024-02-25 13:02:15"; const { composable } = buildExportHarness({ @@ -924,8 +924,8 @@ test("selected query result CSV export formats only typed temporal columns", asy await composable.exportCsv([1]); - // Temporal columns are wrapped as `="..."` so spreadsheet apps keep them as text; plain columns are untouched. - assert.deepEqual(apiMock.exportQueryResultCsv.mock.calls[0][2], [['="2024/2/25 13:02:15"', rawDateTime]]); + // Temporal columns use the configured export format; plain columns are untouched. + assert.deepEqual(apiMock.exportQueryResultCsv.mock.calls[0][2], [["2024/2/25 13:02:15", rawDateTime]]); }); test("selected query result XLSX export uses the current source label as the sheet name", async () => {