Working with the Workbook Model Layer
Working with the Workbook Model Layer
Beneath the user-facing API, the crate keeps a plain-data model layer that mirrors the XLSX package: value structs, rule models, and shared storage. Most applications never touch these types directly, but they surface when you inspect loaded workbooks in depth or contribute to the crate itself.
Value Types
ColorValue carries ARGB color components as plain a, r, g, b
fields, with equals and get_hash_code for comparisons. Number formatting
at the model level lives in NumberFormatValue, which supports clone for
copying format definitions between model records.
Rule Models
Data-quality rules have model twins: ValidationModel exposes the rule’s
areas, validation_type, alert_style, operator, and the formula1 /
formula2 expressions as fields; FormatConditionModel and
ConditionalFormattingModel play the same role for conditional formatting
records. These are constructed with default and populated field-by-field.
Shared String Storage
XLSX files store repeated cell text in a shared-strings table. The
SharedStringRepository models it as an interner:
internadds a string and returns its index (reusing existing entries),try_get_valueresolves an index back to its string,clearresets the repository.
Diagnostics
DiagnosticBag collects structured issues via add during model operations.
At the API level the same information reaches you through
LoadDiagnostics when loading workbooks — see
Features Overview.
Tips and Best Practices
- Prefer the user-facing API (
Workbook,Worksheet, cell collections) — the model layer is not needed for routine spreadsheet work. - Treat model structs as data records: construct with
default, then set fields explicitly. - When investigating malformed files, combine
LoadOptionsrepair flags with the diagnostics the loader reports.
Common Issues
| Issue | Cause | Fix |
|---|---|---|
| Duplicate shared strings | Bypassing the interner | Route text through SharedStringRepository.intern |
| Validation rule ignored | Model fields incomplete | Set validation_type, operator, and formula1 on ValidationModel |
FAQ
Do I need the model layer to write spreadsheets?
No. The Workbook API covers creating, editing, and saving workbooks; the
model layer underpins it.
Why do model structs use default constructors?
They are plain data records mirroring XLSX structures — default gives an
empty record you populate field-by-field.
API Reference Summary
| Class/Method | Description |
|---|---|
ColorValue | ARGB color record (a, r, g, b) |
NumberFormatValue.clone | Copy a number-format definition |
ValidationModel | Validation rule record (areas, operator, formulas) |
FormatConditionModel.default | Empty conditional-format rule record |
SharedStringRepository.intern | Intern a string, returning its index |
SharedStringRepository.try_get_value | Resolve an interned index |
DiagnosticBag.add | Record a structured diagnostic |