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
@@ -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