From e71408fc19d47f5179d12239d51301e301e554ab Mon Sep 17 00:00:00 2001 From: wp_xxyyzz Date: Sat, 20 Jul 2019 10:21:39 +0000 Subject: [PATCH] fpspreadsheet: Add worksheet option soAutoDetectCellType git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@7059 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- .../source/common/fpspreadsheet.pas | 113 ++++++++++-------- .../fpspreadsheet/source/common/fpstypes.pas | 5 +- .../source/visual/fpspreadsheetgrid.pas | 25 ++++ 3 files changed, 88 insertions(+), 55 deletions(-) diff --git a/components/fpspreadsheet/source/common/fpspreadsheet.pas b/components/fpspreadsheet/source/common/fpspreadsheet.pas index bbd5859bd..e29efd3cd 100644 --- a/components/fpspreadsheet/source/common/fpspreadsheet.pas +++ b/components/fpspreadsheet/source/common/fpspreadsheet.pas @@ -1183,7 +1183,7 @@ begin InitCryptoInfo(FCryptoInfo); - FOptions := [soShowGridLines, soShowHeaders]; + FOptions := [soShowGridLines, soShowHeaders, soAutoDetectCellType]; end; {@@ ---------------------------------------------------------------------------- @@ -5274,21 +5274,17 @@ begin isPercent := Pos('%', AValue) = Length(AValue); if isPercent then Delete(AValue, Length(AValue), 1); - { - if IsTextFormat(numFmtParams) then - begin - WriteText(ACell, AValue); - exit; - end; - } - + // Try to detect the cell content type automatically if TryStrToCurrency(AValue, number, currSym, AFormatSettings) then begin - WriteCurrency(ACell, number, nfCurrencyRed, -1, currSym); - if IsTextFormat(numFmtParams) then begin - WriteNumberFormat(ACell, nfText); - WriteText(ACell, AValue); - end; + if (soAutoDetectCellType in FOptions) then begin + WriteCurrency(ACell, number, nfCurrencyRed, -1, currSym); + if IsTextFormat(numFmtParams) then begin + WriteNumberFormat(ACell, nfText); + WriteText(ACell, AValue); + end; + end else + WriteNumber(ACell, number); exit; end; @@ -5296,11 +5292,13 @@ begin if TryFractionStrToFloat(AValue, number, ismixed, maxdig) then begin WriteNumber(ACell, number); - WriteFractionFormat(ACell, ismixed, maxdig, maxdig); - if IsTextFormat(numFmtParams) then - begin - WriteNumberFormat(ACell, nfText); - WriteText(ACell, AValue); + if (soAutoDetectCellType in FOptions) then begin + WriteFractionFormat(ACell, ismixed, maxdig, maxdig); + if IsTextFormat(numFmtParams) then + begin + WriteNumberFormat(ACell, nfText); + WriteText(ACell, AValue); + end; end; exit; end; @@ -5308,55 +5306,64 @@ begin // Check for a "number" value (floating point, or integer) if TryStrToFloat(AValue, number, AFormatSettings) then begin - if isPercent then - WriteNumber(ACell, number/100, nfPercentage) - else - begin - if IsDateTimeFormat(numFmtParams) then - WriteNumber(ACell, number, nfGeneral) + if (soAutoDetectCellType in FOptions) then begin + if isPercent then + WriteNumber(ACell, number/100, nfPercentage) else - WriteNumber(ACell, number); - end; - if IsTextFormat(numFmtParams) then - begin - WriteNumberFormat(ACell, nfText); - WriteText(ACell, AValue); - end; + begin + if IsDateTimeFormat(numFmtParams) then + WriteNumber(ACell, number, nfGeneral) + else + WriteNumber(ACell, number); + end; + if IsTextFormat(numFmtParams) then + begin + WriteNumberFormat(ACell, nfText); + WriteText(ACell, AValue); + end; + end else + // Use pre-formatted style + WriteNumber(ACell, number); exit; end; + // Check for a date/time value: // Must be after float detection because StrToDateTime will accept a string // "1" as a valid date/time. if TryStrToDateTime(AValue, number, AFormatSettings) then begin - if number < 1.0 then // this is a time alone - begin - if not IsTimeFormat(numFmtParams) then + if (soAutoDetectCellType in FOptions) then begin + if number < 1.0 then // this is a time alone begin - ucValue := Uppercase(AValue); - isAMPM := (pos('AM', ucValue) > 0) or (pos('PM', ucValue) > 0); - isLongTime := IsLongTimeFormat(AValue, AFormatSettings.TimeSeparator); - WriteDateTime(ACell, number, TIME_FMT[isAMPM, isLongTime]); + if not IsTimeFormat(numFmtParams) then + begin + ucValue := Uppercase(AValue); + 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 - WriteDateTime(ACell, number); - end else - if frac(number) = 0.0 then // this is a date alone - begin - if pos(' ', AValue) > 0 then + if frac(number) = 0.0 then // this is a date alone + begin + if pos(' ', AValue) > 0 then + WriteDateTime(ACell, number, nfShortDateTime) + else + WriteDateTime(ACell, number, nfShortDate); + end else + if not IsDateTimeFormat(fmt.NumberFormat) then WriteDateTime(ACell, number, nfShortDateTime) else - WriteDateTime(ACell, number, nfShortDate); + WriteDateTime(ACell, number); + if IsTextFormat(numFmtParams) then + begin + WriteNumberFormat(ACell, nfText); + WriteText(ACell, AValue); + end; end else - if not IsDateTimeFormat(fmt.NumberFormat) then - WriteDateTime(ACell, number, nfShortDateTime) - else + // Use pre-formatted style WriteDateTime(ACell, number); - if IsTextFormat(numFmtParams) then - begin - WriteNumberFormat(ACell, nfText); - WriteText(ACell, AValue); - end; exit; end; diff --git a/components/fpspreadsheet/source/common/fpstypes.pas b/components/fpspreadsheet/source/common/fpstypes.pas index 25a7029d0..db4c0f4f5 100644 --- a/components/fpspreadsheet/source/common/fpstypes.pas +++ b/components/fpspreadsheet/source/common/fpstypes.pas @@ -896,9 +896,10 @@ type is defined by LeftPaneWidth and TopPaneHeight. @param soHidden Worksheet is hidden. @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, - soProtected, soPanesProtection); + soProtected, soPanesProtection, soAutoDetectCellType); {@@ Set of user interface options @ see TsSheetOption } diff --git a/components/fpspreadsheet/source/visual/fpspreadsheetgrid.pas b/components/fpspreadsheet/source/visual/fpspreadsheetgrid.pas index 24a129a3a..13bf667ef 100644 --- a/components/fpspreadsheet/source/visual/fpspreadsheetgrid.pas +++ b/components/fpspreadsheet/source/visual/fpspreadsheetgrid.pas @@ -99,6 +99,7 @@ type FEditing: Boolean; FCellFont: TFont; FAutoCalc: Boolean; + FAutoDetectCellType: Boolean; FTextOverflow: Boolean; FReadFormulas: Boolean; FDrawingCell: PCell; @@ -187,6 +188,7 @@ type function GetWordwraps(ALeft, ATop, ARight, ABottom: Integer): Boolean; function GetZoomFactor: Double; procedure SetAutoCalc(AValue: Boolean); + procedure SetAutoDetectCellType(AValue: Boolean); procedure SetBackgroundColor(ACol, ARow: Integer; AValue: TsColor); procedure SetBackgroundColors(ALeft, ATop, ARight, ABottom: Integer; AValue: TsColor); procedure SetCellBiDiMode(ACol, ARow: Integer; AValue: TsBiDiMode); @@ -338,6 +340,9 @@ type write FAllowDragAndDrop default true; {@@ Automatically recalculate formulas whenever a cell value changes. } 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 } property AutoExpand: TsAutoExpandModes read FAutoExpand write FAutoExpand default [aeData, aeNavigation, aeDefault]; @@ -618,6 +623,8 @@ type property AllowDragAndDrop; {@@ Automatically recalculates the worksheet formulas if a cell value changes. } property AutoCalc; + {@@ Automatically detect the cell's content type } + property AutoDetectCellType; {@@ Automatically expand grid dimensions } property AutoExpand; {@@ Displays column and row headers in the fixed col/row style of the grid. @@ -1312,6 +1319,7 @@ begin AutoAdvance := aaDown; ExtendedSelect := true; FHeaderCount := 1; + FAutoDetectCellType := true; ColCount := DEFAULT_COL_COUNT + FHeaderCount; RowCount := DEFAULT_ROW_COUNT + FHeaderCount; @@ -1970,6 +1978,7 @@ begin WorkbookSource.Options := WorkbookSource.Options + [boReadFormulas] else WorkbookSource.Options := Workbooksource.Options - [boReadFormulas]; SetAutoCalc(FAutoCalc); + SetAutoDetectCellType(FAutoDetectCellType); end; {@@ ---------------------------------------------------------------------------- @@ -4709,6 +4718,7 @@ begin ae := RelaxAutoExpand; GetWorkbookSource.LoadFromSpreadsheetFile(AFileName, AFormat, AWorksheetIndex); RestoreAutoExpand(ae); + SetAutoDetectCellType(FAutoDetectCellType); end; {@@ ---------------------------------------------------------------------------- @@ -4731,6 +4741,7 @@ begin ae := RelaxAutoExpand; GetWorkbookSource.LoadFromSpreadsheetFile(AFileName, AFormatID, AWorksheetIndex); RestoreAutoExpand(ae); + SetAutoDetectCellType(FAutoDetectCellType); end; {@@ ---------------------------------------------------------------------------- @@ -4751,6 +4762,7 @@ begin ae := RelaxAutoExpand; GetWorkbookSource.LoadFromSpreadsheetFile(AFilename, AFormatID, AWorksheetIndex); RestoreAutoExpand(ae); + SetAutoDetectCellType(FAutoDetectCellType); end; {@@ ---------------------------------------------------------------------------- @@ -4771,6 +4783,7 @@ begin ae := RelaxAutoExpand; GetWorkbookSource.LoadFromWorkbook(AWorkbook, AWorksheetIndex); RestoreAutoExpand(ae); + SetAutoDetectCellType(FAutoDetectCellType); Invalidate; end; @@ -6303,6 +6316,18 @@ begin 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; AValue: TsColor); var