fpspreadsheet: Improvments of TsWorksheetGrid and fpsgrid demo: Fix currency symbol not shown when selecting currency format. Fix showing a zero value when assigning a number format to an empty cell. Fix formula display when selecting an empty cell. Reset number format when selecting "General" format. Move selection to entered cell address in address edit.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@3144 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2014-06-05 21:57:23 +00:00
parent c275de3947
commit 6c0efaf6eb
5 changed files with 77 additions and 16 deletions

View File

@ -411,6 +411,7 @@ object Form1: TForm1
Width = 123 Width = 123
Align = alLeft Align = alLeft
Alignment = taCenter Alignment = taCenter
OnEditingDone = EdCellAddressEditingDone
TabOrder = 1 TabOrder = 1
end end
object FormulaToolbarSplitter: TSplitter object FormulaToolbarSplitter: TSplitter

View File

@ -238,6 +238,7 @@ type
procedure CbShowHeadersClick(Sender: TObject); procedure CbShowHeadersClick(Sender: TObject);
procedure CbShowGridLinesClick(Sender: TObject); procedure CbShowGridLinesClick(Sender: TObject);
procedure CbBackgroundColorGetColors(Sender: TCustomColorBox; Items: TStrings); procedure CbBackgroundColorGetColors(Sender: TCustomColorBox; Items: TStrings);
procedure EdCellAddressEditingDone(Sender: TObject);
procedure EdFrozenColsChange(Sender: TObject); procedure EdFrozenColsChange(Sender: TObject);
procedure EdFrozenRowsChange(Sender: TObject); procedure EdFrozenRowsChange(Sender: TObject);
procedure FontComboBoxSelect(Sender: TObject); procedure FontComboBoxSelect(Sender: TObject);
@ -500,16 +501,21 @@ begin
c := GetWorksheetCol(Col); c := GetWorksheetCol(Col);
r := GetWorksheetRow(Row); r := GetWorksheetRow(Row);
cell := Worksheet.GetCell(r, c); cell := Worksheet.GetCell(r, c);
if IsDateTimeFormat(nf) then begin case cell^.ContentType of
if IsDateTimeFormat(cell^.NumberFormat) then cctNumber, cctDateTime:
Worksheet.WriteDateTime(cell, cell^.DateTimeValue, nf, fmt) if IsDateTimeFormat(nf) then begin
if IsDateTimeFormat(cell^.NumberFormat) then
Worksheet.WriteDateTime(cell, cell^.DateTimeValue, nf, fmt)
else
Worksheet.WriteDateTime(cell, cell^.NumberValue, nf, fmt);
end else begin
if IsDateTimeFormat(cell^.NumberFormat) then
Worksheet.WriteNumber(cell, cell^.DateTimeValue, nf, cell^.Decimals, cell^.CurrencySymbol)
else
Worksheet.WriteNumber(cell, cell^.NumberValue, nf, cell^.Decimals, cell^.CurrencySymbol);
end;
else else
Worksheet.WriteDateTime(cell, cell^.NumberValue, nf, fmt); Worksheet.WriteNumberformat(cell, nf, fmt);
end else begin
if IsDateTimeFormat(cell^.NumberFormat) then
Worksheet.WriteNumber(cell, cell^.DateTimeValue, nf, cell^.Decimals, cell^.CurrencySymbol)
else
Worksheet.WriteNumber(cell, cell^.NumberValue, nf, cell^.Decimals, cell^.CurrencySymbol);
end; end;
end; end;
@ -616,6 +622,16 @@ begin
end; end;
end; end;
procedure TForm1.EdCellAddressEditingDone(Sender: TObject);
var
c, r: integer;
begin
if ParseCellString(EdCellAddress.Text, r, c) then begin
WorksheetGrid.Row := WorksheetGrid.GetGridRow(r);
WorksheetGrid.Col := WorksheetGrid.GetGridCol(c);
end;
end;
procedure TForm1.EdFrozenColsChange(Sender: TObject); procedure TForm1.EdFrozenColsChange(Sender: TObject);
begin begin
WorksheetGrid.FrozenCols := EdFrozenCols.Value; WorksheetGrid.FrozenCols := EdFrozenCols.Value;
@ -755,7 +771,8 @@ begin
EdFormula.Text := WorksheetGrid.Worksheet.ReadRPNFormulaAsString(cell) EdFormula.Text := WorksheetGrid.Worksheet.ReadRPNFormulaAsString(cell)
else else
EdFormula.Text := WorksheetGrid.Worksheet.ReadAsUTF8Text(cell); EdFormula.Text := WorksheetGrid.Worksheet.ReadAsUTF8Text(cell);
end; end else
EdFormula.Text := '';
EdCellAddress.Text := GetCellString(r, c, [rfRelRow, rfRelCol]); EdCellAddress.Text := GetCellString(r, c, [rfRelRow, rfRelCol]);

View File

