From f4b9be524fc801d307c953d48a0fb31e13c583cf Mon Sep 17 00:00:00 2001 From: sjh9714 <163989462+sjh9714@users.noreply.github.com> Date: Wed, 14 Oct 2026 18:09:21 +0900 Subject: [PATCH 1/2] ods: preserve displayed text for cached error cells --- src/ods.rs | 8 ++++++-- tests/test.rs | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/ods.rs b/src/ods.rs index 9b0247da..6f503307 100644 --- a/src/ods.rs +++ b/src/ods.rs @@ -663,6 +663,7 @@ where RS: Read + Seek, { let mut is_string = false; + let mut is_error = false; let mut is_value_set = false; let mut val = Data::Empty; let mut formula = String::new(); @@ -691,15 +692,18 @@ where is_value_set = true; } "office:value-type" if !is_value_set => is_string = value == "string", + "calcext:value-type" => is_error = value == "error", "table:formula" => { formula = decode_attr(value)?; } _ => (), } } - if !is_value_set && is_string { + if (!is_value_set && is_string) || (is_error && matches!(&val, Data::String(s) if s.is_empty())) + { // If the value type is string and the office:string-value attribute - // is not present, the element content defines the value. + // is not present, the element content defines the value. LibreOffice + // also stores error text in the content with an empty string-value. let mut s = String::new(); let mut first_paragraph = true; loop { diff --git a/tests/test.rs b/tests/test.rs index e222e039..347ecf17 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -260,6 +260,43 @@ fn ods_covered() { ); } +#[rstest] +#[case(r#"office:string-value="" calcext:value-type="error""#, "#DIV/0!")] +#[case(r#"calcext:value-type="error" office:string-value="""#, "#DIV/0!")] +#[case(r#"office:string-value="""#, "")] +#[case(r#"office:string-value="cached" calcext:value-type="error""#, "cached")] +fn ods_cached_error_text(#[case] attributes: &str, #[case] expected: &str) { + use std::io::Write; + use zip::write::SimpleFileOptions; + + let content = format!( + r#" + + + #DIV/0! + + "# + ); + let mut zip = zip::ZipWriter::new(Cursor::new(Vec::new())); + for (name, data) in [ + ("mimetype", "application/vnd.oasis.opendocument.spreadsheet"), + ("META-INF/manifest.xml", ""), + ("content.xml", content.as_str()), + ] { + zip.start_file(name, SimpleFileOptions::default()).unwrap(); + zip.write_all(data.as_bytes()).unwrap(); + } + let mut ods = Ods::new(zip.finish().unwrap()).unwrap(); + let range = ods.worksheet_range("Sheet1").unwrap(); + assert_eq!(range.get_value((0, 0)), Some(&String(expected.to_string()))); + let formulas = ods.worksheet_formula("Sheet1").unwrap(); + assert_eq!(formulas.get_value((0, 0)).unwrap(), "of:=1/0"); +} + #[test] fn special_cells() { let mut excel: Ods<_> = wb("special_cells.ods"); From 241145eb540f651965dce8bd4f80f38cdb22de18 Mon Sep 17 00:00:00 2001 From: sjh9714 <163989462+sjh9714@users.noreply.github.com> Date: Sun, 18 Oct 2026 21:03:05 +0900 Subject: [PATCH 2/2] test: use a standalone ODS cached error fixture --- tests/cached_error_text.ods | Bin 0 -> 884 bytes tests/test.rs | 50 +++++++++++------------------------- 2 files changed, 15 insertions(+), 35 deletions(-) create mode 100644 tests/cached_error_text.ods diff --git a/tests/cached_error_text.ods b/tests/cached_error_text.ods new file mode 100644 index 0000000000000000000000000000000000000000..5881ba2e37a28ffc15197f9cdd996375500cc4a4 GIT binary patch literal 884 zcmWIWW@Zs#0E2{}*w!4&ESSbK5Czh{t|5-P zo_=onxruq1X{p5}dKI}jy#bDVhYWajf9}%!zV-lTXvv)lMwa|GqhM<6h_<5W97BZL!?zMep8oZnzQFrT``Lnlsxj^(MM(8lcy)-@Aa6AUEYx+ z8h<}0oFCP<-@bA1IRbsE&&t5S4fJhteqISEcz}Mq8ge?X*+5{=bL|)3d_0R*>G&@2I$Ck|A7Ie{EDtgkh4OLN2Em1*n zuVmZL_h0`ZX?9a-=^_o)O>&92~RIbob|YI$w*gg%Nb8M(^tyf ztr^$8ODXx~T)p~5&L%PW^*XicoD)->P8#2R<0mY3X_ z<8}0FSj9c5MHeSMkhxY+R_py!(K;_DH{eNG|BFo}9~RZS9p3f&*u%Zv>vst;Dw%yT zUU)=ks?}GqRR?d#X8CVF_v3T>#uUe8SyLW7xcVt{lg&xlgu?Sr7%pDy+8`q3%vN(- zFEG7dKR^EDCq>_5d*fqX?Bpn&m2Yx%&z+f!|DTF{*9+y^dHl~bd4Gvd4g22(8tu$H zdFH-e^dF`GZ$>5&X56V27~Ejc01SUb+C}KXo*p537#JEDLx4=IsT17@^hib+;RLiA VDar%9S=m66%s{vaNUvc6@c<=*P~ZRn literal 0 HcmV?d00001 diff --git a/tests/test.rs b/tests/test.rs index 347ecf17..b5bf08b7 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -260,41 +260,21 @@ fn ods_covered() { ); } -#[rstest] -#[case(r#"office:string-value="" calcext:value-type="error""#, "#DIV/0!")] -#[case(r#"calcext:value-type="error" office:string-value="""#, "#DIV/0!")] -#[case(r#"office:string-value="""#, "")] -#[case(r#"office:string-value="cached" calcext:value-type="error""#, "cached")] -fn ods_cached_error_text(#[case] attributes: &str, #[case] expected: &str) { - use std::io::Write; - use zip::write::SimpleFileOptions; - - let content = format!( - r#" - - - #DIV/0! - - "# - ); - let mut zip = zip::ZipWriter::new(Cursor::new(Vec::new())); - for (name, data) in [ - ("mimetype", "application/vnd.oasis.opendocument.spreadsheet"), - ("META-INF/manifest.xml", ""), - ("content.xml", content.as_str()), - ] { - zip.start_file(name, SimpleFileOptions::default()).unwrap(); - zip.write_all(data.as_bytes()).unwrap(); - } - let mut ods = Ods::new(zip.finish().unwrap()).unwrap(); - let range = ods.worksheet_range("Sheet1").unwrap(); - assert_eq!(range.get_value((0, 0)), Some(&String(expected.to_string()))); - let formulas = ods.worksheet_formula("Sheet1").unwrap(); - assert_eq!(formulas.get_value((0, 0)).unwrap(), "of:=1/0"); +#[test] +fn ods_cached_error_text() { + let mut excel: Ods<_> = wb("cached_error_text.ods"); + let range = excel.worksheet_range("Sheet1").unwrap(); + range_eq!( + range, + [ + [String("#DIV/0!".to_string())], + [String("#DIV/0!".to_string())], + [String("".to_string())], + [String("cached".to_string())], + ] + ); + let formulas = excel.worksheet_formula("Sheet1").unwrap(); + range_eq!(formulas, [["of:=1/0"]; 4]); } #[test]