Retrieve a dictionary of all named ranges in a spreadsheet

Named ranges are stored in the workbook part under <definedNames/>. Each <definedName/> maps a name to a reference or formula expression.

Workbook markup

<definedNames>
  <definedName name="SalesRange">Summary!$B$2:$B$10</definedName>
</definedNames>

Names can be workbook-scoped or sheet-scoped. Sheet-scoped names include a localSheetId attribute.

Rust workflow

Open the workbook part and inspect xl/workbook.xml:

#![allow(unused)]
fn main() {
pub fn get_defined_names(
  path: &Path,
) -> Result<BTreeMap<String, String>, Box<dyn std::error::Error>> {
  let document = SpreadsheetDocument::new_from_file_with_settings(path, lazy_settings())?;
  let workbook_part = document.workbook_part()?;
  let workbook_xml = workbook_part.data_as_str(&document)?.unwrap_or_default();

  Ok(extract_defined_names(workbook_xml))
}
}

The function returns an ordered map from defined-name text to the workbook expression stored in the element body. If the workbook has no <definedNames/> collection, the map is empty.

The element body can be a plain range, a sheet-qualified range, or a formula-like expression. Do not treat every value as a rectangular cell range without validating it first.