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
WriteCurrency(ACell, number, nfCurrencyRed, -1, currSym); if (soAutoDetectCellType in FOptions) then begin
if IsTextFormat(numFmtParams) then begin WriteCurrency(ACell, number, nfCurrencyRed, -1, currSym);
WriteNumberFormat(ACell, nfText); if IsTextFormat(numFmtParams) then begin
WriteText(ACell, AValue); WriteNumberFormat(ACell, nfText);
end; WriteText(ACell, AValue);
end;
end else
WriteNumber(ACell, number);
exit; exit;
end; end;
@ -5296,11 +5292,13 @@ begin
if TryFractionStrToFloat(AValue, number, ismixed, maxdig) then if TryFractionStrToFloat(AValue, number, ismixed, maxdig) then
begin begin
WriteNumber(ACell, number); WriteNumber(ACell, number);
WriteFractionFormat(ACell, ismixed, maxdig, maxdig); if (soAutoDetectCellType in FOptions) then begin
if IsTextFormat(numFmtParams) then WriteFractionFormat(ACell, ismixed, maxdig, maxdig);
begin if IsTextFormat(numFmtParams) then
WriteNumberFormat(ACell, nfText); begin
WriteText(ACell, AValue); WriteNumberFormat(ACell, nfText);
WriteText(ACell, AValue);
end;
end; end;
exit; exit;
end; end;
@ -5308,55 +5306,64 @@ begin
// 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 isPercent then if (soAutoDetectCellType in FOptions) then begin
WriteNumber(ACell, number/100, nfPercentage) if isPercent then
else WriteNumber(ACell, number/100, nfPercentage)
begin
if IsDateTimeFormat(numFmtParams) then
WriteNumber(ACell, number, nfGeneral)
else else
WriteNumber(ACell, number); begin
end; if IsDateTimeFormat(numFmtParams) then
if IsTextFormat(numFmtParams) then WriteNumber(ACell, number, nfGeneral)
begin else
WriteNumberFormat(ACell, nfText); WriteNumber(ACell, number);
WriteText(ACell, AValue); end;
end; if IsTextFormat(numFmtParams) then
begin
WriteNumberFormat(ACell, nfText);
WriteText(ACell, AValue);
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 number < 1.0 then // this is a time alone if (soAutoDetectCellType in FOptions) then begin
begin if number < 1.0 then // this is a time alone
if not IsTimeFormat(numFmtParams) then
begin begin
ucValue := Uppercase(AValue); if not IsTimeFormat(numFmtParams) then
isAMPM := (pos('AM', ucValue) > 0) or (pos('PM', ucValue) > 0); begin
isLongTime := IsLongTimeFormat(AValue, AFormatSettings.TimeSeparator); ucValue := Uppercase(AValue);
WriteDateTime(ACell, number, TIME_FMT[isAMPM, isLongTime]); isAMPM := (pos('AM', ucValue) > 0) or (pos('PM', ucValue) > 0);
isLongTime := IsLongTimeFormat(AValue, AFormatSettings.TimeSeparator);
WriteDateTime(ACell, number, TIME_FMT[isAMPM, isLongTime]);
end else
WriteDateTime(ACell, number);
end else end else
WriteDateTime(ACell, number); if frac(number) = 0.0 then // this is a date alone
end else begin
if frac(number) = 0.0 then // this is a date alone if pos(' ', AValue) > 0 then
begin WriteDateTime(ACell, number, nfShortDateTime)
if pos(' ', AValue) > 0 then else
WriteDateTime(ACell, number, nfShortDate);
end else
if not IsDateTimeFormat(fmt.NumberFormat) then
WriteDateTime(ACell, number, nfShortDateTime) WriteDateTime(ACell, number, nfShortDateTime)
else else
WriteDateTime(ACell, number, nfShortDate); WriteDateTime(ACell, number);
if IsTextFormat(numFmtParams) then
begin
WriteNumberFormat(ACell, nfText);
WriteText(ACell, AValue);
end;
end else end else
if not IsDateTimeFormat(fmt.NumberFormat) then // Use pre-formatted style
WriteDateTime(ACell, number, nfShortDateTime)
else
WriteDateTime(ACell, number); WriteDateTime(ACell, number);
if IsTextFormat(numFmtParams) then
begin
WriteNumberFormat(ACell, nfText);
WriteText(ACell, AValue);
end;
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