fpspreadsheet: Add worksheet option soAutoDetectCellType

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@7059 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2019-07-20 10:21:39 +00:00
parent cb76d86aff
commit e71408fc19
3 changed files with 88 additions and 55 deletions

View File

@ -1183,7 +1183,7 @@ begin
InitCryptoInfo(FCryptoInfo); InitCryptoInfo(FCryptoInfo);
FOptions := [soShowGridLines, soShowHeaders]; FOptions := [soShowGridLines, soShowHeaders, soAutoDetectCellType];
end; end;
{@@ ---------------------------------------------------------------------------- {@@ ----------------------------------------------------------------------------
@ -5274,21 +5274,17 @@ begin
isPercent := Pos('%', AValue) = Length(AValue); isPercent := Pos('%', AValue) = Length(AValue);
if isPercent then Delete(AValue, Length(AValue), 1); if isPercent then Delete(AValue, Length(AValue), 1);
{ // Try to detect the cell content type automatically
if IsTextFormat(numFmtParams) then
begin
WriteText(ACell, AValue);
exit;
end;
}
if TryStrToCurrency(AValue, number, currSym, AFormatSettings) then if TryStrToCurrency(AValue, number, currSym, AFormatSettings) then
begin begin
if (soAutoDetectCellType in FOptions) then begin
WriteCurrency(ACell, number, nfCurrencyRed, -1, currSym); WriteCurrency(ACell, number, nfCurrencyRed, -1, currSym);
if IsTextFormat(numFmtParams) then begin if IsTextFormat(numFmtParams) then begin
WriteNumberFormat(ACell, nfText); WriteNumberFormat(ACell, nfText);
WriteText(ACell, AValue); WriteText(ACell, AValue);
end; end;
end else
WriteNumber(ACell, number);
exit; exit;
end; end;
@ -5296,18 +5292,21 @@ begin
if TryFractionStrToFloat(AValue, number, ismixed, maxdig) then if TryFractionStrToFloat(AValue, number, ismixed, maxdig) then
begin begin
WriteNumber(ACell, number); WriteNumber(ACell, number);
if (soAutoDetectCellType in FOptions) then begin
WriteFractionFormat(ACell, ismixed, maxdig, maxdig); WriteFractionFormat(ACell, ismixed, maxdig, maxdig);
if IsTextFormat(numFmtParams) then if IsTextFormat(numFmtParams) then
begin begin
WriteNumberFormat(ACell, nfText); WriteNumberFormat(ACell, nfText);
WriteText(ACell, AValue); WriteText(ACell, AValue);
end; end;
end;
exit; exit;
end; end;
// Check for a "number" value (floating point, or integer) // Check for a "number" value (floating point, or integer)
if TryStrToFloat(AValue, number, AFormatSettings) then if TryStrToFloat(AValue, number, AFormatSettings) then
begin begin
if (soAutoDetectCellType in FOptions) then begin
if isPercent then if isPercent then
WriteNumber(ACell, number/100, nfPercentage) WriteNumber(ACell, number/100, nfPercentage)
else else
@ -5322,14 +5321,19 @@ begin
WriteNumberFormat(ACell, nfText); WriteNumberFormat(ACell, nfText);
WriteText(ACell, AValue); WriteText(ACell, AValue);
end; end;
end else
// Use pre-formatted style
WriteNumber(ACell, number);
exit; exit;
end; end;
// Check for a date/time value: // Check for a date/time value:
// Must be after float detection because StrToDateTime will accept a string // Must be after float detection because StrToDateTime will accept a string
// "1" as a valid date/time. // "1" as a valid date/time.
if TryStrToDateTime(AValue, number, AFormatSettings) then if TryStrToDateTime(AValue, number, AFormatSettings) then
begin begin
if (soAutoDetectCellType in FOptions) then begin
if number < 1.0 then // this is a time alone if number < 1.0 then // this is a time alone
begin begin
if not IsTimeFormat(numFmtParams) then if not IsTimeFormat(numFmtParams) then
@ -5357,6 +5361,9 @@ begin
WriteNumberFormat(ACell, nfText); WriteNumberFormat(ACell, nfText);
WriteText(ACell, AValue); WriteText(ACell, AValue);
end; end;
end else
// Use pre-formatted style
WriteDateTime(ACell, number);
exit; exit;
end; end;

View File

@ -896,9 +896,10 @@ type
is defined by LeftPaneWidth and TopPaneHeight. is defined by LeftPaneWidth and TopPaneHeight.
@param soHidden Worksheet is hidden. @param soHidden Worksheet is hidden.
@param soProtected Worksheet is protected @param soProtected Worksheet is protected
@param soPanesProtection Panes are locked due to workbook protection } @param soPanesProtection Panes are locked due to workbook protection
@param soAutoDetectCellType Auomatically detect type of cell content}
TsSheetOption = (soShowGridLines, soShowHeaders, soHasFrozenPanes, soHidden, TsSheetOption = (soShowGridLines, soShowHeaders, soHasFrozenPanes, soHidden,
soProtected, soPanesProtection); soProtected, soPanesProtection, soAutoDetectCellType);
{@@ Set of user interface options {@@ Set of user interface options
@ see TsSheetOption } @ see TsSheetOption }

