fpspreadsheet: Rename virtual mode event handler OnNeedCellData to OnWriteCellData. Add another virtual mode event handler for reading, OnReadCellData.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@3370 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2014-07-23 21:51:06 +00:00
parent 37143dad97
commit 7f8e8fb312
6 changed files with 35 additions and 34 deletions

View File

@ -37,11 +37,11 @@ type
FCurFormat: TsSpreadsheetFormat; FCurFormat: TsSpreadsheetFormat;
procedure EnableControls(AEnable: Boolean); procedure EnableControls(AEnable: Boolean);
function GetRowCount(AIndex: Integer): Integer; function GetRowCount(AIndex: Integer): Integer;
procedure NeedCellString(Sender: TObject; ARow, ACol: Cardinal; procedure WriteCellStringHandler(Sender: TObject; ARow, ACol: Cardinal;
var AValue: Variant; var AStyleCell: PCell); var AValue: Variant; var AStyleCell: PCell);
procedure NeedCellNumber(Sender: TObject; ARow, ACol: Cardinal; procedure WriteCellNumberHandler(Sender: TObject; ARow, ACol: Cardinal;
var AValue: Variant; var AStyleCell: PCell); var AValue: Variant; var AStyleCell: PCell);
procedure NeedCellStringAndNumber(Sender: TObject; ARow, ACol: Cardinal; procedure WriteCellStringAndNumberHandler(Sender: TObject; ARow, ACol: Cardinal;
var AValue: Variant; var AStyleCell: PCell); var AValue: Variant; var AStyleCell: PCell);
procedure ReadFromIni; procedure ReadFromIni;
procedure WriteToIni; procedure WriteToIni;
@ -87,7 +87,7 @@ const
{ TForm1 } { TForm1 }
procedure TForm1.NeedCellString(Sender: TObject; ARow, ACol: cardinal; procedure TForm1.WriteCellStringHandler(Sender: TObject; ARow, ACol: cardinal;
var AValue: variant; var AStyleCell: PCell); var AValue: variant; var AStyleCell: PCell);
var var
S: string; S: string;
@ -98,7 +98,7 @@ begin
StatusMsg(Format('Writing %s row %d...', [GetFileFormatName(FCurFormat), ARow])); StatusMsg(Format('Writing %s row %d...', [GetFileFormatName(FCurFormat), ARow]));
end; end;
procedure TForm1.NeedCellNumber(Sender: TObject; ARow, ACol: cardinal; procedure TForm1.WriteCellNumberHandler(Sender: TObject; ARow, ACol: cardinal;
var AValue: variant; var AStyleCell: PCell); var AValue: variant; var AStyleCell: PCell);
begin begin
AValue := ARow * 1E5 + ACol; AValue := ARow * 1E5 + ACol;
@ -106,13 +106,13 @@ begin
StatusMsg(Format('Writing %s row %d...', [GetFileFormatName(FCurFormat), ARow])); StatusMsg(Format('Writing %s row %d...', [GetFileFormatName(FCurFormat), ARow]));
end; end;
procedure TForm1.NeedCellStringAndNumber(Sender: TObject; ARow, ACol: cardinal; procedure TForm1.WriteCellStringAndNumberHandler(Sender: TObject; ARow, ACol: cardinal;
var AValue: variant; var AStyleCell: PCell); var AValue: variant; var AStyleCell: PCell);
begin begin
if odd(ARow + ACol) then if odd(ARow + ACol) then
NeedCellString(Sender, ARow, ACol, AValue, AStyleCell) WriteCellStringHandler(Sender, ARow, ACol, AValue, AStyleCell)
else else
NeedCellNumber(Sender, ARow, ACol, AValue, AStyleCell); WriteCellNumberHandler(Sender, ARow, ACol, AValue, AStyleCell);
end; end;
procedure TForm1.RunReadTest(Idx: Integer; Log: String; procedure TForm1.RunReadTest(Idx: Integer; Log: String;
@ -211,9 +211,9 @@ begin
MyWorkbook.VirtualRowCount := Rows; MyWorkbook.VirtualRowCount := Rows;
MyWorkbook.VirtualColCount := COLCOUNT; MyWorkbook.VirtualColCount := COLCOUNT;
case RgContent.ItemIndex of case RgContent.ItemIndex of
0: MyWorkbook.OnNeedCellData := @NeedCellString; 0: MyWorkbook.OnWriteCellData := @WriteCellStringHandler;
1: MyWorkbook.OnNeedCellData := @NeedCellNumber; 1: MyWorkbook.OnWriteCellData := @WriteCellNumberHandler;
2: MyWorkbook.OnNeedCellData := @NeedCellStringAndNumber; 2: MyWorkbook.OnWriteCellData := @WriteCellStringAndNumberHandler;
end; end;
end end
else else

View File

@ -14,7 +14,7 @@ uses
type type
TDataProvider = class TDataProvider = class
procedure NeedCellData(Sender: TObject; ARow,ACol: Cardinal; procedure WriteCellDataHandler(Sender: TObject; ARow,ACol: Cardinal;
var AData: variant; var AStyleCell: PCell); var AData: variant; var AStyleCell: PCell);
end; end;
@ -25,8 +25,8 @@ var
headerTemplate: PCell; headerTemplate: PCell;
t: TTime; t: TTime;
procedure TDataProvider.NeedCellData(Sender: TObject; ARow, ACol: Cardinal; procedure TDataProvider.WriteCellDataHandler(Sender: TObject;
var AData: variant; var AStyleCell: PCell); ARow, ACol: Cardinal; var AData: variant; var AStyleCell: PCell);
{ This is just a sample using random data. Normally, in case of a database, { This is just a sample using random data. Normally, in case of a database,
you would read a record and return its field values, such as: you would read a record and return its field values, such as:
@ -81,7 +81,7 @@ begin
{ The event handler for OnNeedCellData links the workbook to the method { The event handler for OnNeedCellData links the workbook to the method
from which it gets the data to be written. } from which it gets the data to be written. }
workbook.OnNeedCellData := @dataprovider.NeedCellData; workbook.OnWriteCellData := @dataprovider.WriteCellDataHandler;
{ If we want to change the format of some cells we have to provide this { If we want to change the format of some cells we have to provide this
format in template cells of the worksheet. In the example, the first format in template cells of the worksheet. In the example, the first

View File

@ -706,7 +706,9 @@ type
@param boVirtualMode If in virtual mode date are not taken from cells @param boVirtualMode If in virtual mode date are not taken from cells
when a spreadsheet is written to file, but are when a spreadsheet is written to file, but are
provided by means of the event OnNeedCellData. provided by means of the event OnWriteCellData.
Similarly, when data are read they are not added as
cells but passed the the event OnReadCellData;
@param boBufStream When this option is set a buffered stream is used @param boBufStream When this option is set a buffered stream is used
for writing (a memory stream swapping to disk) or for writing (a memory stream swapping to disk) or
reading (a file stream pre-reading chunks of data reading (a file stream pre-reading chunks of data
@ -719,14 +721,15 @@ type
{@@ {@@
Event fired when writing a file in virtual mode. The event handler has to Event fired when writing a file in virtual mode. The event handler has to
pass data ("AValue") and formatting ("AStyleCell") to the writer } pass data ("AValue") and formatting style to be copied from a template
TsWorkbookNeedCellDataEvent = procedure(Sender: TObject; ARow, ACol: Cardinal; cell ("AStyleCell") to the writer }
TsWorkbookWriteCellDataEvent = procedure(Sender: TObject; ARow, ACol: Cardinal;
var AValue: variant; var AStyleCell: PCell) of object; var AValue: variant; var AStyleCell: PCell) of object;
{@@ {@@
Event fired when reading a file in virtual mode. The event handler has to Event fired when reading a file in virtual mode. Read data are provided in
process the data provided by the read in the "ADataCell". } the "ADataCell" (which is not added to the worksheet in virtual mode). }
TsWorkbookHaveCellDataEvent = procedure(Sender: TObject; ARow, ACol: Cardinal; TsWorkbookReadCellDataEvent = procedure(Sender: TObject; ARow, ACol: Cardinal;
const ADataCell: PCell) of object; const ADataCell: PCell) of object;
{@@ {@@
@ -749,8 +752,8 @@ type
FVirtualRowCount: Cardinal; FVirtualRowCount: Cardinal;
FWriting: Boolean; FWriting: Boolean;
FOptions: TsWorkbookOptions; FOptions: TsWorkbookOptions;
FOnNeedCellData: TsWorkbookNeedCellDataEvent; FOnWriteCellData: TsWorkbookWriteCellDataEvent;
FOnHaveCellData: TsWorkbookHaveCellDataEvent; FOnReadCellData: TsWorkbookReadCellDataEvent;
FFileName: String; FFileName: String;
{ Setter/Getter } { Setter/Getter }
@ -843,11 +846,11 @@ type
{@@ This event allows to provide external cell data for writing to file, {@@ This event allows to provide external cell data for writing to file,
standard cells are ignored. Intended for converting large database files standard cells are ignored. Intended for converting large database files
to a spreadsheet format. Requires Option boVirtualMode to be set. } to a spreadsheet format. Requires Option boVirtualMode to be set. }
property OnNeedCellData: TsWorkbookNeedCellDataEvent read FOnNeedCellData write FOnNeedCellData; property OnWriteCellData: TsWorkbookWriteCellDataEvent read FOnWriteCellData write FOnWriteCellData;
{@@ This event accepts cell data while reading a spreadsheet file. Data are {@@ This event accepts cell data while reading a spreadsheet file. Data are
not encorporated in a spreadsheet, they are just passed through to the not encorporated in a spreadsheet, they are just passed through to the
event handler for processing. Requires Optio boVirtualMode to be set. } event handler for processing. Requires option boVirtualMode to be set. }
property OnHaveCellData: TsWorkbookHaveCellDataEvent read FOnHaveCellData write FOnHaveCellData; property OnReadCellData: TsWorkbookReadCellDataEvent read FOnReadCellData write FOnReadCellData;
end; end;
{@@ Contents of a number format record } {@@ Contents of a number format record }

View File

@ -28,7 +28,7 @@ type
TSpreadInternalTests= class(TTestCase) TSpreadInternalTests= class(TTestCase)
private private
procedure NeedVirtualCellData(Sender: TObject; ARow, ACol: Cardinal; procedure WriteVirtualCellDataHandler(Sender: TObject; ARow, ACol: Cardinal;
var AValue:Variant; var AStyleCell: PCell); var AValue:Variant; var AStyleCell: PCell);
protected protected
// Set up expected values: // Set up expected values:
@ -373,7 +373,7 @@ begin
end; end;
procedure TSpreadInternalTests.NeedVirtualCellData(Sender: TObject; procedure TSpreadInternalTests.WriteVirtualCellDataHandler(Sender: TObject;
ARow, ACol: Cardinal; var AValue:Variant; var AStyleCell: PCell); ARow, ACol: Cardinal; var AValue:Variant; var AStyleCell: PCell);
begin begin
// First read the SollNumbers, then the first 4 SollStrings // First read the SollNumbers, then the first 4 SollStrings
@ -403,7 +403,7 @@ begin
workbook.VirtualColCount := 1; workbook.VirtualColCount := 1;
workbook.VirtualRowCount := Length(SollNumbers) + 4; workbook.VirtualRowCount := Length(SollNumbers) + 4;
// We'll use only the first 4 SollStrings, the others cause trouble due to utf8 and formatting. // We'll use only the first 4 SollStrings, the others cause trouble due to utf8 and formatting.
workbook.OnNeedCellData := @NeedVirtualCellData; workbook.OnWriteCellData := @WriteVirtualCellDataHandler;
tempFile:=NewTempFile; tempFile:=NewTempFile;
workbook.WriteToFile(tempfile, AFormat, true); workbook.WriteToFile(tempfile, AFormat, true);
finally finally

View File

@ -2587,10 +2587,9 @@ begin
for r := 0 to Workbook.VirtualRowCount-1 do begin for r := 0 to Workbook.VirtualRowCount-1 do begin
for c := 0 to Workbook.VirtualColCount-1 do begin for c := 0 to Workbook.VirtualColCount-1 do begin
InitCell(lCell); InitCell(lCell);
// FillChar(lCell, SizeOf(lCell), 0);
value := varNull; value := varNull;
styleCell := nil; styleCell := nil;
Workbook.OnNeedCellData(Workbook, r, c, value, styleCell); Workbook.OnWriteCellData(Workbook, r, c, value, styleCell);
if styleCell <> nil then lCell := styleCell^; if styleCell <> nil then lCell := styleCell^;
lCell.Row := r; lCell.Row := r;
lCell.Col := c; lCell.Col := c;

View File

@ -902,7 +902,7 @@ begin
AppendToStream(FSSheets[FCurSheetNum], AppendToStream(FSSheets[FCurSheetNum],
'<sheetData>'); '<sheetData>');
if (boVirtualMode in Workbook.Options) and Assigned(Workbook.OnNeedCellData) if (boVirtualMode in Workbook.Options) and Assigned(Workbook.OnWriteCellData)
then begin then begin
for r := 0 to Workbook.VirtualRowCount-1 do begin for r := 0 to Workbook.VirtualRowCount-1 do begin
row := CurSheet.FindRow(r); row := CurSheet.FindRow(r);
@ -914,12 +914,11 @@ begin
AppendToStream(FSSheets[FCurSheetNum], Format( AppendToStream(FSSheets[FCurSheetNum], Format(
'<row r="%d" spans="1:%d"%s>', [r+1, Workbook.VirtualColCount, rh])); '<row r="%d" spans="1:%d"%s>', [r+1, Workbook.VirtualColCount, rh]));
for c := 0 to Workbook.VirtualColCount-1 do begin for c := 0 to Workbook.VirtualColCount-1 do begin
//FillChar(lCell, SizeOf(lCell), 0);
InitCell(lCell); InitCell(lCell);
CellPosText := CurSheet.CellPosToText(r, c); CellPosText := CurSheet.CellPosToText(r, c);
value := varNull; value := varNull;
styleCell := nil; styleCell := nil;
Workbook.OnNeedCellData(Workbook, r, c, value, styleCell); Workbook.OnWriteCellData(Workbook, r, c, value, styleCell);
if styleCell <> nil then if styleCell <> nil then
lCell := styleCell^; lCell := styleCell^;
lCell.Row := r; lCell.Row := r;