2014-09-14 22:53:45 +00:00
|
|
|
unit mainfrm;
|
|
|
|
|
|
|
|
{$mode objfpc}{$H+}
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
|
|
|
|
ComCtrls, StdCtrls, Grids,
|
2015-01-23 21:54:23 +00:00
|
|
|
fpspreadsheet, fpspreadsheetgrid, {%H-}fpsallformats;
|
2014-09-14 22:53:45 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
|
|
|
|
{ TForm1 }
|
|
|
|
|
|
|
|
TForm1 = class(TForm)
|
|
|
|
BtnNew: TButton;
|
|
|
|
BtnLoad: TButton;
|
|
|
|
BtnSave: TButton;
|
|
|
|
ButtonPanel: TPanel;
|
|
|
|
OpenDialog: TOpenDialog;
|
|
|
|
SaveDialog: TSaveDialog;
|
|
|
|
TabControl: TTabControl;
|
|
|
|
procedure BtnLoadClick(Sender: TObject);
|
|
|
|
procedure BtnNewClick(Sender: TObject);
|
|
|
|
procedure BtnSaveClick(Sender: TObject);
|
|
|
|
procedure FormCreate(Sender: TObject);
|
|
|
|
procedure TabControlChange(Sender: TObject);
|
|
|
|
private
|
|
|
|
{ private declarations }
|
|
|
|
Grid: TsWorksheetGrid;
|
|
|
|
procedure LoadFile(const AFileName: String);
|
|
|
|
public
|
|
|
|
{ public declarations }
|
|
|
|
end;
|
|
|
|
|
|
|
|
var
|
|
|
|
Form1: TForm1;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
{$R *.lfm}
|
|
|
|
|
|
|
|
{ TForm1 }
|
|
|
|
|
|
|
|
procedure TForm1.FormCreate(Sender: TObject);
|
|
|
|
begin
|
|
|
|
Grid := TsWorksheetGrid.Create(self);
|
|
|
|
|
2014-09-15 07:33:55 +00:00
|
|
|
// Put the grid into the TabControl and align it to fill the tabcontrol.
|
2014-09-14 22:53:45 +00:00
|
|
|
Grid.Parent := TabControl;
|
|
|
|
Grid.Align := alClient;
|
|
|
|
|
|
|
|
// Useful options
|
2014-09-15 07:33:55 +00:00
|
|
|
Grid.Options := Grid.Options + [goColSizing, goRowSizing,
|
2014-10-03 16:09:54 +00:00
|
|
|
goFixedColSizing, // useful if the spreadsheet contains frozen columns
|
|
|
|
goEditing, // needed for modifying cell content
|
|
|
|
goThumbTracking, // see the grid scroll while you drag the scrollbar
|
|
|
|
goHeaderHotTracking, // hot-tracking of header cells
|
|
|
|
goHeaderPushedLook, // click at header cells --> pushed look
|
|
|
|
goDblClickAutoSize // optimum col width/row height after dbl click at header border
|
2014-09-15 07:33:55 +00:00
|
|
|
];
|
|
|
|
Grid.AutoAdvance := aaDown; // move active cell down on ENTER
|
|
|
|
Grid.MouseWheelOption := mwGrid; // mouse wheel scrolls the grid, not the active cell
|
|
|
|
Grid.TextOverflow := true; // too long text extends into neighbor cells
|
2014-09-14 22:53:45 +00:00
|
|
|
|
|
|
|
// Create an empty worksheet
|
|
|
|
Grid.NewWorkbook(26, 100);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TForm1.BtnLoadClick(Sender: TObject);
|
|
|
|
begin
|
2014-10-03 16:09:54 +00:00
|
|
|
if OpenDialog.FileName <> '' then
|
|
|
|
begin
|
2014-09-14 22:53:45 +00:00
|
|
|
OpenDialog.InitialDir := ExtractFileDir(OpenDialog.FileName);
|
|
|
|
OpenDialog.FileName := ChangeFileExt(ExtractFileName(OpenDialog.FileName), '');
|
|
|
|
end;
|
2014-10-03 16:09:54 +00:00
|
|
|
if OpenDialog.Execute then
|
|
|
|
begin
|
2014-09-14 22:53:45 +00:00
|
|
|
LoadFile(OpenDialog.FileName);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TForm1.BtnNewClick(Sender: TObject);
|
|
|
|
begin
|
|
|
|
TabControl.Tabs.Clear;
|
2014-11-21 15:47:54 +00:00
|
|
|
TabControl.Tabs.Add('Sheet1');
|
2014-09-14 22:53:45 +00:00
|
|
|
Grid.NewWorkbook(26, 100);
|
|
|
|
end;
|
|
|
|
|
|
|
|
// Saves sheet in grid to file, overwriting existing file
|
|
|
|
procedure TForm1.BtnSaveClick(Sender: TObject);
|
|
|
|
var
|
|
|
|
err: String;
|
|
|
|
begin
|
|
|
|
if Grid.Workbook = nil then
|
|
|
|
exit;
|
|
|
|
|
2014-10-03 16:09:54 +00:00
|
|
|
if Grid.Workbook.Filename <>'' then
|
|
|
|
begin
|
2014-09-14 22:53:45 +00:00
|
|
|
SaveDialog.InitialDir := ExtractFileDir(Grid.Workbook.FileName);
|
|
|
|
SaveDialog.FileName := ChangeFileExt(ExtractFileName(Grid.Workbook.FileName), '');
|
|
|
|
end;
|
|
|
|
|
|
|
|
if SaveDialog.Execute then
|
|
|
|
begin
|
|
|
|
Screen.Cursor := crHourglass;
|
|
|
|
try
|
|
|
|
Grid.SaveToSpreadsheetFile(SaveDialog.FileName);
|
|
|
|
finally
|
|
|
|
Screen.Cursor := crDefault;
|
|
|
|
// Show a message in case of error(s)
|
|
|
|
err := Grid.Workbook.ErrorMsg;
|
|
|
|
if err <> '' then
|
|
|
|
MessageDlg(err, mtError, [mbOK], 0);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
// Loads first worksheet from file into grid
|
|
|
|
procedure TForm1.LoadFile(const AFileName: String);
|
|
|
|
var
|
|
|
|
err: String;
|
|
|
|
begin
|
|
|
|
// Load file
|
|
|
|
Screen.Cursor := crHourglass;
|
|
|
|
try
|
|
|
|
try
|
|
|
|
// Load file into workbook and grid
|
|
|
|
Grid.LoadFromSpreadsheetFile(UTF8ToSys(AFileName));
|
|
|
|
|
|
|
|
// Update user interface
|
|
|
|
Caption := Format('fpsGrid - %s (%s)', [
|
|
|
|
AFilename,
|
|
|
|
GetFileFormatName(Grid.Workbook.FileFormat)
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Collect the sheet names in the Tabs of the TabControl for switching sheets.
|
2014-11-21 15:47:54 +00:00
|
|
|
Grid.GetSheets(TabControl.Tabs);
|
|
|
|
TabControl.TabIndex := 0;
|
2014-09-14 22:53:45 +00:00
|
|
|
except
|
|
|
|
on E:Exception do begin
|
|
|
|
// Empty worksheet instead of the loaded one
|
|
|
|
Grid.NewWorkbook(26, 100);
|
|
|
|
Caption := 'fpsGrid - no name';
|
|
|
|
TabControl.Tabs.Clear;
|
|
|
|
// Grab the error message
|
|
|
|
Grid.Workbook.AddErrorMsg(E.Message);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
finally
|
|
|
|
Screen.Cursor := crDefault;
|
|
|
|
|
|
|
|
// Show a message in case of error(s)
|
|
|
|
err := Grid.Workbook.ErrorMsg;
|
|
|
|
if err <> '' then
|
|
|
|
MessageDlg(err, mtError, [mbOK], 0);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TForm1.TabControlChange(Sender: TObject);
|
|
|
|
begin
|
|
|
|
Grid.SelectSheetByIndex(TabControl.TabIndex);
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
end.
|
|
|
|
|