diff --git a/crates/dbx-core/src/xlsx_export.rs b/crates/dbx-core/src/xlsx_export.rs index bed1a13f6a..82ce88131a 100644 --- a/crates/dbx-core/src/xlsx_export.rs +++ b/crates/dbx-core/src/xlsx_export.rs @@ -10,6 +10,33 @@ const XLSX_DATE_STYLE: usize = 2; const XLSX_DATETIME_STYLE: usize = 3; const NUMERIC_RIGHT_ALIGN_STYLE: usize = 4; const NUMERIC_LEFT_ALIGN_STYLE: usize = 5; +// Excel renders numeric cells with the General format in the shortest form, so +// database scale like "0.3500000" displays as "0.35" even though the full value +// is stored. Pre-registered number formats (one per fractional-digit count up +// to MAX_NUMERIC_SCALE_FORMAT) keep the declared decimals visible (#8225). +// Styles are appended after the six fixed entries, two per scale (right/left +// alignment), so the indices stay stable for the streaming writer. +const MAX_NUMERIC_SCALE_FORMAT: usize = 15; +const NUMERIC_SCALE_STYLE_BASE: usize = 6; +const SCALE_NUMFMT_ID_BASE: usize = 166; + +fn numeric_scale_style(scale: usize, right_align: bool) -> Option { + if scale == 0 || scale > MAX_NUMERIC_SCALE_FORMAT { + return None; + } + Some(NUMERIC_SCALE_STYLE_BASE + (scale - 1) * 2 + usize::from(!right_align)) +} + +/// Fractional digit count of a plain (non-exponent) decimal string. Values in +/// exponent notation return 0: a fixed "0.00"-style format would round tiny +/// magnitudes to zero, so they keep the General format's scientific display. +fn decimal_fraction_digits(value: &str) -> usize { + let trimmed = value.trim(); + if trimmed.contains(['e', 'E']) { + return 0; + } + trimmed.split_once('.').map_or(0, |(_, fraction)| fraction.chars().filter(|ch| ch.is_ascii_digit()).count()) +} #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] @@ -767,10 +794,15 @@ fn push_typed_cell_xml( if is_numeric_column_type(column_type) { if let Some(Value::String(value)) = value { if let Some(number) = safe_excel_number(value) { + // Excel's General format drops database scale ("0.3500000" + // renders as "0.35"), so numeric cells carrying a fractional + // part switch to the matching pre-registered scale format. + let scale_style = + numeric_scale_style(decimal_fraction_digits(number), style == Some(NUMERIC_RIGHT_ALIGN_STYLE)); output.push_str("{number}").expect("writing XLSX numeric cells into a String cannot fail"); return; } @@ -979,21 +1011,43 @@ fn styles_xml(date_time_format: Option<&str>) -> String { } }) .unwrap_or((default_date, default_datetime)); + // One fixed "0.0…0" format per fractional-digit count plus right/left xfs + // per format, appended after the six fixed styles (see numeric_scale_style). + let mut scale_numfmts = String::new(); + let mut scale_xfs = String::new(); + for scale in 1..=MAX_NUMERIC_SCALE_FORMAT { + let format_code = format!("0.{}", "0".repeat(scale)); + scale_numfmts.push_str(&format!( + "", + SCALE_NUMFMT_ID_BASE + scale - 1, + format_code + )); + for (alignment, _) in [("right", true), ("left", false)] { + scale_xfs.push_str(&format!( + "", + SCALE_NUMFMT_ID_BASE + scale - 1 + )); + } + } format!( concat!( "", "", - "", + "{}", "", "", "", "", - "", + "{}", "", "" ), + 2 + MAX_NUMERIC_SCALE_FORMAT, escape_xml(&date_format), - escape_xml(&datetime_format) + escape_xml(&datetime_format), + scale_numfmts, + 6 + MAX_NUMERIC_SCALE_FORMAT * 2, + scale_xfs ) } @@ -1272,11 +1326,68 @@ mod tests { .expect("build workbook"); let sheet = read_zip_entry(&workbook, "xl/worksheets/sheet1.xml"); - assert!(sheet.contains("1.00000")); - assert!(sheet.contains("2800.000000")); + assert!(sheet.contains("1.00000")); + assert!(sheet.contains("2800.000000")); assert!(sheet.contains("00123")); } + #[test] + fn applies_scale_preserving_number_formats_to_fractional_numeric_cells() { + let workbook = build_xlsx_workbook(&XlsxWorksheetData { + sheet_name: Some("Scale formats".to_string()), + columns: vec!["qty".to_string(), "cost".to_string(), "exp".to_string()], + column_types: vec!["numeric(18,4)".to_string(), "decimal(10,7)".to_string(), "double".to_string()], + column_comments: vec![], + rows: vec![vec![json!("5.0000"), json!("0.3500000"), json!("1.23E-5")]], + numeric_column_right_align: true, + }) + .expect("build workbook"); + + let styles = read_zip_entry(&workbook, "xl/styles.xml"); + assert!(styles.contains("numFmtId=\"169\" formatCode=\"0.0000\"")); + assert!(styles.contains("numFmtId=\"172\" formatCode=\"0.0000000\"")); + assert!(styles.contains("")); + + let sheet = read_zip_entry(&workbook, "xl/worksheets/sheet1.xml"); + assert!(sheet.contains("5.0000")); + assert!(sheet.contains("0.3500000")); + // Exponent-notation values keep the General format's scientific display. + assert!(sheet.contains("1.23E-5")); + } + + #[test] + fn keeps_integral_and_left_aligned_numeric_cells_on_plain_styles() { + let workbook = build_xlsx_workbook(&XlsxWorksheetData { + sheet_name: Some("Left align".to_string()), + columns: vec!["qty".to_string(), "count".to_string()], + column_types: vec!["numeric(18,4)".to_string(), "int".to_string()], + column_comments: vec![], + rows: vec![vec![json!("5.0000"), json!("7")]], + numeric_column_right_align: false, + }) + .expect("build workbook"); + + let sheet = read_zip_entry(&workbook, "xl/worksheets/sheet1.xml"); + assert!(sheet.contains("5.0000")); + assert!(sheet.contains("7")); + } + + #[test] + fn caps_scale_formats_beyond_fifteen_decimals() { + let workbook = build_xlsx_workbook(&XlsxWorksheetData { + sheet_name: Some("Deep scale".to_string()), + columns: vec!["value".to_string()], + column_types: vec!["numeric(38,20)".to_string()], + column_comments: vec![], + rows: vec![vec![json!("0.00000000000000000001")]], + numeric_column_right_align: true, + }) + .expect("build workbook"); + + let sheet = read_zip_entry(&workbook, "xl/worksheets/sheet1.xml"); + assert!(sheet.contains("0.00000000000000000001")); + } + #[test] fn ignores_fractional_trailing_zeros_when_checking_excel_numeric_precision() { let workbook = build_xlsx_workbook(&XlsxWorksheetData { @@ -1304,9 +1415,9 @@ mod tests { .expect("build workbook"); let sheet = read_zip_entry(&workbook, "xl/worksheets/sheet1.xml"); - assert!(sheet.contains("-100000.0000000000")); - assert!(sheet.contains("-999999999999999.0000")); - assert!(sheet.contains("123456789012345.0000000000")); + assert!(sheet.contains("-100000.0000000000")); + assert!(sheet.contains("-999999999999999.0000")); + assert!(sheet.contains("123456789012345.0000000000")); assert!(sheet.contains("1234567890123456.0000")); assert!(sheet.contains("100000.0000000001")); assert!(sheet.contains("not-a-number")); @@ -1351,7 +1462,7 @@ mod tests { assert!(sheet.contains("45347.543229166666")); assert!(sheet.contains("2024-02-25")); assert!(sheet.contains("not-a-date")); - assert!(sheet.contains("2800.000000")); + assert!(sheet.contains("2800.000000")); assert!(sheet.contains("2024-02-25T13:02:15+08:00")); assert!(styles.contains("numFmtId=\"164\" formatCode=\"yyyy-mm-dd\"")); assert!(styles.contains("numFmtId=\"165\" formatCode=\"yyyy-mm-dd hh:mm:ss\"")); @@ -1425,17 +1536,17 @@ mod tests { .expect("build workbook"); let sheet = read_zip_entry(&workbook, "xl/worksheets/sheet1.xml"); - for (reference, value) in [ - ("A2", "2"), - ("B2", "42"), - ("C2", "-7"), - ("D2", "4000000000"), - ("E2", "123456789012345"), - ("F2", "123.5"), - ("G2", "987654.321"), - ("H2", "2800.000000"), + for (reference, value, style) in [ + ("A2", "2", "5"), + ("B2", "42", "5"), + ("C2", "-7", "5"), + ("D2", "4000000000", "5"), + ("E2", "123456789012345", "5"), + ("F2", "123.5", "7"), + ("G2", "987654.321", "11"), + ("H2", "2800.000000", "17"), ] { - assert!(sheet.contains(&format!("{value}")), "sheet={sheet}"); + assert!(sheet.contains(&format!("{value}")), "sheet={sheet}"); } } @@ -1542,8 +1653,8 @@ mod tests { let bytes = fs::read(&path).expect("read workbook"); let sheet = read_zip_entry(&bytes, "xl/worksheets/sheet1.xml"); assert!(sheet.contains("42")); - assert!(sheet.contains("123.5")); - assert!(sheet.contains("2800.000000")); + assert!(sheet.contains("123.5")); + assert!(sheet.contains("2800.000000")); let _ = fs::remove_file(&path); }