Working with sheets

Sheets are workbook entries that point to sheet parts. The most common sheet type is a worksheet, which stores a grid of rows and cells. Workbooks can also contain chartsheets, dialog sheets, and macro sheets.

Sheets are where most spreadsheet work happens. Worksheets store rows, cells, formulas, formatting references, tables, filters, drawings, and other grid-connected features. Chartsheets store a chart as its own sheet, and dialog sheets represent legacy custom dialog UI.

Workbook sheet list

The workbook part stores sheet metadata:

<sheets>
  <sheet name="Summary" sheetId="1" r:id="rId1"/>
  <sheet name="Hidden Data" sheetId="2" state="hidden" r:id="rId2"/>
</sheets>

The name and state attributes are workbook metadata. The actual worksheet XML is in the target part resolved by r:id.

Worksheet parts

A worksheet part is rooted at <worksheet/> and contains the required <sheetData/> element.

<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
  <sheetData>
    <row r="1">
      <c r="A1" t="s"><v>0</v></c>
    </row>
  </sheetData>
</worksheet>

Use the workbook part to traverse worksheet parts:

#![allow(unused)]
fn main() {
pub fn get_worksheet_xml(path: &Path) -> Result<Vec<String>, Box<dyn std::error::Error>> {
  let document = SpreadsheetDocument::new_from_file_with_settings(path, lazy_settings())?;
  let workbook_part = document.workbook_part()?;
  let mut worksheets = Vec::new();

  for worksheet_part in workbook_part.worksheet_parts(&document) {
    worksheets.push(
      worksheet_part
        .data_as_str(&document)?
        .unwrap_or_default()
        .to_string(),
    );
  }

  Ok(worksheets)
}
}

For display names or hidden state, read the workbook sheet list:

#![allow(unused)]
fn main() {
pub fn list_worksheets(path: &Path) -> Result<Vec<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_sheet_names(workbook_xml))
}
}

Cell basics

Rows are stored as <row/> elements and cells as <c/> elements. Cell references use A1 notation such as A1 or B2. The <v/> value is either the raw value or an index into another structure, depending on the cell type.

The smallest valid blank worksheet is a worksheet root with an empty sheetData child. Optional sheet properties can appear before sheetData; optional supporting features such as protection, filters, drawings, and table references can appear after it.

In ooxmlsdk, generated schema types include Worksheet, SheetData, Row, Cell, CellValue, Chartsheet, and Drawing. Prefer typed part accessors such as WorkbookPart::worksheet_parts(&document) and WorkbookPart::chartsheet_parts(&document) when traversing package structure.