Retrieve the values of cells in a spreadsheet

Cell values are stored in worksheet XML. Text cells often use the shared string table: the cell stores an integer index and the actual text is stored once in xl/sharedStrings.xml.

Read cell values

#![allow(unused)]
fn main() {
pub fn get_cell_values(path: &Path) -> Result<Vec<(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 shared_strings = workbook_part
    .shared_string_table_part(&document)
    .and_then(|part| {
      part
        .data_as_str(&document)
        .ok()
        .flatten()
        .map(extract_shared_strings)
    })
    .unwrap_or_default();
  let Some(first_sheet) = workbook_part.worksheet_parts(&document).next() else {
    return Ok(Vec::new());
  };
  let worksheet_xml = first_sheet.data_as_str(&document)?.unwrap_or_default();

  Ok(extract_cell_values(worksheet_xml, &shared_strings))
}
}

The helper reads the shared string table when present, then reads the first worksheet and resolves cells with t="s" through that table. Numeric cells are returned from their <v/> value directly.

For a sheet-specific helper, first find the workbook sheet entry by name, use its relationship id to resolve the worksheet part, and then search that worksheet for the requested cell reference. If the sheet or cell is missing, return None or an explicit domain error instead of defaulting to a misleading value.

Cell markup

<row r="1">
  <c r="A1" t="s"><v>0</v></c>
  <c r="B1"><v>42</v></c>
</row>

In this example, A1 uses shared string index 0; B1 stores the numeric value directly. Dates, formulas, booleans, inline strings, and styled values need additional interpretation from styles and formula cache data.

Cell type drives interpretation. A missing t attribute usually means the value is numeric or date-like, depending on the cell style. t="s" means the value is a shared string index. t="b" stores booleans as 0 or 1. Formula cells can contain <f> plus a cached <v> result; reading the cached value does not recalculate the formula.