25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。
TypeScript aicliclickhousedatabasedatabase-clientdatabase-managementdockerguimcpmongodbmysqlpostgresqlredisrustsql-serversqlitetaurivue
fix(import): preserve workbook selection when an Excel sheet is empty (#12738)
ericbyz87f1432fc4c3
diff --git a/apps/desktop/src/components/import/TableImportDialog.vue b/apps/desktop/src/components/import/TableImportDialog.vueindex 4e521daaef..a00cd06585 100644--- a/apps/desktop/src/components/import/TableImportDialog.vue+++ b/apps/desktop/src/components/import/TableImportDialog.vue@@ -257,11 +257,11 @@ const batchTargetNamesValid = computed(() => {});const canImport = computed(() => {if (running.value || !props.prefillConnectionId || !existingTargetMetadataReady.value || conflictPolicyError.value) return false;- if (!isBatchImport.value) return !!preview.value && !!targetTableName.value && mappingValidation.value.valid;+ if (!isBatchImport.value) return !!preview.value?.rows.length && !!targetTableName.value && mappingValidation.value.valid;if (!batchTargetNamesValid.value) return false;return selectedBatchTasks.value.every((task) => {const mappings = task.preview.columns.map((sourceColumn) => ({ sourceColumn, targetColumn: task.columnMapping[sourceColumn] ?? "", targetDataType: task.columnDataTypes[sourceColumn] ?? "" })).filter((mapping) => mapping.targetColumn);- return !!task.tableName.trim() && validateImportMappings(mappings).valid;+ return task.preview.rows.length > 0 && !!task.tableName.trim() && validateImportMappings(mappings).valid;});});const canGoBack = computed(() => wizardStep.value !== "source" && wizardStep.value !== "execution" && !running.value);@@ -271,7 +271,7 @@ const canGoNext = computed(() => {if (wizardStep.value === "options") return batchTargetNamesValid.value;if (wizardStep.value === "mapping") return canImport.value;}- if (wizardStep.value === "options") return !!preview.value && !!targetTableName.value && existingTargetMetadataReady.value;+ if (wizardStep.value === "options") return !!preview.value?.rows.length && !!targetTableName.value && existingTargetMetadataReady.value;if (wizardStep.value === "mapping") return existingTargetMetadataReady.value && mappingValidation.value.valid && !conflictPolicyError.value;return false;});@@ -883,7 +883,7 @@ async function prepareBatchSources(sources: ImportSource[]) {const tableBase = sheetName ? `${suggestedTableName(sourceName(source))}_${sheetName}` : sourceName(source);tasks.push({id: uuid(),- selected: true,+ selected: taskPreview.rows.length > 0,source,format,sheetName: effectiveSheetName,@@ -898,7 +898,8 @@ async function prepareBatchSources(sources: ImportSource[]) {}batchTasks.value = tasks;if (tasks.length) {- activateBatchTask(0);+ const firstSelectedIndex = tasks.findIndex((task) => task.selected);+ activateBatchTask(Math.max(0, firstSelectedIndex));wizardStep.value = "options";}} catch (e: any) {@@ -1421,7 +1422,7 @@ watch(rawProgressPercent, (percent) => {</div><div class="flex max-h-32 flex-wrap gap-1.5 overflow-auto p-2"><div v-for="(task, index) in batchTasks" :key="task.id" class="flex max-w-[260px] items-center gap-1.5 rounded-md border pl-2 text-xs transition-colors" :class="index === activeTaskIndex ? 'border-primary bg-primary/10 text-primary' : 'hover:bg-muted/60'">- <input v-model="task.selected" type="checkbox" class="h-3.5 w-3.5 shrink-0 accent-primary" :aria-label="t('tableImport.selectTask', { name: task.tableName })" :disabled="running" />+ <input v-model="task.selected" type="checkbox" class="h-3.5 w-3.5 shrink-0 accent-primary" :aria-label="t('tableImport.selectTask', { name: task.tableName })" :disabled="running || !task.preview.rows.length" /><buttontype="button"class="flex min-w-0 items-center gap-1.5 py-1 pr-2"@@ -1437,6 +1438,7 @@ watch(rawProgressPercent, (percent) => {<FileSpreadsheet v-else-if="task.format === 'excel'" class="h-3.5 w-3.5 shrink-0" /><FileText v-else class="h-3.5 w-3.5 shrink-0" /><span class="truncate">{{ task.tableName }}</span>+ <span v-if="!task.preview.rows.length" class="shrink-0 text-muted-foreground">{{ t("tableImport.rows") }}: 0</span></button></div></div>diff --git a/apps/desktop/src/components/import/__tests__/TableImportDialog.batchSelection.spec.ts b/apps/desktop/src/components/import/__tests__/TableImportDialog.batchSelection.spec.tsindex 5aedfdf3b1..e8caed42af 100644--- a/apps/desktop/src/components/import/__tests__/TableImportDialog.batchSelection.spec.ts+++ b/apps/desktop/src/components/import/__tests__/TableImportDialog.batchSelection.spec.ts@@ -292,6 +292,29 @@ afterEach(() => {});describe("TableImportDialog batch selection", () => {+ it.each([false, true])("blocks import when every sheet is empty (single sheet: %s)", async (singleSheet) => {+ mocks.previewTableImportFile.mockResolvedValue({ ...workbookPreview("First"), sheets: singleSheet ? ["First"] : sheets, columns: ["id"], rows: [], totalRows: 0 });+ await mountDialog();+ expect(button("Next").disabled).toBe(true);+ expect(mocks.importTableFile).not.toHaveBeenCalled();+ });++ it.each(["First", "Second"])("keeps data sheets importable when %s is empty", async (emptySheet) => {+ mocks.previewTableImportFile.mockImplementation((_source, options) => {+ const name = options.parseOptions.sheetName || "First";+ const preview = workbookPreview(name);+ if (name === emptySheet) Object.assign(preview, { columns: [], rows: [], totalRows: 0, totalRowsExact: true });+ return Promise.resolve(preview);+ });+ await mountDialog();++ expect(taskCheckbox(`rows_${emptySheet}`).checked).toBe(false);+ expect(taskCheckbox(`rows_${emptySheet}`).disabled).toBe(true);+ await startImport();+ expect(mocks.importTableFile).toHaveBeenCalledTimes(2);+ expect(mocks.importTableFile.mock.calls.every(([request]) => request.preparedSource!.totalRows > 0)).toBe(true);+ });+it("reloads a single CSV preview and passes the selected decimal separator to import", async () => {mocks.previewTableImportFile.mockImplementation(() => Promise.resolve(delimitedPreview()));await mountDialog([new File(['amount,note\n"12,50",plain'], "rows.csv")]);diff --git a/crates/dbx-core/src/data/table_import.rs b/crates/dbx-core/src/data/table_import.rsindex 35d30fb86a..3f70020f85 100644--- a/crates/dbx-core/src/data/table_import.rs+++ b/crates/dbx-core/src/data/table_import.rs@@ -3519,6 +3519,11 @@ fn parse_xlsx_preview_file_with_options(let preview_last_row = first_row.saturating_add(preview_limit.saturating_sub(1));row_range.last_data_row.map_or(preview_last_row, |last| last.min(preview_last_row))};+ // The preview window is anchored at the first data row that actually carries content.+ // Until one is found we keep scanning (up to the selected row range) instead of stopping+ // at the provisional window, so a sheet whose only data sits beyond preview_limit is not+ // misjudged as empty (see review on PR #12738).+ let mut first_content_row: Option<usize> = None;let mut requested_last_row = preview_end_from(row_range.data_start_row);let mut dimension = None;@@ -3553,7 +3558,7 @@ fn parse_xlsx_preview_file_with_options(.unwrap_or_else(|| current_row.saturating_add(1).max(1));current_column = 0;if row_range.last_data_row.is_some_and(|last| current_row > last)- || (observed_min_row != usize::MAX+ || (first_content_row.is_some()&& current_row > requested_last_row.max(row_range.title_row.unwrap_or_default())){break;@@ -3571,7 +3576,6 @@ fn parse_xlsx_preview_file_with_options(current_column = position.1;if current_row >= min_import_row {observed_min_row = observed_min_row.min(position.0);- requested_last_row = preview_end_from(row_range.data_start_row.max(observed_min_row));observed_min_column = observed_min_column.min(position.1);observed_max_row = observed_max_row.max(position.0);observed_max_column = observed_max_column.max(position.1);@@ -3627,10 +3631,16 @@ fn parse_xlsx_preview_file_with_options(if let Some((row, column)) = current_position.take() {if row >= min_import_row {observed_min_row = observed_min_row.min(row);- requested_last_row = preview_end_from(row_range.data_start_row.max(observed_min_row));observed_min_column = observed_min_column.min(column);observed_max_column = observed_max_column.max(column);observed_max_row = observed_max_row.max(row);+ if first_content_row.is_none()+ && row >= row_range.data_start_row+ && current_cell.has_content()+ {+ first_content_row = Some(row);+ requested_last_row = preview_end_from(row);+ }if row == row_range.title_row.unwrap_or_default()|| (row >= row_range.data_start_row && row <= requested_last_row){@@ -3656,14 +3666,20 @@ fn parse_xlsx_preview_file_with_options(.collect::<HashSet<_>>();let shared_strings = read_xlsx_shared_strings(&mut zip, &needed_shared_strings)?;if observed_min_row == usize::MAX || observed_min_column == usize::MAX {- return Err("Import file has no data rows in the selected row range".to_string());+ return Ok((+ ParsedImportFile { columns: Vec::new(), rows: Vec::new(), total_rows: 0, effective_encoding: None },+ sheets,+ ));}let start_row = observed_min_row;let start_column = observed_min_column;let observed_end_column = observed_max_column.max(start_column);let observed_column_count = observed_end_column.saturating_sub(start_column).saturating_add(1);+ // Count only the anchored preview window: when the first content row sits far below+ // data_start_row, the row span up to requested_last_row is mostly skipped blank rows+ // that are never stored in raw_cells, so counting it would falsely trip the cell limit.let preview_row_count = requested_last_row- .saturating_sub(row_range.data_start_row.max(start_row))+ .saturating_sub(first_content_row.unwrap_or(row_range.data_start_row).max(start_row)).saturating_add(1).saturating_add(usize::from(row_range.title_row.is_some()));if observed_column_count.saturating_mul(preview_row_count) > MAX_FAST_PREVIEW_CELLS {@@ -3707,7 +3723,7 @@ fn parse_xlsx_preview_file_with_options(let first_preview_row = row_range.data_start_row.max(start_row);let last_preview_row = requested_last_row.min(observed_max_row);if last_preview_row < first_preview_row {- return Err("Import file has no data rows in the selected row range".to_string());+ return Ok((ParsedImportFile { columns, rows: Vec::new(), total_rows: 0, effective_encoding: None }, sheets));}// Rows made of style-only cells (blank rows inserted by a spreadsheet app) carry no// data and are skipped here as well so the preview matches what the import writes (#8604).@@ -3728,9 +3744,6 @@ fn parse_xlsx_preview_file_with_options(.collect::<Vec<_>>()}).collect::<Vec<_>>();- if rows.is_empty() {- return Err("Import file has no data rows in the selected row range".to_string());- }Ok((ParsedImportFile { columns, total_rows: rows.len(), rows, effective_encoding: None }, sheets))}@@ -4672,7 +4685,8 @@ async fn parse_import_preview_file_with_options(tokio::task::spawn_blocking(move || parse_xlsx_preview_file_with_options(&path, &options, preview_limit)).await.map_err(|e| e.to_string())??;- return Ok((parsed, false, sheets));+ let total_rows_exact = parsed.total_rows == 0;+ return Ok((parsed, total_rows_exact, sheets));}let parsed = parse_import_file_with_options(path, Some(format), options, preview_limit).await?;@@ -9116,7 +9130,10 @@ mod tests {assert_eq!(preview.rows, parsed.rows);assert_eq!(parsed.total_rows, 2);let limited = TableImportParseOptions { last_data_row: Some(99), ..options };- assert!(parse_xlsx_preview_file_with_options(&path.to_string_lossy(), &limited, 1).is_err());+ let (empty_preview, sheets) =+ parse_xlsx_preview_file_with_options(&path.to_string_lossy(), &limited, 1).unwrap();+ assert!(empty_preview.rows.is_empty());+ assert!(!sheets.is_empty());assert!(parse_xlsx_file_with_options(&path.to_string_lossy(), &limited, 1).is_err());let _ = std::fs::remove_file(path);}diff --git a/crates/dbx-core/src/data/transfer.rs b/crates/dbx-core/src/data/transfer.rsindex f48a688f32..e7619e1b9d 100644--- a/crates/dbx-core/src/data/transfer.rs+++ b/crates/dbx-core/src/data/transfer.rs@@ -2290,11 +2290,6 @@ fn transfer_column_names_match(/// column exists. Reusing the catalog's declared name keeps the statement on a/// column that really exists; an exact match still wins so case-sensitive/// targets that do have the source-cased column keep addressing it.-#[cfg(test)]-fn resolve_transfer_target_column_names(col_names: &[String], target_columns: &[db::ColumnInfo]) -> Vec<String> {- resolve_transfer_target_column_names_or(col_names, target_columns, col_names)-}-/// Maps source names to target names; a name the target does not declare/// falls back to the matching entry of `fallbacks` (the requested target column case).fn resolve_transfer_target_column_names_or(@@ -17051,7 +17046,7 @@ CREATE TABLE "Other"."prefix""Source"."NAME" ("ID" INT);"#;resolve_transfer_target_column_names_or(&names, &target, &upper),vec!["ID".to_string(), "Name".to_string(), "EXTRA".to_string()]);- assert_eq!(resolve_transfer_target_column_names(&names, &target)[2], "extra");+ assert_eq!(resolve_transfer_target_column_names_or(&names, &target, &names)[2], "extra");}#[test]@@ -17433,7 +17428,7 @@ CREATE TABLE "Other"."prefix""Source"."NAME" ("ID" INT);"#;let col_names = vec!["id".to_string(), "name".to_string()];assert_eq!(- resolve_transfer_target_column_names(&col_names, &target_columns),+ resolve_transfer_target_column_names_or(&col_names, &target_columns, &col_names),vec!["ID".to_string(), "NAME".to_string()]);}@@ -17446,7 +17441,7 @@ CREATE TABLE "Other"."prefix""Source"."NAME" ("ID" INT);"#;let col_names = vec!["id".to_string(), "ID".to_string()];assert_eq!(- resolve_transfer_target_column_names(&col_names, &target_columns),+ resolve_transfer_target_column_names_or(&col_names, &target_columns, &col_names),vec!["id".to_string(), "ID".to_string()]);}@@ -17457,7 +17452,7 @@ CREATE TABLE "Other"."prefix""Source"."NAME" ("ID" INT);"#;let col_names = vec!["id".to_string(), "missing".to_string()];assert_eq!(- resolve_transfer_target_column_names(&col_names, &target_columns),+ resolve_transfer_target_column_names_or(&col_names, &target_columns, &col_names),vec!["ID".to_string(), "missing".to_string()]);}diff --git a/crates/dbx-core/tests/xlsx_empty_sheet_preview.rs b/crates/dbx-core/tests/xlsx_empty_sheet_preview.rsnew file mode 100644index 0000000000..4a93dcd79f--- /dev/null+++ b/crates/dbx-core/tests/xlsx_empty_sheet_preview.rs@@ -0,0 +1,168 @@+use std::path::{Path, PathBuf};++use dbx_core::table_import::{+ parse_xlsx_file_with_options, preview_table_import_file_with_request, TableImportParseOptions,+ TableImportPreviewRequest,+};+use dbx_formats::xlsx_export::{build_xlsx_workbook_multi, XlsxWorksheetData};+use serde_json::{json, Value};++fn worksheet(name: &str, columns: &[&str], rows: Vec<Vec<Value>>) -> XlsxWorksheetData {+ XlsxWorksheetData {+ sheet_name: Some(name.to_string()),+ columns: columns.iter().map(|column| column.to_string()).collect(),+ column_types: vec![],+ column_comments: vec![],+ rows,+ numeric_column_right_align: false,+ auto_filter: None,+ header_comment_rows: false,+ }+}++fn workbook_file() -> (tempfile::TempDir, PathBuf) {+ let directory = tempfile::tempdir().unwrap();+ let path = directory.path().join("sheets.xlsx");+ let bytes = build_xlsx_workbook_multi(&[+ worksheet("EmptyFirst", &[], vec![]),+ worksheet("Data", &["id"], vec![vec![json!(7)]]),+ worksheet("HeaderOnly", &["id"], vec![]),+ worksheet("EmptyLast", &[], vec![]),+ ])+ .unwrap();+ std::fs::write(&path, bytes).unwrap();+ (directory, path)+}++fn request(path: &Path, options: TableImportParseOptions) -> TableImportPreviewRequest {+ TableImportPreviewRequest {+ file_path: path.to_string_lossy().into_owned(),+ connection_id: None,+ database: None,+ source_ref: None,+ source_format: None,+ parse_options: options,+ preview_limit: Some(10),+ }+}++#[tokio::test]+async fn empty_excel_preview_preserves_sheet_choices_without_allowing_empty_imports() {+ let (_directory, path) = workbook_file();+ let expected_sheets = ["EmptyFirst", "Data", "HeaderOnly", "EmptyLast"];+ for sheet_name in [None, Some("EmptyFirst"), Some("HeaderOnly"), Some("EmptyLast")] {+ let options = TableImportParseOptions {+ sheet_name: sheet_name.map(str::to_string),+ ..TableImportParseOptions::default()+ };+ let preview = preview_table_import_file_with_request(request(&path, options.clone())).await.unwrap();+ assert_eq!(preview.sheets, expected_sheets);+ assert!(preview.rows.is_empty());+ assert_eq!(preview.total_rows, 0);+ assert!(preview.total_rows_exact);+ assert!(parse_xlsx_file_with_options(&path.to_string_lossy(), &options, 10).is_err());+ }++ let options =+ TableImportParseOptions { sheet_name: Some("Data".to_string()), ..TableImportParseOptions::default() };+ let preview = preview_table_import_file_with_request(request(&path, options.clone())).await.unwrap();+ assert_eq!(preview.sheets, expected_sheets);+ assert_eq!(preview.columns, ["id"]);+ assert_eq!(preview.rows, [vec![json!(7)]]);+ assert_eq!(parse_xlsx_file_with_options(&path.to_string_lossy(), &options, 10).unwrap().rows, preview.rows);+}++#[tokio::test]+async fn empty_excel_row_range_keeps_the_workbook_selectable() {+ let (_directory, path) = workbook_file();+ let options = TableImportParseOptions {+ sheet_name: Some("Data".to_string()),+ data_start_row: Some(20),+ ..TableImportParseOptions::default()+ };+ let preview = preview_table_import_file_with_request(request(&path, options.clone())).await.unwrap();+ assert_eq!(preview.sheets, ["EmptyFirst", "Data", "HeaderOnly", "EmptyLast"]);+ assert!(preview.rows.is_empty());+ assert!(preview.total_rows_exact);+ assert!(parse_xlsx_file_with_options(&path.to_string_lossy(), &options, 10).is_err());+}++/// Replace one ZIP entry's content, keeping every other entry byte-for-byte.+fn rewrite_zip_entry(bytes: &[u8], entry: &str, replacement: &str) -> Vec<u8> {+ use std::io::{Cursor, Read, Write};+ let mut archive = zip::ZipArchive::new(Cursor::new(bytes)).unwrap();+ let mut writer = zip::ZipWriter::new(Cursor::new(Vec::new()));+ for index in 0..archive.len() {+ let mut file = archive.by_index(index).unwrap();+ writer.start_file(file.name().to_string(), zip::write::SimpleFileOptions::default()).unwrap();+ if file.name() == entry {+ writer.write_all(replacement.as_bytes()).unwrap();+ } else {+ let mut content = String::new();+ file.read_to_string(&mut content).unwrap();+ writer.write_all(content.as_bytes()).unwrap();+ }+ }+ writer.finish().unwrap().into_inner()+}++// Regression for the review on PR #12738: a sheet whose only data row sits beyond the+// preview limit (id header in A1, first value in A100) must not be reported as an empty+// preview, and the other data sheets in the same workbook must stay importable.+#[tokio::test]+async fn sparse_excel_data_beyond_preview_limit_is_not_treated_as_empty() {+ let directory = tempfile::tempdir().unwrap();+ let path = directory.path().join("sparse.xlsx");+ let built = build_xlsx_workbook_multi(&[+ worksheet("Sparse", &["id"], vec![vec![json!(7)]]),+ worksheet("Normal", &["id"], vec![vec![json!(1)]]),+ ])+ .unwrap();+ let sparse_sheet_xml = r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>+<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><sheetData>+<row r="1"><c r="A1" t="inlineStr"><is><t>id</t></is></c></row>+<row r="100"><c r="A100"><v>7</v></c></row>+</sheetData></worksheet>"#;+ std::fs::write(&path, rewrite_zip_entry(&built, "xl/worksheets/sheet1.xml", sparse_sheet_xml)).unwrap();++ let options =+ TableImportParseOptions { sheet_name: Some("Sparse".to_string()), ..TableImportParseOptions::default() };+ let preview = preview_table_import_file_with_request(request(&path, options.clone())).await.unwrap();+ assert_eq!(preview.columns, ["id"]);+ assert_eq!(preview.rows, [vec![json!(7)]]);+ assert_eq!(preview.total_rows, 1);+ assert!(!preview.total_rows_exact);+ assert_eq!(parse_xlsx_file_with_options(&path.to_string_lossy(), &options, 10).unwrap().rows, preview.rows);++ let options =+ TableImportParseOptions { sheet_name: Some("Normal".to_string()), ..TableImportParseOptions::default() };+ let preview = preview_table_import_file_with_request(request(&path, options.clone())).await.unwrap();+ assert_eq!(preview.rows, [vec![json!(1)]]);+ assert!(preview.total_rows > 0);+ assert_eq!(parse_xlsx_file_with_options(&path.to_string_lossy(), &options, 10).unwrap().rows, preview.rows);+}++// The preview grid-size guard counts only the anchored window: with the first data row at+// A200000 and a preview limit of 500, the blank-row span must not count as 200k preview+// cells and falsely fail with "preview grid is too large".+#[tokio::test]+async fn far_sparse_excel_preview_does_not_trip_the_grid_size_guard() {+ let directory = tempfile::tempdir().unwrap();+ let path = directory.path().join("far-sparse.xlsx");+ let built = build_xlsx_workbook_multi(&[worksheet("Sparse", &["id"], vec![vec![json!(7)]])]).unwrap();+ let sparse_sheet_xml = r#"<?xml version="1.0" encoding="UTF-8" standalone="yes"?>+<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><sheetData>+<row r="1"><c r="A1" t="inlineStr"><is><t>id</t></is></c></row>+<row r="200000"><c r="A200000"><v>7</v></c></row>+</sheetData></worksheet>"#;+ std::fs::write(&path, rewrite_zip_entry(&built, "xl/worksheets/sheet1.xml", sparse_sheet_xml)).unwrap();++ let options =+ TableImportParseOptions { sheet_name: Some("Sparse".to_string()), ..TableImportParseOptions::default() };+ let mut req = request(&path, options.clone());+ req.preview_limit = Some(500);+ let preview = preview_table_import_file_with_request(req).await.unwrap();+ assert_eq!(preview.columns, ["id"]);+ assert_eq!(preview.rows, [vec![json!(7)]]);+ assert_eq!(parse_xlsx_file_with_options(&path.to_string_lossy(), &options, 500).unwrap().rows, preview.rows);+}