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";
|
} from "@/lib/table/tableClipboard";
|
||||||
import { buildSingleDdlExportFileContent } from "@/lib/export/ddlExport";
|
import { buildSingleDdlExportFileContent } from "@/lib/export/ddlExport";
|
||||||
import { fetchTableDataForExport } from "@/lib/table/tableDataExport";
|
import { fetchTableDataForExport } from "@/lib/table/tableDataExport";
|
||||||
import { forceCsvTextForTemporalColumns } from "@/lib/dataGrid/columnFormatter";
|
|
||||||
import { useConnectionStore } from "@/stores/connectionStore";
|
import { useConnectionStore } from "@/stores/connectionStore";
|
||||||
import { treeNodePinIdentity, type PinnedTreeNodeIdentity } from "@/lib/app/pinnedItems";
|
import { treeNodePinIdentity, type PinnedTreeNodeIdentity } from "@/lib/app/pinnedItems";
|
||||||
import { useExportTracker, type ExportTask } from "@/composables/useExportTracker";
|
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),
|
executePage: (sql) => api.executeQuery(props.connection.id, props.database, sql),
|
||||||
});
|
});
|
||||||
if (format === "csv") {
|
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 {
|
} else {
|
||||||
const comments = result.columns.map((name) => columnInfos?.find((column) => column.name.toLocaleLowerCase() === name.toLocaleLowerCase())?.comment);
|
const comments = result.columns.map((name) => columnInfos?.find((column) => column.name.toLocaleLowerCase() === name.toLocaleLowerCase())?.comment);
|
||||||
const headerOverrides = buildXlsxHeaderOverrides(result.columns, comments, headerMode);
|
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());
|
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 () => {
|
it("exports only visible Mongo columns from the full result set", async () => {
|
||||||
setActivePinia(createPinia());
|
setActivePinia(createPinia());
|
||||||
const state = createMongoExportState({
|
const state = createMongoExportState({
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ import type { DatabaseType, QueryResult } from "@/types/database";
|
|||||||
import type { QueryResultExportRequest } from "@/lib/backend/api";
|
import type { QueryResultExportRequest } from "@/lib/backend/api";
|
||||||
import { usesSyntheticRowIdKey } from "@/lib/table/tableEditing";
|
import { usesSyntheticRowIdKey } from "@/lib/table/tableEditing";
|
||||||
import { buildXlsxSqlWorksheet } from "@/lib/export/xlsxSqlSheet";
|
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 { translateBackendError } from "@/i18n/backend-errors";
|
||||||
import XlsxHeaderDialog from "@/components/export/XlsxHeaderDialog.vue";
|
import XlsxHeaderDialog from "@/components/export/XlsxHeaderDialog.vue";
|
||||||
import i18n from "@/i18n";
|
import i18n from "@/i18n";
|
||||||
@@ -885,13 +885,7 @@ export function useDataGridExport(options: UseDataGridExportOptions) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
// Hand the raw rows straight to the Rust command. Formatting (NULL→"",
|
// Let the Rust command format and write the raw values off the UI thread.
|
||||||
// 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);
|
|
||||||
if (needsFullExport && exportProgressState) {
|
if (needsFullExport && exportProgressState) {
|
||||||
exportProgressState.value = {
|
exportProgressState.value = {
|
||||||
...exportProgressState.value,
|
...exportProgressState.value,
|
||||||
@@ -913,7 +907,7 @@ export function useDataGridExport(options: UseDataGridExportOptions) {
|
|||||||
}
|
}
|
||||||
outputPath = path as string;
|
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) {
|
if (needsFullExport && exportProgressState) {
|
||||||
exportProgressState.value = {
|
exportProgressState.value = {
|
||||||
...exportProgressState.value,
|
...exportProgressState.value,
|
||||||
@@ -952,8 +946,7 @@ export function useDataGridExport(options: UseDataGridExportOptions) {
|
|||||||
outputPath = path as string;
|
outputPath = path as string;
|
||||||
}
|
}
|
||||||
const result = await resultToExport(undefined, undefined, false);
|
const result = await resultToExport(undefined, undefined, false);
|
||||||
const rows = forceCsvTextForTemporalColumns(result.rows, result.columnTypes);
|
await api.exportQueryResultCsv(outputPath, result.columns, result.rows, useSettingsStore().editorSettings.csvQuoteMode, csvNullLiteralForMode(useSettingsStore().editorSettings.csvNullMode));
|
||||||
await api.exportQueryResultCsv(outputPath, result.columns, rows, useSettingsStore().editorSettings.csvQuoteMode, csvNullLiteralForMode(useSettingsStore().editorSettings.csvNullMode));
|
|
||||||
toast(t("grid.exported"));
|
toast(t("grid.exported"));
|
||||||
} catch (e: any) {
|
} catch (e: any) {
|
||||||
toast(t("grid.exportFailed", { message: translateBackendError(t, e) }), 5000);
|
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 { translateBackendError } from "@/i18n/backend-errors";
|
||||||
import { sidebarStructureExportTargets, sidebarTableDataExportTargets } from "@/lib/sidebar/sidebarExportRuntime";
|
import { sidebarStructureExportTargets, sidebarTableDataExportTargets } from "@/lib/sidebar/sidebarExportRuntime";
|
||||||
import { fetchTableDataForExport } from "@/lib/table/tableDataExport";
|
import { fetchTableDataForExport } from "@/lib/table/tableDataExport";
|
||||||
import { forceCsvTextForTemporalColumns } from "@/lib/dataGrid/columnFormatter";
|
|
||||||
import XlsxHeaderDialog from "@/components/export/XlsxHeaderDialog.vue";
|
import XlsxHeaderDialog from "@/components/export/XlsxHeaderDialog.vue";
|
||||||
import { buildXlsxHeaderOverrides, hasXlsxHeaderComments, type XlsxExportOptions, type XlsxHeaderMode } from "@/lib/export/xlsxHeader";
|
import { buildXlsxHeaderOverrides, hasXlsxHeaderComments, type XlsxExportOptions, type XlsxHeaderMode } from "@/lib/export/xlsxHeader";
|
||||||
import {
|
import {
|
||||||
@@ -476,7 +475,7 @@ export function useSidebarTreeExportRuntime(options: SidebarTreeExportRuntimeOpt
|
|||||||
executePage: (sql) => api.executeQuery(connectionId, database, sql),
|
executePage: (sql) => api.executeQuery(connectionId, database, sql),
|
||||||
});
|
});
|
||||||
if (format === "csv") {
|
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 {
|
} else {
|
||||||
const comments = result.columns.map((name) => exportColumnInfos?.find((column) => column.name.toLocaleLowerCase() === name.toLocaleLowerCase())?.comment);
|
const comments = result.columns.map((name) => exportColumnInfos?.find((column) => column.name.toLocaleLowerCase() === name.toLocaleLowerCase())?.comment);
|
||||||
const headerOverrides = buildXlsxHeaderOverrides(result.columns, comments, headerMode);
|
const headerOverrides = buildXlsxHeaderOverrides(result.columns, comments, headerMode);
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { describe, expect, it } from "vitest";
|
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", () => {
|
describe("columnFormatterKeys", () => {
|
||||||
// MySQL query metadata mirrors the database into the schema slot, while the
|
// 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();
|
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 {
|
function formatTemporalValueForExport(value: CellValue, pattern: string): string {
|
||||||
if (typeof value === "string") {
|
if (typeof value === "string") {
|
||||||
const match = value.match(ISO_OFFSET_DATETIME_PATTERN);
|
const match = value.match(ISO_OFFSET_DATETIME_PATTERN);
|
||||||
|
|||||||
+3
-3
@@ -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");
|
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" });
|
useSettingsStore().updateEditorSettings({ globalDateTimeExportFormat: "YYYY/M/D HH:mm:ss" });
|
||||||
const rawDateTime = "2024-02-25 13:02:15";
|
const rawDateTime = "2024-02-25 13:02:15";
|
||||||
const { composable } = buildExportHarness({
|
const { composable } = buildExportHarness({
|
||||||
@@ -924,8 +924,8 @@ test("selected query result CSV export formats only typed temporal columns", asy
|
|||||||
|
|
||||||
await composable.exportCsv([1]);
|
await composable.exportCsv([1]);
|
||||||
|
|
||||||
// Temporal columns are wrapped as `="..."` so spreadsheet apps keep them as text; plain columns are untouched.
|
// 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]]);
|
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 () => {
|
test("selected query result XLSX export uses the current source label as the sheet name", async () => {
|
||||||
|
|||||||
Reference in new issue
Block a user