CreateReadOnlyView method
Creates a lightweight read-only view over a loaded Classic or XLSX workbook model
Syntax
function CreateReadOnlyView: IXLSReadOnlyWorkbookView;
CreateReadOnlyView is declared by Classic IXLSWorkbook in lxHandle and XLSX IXLSXWorkbookOwner in lxHandleX; the shared view interface, options, and snapshot types are declared in lxStandard
View state and lifetime
Creating a view is constant time and does not traverse worksheets, cells, styles, names, or drawings
Each view has its own one-based ActiveSheetIndex, so several views can select different worksheets without changing Classic ActiveSheetIndex or XLSX ActiveIndex, IsSelected, and ActiveTab
The view holds a read lease while it exists, and workbook mutations fail fast with EXLSWorkbookWriteGuardUnavailable before changing the model; conversely, requesting a read lease while a write transaction is active raises EXLSWorkbookReadLeaseUnavailable
Hold Classic workbooks through IXLSWorkbook and XLSX workbooks through IXLSXWorkbookOwner when using views; the owner interface and every view then retain the same reference-counted workbook and may be released in either order
Do not call Free on a concrete TXLSWorkbook or TXLSXWorkbook while an owner interface or view still references it
Cell snapshots
TryReadCell uses one-based row and column coordinates on the view's active sheet and returns False only when no stored cell exists at that coordinate
A missing-cell read does not create storage or advance the workbook generation
TXLSReadOnlyCellSnapshot owns its sheet name, value, formula text, cache and evaluation information, coordinates, kind, and style index and never exposes a writable workbook object
Starting with 2.384.51, SheetName is a readable and writable WideString property backed by a private UnicodeString; ordinary reads such as Name := Cell.SheetName and assignments such as Cell.SheetName := Name keep the same syntax and public value type, and changing a snapshot's name does not rename the workbook worksheet
Callers must recompile against 2.384.51 because the record's raw layout has changed; taking the address of SheetName, passing it as a var or out field argument, relying on field offsets, or reusing binaries compiled against the previous layout is unsupported
With the classic Win32 bcc32 compiler in XE5 and RAD Studio 37.0, declare a snapshot first and then assign an existing snapshot to it; direct copy construction of a populated snapshot cleared the nested cache and evaluation Variant values in runtime checks, even though the sheet name survived
Lxstandard::TXLSReadOnlyCellSnapshot saved; saved = cell;
The public TryReadCell interface fills an existing snapshot through an out parameter; avoid custom Pascal helpers that return this record directly into a C++ constructor, which showed the same nested-value loss in those compiler checks
| Member | Meaning |
|---|---|
SheetIndex / SheetName | View-local worksheet identity |
Row / Col | One-based cell coordinates |
StyleIndex | Stored Classic XF index or XLSX cell-format index |
Kind | Text, number, date-time, Boolean, formula, error, or blank classification |
Value | Stored scalar value or formula-cache value without recalculation |
HasFormulaCache / FormulaCache | Formula-cache presence, provenance (TXLSFormulaCacheState), value kind (TXLSFormulaCacheValueKind), and owned cached value |
FormulaEvaluation | A TXLSFormulaEvaluationInfo record whose TXLSFormulaEvaluationState reports not applicable, not requested, calculated, or failed, plus the evaluator result code and owned result value |
Formula | Empty by default and populated only when formula text is explicitly enabled |
Formula policy
By default formula cells use the existing stored cache and never calculate, invoke a user function, update dirty state, or write a cache back to the workbook
Add xlsvIncludeFormulaText to Options when formula text is required; the option set has type TXLSReadOnlyViewOptions = set of TXLSReadOnlyViewOption. Classic creates a view-private decoder on demand, while XLSX copies its stored formula string, and neither path initializes or modifies the workbook calculator
A formula without a loaded or calculated cache reports HasFormulaCache=False and leaves Value as Unassigned
Add xlsvEvaluateFormulas to evaluate formulas through a view-private calculator, recursion state, scalar cache, array-matrix cache, and XLSX compiled-formula cache; the resulting Value and FormulaEvaluation.Value never replace the workbook's stored cache or compiled formula runtime fields
Evaluated formula values project completed blank results to numeric zero while retaining empty text and typed errors; raw blank cell snapshots keep their original blank identity, and evaluation does not replace stale stored caches or advance the workbook generation
Formula evaluation applies to source cells with their own formula text; array followers without their own formula text expose their stored values and report xlfesNotFormula, even when the source cell is evaluated by the view; call the public workbook Recalculate method before creating the view when follower caches must reflect recent edits
Standard spreadsheet errors are returned as Variant error values with xlfesCalculated, while unsupported or internal evaluation failures report xlfesFailed and retain the original stored cache in FormulaCache
Repeated reads through one view reuse its private evaluation cache for the lease generation, while different views remain independent and may evaluate the same shared formula concurrently
XLSX external formula evaluation reads only the file-local externalLink cache and never dereferences a weak live-workbook registration or performs external I/O
Threading and performance
Use one view per worker thread; separate views may scan different worksheets or evaluate the same formula concurrently, while callers must not mutate one view's active sheet or options from another thread
After creation, ordinary cell reads do not enter a monitor or critical section because the lease prevents concurrent workbook mutation
Each view retains its active worksheet name as a UnicodeString and shares that managed string with cell snapshots, avoiding a separate WideString allocation for every captured cell; reading the public SheetName property converts the retained name to WideString
Release validation keeps each idle view below 64 KiB of fixed state, one hundred idle views below 8 MiB, and view creation independent of workbook size with a one-millisecond p95 gate on a one-million-cell model
The performance suite also limits the effect of an idle view on existing single-thread cursor scans to five percent and requires eight views scanning independent worksheets to reach at least five times the single-view throughput on the validation host
Example
var
Workbook: IXLSWorkbook;
View: IXLSReadOnlyWorkbookView;
Cell: TXLSReadOnlyCellSnapshot;
begin
Workbook := TXLSWorkbook.Create;
Workbook.Sheets.Add;
Workbook.Sheets[1].Cells[1, 1].Value := 'ready';
View := Workbook.CreateReadOnlyView;
View.Options := [xlsvEvaluateFormulas];
if View.TryReadCell(1, 1, Cell) then
Memo1.Lines.Add(VarToStr(Cell.Value));
end;
For XLSX, retain the concrete workbook through its owner interface before creating views
var
Owner: IXLSXWorkbookOwner;
Workbook: TXLSXWorkbook;
View: IXLSReadOnlyWorkbookView;
begin
Workbook := TXLSXWorkbook.Create;
Owner := Workbook;
Workbook.Sheets.Add('Data').Cells[1, 1].Value := 42;
View := Owner.CreateReadOnlyView;
end;