Documentazione HotXLS

OnUserFunction / OnUserFunctionEx callback

The OnUserFunction callback lets application code evaluate custom o otherwise unsupported worksheet functions during Calculate. Assegna it on TXLSWorkbook o TXLSXWorkbook before calling Calculate
Usa OnUserFunctionEx when the callback needs the current formula context. The context reports the 1-based sheet index plus the 1-based row e column of the formula cell; workbook-level Calculate calls that are not evaluating a cell report row e column as 0

Sintassi

type
  TXLSUserFunctionContext = record
    SheetIndex: Integer;
    Row: Integer;
    Col: Integer;
  end;

  TXLSUserFunctionEvent = procedure(
    Sender: TObject;
    const FunctionName: WideString;
    const Args: Variant;
    var Value: Variant;
    var Handled: Boolean) of object;

  TXLSUserFunctionExEvent = procedure(
    Sender: TObject;
    const FunctionName: WideString;
    const Args: Variant;
    const Context: TXLSUserFunctionContext;
    var Value: Variant;
    var Handled: Boolean) of object;

property OnUserFunction: TXLSUserFunctionEvent;
property OnUserFunctionEx: TXLSUserFunctionExEvent;

Arguments

FunctionName The funzione name from the formula text
Args A zero-based Variant array containing evaluated argument values. For zero-argument functions this value is Null
Context For OnUserFunctionEx, reports SheetIndex, Row, e Col. Sheet, row e column values are 1-based for cell formulas; row e column are 0 when no cell is being evaluated
Value Imposta this to the funzione result
Handled Imposta to True when the callback supplies a result. Leave False to keep the normal unsupported funzione behavior

Esempio

procedure TForm1.WorkbookUserFunction(Sender: TObject;
  const FunctionName: WideString; const Args: Variant;
  var Value: Variant; var Handled: Boolean);
begin
  if SameText(FunctionName, 'DOUBLEPLUS') then
  begin
    Value := Double(Args[0]) * 2 + Double(Args[1]);
    Handled := True;
  end;
end;

Workbook.OnUserFunction := WorkbookUserFunction;
Value := Workbook.Calculate('=DOUBLEPLUS(A1;5)');

Context-aware example

procedure TForm1.WorkbookUserFunctionEx(Sender: TObject;
  const FunctionName: WideString; const Args: Variant;
  const Context: TXLSUserFunctionContext;
  var Value: Variant; var Handled: Boolean);
begin
  if SameText(FunctionName, 'ROWBONUS') then
  begin
    Value := Double(Args[0]) + Context.Row + Context.Col;
    Handled := True;
  end;
end;

Workbook.OnUserFunctionEx := WorkbookUserFunctionEx;
Value := Worksheet.Calculate('=C2');

Vedere also