fix(export): preserve datetime precision across CSV and XLSX exports

This commit is contained in:
Melody Ruch authored and GitHub committed 2026-09-05 09:22:08 +08:00
1 parent ffe1e58fbb
commit ab6a7645f6
14 files changed
+438 -43

No files matched your search

@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
import { applyColumnFormatter, columnFormatterKeys, defaultIoTDBTimestampFormatter, formatIoTDBTimestampEditorValue, normalizeColumnFormatter, normalizeSupportedDateTimePattern, parseIoTDBTimestampEditorValue } from "@/lib/dataGrid/columnFormatter";
import { applyColumnFormatter, columnFormatterKeys, defaultIoTDBTimestampFormatter, forceCsvTextForTemporalColumns, formatIoTDBTimestampEditorValue, normalizeColumnFormatter, normalizeSupportedDateTimePattern, parseIoTDBTimestampEditorValue } from "@/lib/dataGrid/columnFormatter";
describe("columnFormatterKeys", () => {
// MySQL query metadata mirrors the database into the schema slot, while the
@@ -199,3 +199,25 @@ 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]);
});
});
+6
View File
@@ -2637,6 +2637,11 @@ export async function exportQueryResultXlsx(
rows: readonly (readonly XlsxCellValue[])[],
numericColumnRightAlign?: boolean,
autoFilter?: boolean,
// Accepted for signature parity with the Tauri backend but unused: the web
// builder writes every cell as `inlineStr`, so a temporal value keeps the
// text the grid already formatted (milliseconds included) and never goes
// through a `numFmt` display pattern.
_dateTimeFormat?: string,
): Promise<void> {
const { buildXlsxWorkbook } = await import("@/lib/export/xlsxExport");
const workbook = buildXlsxWorkbook({
@@ -2672,6 +2677,7 @@ export async function exportQueryResultsXlsx(
autoFilter?: boolean;
}[],
autoFilter?: boolean,
_dateTimeFormat?: string,
): Promise<void> {
const { buildXlsxWorkbookMulti } = await import("@/lib/export/xlsxExport");
const workbook = buildXlsxWorkbookMulti(autoFilter === undefined ? worksheets : worksheets.map((worksheet) => ({ ...worksheet, autoFilter })));
+4
View File
@@ -5056,6 +5056,7 @@ export async function exportQueryResultXlsx(
rows: readonly (readonly XlsxCellValue[])[],
numericColumnRightAlign?: boolean,
autoFilter?: boolean,
dateTimeFormat?: string,
): Promise<void> {
return invoke("export_query_result_xlsx", {
request: {
@@ -5067,6 +5068,7 @@ export async function exportQueryResultXlsx(
rows,
numericColumnRightAlign,
autoFilter,
dateTimeFormat,
},
});
}
@@ -5083,12 +5085,14 @@ export async function exportQueryResultsXlsx(
autoFilter?: boolean;
}[],
autoFilter?: boolean,
dateTimeFormat?: string,
): Promise<void> {
return invoke("export_query_results_xlsx", {
request: {
filePath,
worksheets,
autoFilter,
dateTimeFormat,
},
});
}
@@ -452,6 +452,24 @@ 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);