CSV export values: choose the date or time shape from the format section that formats the value

This commit is contained in:
Harrison Booth committed 2026-10-26 12:51:42 -05:00
1 parent 9595fc8ee7
commit f4f48f3b23
3 files changed
+6 -5

No files matched your search

+1 -1
View File
@@ -301,7 +301,7 @@ pub fn csv_values_bytes(wb: &Workbook) -> Result<Vec<u8>> {
let Some(sh) = wb.sheet(wb.active_sheet()) else { return Ok(Vec::new()) };
gridcraft_xlsx::write_csv(sh, wb.csv_delimiter.unwrap_or(b','), |c| match sh.value(c) {
gridcraft_core::Value::Number(n) if n.is_finite() => {
let shape = crate::display::number_format(wb.styles.get(sh.style_id(c)).num_fmt.as_str()).date_shape();
let shape = crate::display::number_format(wb.styles.get(sh.style_id(c)).num_fmt.as_str()).date_shape(n);
match shape.zip(gridcraft_core::date::datetime_from_serial(wb.date_system, n)) {
Some(((date, time), d)) => iso(&d, date, time),
None => n.to_string(),
+2 -1
View File
@@ -2479,6 +2479,7 @@ fn export_csv_values_writes_raw_values_and_the_default_stays_displayed_text() {
("J1", "1.5", "h:mm"),
("K1", "46023", "yyyy-mm-dd hh:mm"),
("L1", "1.5", "[h]:mm"),
("M1", "2.5", "[>=3]yyyy-mm-dd;[<3]hh:mm"),
] {
s.execute("cell.set", json!({"cell": cell, "input": input})).unwrap();
s.execute("home.numberFormat", json!({"range": cell, "code": code})).unwrap();
@@ -2501,7 +2502,7 @@ fn export_csv_values_writes_raw_values_and_the_default_stays_displayed_text() {
assert!(default.starts_with("\"$1,234.50\",0.12,1-Jan-26,"), "{default}");
assert_eq!(
std::fs::read_to_string(&raw).unwrap(),
"1234.5,0.123456789,2026-01-01,2026-01-01T12:00:00,18:00:00,TRUE,\"a,b\",#DIV/0!,00:00:00,12:00:00,2026-01-01T00:00:00,1.5\r\n"
"1234.5,0.123456789,2026-01-01,2026-01-01T12:00:00,18:00:00,TRUE,\"a,b\",#DIV/0!,00:00:00,12:00:00,2026-01-01T00:00:00,1.5,12:00:00\r\n"
);
std::fs::remove_dir_all(dir).unwrap();
}
+3 -3
View File
@@ -193,11 +193,11 @@ impl NumberFormat {
self.num_sections().iter().any(|s| s.kind == SecKind::Date)
}
/// For a date or time format, whether its first section shows calendar parts (year, month,
/// For a date or time format, whether the section that formats `value` shows calendar parts (year, month,
/// day) and whether it shows time parts, as `(date, time)`. `None` for any other format and for
/// elapsed-time codes like `[h]:mm`, whose value is a duration, not a moment.
pub fn date_shape(&self) -> Option<(bool, bool)> {
let s = self.num_sections().first().filter(|s| s.kind == SecKind::Date)?;
pub fn date_shape(&self, value: f64) -> Option<(bool, bool)> {
let s = self.select(value).map(|(s, _)| s).filter(|s| s.kind == SecKind::Date)?;
let parts = || s.toks.iter().filter_map(|t| if let Tok::Date(d) = t { Some(*d) } else { None });
if parts().any(|d| matches!(d, DatePart::ElapsedH(_) | DatePart::ElapsedM(_) | DatePart::ElapsedS(_))) {
return None;