mirror of
https://github.com/storytold/craft-libs.git
synced 2026-12-01 10:48:15 +01:00
* craft-xlsx: typed external links, read from .xlsx and .xlsb; external references in binary formulas ExternalLink (another workbook as ExternalBook: its target, sheet names, defined names with refersTo and sheetId, and cached cells; a DDE or OLE link as Other) reads from externalLinkN.xml and externalLinkN.bin alike. Package::external_links gives one entry per Workbook::external_references, which is now a typed field, read from externalReferences or the binary workbook's BrtSupBookSrc records and written back from the field. A binary formula's reference to another workbook decodes as .xlsx spells it: [1]Data!$A$1, '[1]Other Sheet'!A1, '[1]A:C'!A1, [1]!ExtName, [1]Data!Local for a sheet-scoped name, a DDE item [2]!'R1C1'. The link parts are read when the workbook part is; one that can't be read, a deleted sheet of it or a name it lacks keeps the cell's saved result. Closes #1252. * craft-xlsx: review fixes: external links keep their numbers, are read once, and deleted sheets keep values - An externalReference without an r:id, or a BrtSupBookSrc whose id can't be read, keeps its place as an empty id, so the links after it keep their [n]; it is written back without r:id. - A binary workbook's external link parts are read once, with its workbook part: the formulas' index and Package::external_links share that read, so cached cells are charged to binary_cells once. - PtgNameX through a deleted sheet of another workbook (-1) keeps the cell's saved result instead of falling back to the name's own scope. * craft-xlsx: deduplicate binary external links against budget and add cache-cap test * craft-xlsx: avoid cloning ExternalLink on duplicate binary external references --------- Co-authored-by: kiwi4boy <kiwi4boy@gmail.com> Co-authored-by: FredApps <FredApps@users.noreply.github.com> Co-authored-by: Dylan M. Taylor <dylan@dylanmtaylor.com>
105 lines
4.5 KiB
Rust
105 lines
4.5 KiB
Rust
//! External links (`xl/externalLinks/externalLinkN.xml`, ECMA-376 Part 1, 18.14): the other
|
|
//! workbooks a workbook's formulas refer to as `[1]Sheet1!A1`, numbered in the order of the
|
|
//! workbook's `externalReferences` ([`crate::Workbook::external_references`]). A binary workbook's
|
|
//! `externalLinkN.bin` reads into the same types.
|
|
|
|
use craft_opc::Rels;
|
|
use craft_sheet::{CellError, CellRef, Value};
|
|
use craft_xml::El;
|
|
|
|
/// One external link part.
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
pub enum ExternalLink {
|
|
/// `externalBook`: another workbook.
|
|
Book(ExternalBook),
|
|
/// `ddeLink` or `oleLink`: a DDE or OLE source, not read.
|
|
Other,
|
|
}
|
|
|
|
/// `externalBook`: another workbook, what its formulas use of it, and the values last read from it.
|
|
#[derive(Clone, Debug, Default, PartialEq)]
|
|
pub struct ExternalBook {
|
|
/// The other workbook as its relationship's target is written (`Src%20One.xlsx`,
|
|
/// `sub/three.xlsx`, a URL); `None` without one.
|
|
pub target: Option<String>,
|
|
/// `sheetNames`: the other workbook's sheets, which `sheet_id`s index.
|
|
pub sheet_names: Vec<String>,
|
|
pub defined_names: Vec<ExternalName>,
|
|
/// `sheetDataSet`: the cached cells, one entry per cached sheet.
|
|
pub sheet_data: Vec<ExternalSheetData>,
|
|
}
|
|
|
|
/// `definedName` in an external book: a name in the other workbook that formulas here use.
|
|
#[derive(Clone, Debug, Default, PartialEq, Eq)]
|
|
pub struct ExternalName {
|
|
pub name: String,
|
|
/// `refersTo` as written (`='Data'!$A$1:$A$3`, `#REF!`); `None` when the other workbook didn't
|
|
/// define it.
|
|
pub refers_to: Option<String>,
|
|
/// `sheetId`: the sheet it is scoped to, an index into [`ExternalBook::sheet_names`].
|
|
pub sheet_id: Option<u32>,
|
|
}
|
|
|
|
/// `sheetData` in an external book: the cached cells of one sheet.
|
|
#[derive(Clone, Debug, Default, PartialEq)]
|
|
pub struct ExternalSheetData {
|
|
/// An index into [`ExternalBook::sheet_names`].
|
|
pub sheet_id: u32,
|
|
/// `refreshError`: the last update of this sheet's cells failed.
|
|
pub refresh_error: bool,
|
|
/// The cells, in file order: `Value::Empty` for a cell with no value.
|
|
pub cells: Vec<(CellRef, Value)>,
|
|
}
|
|
|
|
impl ExternalLink {
|
|
/// An external link part read with [`crate::ns::CANONICAL`], with its relationships (for the
|
|
/// target).
|
|
pub fn read(root: &El, rels: &Rels) -> ExternalLink {
|
|
let Some(book) = root.child("externalBook") else { return ExternalLink::Other };
|
|
let target = book.attr_or_plain("r:id").and_then(|id| rels.by_id(id)).map(|r| r.target_raw.clone());
|
|
let sheet_names = book
|
|
.child("sheetNames")
|
|
.map(|s| s.children("sheetName").map(|n| n.attr("val").unwrap_or_default().to_string()).collect())
|
|
.unwrap_or_default();
|
|
let defined_names = book
|
|
.child("definedNames")
|
|
.map(|d| {
|
|
d.children("definedName")
|
|
.map(|n| ExternalName {
|
|
name: n.attr("name").unwrap_or_default().to_string(),
|
|
refers_to: n.attr("refersTo").map(str::to_string),
|
|
sheet_id: n.attr_u32("sheetId"),
|
|
})
|
|
.collect()
|
|
})
|
|
.unwrap_or_default();
|
|
let sheet_data = book
|
|
.child("sheetDataSet")
|
|
.map(|set| {
|
|
set.children("sheetData")
|
|
.map(|d| ExternalSheetData {
|
|
sheet_id: d.attr_u32("sheetId").unwrap_or(0),
|
|
refresh_error: d.flag("refreshError", false),
|
|
cells: d.children("row").flat_map(|r| r.children("cell")).filter_map(cell).collect(),
|
|
})
|
|
.collect()
|
|
})
|
|
.unwrap_or_default();
|
|
ExternalLink::Book(ExternalBook { target, sheet_names, defined_names, sheet_data })
|
|
}
|
|
}
|
|
|
|
/// A cached `cell`: its value by `t` (a number by default; `str`, `b`, `e`). One without a
|
|
/// readable reference is skipped.
|
|
fn cell(c: &El) -> Option<(CellRef, Value)> {
|
|
let at = CellRef::parse(c.attr("r")?)?;
|
|
let Some(v) = c.child("v").map(|v| v.text().into_owned()) else { return Some((at, Value::Empty)) };
|
|
let value = match c.attr("t") {
|
|
Some("str" | "s" | "inlineStr") => Value::Text(v.into()),
|
|
Some("b") => Value::Bool(v.trim() == "1" || v.trim().eq_ignore_ascii_case("true")),
|
|
Some("e") => CellError::parse(v.trim()).map_or(Value::Text(v.into()), Value::Error),
|
|
_ => v.trim().parse::<f64>().map_or(Value::Text(v.into()), Value::number),
|
|
};
|
|
Some((at, value))
|
|
}
|