# OOXML SpreadsheetML Cheat Sheet Quick reference for XML manipulation of xlsx files. --- ## Package Structure ``` my_file.xlsx (ZIP archive) ├── [Content_Types].xml ← declares MIME types for all files ├── _rels/ │ └── .rels ← root relationship: points to xl/workbook.xml └── xl/ ├── workbook.xml ← sheet list, calc settings ├── styles.xml ← ALL style definitions ├── sharedStrings.xml ← ALL text strings (referenced by index) ├── _rels/ │ └── workbook.xml.rels ← maps r:id → worksheet/styles/sharedStrings files ├── worksheets/ │ ├── sheet1.xml ← Sheet 1 data │ ├── sheet2.xml ← Sheet 2 data │ └── ... ├── charts/ ← chart XML (if any) ├── pivotTables/ ← pivot table XML (if any) └── theme/ └── theme1.xml ← color/font theme ``` --- ## Cell Reference Format ``` A1 → column A (1), row 1 B5 → column B (2), row 5 AA1 → column 27, row 1 ``` Column letter ↔ number conversion: ```python def col_letter(n): # 1-based → letter r = "" while n > 0: n, rem = divmod(n - 1, 26) r = chr(65 + rem) + r return r def col_number(s): # letter → 1-based n = 0 for c in s.upper(): n = n * 26 + (ord(c) - 64) return n ``` --- ## Cell XML Reference ### Data Types | Type | `t` attr | XML Example | Value | |------|---------|-------------|-------| | Number | omit | `1000` | 1000 | | String (shared) | `s` | `0` | sharedStrings[0] | | String (inline) | `inlineStr` | `Hi` | "Hi" | | Boolean | `b` | `1` | TRUE | | Error | `e` | `#REF!` | #REF! | | Formula | omit | `SUM(B2:B3)` | computed | ### Formula Types ```xml SUM(B2:B3) Assumptions!B5 'Sheet With Spaces'!B5 B2*C2 SORT(A1:A5) ``` --- ## styles.xml Reference ### Indirect Reference Chain ``` Cell s="3" ↓ cellXfs[3] → fontId="2", fillId="0", borderId="0", numFmtId="165" ↓ ↓ ↓ ↓ ↓ fonts[2] fills[0] borders[0] numFmts: id=165 blue color no fill no border "0.0%" ``` ### Adding a New Style (step-by-step) 1. In ``: add ``, update `count` 2. In ``: add font entry, note its index 3. In ``: append ``, update `count` 4. New style index = old `cellXfs count` value (before incrementing) 5. Apply to cells: `...` ### Color Format `AARRGGBB` — Alpha (always `00` for opaque) + Red + Green + Blue ``` 000000FF → Blue 00000000 → Black 00008000 → Green (dark) 00FF0000 → Red 00FFFF00 → Yellow (for fills) 00FFFFFF → White ``` ### Built-in numFmtIds (no declaration needed) | ID | Format | Display | |----|--------|---------| | 0 | General | as-is | | 1 | 0 | 2024 (use for years!) | | 2 | 0.00 | 1000.00 | | 3 | #,##0 | 1,000 | | 4 | #,##0.00 | 1,000.00 | | 9 | 0% | 15% | | 10 | 0.00% | 15.25% | | 14 | m/d/yyyy | 3/21/2026 | --- ## sharedStrings.xml Reference ```xml Revenue Cost Margin ``` Text with leading/trailing spaces: ```xml indented ``` Special characters: ```xml R&D Expenses ``` --- ## workbook.xml / .rels Sync Every `` in workbook.xml needs a matching `` in workbook.xml.rels: ```xml ``` And a matching `` in `[Content_Types].xml`: ```xml ``` --- ## Column / Row Dimensions ```xml ... ``` --- ## Freeze Panes Inside ``: ```xml ``` --- ## 7 Excel Error Types (All Must Be Absent at Delivery) | Error | Meaning | Detect in XML | |-------|---------|---------------| | `#REF!` | Invalid cell reference | `#REF!` | | `#DIV/0!` | Divide by zero | `#DIV/0!` | | `#VALUE!` | Wrong data type | `#VALUE!` | | `#NAME?` | Unknown function/name | `#NAME?` | | `#NULL!` | Empty intersection | `#NULL!` | | `#NUM!` | Number out of range | `#NUM!` | | `#N/A` | Value not found | `#N/A` |