//! SpreadsheetML workbooks through the part layer. mod common; use common::{read, read_with}; use craft_xlsx::testing::{workbook, zip}; use craft_xlsx::{CellRef, CellValue, Error, FormulaKind, Limits, Package, Range, SheetKind, Value, Visibility}; /// A workbook in the strict namespaces with the `x:` prefix, as some writers save it. fn strict() -> Vec { const NS: &str = r#"xmlns:x="http://purl.oclc.org/ooxml/spreadsheetml/main" xmlns:r="http://purl.oclc.org/ooxml/officeDocument/relationships""#; const RT: &str = "http://purl.oclc.org/ooxml/officeDocument/relationships"; let book = format!( r#" One!$A$1:$B$2 orphan"# ); let rels = format!( r#""# ); let one = format!( r#" 11B1*22 1904-01-039 "# ); zip(&[ ("[Content_Types].xml", b""), ( "_rels/.rels", format!(r#""#).as_bytes(), ), ("xl/book.xml", book.as_bytes()), ("xl/_rels/book.xml.rels", rels.as_bytes()), ("xl/sheets/one.xml", one.as_bytes()), ("xl/sheets/two.xml", format!("").as_bytes()), ("xl/strings.xml", format!(r#"zeroone"#).as_bytes()), ( "xl/styles.xml", format!(r#""#) .as_bytes(), ), ]) } #[test] fn a_strict_workbook_with_prefixes() { let bytes = strict(); let mut p = Package::open(&bytes, &Limits::default()).unwrap(); assert!(!p.is_binary()); assert_eq!(p.workbook_part(), "xl/book.xml"); let wb = p.workbook().unwrap(); assert!(wb.date_1904); assert_eq!(wb.active_tab, Some(2)); let kinds: Vec<_> = wb.sheets.iter().map(|s| (s.name.as_str(), s.sheet_id, s.kind.clone(), s.part.as_deref(), s.visibility)).collect(); assert_eq!( kinds, [ ("One", 4, SheetKind::Worksheet, Some("xl/sheets/one.xml"), Visibility::Visible), ("Chart", 5, SheetKind::Chartsheet, Some("xl/charts/c.xml"), Visibility::Visible), ("Two", 6, SheetKind::Worksheet, Some("xl/sheets/two.xml"), Visibility::VeryHidden), ("Lost", 7, SheetKind::Other(String::new()), None, Visibility::Visible), ] ); let names: Vec<_> = wb.defined_names.iter().map(|d| (d.name.as_str(), d.local_sheet_id, d.formula.as_str(), d.hidden)).collect(); assert_eq!(names, [("_xlnm.Print_Area", Some(0), "One!$A$1:$B$2", false), ("Rate", None, "0.2", true)]); assert!(wb.root.child("bookViews").is_some()); let styles = p.styles().unwrap().unwrap(); assert_eq!(styles.cell_number_format(1), Some("0.000")); let strings = p.shared_strings().unwrap(); assert_eq!(strings.iter().map(|s| &*s.text).collect::>(), ["zero", "one"]); let mut cells = vec![]; let part = p .sheet(&wb.sheets[0], |row| { cells.extend(row.cells); Ok(()) }) .unwrap(); assert_eq!(part.merges, [Range::parse("A3:B4").unwrap()]); assert_eq!(part.columns.len(), 1); assert_eq!((part.columns[0].first, part.columns[0].last, part.columns[0].width), (1, 2, Some(20.5))); assert_eq!(part.root.child("pageSetup").and_then(|e| e.attr("orientation")), Some("landscape")); assert!(part.root.child("sheetData").is_some_and(|d| d.elements().next().is_none())); assert_eq!((part.rows, part.dropped, part.damaged.as_deref()), (2, 0, None)); let values: Vec<_> = cells.iter().map(|c| c.value.resolve(&strings, true)).collect(); assert_eq!(values, [Value::text("one"), Value::Number(1.0), Value::Number(2.0), Value::Number(2.0), Value::Empty]); assert_eq!(cells[1].style, 1); assert_eq!(cells[2].formula.as_ref().map(|f| (f.kind, f.text.as_str())), Some((FormulaKind::Normal, "B1*2"))); assert_eq!(cells[4].value, CellValue::Shared(9)); assert!(p.sheet(&wb.sheets[3], |_| Ok(())).is_err()); } #[test] fn a_damaged_sheet_keeps_the_rows_before_the_damage() { let bytes = workbook(&[("S", r#"12