2009-10-06 19:25:18 +00:00
|
|
|
unit mainform;
|
|
|
|
|
|
|
|
{$mode objfpc}{$H+}
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
|
2014-04-19 19:29:13 +00:00
|
|
|
StdCtrls, Menus, ExtCtrls, ComCtrls, ActnList,
|
2013-12-28 15:29:22 +00:00
|
|
|
fpspreadsheetgrid, fpspreadsheet, fpsallformats;
|
2009-10-06 19:25:18 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
|
|
|
|
{ TForm1 }
|
|
|
|
|
|
|
|
TForm1 = class(TForm)
|
2014-04-19 19:29:13 +00:00
|
|
|
AcOpen: TAction;
|
|
|
|
AcSaveAs: TAction;
|
|
|
|
AcQuit: TAction;
|
|
|
|
ActionList1: TActionList;
|
|
|
|
btnPopulateGrid: TButton;
|
|
|
|
CbDisplayFixedColRow: TCheckBox;
|
|
|
|
CbDisplayGrid: TCheckBox;
|
|
|
|
ImageList1: TImageList;
|
2013-12-28 15:29:22 +00:00
|
|
|
MainMenu1: TMainMenu;
|
2014-04-19 19:29:13 +00:00
|
|
|
MenuItem1: TMenuItem;
|
2013-12-28 15:29:22 +00:00
|
|
|
mnuFile: TMenuItem;
|
|
|
|
mnuOpen: TMenuItem;
|
|
|
|
mnuQuit: TMenuItem;
|
|
|
|
mnuSaveAs: TMenuItem;
|
|
|
|
OpenDialog1: TOpenDialog;
|
2014-04-19 19:29:13 +00:00
|
|
|
PageControl1: TPageControl;
|
|
|
|
Panel1: TPanel;
|
2013-12-28 15:29:22 +00:00
|
|
|
SaveDialog1: TSaveDialog;
|
2009-10-06 19:25:18 +00:00
|
|
|
sWorksheetGrid1: TsWorksheetGrid;
|
2014-04-19 19:29:13 +00:00
|
|
|
TabSheet1: TTabSheet;
|
|
|
|
ToolBar1: TToolBar;
|
|
|
|
ToolButton1: TToolButton;
|
|
|
|
ToolButton2: TToolButton;
|
|
|
|
ToolButton3: TToolButton;
|
|
|
|
ToolButton5: TToolButton;
|
|
|
|
procedure btnPopulateGridClick(Sender: TObject);
|
|
|
|
procedure CbDisplayFixedColRowClick(Sender: TObject);
|
|
|
|
procedure CbDisplayGridClick(Sender: TObject);
|
|
|
|
procedure acOpenExecute(Sender: TObject);
|
|
|
|
procedure acQuitExecute(Sender: TObject);
|
|
|
|
procedure acSaveAsExecute(Sender: TObject);
|
2014-04-29 21:58:48 +00:00
|
|
|
procedure FormActivate(Sender: TObject);
|
2014-04-19 19:29:13 +00:00
|
|
|
procedure PageControl1Change(Sender: TObject);
|
2009-10-06 19:25:18 +00:00
|
|
|
private
|
|
|
|
{ private declarations }
|
2014-04-29 21:58:48 +00:00
|
|
|
procedure LoadFile(const AFileName: String);
|
2009-10-06 19:25:18 +00:00
|
|
|
public
|
|
|
|
{ public declarations }
|
|
|
|
end;
|
|
|
|
|
|
|
|
var
|
|
|
|
Form1: TForm1;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
2014-04-19 19:29:13 +00:00
|
|
|
uses
|
2014-04-21 21:43:43 +00:00
|
|
|
Grids, fpcanvas;
|
2014-04-19 19:29:13 +00:00
|
|
|
|
2009-10-06 19:25:18 +00:00
|
|
|
{ TForm1 }
|
|
|
|
|
2014-04-19 19:29:13 +00:00
|
|
|
procedure TForm1.btnPopulateGridClick(Sender: TObject);
|
2013-12-28 15:29:22 +00:00
|
|
|
// Populate grid with some demo data
|
2009-10-06 19:25:18 +00:00
|
|
|
var
|
2014-04-19 19:29:13 +00:00
|
|
|
lCell: PCell;
|
2009-10-06 19:25:18 +00:00
|
|
|
begin
|
2014-04-19 19:29:13 +00:00
|
|
|
// create a cell (2,2) if not yet available
|
|
|
|
lCell := sWorksheetGrid1.Worksheet.GetCell(2, 2);
|
|
|
|
sWorksheetGrid1.Worksheet.WriteUTF8Text(2, 2, 'Algo');
|
|
|
|
sWorksheetGrid1.Invalidate;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TForm1.CbDisplayFixedColRowClick(Sender: TObject);
|
|
|
|
begin
|
|
|
|
sWorksheetGrid1.DisplayFixedColRow := CbDisplayFixedColRow.Checked;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TForm1.CbDisplayGridClick(Sender: TObject);
|
|
|
|
begin
|
|
|
|
if CbDisplayGrid.Checked then
|
|
|
|
sWorksheetGrid1.Options := sWorksheetGrid1.Options + [goHorzLine, goVertLine]
|
|
|
|
else
|
|
|
|
sWorksheetGrid1.Options := sWorksheetGrid1.Options - [goHorzLine, goVertLine];
|
2009-10-06 19:25:18 +00:00
|
|
|
end;
|
|
|
|
|
2014-04-19 19:29:13 +00:00
|
|
|
procedure TForm1.acOpenExecute(Sender: TObject);
|
2013-12-28 15:29:22 +00:00
|
|
|
begin
|
|
|
|
if OpenDialog1.Execute then
|
2014-04-29 21:58:48 +00:00
|
|
|
LoadFile(OpenDialog1.FileName);
|
2013-12-28 15:29:22 +00:00
|
|
|
end;
|
|
|
|
|
2014-04-19 19:29:13 +00:00
|
|
|
procedure TForm1.acQuitExecute(Sender: TObject);
|
2013-12-28 15:29:22 +00:00
|
|
|
begin
|
|
|
|
Close;
|
|
|
|
end;
|
|
|
|
|
2014-04-19 19:29:13 +00:00
|
|
|
procedure TForm1.acSaveAsExecute(Sender: TObject);
|
2013-12-28 15:29:22 +00:00
|
|
|
// Saves sheet in grid to file, overwriting existing file
|
|
|
|
var
|
|
|
|
lWorkBook: TsWorkbook;
|
|
|
|
lWorkSheet:TsWorksheet;
|
|
|
|
begin
|
2014-04-19 19:29:13 +00:00
|
|
|
ShowMessage('Not implemented...');
|
|
|
|
exit;
|
|
|
|
|
2013-12-28 15:29:22 +00:00
|
|
|
if SaveDialog1.Execute then
|
|
|
|
begin
|
|
|
|
lWorkBook := TsWorkBook.Create;
|
|
|
|
lWorkSheet := lWorkBook.AddWorksheet('Sheet1');
|
|
|
|
try
|
|
|
|
sWorksheetGrid1.SaveToWorksheet(lWorkSheet);
|
|
|
|
lWorkBook.WriteToFile(SaveDialog1.FileName,true);
|
|
|
|
finally
|
|
|
|
lWorkBook.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2014-04-29 21:58:48 +00:00
|
|
|
procedure TForm1.FormActivate(Sender: TObject);
|
|
|
|
begin
|
|
|
|
if ParamCount > 0 then
|
|
|
|
LoadFile(ParamStr(1));
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TForm1.LoadFile(const AFileName: String);
|
|
|
|
// Loads first worksheet from file into grid
|
|
|
|
var
|
|
|
|
pages: TStrings;
|
|
|
|
i: Integer;
|
|
|
|
begin
|
|
|
|
sWorksheetGrid1.LoadFromSpreadsheetFile(AFileName);
|
|
|
|
Caption := Format('fpsGrid - %s (%s)', [
|
|
|
|
AFilename,
|
|
|
|
GetFileFormatName(sWorksheetGrid1.Workbook.FileFormat)
|
|
|
|
]);
|
2014-05-03 19:07:55 +00:00
|
|
|
CbDisplayGrid.Checked := sWorksheetGrid1.Worksheet.ShowGridLines;
|
2014-04-29 21:58:48 +00:00
|
|
|
|
|
|
|
// Create a tab in the pagecontrol for each worksheet contained in the workbook
|
|
|
|
// This would be easer with a TTabControl. This has display issues, though.
|
|
|
|
pages := TStringList.Create;
|
|
|
|
try
|
|
|
|
sWorksheetGrid1.GetSheets(pages);
|
|
|
|
sWorksheetGrid1.Parent := PageControl1.Pages[0];
|
|
|
|
while PageControl1.PageCount > pages.Count do PageControl1.Pages[1].Free;
|
|
|
|
while PageControl1.PageCount < pages.Count do PageControl1.AddTabSheet;
|
|
|
|
for i:=0 to PageControl1.PageCount-1 do
|
|
|
|
PageControl1.Pages[i].Caption := pages[i];
|
|
|
|
finally
|
|
|
|
pages.Free;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2014-04-19 19:29:13 +00:00
|
|
|
procedure TForm1.PageControl1Change(Sender: TObject);
|
|
|
|
begin
|
|
|
|
sWorksheetGrid1.Parent := PageControl1.Pages[PageControl1.ActivePageIndex];
|
|
|
|
sWorksheetGrid1.SelectSheetByIndex(PageControl1.ActivePageIndex);
|
|
|
|
end;
|
|
|
|
|
2009-10-06 19:25:18 +00:00
|
|
|
initialization
|
|
|
|
{$I mainform.lrs}
|
|
|
|
|
|
|
|
end.
|
|
|
|
|