Skip to content

World's first open-source enterprise world model.

A number in a spreadsheet is read with the 15 significant digits Excel keeps, not the binary tail of a computed value (#1032)

Maya Kid468ef0f94139
diff --git a/crates/utopia-ingest/src/parsers.rs b/crates/utopia-ingest/src/parsers.rs
index 7e017740..e3fbc828 100644
--- a/crates/utopia-ingest/src/parsers.rs
+++ b/crates/utopia-ingest/src/parsers.rs
@@ -756,6 +756,7 @@ pub fn spreadsheet(bytes: &[u8]) -> anyhow::Result<String> {
let text = match c {
Data::Empty => String::new(),
Data::DateTime(d) => excel_date(d),
+ Data::Float(f) => excel_number(*f),
other => other.to_string(),
};
(text, 1, 0)
@@ -824,6 +825,22 @@ fn excel_date(d: &calamine::ExcelDateTime) -> String {
}
}
+/// 数字格按 Excel 认的精度写:15 位有效数字。
+///
+/// Excel 比较、显示一个数都只看 15 位有效数字,公式的缓存值却按双精度的 17 位写进文件:
+/// `=0.1+0.2` 存成 0.30000000000000004,`=1.1*1.1-1` 存成 0.21000000000000019,Excel 里
+/// 看到的是 0.3 和 0.21。原样写出来,正文、引文和抽出来的值都带着这截二进制的尾巴。先舍到
+/// 15 位有效数字,再写最短的十进制;本来就干净的数(19.9、1200、45306)一个字不变
+fn excel_number(value: f64) -> String {
+ if !value.is_finite() {
+ return value.to_string();
+ }
+ format!("{value:.14e}")
+ .parse::<f64>()
+ .unwrap_or(value)
+ .to_string()
+}
+
/// Decode before conversion so legacy HTML encodings remain supported.
pub fn html(bytes: &[u8]) -> anyhow::Result<String> {
Ok(crate::html::page_to_markdown(&plain_text(bytes), None)?)
diff --git a/crates/utopia-ingest/tests/spreadsheet_numbers.rs b/crates/utopia-ingest/tests/spreadsheet_numbers.rs
new file mode 100644
index 00000000..8419beae
--- /dev/null
+++ b/crates/utopia-ingest/tests/spreadsheet_numbers.rs
@@ -0,0 +1,66 @@
+//! 电子表格里的数按 Excel 认的精度读:15 位有效数字。
+//!
+//! 公式的缓存值是按双精度的 17 位写进文件的:`=0.1+0.2` 存成 `<v>0.30000000000000004</v>`,
+//! Excel 里看到的却是 0.3。这里照 Excel 写出来的样子拼一份 xlsx,值都是缓存值的写法,
+//! 数字格式是常规(没有 `s`)。
+
+use std::io::{Cursor, Write};
+
+fn read(values: &[(&str, &str)]) -> String {
+ let rows: String = values
+ .iter()
+ .enumerate()
+ .map(|(i, (label, value))| {
+ let r = i + 2;
+ format!(
+ r#"<row r="{r}"><c r="A{r}" t="inlineStr"><is><t>{label}</t></is></c><c r="B{r}"><v>{value}</v></c></row>"#
+ )
+ })
+ .collect();
+ let mut zip = zip::ZipWriter::new(Cursor::new(Vec::new()));
+ for (part, xml) in [
+ ("[Content_Types].xml", r#"<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"><Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/><Default Extension="xml" ContentType="application/xml"/><Override PartName="/xl/workbook.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml"/><Override PartName="/xl/worksheets/sheet1.xml" ContentType="application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml"/></Types>"#.to_string()),
+ ("_rels/.rels", r#"<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/></Relationships>"#.to_string()),
+ ("xl/workbook.xml", r#"<workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><sheets><sheet name="计算" sheetId="1" r:id="rId1"/></sheets></workbook>"#.to_string()),
+ ("xl/_rels/workbook.xml.rels", r#"<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet1.xml"/></Relationships>"#.to_string()),
+ ("xl/worksheets/sheet1.xml", format!(r#"<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><sheetData><row r="1"><c r="A1" t="inlineStr"><is><t>项目</t></is></c><c r="B1" t="inlineStr"><is><t>金额</t></is></c></row>{rows}</sheetData></worksheet>"#)),
+ ] {
+ zip.start_file(part, zip::write::SimpleFileOptions::default()).unwrap();
+ zip.write_all(xml.as_bytes()).unwrap();
+ }
+ utopia_ingest::parse("计算.xlsx", &zip.finish().unwrap().into_inner())
+ .unwrap()
+ .text
+}
+
+#[test]
+fn a_computed_number_reads_with_the_digits_excel_keeps() {
+ let text = read(&[
+ ("合计", "0.30000000000000004"),
+ ("增长率", "0.21000000000000019"),
+ ("差额", "-0.30000000000000004"),
+ ]);
+ for row in ["| 合计 | 0.3 |", "| 增长率 | 0.21 |", "| 差额 | -0.3 |"] {
+ assert!(text.contains(row), "{row}: {text}");
+ }
+}
+
+#[test]
+fn a_number_that_was_already_clean_reads_as_before() {
+ let text = read(&[
+ ("含税金额", "1395.0527999999999"),
+ ("单价", "19.899999999999999"),
+ ("数量", "1200"),
+ ("编号", "123456789012345000"),
+ ("比例", "0.125"),
+ ]);
+ for row in [
+ "| 含税金额 | 1395.0528 |",
+ "| 单价 | 19.9 |",
+ "| 数量 | 1200 |",
+ "| 编号 | 123456789012345000 |",
+ "| 比例 | 0.125 |",
+ ] {
+ assert!(text.contains(row), "{row}: {text}");
+ }
+}