View File

@ -99,6 +99,7 @@ type
FEditing: Boolean; FEditing: Boolean;
FCellFont: TFont; FCellFont: TFont;
FAutoCalc: Boolean; FAutoCalc: Boolean;
FAutoDetectCellType: Boolean;
FTextOverflow: Boolean; FTextOverflow: Boolean;
FReadFormulas: Boolean; FReadFormulas: Boolean;
FDrawingCell: PCell; FDrawingCell: PCell;
@ -187,6 +188,7 @@ type
function GetWordwraps(ALeft, ATop, ARight, ABottom: Integer): Boolean; function GetWordwraps(ALeft, ATop, ARight, ABottom: Integer): Boolean;
function GetZoomFactor: Double; function GetZoomFactor: Double;
procedure SetAutoCalc(AValue: Boolean); procedure SetAutoCalc(AValue: Boolean);
procedure SetAutoDetectCellType(AValue: Boolean);
procedure SetBackgroundColor(ACol, ARow: Integer; AValue: TsColor); procedure SetBackgroundColor(ACol, ARow: Integer; AValue: TsColor);
procedure SetBackgroundColors(ALeft, ATop, ARight, ABottom: Integer; AValue: TsColor); procedure SetBackgroundColors(ALeft, ATop, ARight, ABottom: Integer; AValue: TsColor);
procedure SetCellBiDiMode(ACol, ARow: Integer; AValue: TsBiDiMode); procedure SetCellBiDiMode(ACol, ARow: Integer; AValue: TsBiDiMode);
@ -338,6 +340,9 @@ type
write FAllowDragAndDrop default true; write FAllowDragAndDrop default true;
{@@ Automatically recalculate formulas whenever a cell value changes. } {@@ Automatically recalculate formulas whenever a cell value changes. }
property AutoCalc: Boolean read FAutoCalc write SetAutoCalc default false; property AutoCalc: Boolean read FAutoCalc write SetAutoCalc default false;
{@@ Automatically detect the cell's content type }
property AutoDetectCellType: Boolean read FAutoDetectCellType
write SetAutoDetectCellType default true;
{@@ Automatically expand grid dimensions } {@@ Automatically expand grid dimensions }
property AutoExpand: TsAutoExpandModes read FAutoExpand write FAutoExpand property AutoExpand: TsAutoExpandModes read FAutoExpand write FAutoExpand
default [aeData, aeNavigation, aeDefault]; default [aeData, aeNavigation, aeDefault];
@ -618,6 +623,8 @@ type
property AllowDragAndDrop; property AllowDragAndDrop;
{@@ Automatically recalculates the worksheet formulas if a cell value changes. } {@@ Automatically recalculates the worksheet formulas if a cell value changes. }
property AutoCalc; property AutoCalc;
{@@ Automatically detect the cell's content type }
property AutoDetectCellType;
{@@ Automatically expand grid dimensions } {@@ Automatically expand grid dimensions }
property AutoExpand; property AutoExpand;
{@@ Displays column and row headers in the fixed col/row style of the grid. {@@ Displays column and row headers in the fixed col/row style of the grid.
@ -1312,6 +1319,7 @@ begin
AutoAdvance := aaDown; AutoAdvance := aaDown;
ExtendedSelect := true; ExtendedSelect := true;
FHeaderCount := 1; FHeaderCount := 1;
FAutoDetectCellType := true;
ColCount := DEFAULT_COL_COUNT + FHeaderCount; ColCount := DEFAULT_COL_COUNT + FHeaderCount;
RowCount := DEFAULT_ROW_COUNT + FHeaderCount; RowCount := DEFAULT_ROW_COUNT + FHeaderCount;
@ -1970,6 +1978,7 @@ begin
WorkbookSource.Options := WorkbookSource.Options + [boReadFormulas] else WorkbookSource.Options := WorkbookSource.Options + [boReadFormulas] else
WorkbookSource.Options := Workbooksource.Options - [boReadFormulas]; WorkbookSource.Options := Workbooksource.Options - [boReadFormulas];
SetAutoCalc(FAutoCalc); SetAutoCalc(FAutoCalc);
SetAutoDetectCellType(FAutoDetectCellType);
end; end;
{@@ ---------------------------------------------------------------------------- {@@ ----------------------------------------------------------------------------
@ -4709,6 +4718,7 @@ begin
ae := RelaxAutoExpand; ae := RelaxAutoExpand;
GetWorkbookSource.LoadFromSpreadsheetFile(AFileName, AFormat, AWorksheetIndex); GetWorkbookSource.LoadFromSpreadsheetFile(AFileName, AFormat, AWorksheetIndex);
RestoreAutoExpand(ae); RestoreAutoExpand(ae);
SetAutoDetectCellType(FAutoDetectCellType);
end; end;
{@@ ---------------------------------------------------------------------------- {@@ ----------------------------------------------------------------------------
@ -4731,6 +4741,7 @@ begin
ae := RelaxAutoExpand; ae := RelaxAutoExpand;
GetWorkbookSource.LoadFromSpreadsheetFile(AFileName, AFormatID, AWorksheetIndex); GetWorkbookSource.LoadFromSpreadsheetFile(AFileName, AFormatID, AWorksheetIndex);
RestoreAutoExpand(ae); RestoreAutoExpand(ae);
SetAutoDetectCellType(FAutoDetectCellType);
end; end;
{@@ ---------------------------------------------------------------------------- {@@ ----------------------------------------------------------------------------
@ -4751,6 +4762,7 @@ begin
ae := RelaxAutoExpand; ae := RelaxAutoExpand;
GetWorkbookSource.LoadFromSpreadsheetFile(AFilename, AFormatID, AWorksheetIndex); GetWorkbookSource.LoadFromSpreadsheetFile(AFilename, AFormatID, AWorksheetIndex);
RestoreAutoExpand(ae); RestoreAutoExpand(ae);
SetAutoDetectCellType(FAutoDetectCellType);
end; end;
{@@ ---------------------------------------------------------------------------- {@@ ----------------------------------------------------------------------------
@ -4771,6 +4783,7 @@ begin
ae := RelaxAutoExpand; ae := RelaxAutoExpand;
GetWorkbookSource.LoadFromWorkbook(AWorkbook, AWorksheetIndex); GetWorkbookSource.LoadFromWorkbook(AWorkbook, AWorksheetIndex);
RestoreAutoExpand(ae); RestoreAutoExpand(ae);
SetAutoDetectCellType(FAutoDetectCellType);
Invalidate; Invalidate;
end; end;
@ -6303,6 +6316,18 @@ begin
end; end;
end; end;
procedure TsCustomWorksheetGrid.SetAutoDetectCellType(AValue: Boolean);
begin
FAutoDetectCellType := AValue;
if Assigned(Worksheet) then begin
if FAutoDetectCellType then
Worksheet.Options := Worksheet.Options + [soAutoDetectCellType]
else
Worksheet.Options := Worksheet.Options - [soAutoDetectCellType];
end;
end;
procedure TsCustomWorksheetGrid.SetBackgroundColor(ACol, ARow: Integer; procedure TsCustomWorksheetGrid.SetBackgroundColor(ACol, ARow: Integer;
AValue: TsColor); AValue: TsColor);
var var