2015-02-25 09:43:37 +00:00
|
|
|
unit fpsNumFormat;
|
|
|
|
|
|
|
|
{$ifdef fpc}
|
|
|
|
{$mode objfpc}{$H+}
|
|
|
|
{$endif}
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
Classes, SysUtils,
|
|
|
|
fpstypes, fpspreadsheet;
|
|
|
|
|
|
|
|
type
|
2015-04-18 14:58:38 +00:00
|
|
|
{ TsNumFormatList }
|
2015-02-25 09:43:37 +00:00
|
|
|
|
2015-04-18 14:58:38 +00:00
|
|
|
TsNumFormatList = class(TFPList)
|
2015-02-25 09:43:37 +00:00
|
|
|
private
|
2015-04-18 14:58:38 +00:00
|
|
|
FOwnsData: Boolean;
|
|
|
|
function GetItem(AIndex: Integer): TsNumFormatParams;
|
|
|
|
procedure SetItem(AIndex: Integer; const AValue: TsNumFormatParams);
|
2015-02-25 09:43:37 +00:00
|
|
|
protected
|
|
|
|
FWorkbook: TsWorkbook;
|
2015-04-18 14:58:38 +00:00
|
|
|
FClass: TsNumFormatParamsClass;
|
2015-02-25 09:43:37 +00:00
|
|
|
procedure AddBuiltinFormats; virtual;
|
|
|
|
public
|
2015-04-18 14:58:38 +00:00
|
|
|
constructor Create(AWorkbook: TsWorkbook; AOwnsData: Boolean);
|
2015-02-25 09:43:37 +00:00
|
|
|
destructor Destroy; override;
|
2015-04-18 14:58:38 +00:00
|
|
|
function AddFormat(ASections: TsNumFormatSections): Integer; overload;
|
2015-04-19 22:03:33 +00:00
|
|
|
function AddFormat(AFormatStr: String): Integer; overload;
|
2015-02-25 09:43:37 +00:00
|
|
|
procedure Clear;
|
|
|
|
procedure Delete(AIndex: Integer);
|
2015-04-26 16:24:19 +00:00
|
|
|
function Find(ASections: TsNumFormatSections): Integer; overload;
|
|
|
|
function Find(AFormatstr: String): Integer; overload;
|
2015-04-18 14:58:38 +00:00
|
|
|
property Items[AIndex: Integer]: TsNumFormatParams read GetItem write SetItem; default;
|
2015-02-25 09:43:37 +00:00
|
|
|
{@@ Workbook from which the number formats are collected in the list. It is
|
2015-04-18 14:58:38 +00:00
|
|
|
mainly needed to get access to the FormatSettings for easy localization of
|
|
|
|
some formatting strings. }
|
2015-02-25 09:43:37 +00:00
|
|
|
property Workbook: TsWorkbook read FWorkbook;
|
|
|
|
end;
|
|
|
|
|
2015-03-31 19:01:16 +00:00
|
|
|
|
2015-04-18 14:58:38 +00:00
|
|
|
function IsCurrencyFormat(AFormat: TsNumberFormat): Boolean; overload;
|
|
|
|
function IsCurrencyFormat(ANumFormat: TsNumFormatParams): Boolean; overload;
|
|
|
|
|
2015-02-25 09:43:37 +00:00
|
|
|
function IsDateTimeFormat(AFormat: TsNumberFormat): Boolean; overload;
|
|
|
|
function IsDateTimeFormat(AFormatStr: String): Boolean; overload;
|
2015-04-18 14:58:38 +00:00
|
|
|
function IsDateTimeFormat(ANumFormat: TsNumFormatParams): Boolean; overload;
|
|
|
|
|
2015-04-26 16:24:19 +00:00
|
|
|
function IsDateFormat(ANumFormat: TsNumFormatParams): Boolean;
|
|
|
|
|
2015-02-25 09:43:37 +00:00
|
|
|
function IsTimeFormat(AFormat: TsNumberFormat): Boolean; overload;
|
|
|
|
function IsTimeFormat(AFormatStr: String): Boolean; overload;
|
2015-04-18 14:58:38 +00:00
|
|
|
function IsTimeFormat(ANumFormat: TsNumFormatParams): Boolean; overload;
|
2015-04-27 09:59:59 +00:00
|
|
|
function IsLongTimeFormat(AFormatStr: String; ATimeSeparator: char): Boolean; overload;
|
2015-04-18 14:58:38 +00:00
|
|
|
|
|
|
|
function IsTimeIntervalFormat(ANumFormat: TsNumFormatParams): Boolean;
|
2015-02-25 09:43:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
uses
|
|
|
|
fpsUtils, fpsNumFormatParser;
|
|
|
|
|
|
|
|
{@@ ----------------------------------------------------------------------------
|
|
|
|
Checks whether the given number format code is for currency,
|
|
|
|
i.e. requires currency symbol.
|
|
|
|
|
|
|
|
@param AFormat Built-in number format identifier to be checked
|
|
|
|
@return True if AFormat is nfCurrency or nfCurrencyRed, false otherwise.
|
|
|
|
-------------------------------------------------------------------------------}
|
|
|
|
function IsCurrencyFormat(AFormat: TsNumberFormat): Boolean;
|
|
|
|
begin
|
|
|
|
Result := AFormat in [nfCurrency, nfCurrencyRed];
|
|
|
|
end;
|
|
|
|
|
2015-04-18 14:58:38 +00:00
|
|
|
{@@ ----------------------------------------------------------------------------
|
|
|
|
Checks whether the specified number format parameters apply to currency values.
|
|
|
|
|
|
|
|
@param ANumFormat Number format parameters
|
|
|
|
@return True if Kind of the 1st format parameter section contains the
|
|
|
|
nfkCurrency elements; false otherwise
|
|
|
|
-------------------------------------------------------------------------------}
|
|
|
|
function IsCurrencyFormat(ANumFormat: TsNumFormatParams): Boolean;
|
|
|
|
begin
|
|
|
|
Result := (ANumFormat <> nil) and
|
|
|
|
(ANumFormat.Sections[0].Kind * [nfkCurrency] <> []);
|
|
|
|
end;
|
|
|
|
|
2015-02-25 09:43:37 +00:00
|
|
|
{@@ ----------------------------------------------------------------------------
|
|
|
|
Checks whether the given number format code is for date/time values.
|
|
|
|
|
|
|
|
@param AFormat Built-in number format identifier to be checked
|
|
|
|
@return True if AFormat is a date/time format (such as nfShortTime),
|
|
|
|
false otherwise
|
|
|
|
-------------------------------------------------------------------------------}
|
|
|
|
function IsDateTimeFormat(AFormat: TsNumberFormat): Boolean;
|
|
|
|
begin
|
2015-04-18 14:58:38 +00:00
|
|
|
Result := AFormat in [nfShortDateTime, nfShortDate, nfLongDate,
|
|
|
|
nfShortTime, nfLongTime, nfShortTimeAM, nfLongTimeAM,
|
|
|
|
nfDayMonth, nfMonthYear, nfTimeInterval];
|
2015-02-25 09:43:37 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
{@@ ----------------------------------------------------------------------------
|
|
|
|
Checks whether the given string with formatting codes is for date/time values.
|
|
|
|
|
|
|
|
@param AFormatStr String with formatting codes to be checked.
|
|
|
|
@return True if AFormatStr is a date/time format string (such as 'hh:nn'),
|
|
|
|
false otherwise
|
|
|
|
-------------------------------------------------------------------------------}
|
|
|
|
function IsDateTimeFormat(AFormatStr: string): Boolean;
|
|
|
|
var
|
|
|
|
parser: TsNumFormatParser;
|
|
|
|
begin
|
|
|
|
parser := TsNumFormatParser.Create(nil, AFormatStr);
|
|
|
|
try
|
|
|
|
Result := parser.IsDateTimeFormat;
|
|
|
|
finally
|
|
|
|
parser.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2015-04-18 14:58:38 +00:00
|
|
|
{@@ ----------------------------------------------------------------------------
|
|
|
|
Checks whether the specified number format parameters apply to date/time values.
|
|
|
|
|
|
|
|
@param ANumFormat Number format parameters
|
|
|
|
@return True if Kind of the 1st format parameter section contains the
|
|
|
|
nfkDate or nfkTime elements; false otherwise
|
|
|
|
-------------------------------------------------------------------------------}
|
|
|
|
function IsDateTimeFormat(ANumFormat: TsNumFormatParams): Boolean;
|
|
|
|
begin
|
|
|
|
Result := (ANumFormat <> nil) and
|
|
|
|
(ANumFormat.Sections[0].Kind * [nfkDate, nfkTime] <> []);
|
|
|
|
end;
|
|
|
|
|
2015-04-26 16:24:19 +00:00
|
|
|
{@@ ----------------------------------------------------------------------------
|
|
|
|
Checks whether the specified number format parameters apply to a date value.
|
|
|
|
|
|
|
|
@param ANumFormat Number format parameters
|
|
|
|
@return True if Kind of the 1st format parameter section contains the
|
|
|
|
nfkDate, but no nfkTime elements; false otherwise
|
|
|
|
-------------------------------------------------------------------------------}
|
|
|
|
function IsDateFormat(ANumFormat: TsNumFormatParams): Boolean;
|
|
|
|
begin
|
|
|
|
Result := (ANumFormat <> nil) and (nfkDate in ANumFormat.Sections[0].Kind);
|
|
|
|
end;
|
|
|
|
|
2015-02-25 09:43:37 +00:00
|
|
|
{@@ ----------------------------------------------------------------------------
|
|
|
|
Checks whether the given built-in number format code is for time values.
|
|
|
|
|
|
|
|
@param AFormat Built-in number format identifier to be checked
|
|
|
|
@return True if AFormat represents to a time-format, false otherwise
|
|
|
|
-------------------------------------------------------------------------------}
|
|
|
|
function IsTimeFormat(AFormat: TsNumberFormat): boolean;
|
|
|
|
begin
|
|
|
|
Result := AFormat in [nfShortTime, nfLongTime, nfShortTimeAM, nfLongTimeAM,
|
|
|
|
nfTimeInterval];
|
|
|
|
end;
|
|
|
|
|
|
|
|
{@@ ----------------------------------------------------------------------------
|
|
|
|
Checks whether the given string with formatting codes is for time values.
|
|
|
|
|
|
|
|
@param AFormatStr String with formatting codes to be checked
|
|
|
|
@return True if AFormatStr represents a time-format, false otherwise
|
|
|
|
-------------------------------------------------------------------------------}
|
|
|
|
function IsTimeFormat(AFormatStr: String): Boolean;
|
|
|
|
var
|
|
|
|
parser: TsNumFormatParser;
|
|
|
|
begin
|
|
|
|
parser := TsNumFormatParser.Create(nil, AFormatStr);
|
|
|
|
try
|
|
|
|
Result := parser.IsTimeFormat;
|
|
|
|
finally
|
|
|
|
parser.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
{@@ ----------------------------------------------------------------------------
|
2015-04-18 14:58:38 +00:00
|
|
|
Checks whether the specified number format parameters apply to time values.
|
2015-02-25 09:43:37 +00:00
|
|
|
|
2015-04-18 14:58:38 +00:00
|
|
|
@param ANumFormat Number format parameters
|
|
|
|
@return True if Kind of the 1st format parameter section contains the
|
2015-04-26 16:24:19 +00:00
|
|
|
nfkTime, but no nfkDate elements; false otherwise
|
2015-02-25 09:43:37 +00:00
|
|
|
-------------------------------------------------------------------------------}
|
2015-04-18 14:58:38 +00:00
|
|
|
function IsTimeFormat(ANumFormat: TsNumFormatParams): Boolean;
|
2015-02-25 09:43:37 +00:00
|
|
|
begin
|
2015-04-18 14:58:38 +00:00
|
|
|
Result := (ANumFormat <> nil) and
|
|
|
|
(ANumFormat.Sections[0].Kind * [nfkTime] <> []);
|
2015-02-25 09:43:37 +00:00
|
|
|
end;
|
|
|
|
|
2015-04-27 09:59:59 +00:00
|
|
|
{@@ ----------------------------------------------------------------------------
|
|
|
|
Returns TRUE if the specified format string represents a long time format, i.e.
|
|
|
|
it contains two TimeSeparators.
|
|
|
|
-------------------------------------------------------------------------------}
|
|
|
|
function IsLongTimeFormat(AFormatStr: String; ATimeSeparator: Char): Boolean;
|
|
|
|
var
|
|
|
|
i, n: Integer;
|
|
|
|
begin
|
|
|
|
n := 0;
|
|
|
|
for i:=1 to Length(AFormatStr) do
|
|
|
|
if AFormatStr[i] = ATimeSeparator then inc(n);
|
|
|
|
Result := (n=2);
|
|
|
|
end;
|
|
|
|
|
2015-02-25 09:43:37 +00:00
|
|
|
{@@ ----------------------------------------------------------------------------
|
2015-04-18 14:58:38 +00:00
|
|
|
Checks whether the specified number format parameters is a time interval
|
|
|
|
format.
|
2015-02-25 09:43:37 +00:00
|
|
|
|
2015-04-18 14:58:38 +00:00
|
|
|
@param ANumFormat Number format parameters
|
|
|
|
@return True if Kind of the 1st format parameter section contains the
|
|
|
|
nfkTimeInterval elements; false otherwise
|
2015-02-25 09:43:37 +00:00
|
|
|
-------------------------------------------------------------------------------}
|
2015-04-18 14:58:38 +00:00
|
|
|
function IsTimeIntervalFormat(ANumFormat: TsNumFormatParams): Boolean;
|
2015-02-25 09:43:37 +00:00
|
|
|
begin
|
2015-04-18 14:58:38 +00:00
|
|
|
Result := (ANumFormat <> nil) and
|
|
|
|
(ANumFormat.Sections[0].Kind * [nfkTimeInterval] <> []);
|
2015-02-25 09:43:37 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
2015-04-18 14:58:38 +00:00
|
|
|
{ TsNumFormatList }
|
2015-02-25 09:43:37 +00:00
|
|
|
|
2015-04-18 14:58:38 +00:00
|
|
|
constructor TsNumFormatList.Create(AWorkbook: TsWorkbook; AOwnsData: Boolean);
|
2015-02-25 09:43:37 +00:00
|
|
|
begin
|
2015-04-18 14:58:38 +00:00
|
|
|
inherited Create;
|
|
|
|
FClass := TsNumFormatParams;
|
|
|
|
FWorkbook := AWorkbook;
|
|
|
|
FOwnsData := AOwnsData;
|
2015-02-25 09:43:37 +00:00
|
|
|
end;
|
|
|
|
|
2015-04-18 14:58:38 +00:00
|
|
|
destructor TsNumFormatList.Destroy;
|
2015-02-25 09:43:37 +00:00
|
|
|
begin
|
2015-04-18 14:58:38 +00:00
|
|
|
Clear;
|
|
|
|
inherited;
|
2015-02-25 09:43:37 +00:00
|
|
|
end;
|
|
|
|
|
2015-04-18 14:58:38 +00:00
|
|
|
function TsNumFormatList.AddFormat(ASections: TsNumFormatSections): Integer;
|
2015-02-25 09:43:37 +00:00
|
|
|
var
|
2015-04-26 16:24:19 +00:00
|
|
|
nfp: TsNumFormatParams;
|
2015-02-25 09:43:37 +00:00
|
|
|
begin
|
2015-04-18 14:58:38 +00:00
|
|
|
Result := Find(ASections);
|
|
|
|
if Result = -1 then begin
|
2015-04-26 16:24:19 +00:00
|
|
|
nfp := FClass.Create;
|
|
|
|
nfp.Sections := ASections;
|
|
|
|
Result := inherited Add(nfp);
|
2015-04-18 14:58:38 +00:00
|
|
|
end;
|
2015-02-25 09:43:37 +00:00
|
|
|
end;
|
|
|
|
|
2015-04-19 22:03:33 +00:00
|
|
|
function TsNumFormatList.AddFormat(AFormatStr: String): Integer;
|
2015-02-25 09:43:37 +00:00
|
|
|
var
|
|
|
|
parser: TsNumFormatParser;
|
2015-04-18 14:58:38 +00:00
|
|
|
newSections: TsNumFormatSections;
|
2015-02-25 09:43:37 +00:00
|
|
|
i: Integer;
|
|
|
|
begin
|
2015-04-19 22:03:33 +00:00
|
|
|
parser := TsNumFormatParser.Create(FWorkbook, AFormatStr);
|
2015-02-25 09:43:37 +00:00
|
|
|
try
|
2015-04-18 14:58:38 +00:00
|
|
|
SetLength(newSections, parser.ParsedSectionCount);
|
|
|
|
for i:=0 to High(newSections) do
|
|
|
|
newSections[i] := parser.ParsedSections[i];
|
|
|
|
Result := AddFormat(newSections);
|
2015-02-25 09:43:37 +00:00
|
|
|
finally
|
|
|
|
parser.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2015-04-18 14:58:38 +00:00
|
|
|
procedure TsNumFormatList.AddBuiltinFormats;
|
2015-02-25 09:43:37 +00:00
|
|
|
begin
|
|
|
|
end;
|
|
|
|
|
2015-04-18 14:58:38 +00:00
|
|
|
procedure TsNumFormatList.Clear;
|
2015-02-25 09:43:37 +00:00
|
|
|
var
|
2015-04-18 14:58:38 +00:00
|
|
|
i: Integer;
|
2015-02-25 09:43:37 +00:00
|
|
|
begin
|
2015-04-18 14:58:38 +00:00
|
|
|
for i := Count-1 downto 0 do Delete(i);
|
|
|
|
inherited;
|
2015-02-25 09:43:37 +00:00
|
|
|
end;
|
|
|
|
|
2015-04-18 14:58:38 +00:00
|
|
|
procedure TsNumFormatList.Delete(AIndex: Integer);
|
2015-02-25 09:43:37 +00:00
|
|
|
var
|
2015-04-18 14:58:38 +00:00
|
|
|
p: TsNumFormatParams;
|
2015-02-25 09:43:37 +00:00
|
|
|
begin
|
2015-04-18 14:58:38 +00:00
|
|
|
if FOwnsData then
|
2015-02-25 09:43:37 +00:00
|
|
|
begin
|
2015-04-18 14:58:38 +00:00
|
|
|
p := GetItem(AIndex);
|
|
|
|
if p <> nil then p.Free;
|
2015-02-25 09:43:37 +00:00
|
|
|
end;
|
2015-04-18 14:58:38 +00:00
|
|
|
inherited Delete(AIndex);
|
2015-02-25 09:43:37 +00:00
|
|
|
end;
|
|
|
|
|
2015-04-18 14:58:38 +00:00
|
|
|
function TsNumFormatList.Find(ASections: TsNumFormatSections): Integer;
|
2015-02-25 09:43:37 +00:00
|
|
|
var
|
2015-04-26 16:24:19 +00:00
|
|
|
nfp: TsNumFormatParams;
|
2015-02-25 09:43:37 +00:00
|
|
|
begin
|
2015-04-18 14:58:38 +00:00
|
|
|
for Result := 0 to Count-1 do begin
|
2015-04-26 16:24:19 +00:00
|
|
|
nfp := GetItem(Result);
|
|
|
|
if nfp.SectionsEqualTo(ASections) then
|
2015-02-25 09:43:37 +00:00
|
|
|
exit;
|
|
|
|
end;
|
|
|
|
Result := -1;
|
|
|
|
end;
|
|
|
|
|
2015-04-26 16:24:19 +00:00
|
|
|
function TsNumFormatList.Find(AFormatStr: String): Integer;
|
|
|
|
var
|
|
|
|
nfp: TsNumFormatParams;
|
|
|
|
begin
|
|
|
|
nfp := CreateNumFormatParams(FWorkbook, AFormatStr);
|
|
|
|
if nfp = nil then
|
|
|
|
Result := -1
|
|
|
|
else
|
|
|
|
Result := Find(nfp.Sections);
|
|
|
|
end;
|
|
|
|
|
2015-04-18 14:58:38 +00:00
|
|
|
function TsNumFormatList.GetItem(AIndex: Integer): TsNumFormatParams;
|
2015-02-25 09:43:37 +00:00
|
|
|
begin
|
2015-04-18 14:58:38 +00:00
|
|
|
Result := TsNumFormatParams(inherited Items[AIndex]);
|
2015-02-25 09:43:37 +00:00
|
|
|
end;
|
|
|
|
|
2015-04-18 14:58:38 +00:00
|
|
|
procedure TsNumFormatList.SetItem(AIndex: Integer;
|
|
|
|
const AValue: TsNumFormatParams);
|
2015-02-25 09:43:37 +00:00
|
|
|
begin
|
|
|
|
inherited Items[AIndex] := AValue;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
end.
|