feat(export): support name and comment XLSX headers

This commit is contained in:
二丫讲梵 authored and GitHub committed 2026-08-23 01:40:22 +08:00
1 parent 0908a944e8
commit c4630540dd
15 files changed
+171 -63

No files matched your search

@@ -3,20 +3,21 @@ import { ref } from "vue";
import { useI18n } from "vue-i18n";
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter } from "@/components/ui/dialog";
import { Button } from "@/components/ui/button";
import type { XlsxHeaderMode } from "@/lib/export/xlsxHeader";
const { t } = useI18n();
const open = defineModel<boolean>("open", { default: false });
const selected = ref<"original" | "comment">("original");
const selected = ref<XlsxHeaderMode>("name");
const emit = defineEmits<{
confirm: [useCommentHeader: boolean];
confirm: [mode: XlsxHeaderMode];
cancel: [];
}>();
function onConfirm() {
open.value = false;
emit("confirm", selected.value === "comment");
emit("confirm", selected.value);
}
function onCancel() {
@@ -39,18 +40,22 @@ function onOpenChange(value: boolean) {
<p class="text-sm text-muted-foreground mb-3">{{ t("grid.xlsxHeaderPrompt") }}</p>
<div class="flex flex-col gap-3">
<label class="flex items-center gap-2 cursor-pointer">
<input type="radio" v-model="selected" value="original" class="h-4 w-4" />
<input type="radio" v-model="selected" value="name" class="h-4 w-4" />
<span class="text-sm">{{ t("grid.xlsxHeaderOriginal") }}</span>
</label>
<label class="flex items-center gap-2 cursor-pointer">
<input type="radio" v-model="selected" value="comment" class="h-4 w-4" />
<span class="text-sm">{{ t("grid.xlsxHeaderComment") }}</span>
</label>
<label class="flex items-center gap-2 cursor-pointer">
<input type="radio" v-model="selected" value="name-comment" class="h-4 w-4" />
<span class="text-sm">{{ t("grid.xlsxHeaderNameAndComment") }}</span>
</label>
</div>
</div>
<DialogFooter>
<Button variant="outline" @click="onCancel">{{ t("common.cancel") }}</Button>
<Button @click="onConfirm">{{ t("common.confirm") }}</Button>
<Button data-xlsx-header-confirm @click="onConfirm">{{ t("common.confirm") }}</Button>
</DialogFooter>
</DialogContent>
</Dialog>
@@ -30,4 +30,27 @@ describe("XlsxHeaderDialog", () => {
expect(cancelCount).toBe(1);
app.unmount();
});
it("emits the combined name and comment mode", async () => {
const container = document.createElement("div");
document.body.appendChild(container);
let selectedMode = "";
const app = createApp(XlsxHeaderDialog, {
open: true,
onConfirm: (mode: string) => {
selectedMode = mode;
},
});
app.use(i18n);
app.mount(container);
await nextTick();
document.querySelector<HTMLInputElement>('input[value="name-comment"]')!.click();
await nextTick();
document.querySelector<HTMLButtonElement>("[data-xlsx-header-confirm]")!.click();
await nextTick();
expect(selectedMode).toBe("name-comment");
app.unmount();
});
});
@@ -2576,13 +2576,8 @@ const allColumnTypes = computed(() =>
const visibleColumnTypes = computed(() => visibleColumnIndexes.value.map((index) => allColumnTypes.value[index]));
const allColumnTypeVisualKinds = computed(() => allColumnTypes.value.map((type) => resolveDataGridTypeVisualKind(type, resolvedDatabaseType.value)));
const visibleColumnTypeVisualKinds = computed(() => visibleColumnIndexes.value.map((index) => allColumnTypeVisualKinds.value[index] ?? "unknown"));
const visibleColumnComments = computed(() =>
visibleColumnIndexes.value.map((index) => {
const ordinalComments = props.resultColumnComments;
if (ordinalComments) return ordinalComments[index];
return dataGridColumnCommentFor(columnCommentMap.value, props.result.columns[index] ?? "", props.sourceColumns?.[index]);
}),
);
const allColumnComments = computed(() => props.result.columns.map((column, index) => resolvedColumnComment(column, index)));
const visibleColumnComments = computed(() => visibleColumnIndexes.value.map((index) => allColumnComments.value[index]));
const visibleColumnCount = computed(() => visibleColumnIndexes.value.length);
const numericColumnRightAlign = computed(() => (settingsStore.editorSettings.numericColumnRightAlign ?? true) && !showTranspose.value);
@@ -7615,6 +7610,8 @@ const {
database: computed(() => props.executionDatabase ?? props.database),
context: computed(() => props.context),
sourceColumns: visibleSourceColumns,
columnComments: visibleColumnComments,
allColumnComments,
mongoDocuments: computed(() => props.result.mongo_copy_documents ?? props.result.mongo_documents),
columnTypes: visibleColumnTypes,
allColumnTypes,
@@ -84,6 +84,7 @@ import { buildExecutableObjectSourceStatements, buildRoutineRenameObjectSourceSt
import { buildRenameObjectSql, supportsObjectRename } from "@/lib/table/objectRenameSql";
import { isTauriRuntime } from "@/lib/backend/tauriRuntime";
import { generateDatabaseExportId } from "@/lib/export/databaseExport";
import { buildXlsxHeaderOverrides, hasXlsxHeaderComments, type XlsxHeaderMode } from "@/lib/export/xlsxHeader";
import { copyToClipboard, eventTargetAllowsAppClipboardShortcut } from "@/lib/common/clipboard";
import { defaultPasteTableMode, pasteTableModeCopiesData, supportsWholeRowTableDataCopy, tableClipboardMatchesTarget, tableClipboardMenuState, tableClipboardSourceContext, tableDataCopyColumnOptions, type PasteTableMode, type TableClipboardContext } from "@/lib/table/tableClipboard";
import { buildSingleDdlExportFileContent } from "@/lib/export/ddlExport";
@@ -1923,16 +1924,16 @@ async function exportData(row: ObjectBrowserRow, format: "csv" | "json" | "sql")
else await exportTableData(row, format);
}
function showObjectBrowserXlsxHeaderDialog(hasComments: boolean): Promise<boolean | null> {
if (!hasComments) return Promise.resolve(false);
function showObjectBrowserXlsxHeaderDialog(hasComments: boolean): Promise<XlsxHeaderMode | null> {
if (!hasComments) return Promise.resolve("name");
return new Promise((resolve) => {
const container = document.createElement("div");
document.body.appendChild(container);
const app = createApp(XlsxHeaderDialog, {
open: true,
onConfirm: (useCommentHeader: boolean) => {
resolve(useCommentHeader);
onConfirm: (mode: XlsxHeaderMode) => {
resolve(mode);
app.unmount();
document.body.removeChild(container);
},
@@ -1949,24 +1950,24 @@ function showObjectBrowserXlsxHeaderDialog(hasComments: boolean): Promise<boolea
async function exportDataXlsx(row: ObjectBrowserRow) {
const schema = row.schema || selectedSchema.value;
let useCommentHeader = false;
let headerMode: XlsxHeaderMode = "name";
let columnInfos: ColumnInfo[] | undefined;
try {
columnInfos = await api.getColumns(props.connection.id, props.database, schema || props.database, row.name, props.catalog);
const hasComments = columnInfos.some((col) => col.comment && col.comment.trim().length > 0);
const hasComments = hasXlsxHeaderComments(columnInfos.map((column) => column.comment));
const result = await showObjectBrowserXlsxHeaderDialog(hasComments);
if (result === null) return;
useCommentHeader = result;
headerMode = result;
} catch {
// Column fetch failed, fallback to export without comments
columnInfos = undefined;
}
await exportTableData(row, "xlsx", columnInfos, useCommentHeader);
await exportTableData(row, "xlsx", columnInfos, headerMode);
}
async function exportTableData(row: ObjectBrowserRow, format: "csv" | "xlsx" | "sql", columnInfos?: ColumnInfo[], useCommentHeader = false) {
async function exportTableData(row: ObjectBrowserRow, format: "csv" | "xlsx" | "sql", columnInfos?: ColumnInfo[], headerMode: XlsxHeaderMode = "name") {
const schema = row.schema || selectedSchema.value;
// Save dialog first
@@ -2004,8 +2005,9 @@ async function exportTableData(row: ObjectBrowserRow, format: "csv" | "xlsx" | "
if (format === "csv") {
await api.exportQueryResultCsv(filePath, result.columns, result.rows);
} else {
const comments = useCommentHeader ? result.columns.map((name) => columnInfos?.find((column) => column.name.toLocaleLowerCase() === name.toLocaleLowerCase())?.comment ?? null) : undefined;
await api.exportQueryResultXlsx(filePath, row.name, result.columns, result.column_types ?? result.columns.map(() => ""), comments, result.rows);
const comments = result.columns.map((name) => columnInfos?.find((column) => column.name.toLocaleLowerCase() === name.toLocaleLowerCase())?.comment);
const headerOverrides = buildXlsxHeaderOverrides(result.columns, comments, headerMode);
await api.exportQueryResultXlsx(filePath, row.name, result.columns, result.column_types ?? result.columns.map(() => ""), headerOverrides, result.rows);
}
toast(t("grid.exported"));
return;
@@ -2015,8 +2017,12 @@ async function exportTableData(row: ObjectBrowserRow, format: "csv" | "xlsx" | "
if (columnInfos) {
columns = columnInfos.map((c) => c.name);
if (format === "xlsx" && useCommentHeader) {
columnComments = columnInfos.map((c) => c.comment ?? null);
if (format === "xlsx") {
columnComments = buildXlsxHeaderOverrides(
columns,
columnInfos.map((column) => column.comment),
headerMode,
);
}
} else if (props.connection.db_type === "neo4j") {
const infos = await api.getColumns(props.connection.id, props.database, schema || props.database, row.name, props.catalog);