@ -380,7 +380,8 @@ type
FOptions: TsSheetOptions; FOptions: TsSheetOptions;
FOnChangeCell: TsCellEvent; FOnChangeCell: TsCellEvent;
FOnChangeFont: TsCellEvent; FOnChangeFont: TsCellEvent;
procedure RemoveCallback(data, arg: pointer); function GetFormatSettings: TFormatSettings;
procedure RemoveCallback(data, arg: pointer);
protected protected
procedure ChangedCell(ARow, ACol: Cardinal); procedure ChangedCell(ARow, ACol: Cardinal);
@ -468,7 +469,9 @@ type
procedure WriteHorAlignment(ARow, ACol: Cardinal; AValue: TsHorAlignment); procedure WriteHorAlignment(ARow, ACol: Cardinal; AValue: TsHorAlignment);
procedure WriteNumberFormat(ARow, ACol: Cardinal; ANumberFormat: TsNumberFormat; procedure WriteNumberFormat(ARow, ACol: Cardinal; ANumberFormat: TsNumberFormat;
const AFormatString: String = ''); const AFormatString: String = ''); overload;
procedure WriteNumberFormat(ACell: PCell; ANumberFormat: TsNumberFormat;
const AFormatString: String = ''); overload;
procedure WriteTextRotation(ARow, ACol: Cardinal; ARotation: TsTextRotation); procedure WriteTextRotation(ARow, ACol: Cardinal; ARotation: TsTextRotation);
@ -497,6 +500,7 @@ type
{ Properties } { Properties }
property Cells: TAVLTree read FCells; property Cells: TAVLTree read FCells;
property Cols: TIndexedAVLTree read FCols; property Cols: TIndexedAVLTree read FCols;
property FormatSettings: TFormatSettings read GetFormatSettings;
property Rows: TIndexedAVLTree read FRows; property Rows: TIndexedAVLTree read FRows;
property Workbook: TsWorkbook read FWorkbook; property Workbook: TsWorkbook read FWorkbook;
@ -1314,6 +1318,7 @@ begin
Result^.Col := ACol; Result^.Col := ACol;
Result^.ContentType := cctEmpty; Result^.ContentType := cctEmpty;
Result^.BorderStyles := DEFAULT_BORDERSTYLES; Result^.BorderStyles := DEFAULT_BORDERSTYLES;
Result^.CurrencySymbol := '?';
Cells.Add(Result); Cells.Add(Result);
end; end;
@ -1846,6 +1851,10 @@ begin
ACell^.CurrencySymbol := ACurrencySymbol; ACell^.CurrencySymbol := ACurrencySymbol;
ACell^.NumberFormatStr := BuildNumberFormatString(ACell^.NumberFormat, ACell^.NumberFormatStr := BuildNumberFormatString(ACell^.NumberFormat,
Workbook.FormatSettings, ADecimals, ACurrencySymbol); Workbook.FormatSettings, ADecimals, ACurrencySymbol);
end else begin
Exclude(ACell^.UsedFormattingFields, uffNumberFormat);
ACell^.NumberFormat := nfGeneral;
ACell^.NumberFormatStr := '';
end; end;
ChangedCell(ACell^.Row, ACell^.Col); ChangedCell(ACell^.Row, ACell^.Col);
@ -2037,9 +2046,16 @@ procedure TsWorksheet.WriteNumberFormat(ARow, ACol: Cardinal;
ANumberFormat: TsNumberFormat; const AFormatString: String = ''); ANumberFormat: TsNumberFormat; const AFormatString: String = '');
var var
ACell: PCell; ACell: PCell;
oldNumFmt: TsNumberFormat;
begin begin
ACell := GetCell(ARow, ACol); ACell := GetCell(ARow, ACol);
WriteNumberFormat(ACell, ANumberFormat, AFormatString);
end;
procedure TsWorksheet.WriteNumberFormat(ACell: PCell;
ANumberFormat: TsNumberFormat; const AFormatString: String = '');
begin
if ACell = nil then
exit;
Include(ACell^.UsedFormattingFields, uffNumberFormat); Include(ACell^.UsedFormattingFields, uffNumberFormat);
ACell^.NumberFormat := ANumberFormat; ACell^.NumberFormat := ANumberFormat;
if (AFormatString = '') then if (AFormatString = '') then
@ -2047,7 +2063,7 @@ begin
Workbook.FormatSettings, ACell^.Decimals, ACell^.CurrencySymbol) Workbook.FormatSettings, ACell^.Decimals, ACell^.CurrencySymbol)
else else
ACell^.NumberFormatStr := AFormatString; ACell^.NumberFormatStr := AFormatString;
ChangedCell(ARow, ACol); ChangedCell(ACell^.Row, ACell^.Col);
end; end;
procedure TsWorksheet.WriteRPNFormula(ARow, ACol: Cardinal; procedure TsWorksheet.WriteRPNFormula(ARow, ACol: Cardinal;
@ -2290,6 +2306,11 @@ begin
ChangedCell(ARow, ACol); ChangedCell(ARow, ACol);
end; end;
function TsWorksheet.GetFormatSettings: TFormatSettings;
begin
Result := FWorkbook.FormatSettings;
end;
function TsWorksheet.FindRow(ARow: Cardinal): PRow; function TsWorksheet.FindRow(ARow: Cardinal): PRow;
var var
LElement: TRow; LElement: TRow;

View File

@ -147,6 +147,8 @@ type
procedure EditingDone; override; procedure EditingDone; override;
procedure EndUpdate; procedure EndUpdate;
procedure GetSheets(const ASheets: TStrings); procedure GetSheets(const ASheets: TStrings);
function GetGridCol(ASheetCol: Cardinal): Integer;
function GetGridRow(ASheetRow: Cardinal): Integer;
function GetWorksheetCol(AGridCol: Integer): Cardinal; function GetWorksheetCol(AGridCol: Integer): Cardinal;
function GetWorksheetRow(AGridRow: Integer): Cardinal; function GetWorksheetRow(AGridRow: Integer): Cardinal;
procedure LoadFromSpreadsheetFile(AFileName: string; procedure LoadFromSpreadsheetFile(AFileName: string;
@ -1710,6 +1712,16 @@ begin
Result := false; Result := false;
end; end;
function TsCustomWorksheetGrid.GetGridCol(ASheetCol: Cardinal): Integer;
begin
Result := ASheetCol + FHeaderCount
end;
function TsCustomWorksheetGrid.GetGridRow(ASheetRow: Cardinal): Integer;
begin
Result := ASheetRow + FHeaderCount;
end;
function TsCustomWorksheetGrid.GetHorAlignment(ACol, ARow: Integer): TsHorAlignment; function TsCustomWorksheetGrid.GetHorAlignment(ACol, ARow: Integer): TsHorAlignment;
var var
cell: PCell; cell: PCell;

View File

@ -68,6 +68,7 @@ function UTF8TextToXMLText(AText: ansistring): ansistring;
function IfThen(ACondition: Boolean; AValue1,AValue2: TsNumberFormat): TsNumberFormat; overload; function IfThen(ACondition: Boolean; AValue1,AValue2: TsNumberFormat): TsNumberFormat; overload;
function IsCurrencyFormat(AFormat: TsNumberFormat): Boolean;
function IsDateTimeFormat(AFormat: TsNumberFormat): Boolean; overload; function IsDateTimeFormat(AFormat: TsNumberFormat): Boolean; overload;
function IsDateTimeFormat(AFormatStr: String): Boolean; overload; function IsDateTimeFormat(AFormatStr: String): Boolean; overload;
@ -552,6 +553,13 @@ begin
if ACondition then Result := AValue1 else Result := AValue2; if ACondition then Result := AValue1 else Result := AValue2;
end; end;
{ Checks whether the given number format code is for currency or accounting
i.e. requires currency symbol. }
function IsCurrencyFormat(AFormat: TsNumberFormat): Boolean;
begin
Result := AFormat in [nfCurrency, nfCurrencyRed, nfAccounting, nfAccountingRed];
end;
{ Checks whether the given number format code is for date/times. } { Checks whether the given number format code is for date/times. }
function IsDateTimeFormat(AFormat: TsNumberFormat): Boolean; function IsDateTimeFormat(AFormat: TsNumberFormat): Boolean;
begin begin
@ -691,7 +699,8 @@ begin
cf := AFormatSettings.CurrencyFormat; cf := AFormatSettings.CurrencyFormat;
ncf := AFormatSettings.NegCurrFormat; ncf := AFormatSettings.NegCurrFormat;
if ADecimals < 0 then ADecimals := AFormatSettings.CurrencyDecimals; if ADecimals < 0 then ADecimals := AFormatSettings.CurrencyDecimals;
if ACurrencySymbol = '?' then ACurrencySymbol := AFormatSettings.CurrencyString; if ACurrencySymbol = '?' then
ACurrencySymbol := AnsiToUTF8(AFormatSettings.CurrencyString);
decs := DupeString('0', ADecimals); decs := DupeString('0', ADecimals);
if ADecimals > 0 then decs := '.' + decs; if ADecimals > 0 then decs := '.' + decs;
@ -735,7 +744,8 @@ begin
cf := AFormatSettings.CurrencyFormat; cf := AFormatSettings.CurrencyFormat;
ncf := AFormatSettings.NegCurrFormat; ncf := AFormatSettings.NegCurrFormat;
if ADecimals = -1 then ADecimals := AFormatSettings.CurrencyDecimals; if ADecimals = -1 then ADecimals := AFormatSettings.CurrencyDecimals;
if ACurrencySymbol = '?' then ACurrencySymbol := AFormatSettings.CurrencyString; if ACurrencySymbol = '?' then
ACurrencySymbol := AnsiToUTF8(AFormatSettings.CurrencyString);
decs := DupeString('0', ADecimals); decs := DupeString('0', ADecimals);
if ADecimals > 0 then decs := '.' + decs; if ADecimals > 0 then decs := '.' + decs;
case ANumberFormat of case ANumberFormat of