Table of Contents

TFlexCelUserFormat Class

A class used to define a cell format in code, that you can call from a report.

Remarks

To Create a User format:

  1. Create a new class derived from TFlexCelUserFormat.
  2. Override the method Evaluate.
  3. Add the new user format to the report using TFlexCelReport.SetUserFormat.

Syntax

Unit: FlexCel.Report

TFlexCelUserFormat = class(TFlexCelObject);

Methods

Name Description
Evaluate Override this method on a derived class to implement your own defined function.

Examples

To define a user format that will format the cell according to the first parameter you pass to the function, you would: 1) Define the class:

  TMyUserFormat = class (TFlexCelUserFormat)
  public
    constructor Create;
    function Evaluate(const workbook: TExcelFile; const range: TXlsCellRange; const parameters: TFormulaValueArray): TFlxPartialFormat; override;
  end;
...
function TMyUserFormat.Evaluate(const workbook: TExcelFile; const range: TXlsCellRange; const parameters: TFormulaValueArray): TFlxPartialFormat;
var
  color: double;
  Format: TFlxFormat;
  Apply: TFlxApplyFormat;
begin
  if Length(parameters) <> 1 then
    raise Exception.Create('Invalid number of parameters for user defined format "MyUserFormat"');
  
  if not parameters[0].TryToDouble(color, false) then
    raise Exception.Create('The parameter for TMyUserFormat must be a number.');

  Format := workbook.GetDefaultFormat;
  Format.FillPattern.FgColor := TExcelColor.FromArgb(Round(color));
  Format.FillPattern.BgColor := TExcelColor.Automatic;
  Format.FillPattern.Pattern := TFlxPatternStyle.Solid;
  Apply := TFlxApplyFormat.Create;
  Apply.FillPattern.SetAllMembers(true);
  Result := TFlxPartialFormat.Create(Format, Apply, false);
end;

2) Add the function to the report.

  myFormatImpl := TMyUserFormat.Create;  //The MyUserFormat class implements an user defined format.
  //Note that we don't have to free myFormatImpl, it will be freed by the report once it is over.
  flexCelReport.SetUserFormat('MFMT', myFormatImpl);  //The name used in Excel will be =MFMT().

3) Now, you can write "<#format cell(MFMT;255)>" on a cell in the template, and it will painted blue.