mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-16 23:42:27 +00:00
craft-xlsx: binary formulas name macro sheets and span chart sheets
An Excel probe showed two references the decoder refused that .xlsx spells:
- A reference to an XLM macro sheet (Macro1!$A$1): macro sheets have cells,
so the sheet links now name them.
- A 3-D span with a chart sheet between its ends ('Data:After Sheet'!$A$1):
a span is spelled by its ends, as .xlsx has it. An end with no cells is
still refused.
This commit is contained in:
1 parent
abceed3f17
commit
5742279729
3 files changed
+36
-10
No files matched your search
@@ -351,9 +351,10 @@ pub(crate) fn workbook(
|
||||
if let SheetKind::Other(_) = kind {
|
||||
return Err(invalid("unknown sheet relationship type"));
|
||||
}
|
||||
// Only a worksheet has cells a formula can refer to.
|
||||
// Only worksheets and macro sheets have cells a formula can refer to (a macro's
|
||||
// name refers to its macro sheet: `Macro1!$A$1`).
|
||||
if let Some(l) = links.as_mut() {
|
||||
l.sheets.push((kind == SheetKind::Worksheet).then(|| name.clone()));
|
||||
l.sheets.push(matches!(kind, SheetKind::Worksheet | SheetKind::Macrosheet).then(|| name.clone()));
|
||||
}
|
||||
wb.sheets.push(SheetEntry { name, sheet_id, visibility, rel_id, kind, part: Some(rel.target.clone()) });
|
||||
}
|
||||
|
||||
@@ -147,8 +147,8 @@ pub(super) struct Name {
|
||||
/// Workbook-level tables formulas refer to.
|
||||
#[derive(Default)]
|
||||
pub(crate) struct Links {
|
||||
/// Every sheet's name in `BrtBundleSh` order, `None` for sheets the reader skips or that hold
|
||||
/// no cells (chart, dialog and macro sheets).
|
||||
/// Every sheet's name in `BrtBundleSh` order, `None` for the sheets that hold no cells (chart,
|
||||
/// dialog and module sheets).
|
||||
pub(super) sheets: Vec<Option<String>>,
|
||||
/// Every `BrtName`, in order: `PtgName` indexes them from 1.
|
||||
pub(super) names: Vec<Name>,
|
||||
@@ -179,14 +179,11 @@ impl Links {
|
||||
let (Ok(first), Ok(last)) = (usize::try_from(x.first), usize::try_from(x.last)) else {
|
||||
return Err(Unsupported("reference to a missing sheet"));
|
||||
};
|
||||
// A span takes every sheet between its ends, so a skipped one anywhere in it changes
|
||||
// what the reference means.
|
||||
// A span is spelled by its ends. A sheet between them with no cells (a chart sheet) adds
|
||||
// nothing to it, and `.xlsx` spells the span the same; an end with no cells can't be named.
|
||||
let span = self.sheets.get(first..=last).filter(|s| !s.is_empty()).ok_or(Unsupported("reference to a missing sheet"))?;
|
||||
if span.iter().any(Option::is_none) {
|
||||
return Err(Unsupported("references to sheets the reader skips"));
|
||||
}
|
||||
let (Some(Some(a)), Some(Some(b))) = (span.first(), span.last()) else {
|
||||
return Err(Unsupported("reference to a missing sheet"));
|
||||
return Err(Unsupported("references to sheets with no cells"));
|
||||
};
|
||||
Ok(Some(if first == last { SheetSel::Named(a.clone()) } else { SheetSel::Span(a.clone(), b.clone()) }))
|
||||
}
|
||||
|
||||
@@ -1007,3 +1007,31 @@ fn typed_spacing_numbers_and_iso_functions_read_as_in_xlsx() {
|
||||
];
|
||||
assert_eq!(got, want.map(Some));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn references_to_macro_sheets_and_spans_over_chart_sheets_are_named() {
|
||||
// Sheets Data, a chart sheet, an XLM macro sheet, Hidden; sheet links Macro1, Data:Hidden
|
||||
// (over the chart sheet) and Chart1 (no cells).
|
||||
let mut book = record(131, &[]);
|
||||
book.extend(record(153, &[[0u8; 8].as_slice(), &wide("")].concat()));
|
||||
book.extend(record(143, &[]));
|
||||
for (id, rel, name) in [(1u32, "one", "Data"), (3, "c", "Chart1"), (4, "m", "Macro1"), (2, "two", "Hidden")] {
|
||||
book.extend(record(156, &[&0u32.to_le_bytes()[..], &id.to_le_bytes(), &wide(rel), &wide(name)].concat()));
|
||||
}
|
||||
book.extend(record(144, &[]));
|
||||
let mut xti = 3u32.to_le_bytes().to_vec();
|
||||
for (first, last) in [(2i32, 2i32), (0, 3), (1, 1)] {
|
||||
xti.extend([0u32.to_le_bytes(), first.to_le_bytes(), last.to_le_bytes()].concat());
|
||||
}
|
||||
book.extend([record(353, &[]), record(357, &[]), record(362, &xti), record(354, &[]), record(132, &[])].concat());
|
||||
let rels = RELS.replace(
|
||||
"</Relationships>",
|
||||
r#"<Relationship Id="c" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/chartsheet" Target="../data/c.bin"/><Relationship Id="m" Type="http://schemas.microsoft.com/office/2006/relationships/xlMacrosheet" Target="../data/m.bin"/></Relationships>"#,
|
||||
);
|
||||
let ref3d = |ixti: u16| [&[0x5Au8][..], &ixti.to_le_bytes(), &loc(0, 0)].concat();
|
||||
let data = [row(0), formula_cell(1, &ref3d(0), &[]), formula_cell(2, &ref3d(1), &[]), formula_cell(3, &ref3d(2), &[])].concat();
|
||||
let wb = read(&package(&book, &worksheet(&data, &[]), &sst(), ROOT, &rels, TYPES)).unwrap();
|
||||
let got: Vec<_> = (1..=3).map(|c| wb.sheets[0].formula(CellRef::new(0, c))).collect();
|
||||
assert_eq!(got, [Some("Macro1!$A$1"), Some("Data:Hidden!$A$1"), None]);
|
||||
assert!(warned(&wb, "references to sheets with no cells"), "{:?}", wb.warnings);
|
||||
}
|
||||
Reference in new issue
Block a user