Working with the Workbook Model Layer

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:

  • intern adds a string and returns its index (reusing existing entries),
  • try_get_value resolves an index back to its string,
  • clear resets 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 LoadOptions repair flags with the diagnostics the loader reports.

Common Issues

IssueCauseFix
Duplicate shared stringsBypassing the internerRoute text through SharedStringRepository.intern
Validation rule ignoredModel fields incompleteSet 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/MethodDescription
ColorValueARGB color record (a, r, g, b)
NumberFormatValue.cloneCopy a number-format definition
ValidationModelValidation rule record (areas, operator, formulas)
FormatConditionModel.defaultEmpty conditional-format rule record
SharedStringRepository.internIntern a string, returning its index
SharedStringRepository.try_get_valueResolve an interned index
DiagnosticBag.addRecord a structured diagnostic

See Also