fix(csv): stop wrapping exported dates in equals-prefixed formulas
This commit is contained in:
1 parent
6f05afc0e6
commit
ce5217bd23
7 files changed
+25
-59
No files matched your search
@@ -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);
|
||||
|
||||
@@ -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({
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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]);
|
||||
});
|
||||
});
|
||||
@@ -452,24 +452,6 @@ export function formatTemporalRowsForExport<T extends CellValue>(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<T extends CellValue>(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);
|
||||
|
||||
Reference in new issue
Block a user