Technischer Artikel

HotXLS: formula calculation and custom functions in Delphi

Erstellen, bearbeiten, pruefen, berechnen und exportieren Sie Excel-Arbeitsmappen direkt aus Delphi- oder C++Builder-Code. HotXLS ist eine native Object-Pascal-Bibliothek fuer XLS und XLSX, entwickelt fuer Desktop-Tools, Batchauftraege, Berichte und Dokumenterzeugung ohne Microsoft-Excel-Automatisierung.

Dieser Artikel richtet sich an developers generating workbooks whose calculated results are part of the product output. Er behandelt formula calculation and custom functions als produktive Dokumenttechnik und nicht als kurzen Komponentenaufruf.

Das praktische Risiko besteht darin, dass formula results can appear correct in simple cases while dependency order, custom functions, locale parsing, volatile functions, or error values differ in production. Deshalb braucht der Ablauf einen schriftlichen Vertrag, nachvollziehbare Diagnosen und reale Regressionsdateien.

Architekturentscheidungen

Treat calculation as a deterministic service. calculation mode, dependency refresh, and whether cached values are trusted / custom function names, argument types, null handling, and versioning

  • calculation mode, dependency refresh, and whether cached values are trusted
  • custom function names, argument types, null handling, and versioning
  • locale handling for decimal separators, date literals, and function names
  • error-value policy for invalid references, division by zero, and unsupported formulas

Implementierungsablauf

Register custom functions before loading dependent formulas. The order below keeps the workflow reviewable for Delphi and C++Builder teams.

  1. register custom functions and workbook names before calculation
  2. load or generate formulas with explicit dependency expectations
  3. calculate targeted ranges or full workbook output according to the profile
  4. compare critical results with independent fixtures or approved Excel output
  5. write calculation diagnostics into the support bundle when results are disputed

Validierungsnachweise

Calculation evidence for finance and support. Keep these fields with the output or support record.

  • formula count, dependency graph summary, custom function calls, and recalculation time
  • critical cell addresses, formula text, cached value, calculated value, and error state
  • custom function version, arguments, returned value, and exception mapping
  • locale profile and unsupported-formula warnings

Custom functions need versioned behavior

A formula engine workflow should make dependencies, custom function registration, recalculation mode, cached results, and error handling visible. That is especially important when generated workbooks are consumed without Excel automation.

Implementierungshinweise für die Produktion

Behandle HotXLS: formula calculation and custom functions in Delphi als klaren Servicevertrag rund um die HotXLS-Aufrufe, mit getrennten Schritten für Eingabeprüfung, Arbeitsmappenaufbau, Ausgabekontrolle und Support-Evidenz

  • Datenquelle, Zellbereiche und Ausgabeformat festlegen, bevor die Arbeitsmappe erzeugt wird
  • Zeilenanzahl, Blattanzahl, Warnungen und Ausgabepfad in ein prüfbares Support-Protokoll schreiben
  • Anwendungsspezifische Details in testbare Helper kapseln, statt sie in UI-Ereignissen zu verteilen
  • Die gespeicherte Datei erneut öffnen oder prüfen, bevor sie an ein anderes System oder an Kunden geht

Fehlerfälle, die getestet werden sollten

  • Ein erfolgreicher SaveAs-Aufruf beweist noch nicht, dass der fachliche Vertrag stimmt
  • Schriftarten, Rechte und regionale Einstellungen können auf Servern anders sein als auf Entwicklerrechnern
  • Logs dürfen keine Passwörter, Kundendaten oder internen Links offenlegen

Ausführliches Delphi-Beispiel

Das folgende Beispiel zeigt eine praktische Servicegrenze für dieses Thema und hält Policy, Logging und Validierung testbar getrennt

procedure RecalculateInvoiceWorkbook(const TemplateFile, OutputFile: string; const Orders: TArray<TOrderRow>);
var
  Wb: TXLSXWorkbook;
  Sh: IXLSWorksheet;
  RowIndex: Integer;
  Order: TOrderRow;
begin
  Wb := TXLSXWorkbook.Create;
  try
    Wb.Open(TemplateFile);
    RegisterInvoiceFunctions(Wb);
    Sh := Wb.Sheets[0];
    EnsureTemplateVersion(Wb, 'invoice-formulas-v3');

    RowIndex := 2;
    for Order in Orders do
    begin
      Sh.Range['A' + IntToStr(RowIndex)].Value := Order.Sku;
      Sh.Range['B' + IntToStr(RowIndex)].Value := Order.Quantity;
      Sh.Range['C' + IntToStr(RowIndex)].Value := Order.UnitPrice;
      Sh.Range['D' + IntToStr(RowIndex)].Value := '=B' + IntToStr(RowIndex) + '*C' + IntToStr(RowIndex);
      Sh.Range['E' + IntToStr(RowIndex)].Value := '=InvoiceTax(D' + IntToStr(RowIndex) + ')';
      Inc(RowIndex);
    end;

    Wb.Calculate;
    AssertFormulaRangeHasValues(Sh, 'D2:E' + IntToStr(RowIndex - 1));
    WriteCalculationAudit(Wb, 'invoice-formulas', RowIndex - 2);

    if Wb.SaveAs(OutputFile) <> 1 then
      RaiseWorkbookSaveError(OutputFile);
  finally
    Wb.Free;
  end;
end;

Produktionscheckliste

  • Run the workflow on an empty workbook, a normal customer workbook, and a worst-case workbook
  • Open the output with the target spreadsheet application or downstream importer
  • Log product version, template version, profile, row count, output path, elapsed time, and warning count
  • Keep passwords, temporary files, customer data, and support bundles under explicit retention rules
  • Add regression workbooks when a customer file exposes a new edge case

Product documentation

HotXLS Component