fpspreadsheet: Add method LoadFromWorkbook to WorksheetGrid and WorkbookSource. Remove "deprecated" from some overloaded workbook file access methods.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@4448 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz 2016-01-21 10:55:55 +00:00
parent 7940866bb5
commit 7d731629e1
5 changed files with 94 additions and 13 deletions

View File

@ -35,6 +35,8 @@ object MainForm: TMainForm
Align = alClient Align = alClient
AutoAdvance = aaDown AutoAdvance = aaDown
ColCount = 27 ColCount = 27
DefaultColWidth = 64
DefaultRowHeight = 22
Font.Color = clBlack Font.Color = clBlack
Font.Height = -13 Font.Height = -13
Font.Name = 'Arial' Font.Name = 'Arial'
@ -81,7 +83,7 @@ object MainForm: TMainForm
Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goColSizing, goAlwaysShowEditor, goThumbTracking, goCellHints, goTruncCellHints] Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goColSizing, goAlwaysShowEditor, goThumbTracking, goCellHints, goTruncCellHints]
Strings.Strings = ( Strings.Strings = (
'FileName=' 'FileName='
'FileFormat=-1 []' 'FileFormat=(unknown)'
'ActiveWorksheet=Sheet1' 'ActiveWorksheet=Sheet1'
'Options=boAutoCalc, boCalcBeforeSaving, boReadFormulas' 'Options=boAutoCalc, boCalcBeforeSaving, boReadFormulas'
'(-) FormatSettings=' '(-) FormatSettings='

View File

@ -416,16 +416,24 @@ end;
{ Loads the spreadsheet file selected by the AcFileOpen action } { Loads the spreadsheet file selected by the AcFileOpen action }
procedure TMainForm.AcFileOpenAccept(Sender: TObject); procedure TMainForm.AcFileOpenAccept(Sender: TObject);
var
crs: TCursor;
begin begin
WorkbookSource.AutodetectFormat := false; crs := Screen.Cursor;
case AcFileOpen.Dialog.FilterIndex of Screen.Cursor := crHourglass;
1: WorkbookSource.AutoDetectFormat := true; // All spreadsheet files try
2: WorkbookSource.AutoDetectFormat := true; // All Excel files WorkbookSource.AutodetectFormat := false;
else WorkbookSource.FileFormatID := FOpenFormats[AcFileOpen.Dialog.FilterIndex - 3]; case AcFileOpen.Dialog.FilterIndex of
1: WorkbookSource.AutoDetectFormat := true; // All spreadsheet files
2: WorkbookSource.AutoDetectFormat := true; // All Excel files
else WorkbookSource.FileFormatID := FOpenFormats[AcFileOpen.Dialog.FilterIndex - 3];
// -3 because FilterIndex is 1-based and there are 2 add'l items at the top. // -3 because FilterIndex is 1-based and there are 2 add'l items at the top.
end;
WorkbookSource.FileName := UTF8ToAnsi(AcFileOpen.Dialog.FileName); // this loads the file
UpdateCaption;
finally
Screen.Cursor := crs;
end; end;
WorkbookSource.FileName := UTF8ToAnsi(AcFileOpen.Dialog.FileName); // this loads the file
UpdateCaption;
end; end;
{ Saves the spreadsheet to the file selected by the AcFileSaveAs action } { Saves the spreadsheet to the file selected by the AcFileSaveAs action }

View File

@ -657,7 +657,7 @@ type
procedure ReadFromFile(AFileName: string; AFormatID: TsSpreadFormatID; procedure ReadFromFile(AFileName: string; AFormatID: TsSpreadFormatID;
AParams: TsStreamParams = []); overload; AParams: TsStreamParams = []); overload;
procedure ReadFromFile(AFileName: string; AFormat: TsSpreadsheetFormat; procedure ReadFromFile(AFileName: string; AFormat: TsSpreadsheetFormat;
AParams: TsStreamParams = []); overload; deprecated 'Use TsSpreadFormatID instead of TsSpreadsheetFormat'; AParams: TsStreamParams = []); overload;
procedure ReadFromFile(AFileName: string; procedure ReadFromFile(AFileName: string;
AParams: TsStreamParams = []); overload; AParams: TsStreamParams = []); overload;
procedure ReadFromFileIgnoringExtension(AFileName: string; procedure ReadFromFileIgnoringExtension(AFileName: string;
@ -665,18 +665,18 @@ type
procedure ReadFromStream(AStream: TStream; AFormatID: TsSpreadFormatID; procedure ReadFromStream(AStream: TStream; AFormatID: TsSpreadFormatID;
AParams: TsStreamParams = []); overload; AParams: TsStreamParams = []); overload;
procedure ReadFromStream(AStream: TStream; AFormat: TsSpreadsheetFormat; procedure ReadFromStream(AStream: TStream; AFormat: TsSpreadsheetFormat;
AParams: TsStreamParams = []); overload; deprecated 'Use TsSpreadFormatID instead of TsSpreadsheetFormat'; AParams: TsStreamParams = []); overload;
procedure WriteToFile(const AFileName: string; const AFormatID: TsSpreadFormatID; procedure WriteToFile(const AFileName: string; const AFormatID: TsSpreadFormatID;
const AOverwriteExisting: Boolean = False; AParams: TsStreamParams = []); overload; const AOverwriteExisting: Boolean = False; AParams: TsStreamParams = []); overload;
procedure WriteToFile(const AFileName: string; const AFormat: TsSpreadsheetFormat; procedure WriteToFile(const AFileName: string; const AFormat: TsSpreadsheetFormat;
const AOverwriteExisting: Boolean = False; AParams: TsStreamParams = []); overload; deprecated 'Use TsSpreadFormatID instead of TsSpreadsheetFormat'; const AOverwriteExisting: Boolean = False; AParams: TsStreamParams = []); overload;
procedure WriteToFile(const AFileName: String; procedure WriteToFile(const AFileName: String;
const AOverwriteExisting: Boolean = False; AParams: TsStreamParams = []); overload; const AOverwriteExisting: Boolean = False; AParams: TsStreamParams = []); overload;
procedure WriteToStream(AStream: TStream; AFormatID: TsSpreadFormatID; procedure WriteToStream(AStream: TStream; AFormatID: TsSpreadFormatID;
AParams: TsStreamParams = []); overload; AParams: TsStreamParams = []); overload;
procedure WriteToStream(AStream: TStream; AFormat: TsSpreadsheetFormat; procedure WriteToStream(AStream: TStream; AFormat: TsSpreadsheetFormat;
AParams: TsStreamParams = []); overload; deprecated 'Use TsSpreadFormatID instead of TsSpreadsheetFormat'; AParams: TsStreamParams = []); overload;
{ Worksheet list handling methods } { Worksheet list handling methods }
function AddWorksheet(AName: string; function AddWorksheet(AName: string;

View File

@ -94,6 +94,8 @@ type
procedure InternalCreateNewWorkbook(AWorkbook: TsWorkbook = nil); procedure InternalCreateNewWorkbook(AWorkbook: TsWorkbook = nil);
procedure InternalLoadFromFile(AFileName: string; AAutoDetect: Boolean; procedure InternalLoadFromFile(AFileName: string; AAutoDetect: Boolean;
AFormatID: TsSpreadFormatID; AWorksheetIndex: Integer = -1); AFormatID: TsSpreadFormatID; AWorksheetIndex: Integer = -1);
procedure InternalLoadFromWorkbook(AWorkbook: TsWorkbook;
AWorksheetIndex: Integer = -1);
procedure Loaded; override; procedure Loaded; override;
public public
@ -112,6 +114,7 @@ type
AFormat: TsSpreadsheetFormat; AWorksheetIndex: Integer = -1); overload; AFormat: TsSpreadsheetFormat; AWorksheetIndex: Integer = -1); overload;
procedure LoadFromSpreadsheetFile(AFileName: string; procedure LoadFromSpreadsheetFile(AFileName: string;
AFormatID: TsSpreadFormatID = sfidUnknown; AWorksheetIndex: Integer = -1); overload; AFormatID: TsSpreadFormatID = sfidUnknown; AWorksheetIndex: Integer = -1); overload;
procedure LoadFromWorkbook(AWorkbook: TsWorkbook; AWorksheetIndex: Integer = -1);
{ {
procedure LoadFromSpreadsheetFile(AFileName: string; procedure LoadFromSpreadsheetFile(AFileName: string;
AWorksheetIndex: Integer = -1); overload; AWorksheetIndex: Integer = -1); overload;
@ -937,8 +940,26 @@ begin
DoShowError(FWorkbook.ErrorMsg); DoShowError(FWorkbook.ErrorMsg);
end; end;
} }
var
book: TsWorkbook;
begin begin
book := TsWorkbook.Create;
try
if AAutoDetect then
book.ReadfromFile(AFileName)
else
book.ReadFromFile(AFileName, AFormatID);
except
book.AddErrorMsg(rsCannotReadFile, [AFileName]);
// Code executed subsequently will be a pain if there is no worksheet!
if book.GetWorksheetCount = 0 then
book.AddWorksheet(Format(rsDefaultSheetName, [1]));
end;
InternalLoadFromWorkbook(book, AWorksheetIndex);
(*
// Create a new empty workbook // Create a new empty workbook
InternalCreateNewWorkbook; InternalCreateNewWorkbook;
@ -968,6 +989,26 @@ begin
end; end;
SelectWorksheet(FWorkbook.GetWorkSheetByIndex(AWorksheetIndex)); SelectWorksheet(FWorkbook.GetWorkSheetByIndex(AWorksheetIndex));
// If required, display loading error message
if FWorkbook.ErrorMsg <> '' then
DoShowError(FWorkbook.ErrorMsg);
*)
end;
procedure TsWorkbookSource.InternalLoadFromWorkbook(AWorkbook: TsWorkbook;
AWorksheetIndex: Integer = -1);
begin
InternalCreateNewWorkbook(AWorkbook);
WorkbookOpenedHandler(self);
if AWorksheetIndex = -1 then
begin
if FWorkbook.ActiveWorksheet <> nil then
AWorksheetIndex := FWorkbook.GetWorksheetIndex(FWorkbook.ActiveWorksheet) else
AWorksheetIndex := 0;
end;
SelectWorksheet(FWorkbook.GetWorksheetByIndex(AWorksheetIndex));
// If required, display loading error message // If required, display loading error message
if FWorkbook.ErrorMsg <> '' then if FWorkbook.ErrorMsg <> '' then
DoShowError(FWorkbook.ErrorMsg); DoShowError(FWorkbook.ErrorMsg);
@ -1047,6 +1088,18 @@ begin
InternalLoadFromFile(AFileName, true, sfNotNeeded, AWorksheetIndex); InternalLoadFromFile(AFileName, true, sfNotNeeded, AWorksheetIndex);
end; *) end; *)
{@@ ----------------------------------------------------------------------------
Uses an already existing workbook in the visual controls.
IMPORTANT: THE CALLING ROUTINE MUST NOT DESTROY THE WORKBOOK, it is destroyed
here by the TsWorkbookSource.
-------------------------------------------------------------------------------}
procedure TsWorkbookSource.LoadFromWorkbook(AWorkbook: TsWorkbook;
AWorksheetIndex: Integer = -1);
begin
InternalLoadFromWorkbook(AWorkbook, AWorksheetIndex);
end;
{@@ ---------------------------------------------------------------------------- {@@ ----------------------------------------------------------------------------
Notifies listeners of workbook, worksheet, cell, or selection changes. Notifies listeners of workbook, worksheet, cell, or selection changes.
The changed item is identified by the parameter AChangedItems. The changed item is identified by the parameter AChangedItems.

View File

@ -276,6 +276,7 @@ type
AFormatID: TsSpreadFormatID; AWorksheetIndex: Integer = -1); overload; AFormatID: TsSpreadFormatID; AWorksheetIndex: Integer = -1); overload;
procedure LoadFromSpreadsheetFile(AFileName: string; procedure LoadFromSpreadsheetFile(AFileName: string;
AWorksheetIndex: Integer = -1); overload; AWorksheetIndex: Integer = -1); overload;
procedure LoadFromWorkbook(AWorkbook: TsWorkbook; AWorksheetIndex: Integer = -1);
procedure NewWorkbook(AColCount, ARowCount: Integer); procedure NewWorkbook(AColCount, ARowCount: Integer);
procedure SaveToSpreadsheetFile(AFileName: string; procedure SaveToSpreadsheetFile(AFileName: string;
AOverwriteExisting: Boolean = true); overload; AOverwriteExisting: Boolean = true); overload;
@ -3709,6 +3710,23 @@ begin
GetWorkbookSource.LoadFromSpreadsheetFile(AFileName, AWorksheetIndex); GetWorkbookSource.LoadFromSpreadsheetFile(AFileName, AWorksheetIndex);
end; end;
{@@ ----------------------------------------------------------------------------
Loads an existing workbook into the grid.
@param AWorkbook Workbook that had been created/loaded before.
@param AWorksheetIndex Index of the worksheet to be shown in the grid
(If empty then the active worksheet is loaded)
@Note THE CALLING PROCEDURE MUST NOT DESTROY THE WORKBOOK! The workbook will
be destroyed by the workbook source.
-------------------------------------------------------------------------------}
procedure TsCustomWorksheetGrid.LoadFromWorkbook(AWorkbook: TsWorkbook;
AWorksheetIndex: Integer = -1);
begin
GetWorkbookSource.LoadFromWorkbook(AWorkbook, AWorksheetIndex);
Invalidate;
end;
{@@ ---------------------------------------------------------------------------- {@@ ----------------------------------------------------------------------------
Notification message received from the WorkbookLink telling which item of the Notification message received from the WorkbookLink telling which item of the
spreadsheet has changed. spreadsheet has changed.