2011-05-30 09:22:08 +00:00
|
|
|
{
|
|
|
|
test_write_formula.pas
|
|
|
|
|
2014-07-23 10:47:38 +00:00
|
|
|
Demonstrates how to write a formula using the fpspreadsheet library
|
2011-05-30 09:22:08 +00:00
|
|
|
|
|
|
|
AUTHORS: Felipe Monteiro de Carvalho
|
|
|
|
}
|
2014-07-23 20:52:35 +00:00
|
|
|
program demo_write_formula;
|
2011-05-30 09:22:08 +00:00
|
|
|
|
|
|
|
{$mode delphi}{$H+}
|
|
|
|
|
|
|
|
uses
|
2011-06-16 07:55:24 +00:00
|
|
|
Classes, SysUtils,
|
2014-07-23 20:52:35 +00:00
|
|
|
fpspreadsheet, xlsbiff5, xlsbiff8, fpsopendocument;
|
2011-05-30 09:22:08 +00:00
|
|
|
|
|
|
|
var
|
|
|
|
MyWorkbook: TsWorkbook;
|
|
|
|
MyWorksheet: TsWorksheet;
|
|
|
|
MyDir: string;
|
|
|
|
MyCell: PCell;
|
|
|
|
|
|
|
|
procedure WriteFirstWorksheet();
|
|
|
|
var
|
|
|
|
MyFormula: TsFormula;
|
|
|
|
MyRPNFormula: TsRPNFormula;
|
|
|
|
begin
|
|
|
|
MyWorksheet := MyWorkbook.AddWorksheet('Worksheet1');
|
|
|
|
|
|
|
|
// Write some cells
|
2011-05-30 10:07:41 +00:00
|
|
|
MyWorksheet.WriteUTF8Text(0, 1, 'Text Formula');// B1
|
|
|
|
MyWorksheet.WriteUTF8Text(0, 2, 'RPN');// C1
|
2011-05-30 09:22:08 +00:00
|
|
|
|
2011-05-30 10:23:55 +00:00
|
|
|
// =Sum(E2:e5)
|
|
|
|
MyWorksheet.WriteUTF8Text(1, 0, '=Sum(E2:e5)'); // A2
|
|
|
|
//
|
|
|
|
MyFormula.FormulaStr := '=Sum(DE:e5)';
|
2011-05-30 09:22:08 +00:00
|
|
|
MyFormula.DoubleValue := 0.0;
|
2011-05-30 10:07:41 +00:00
|
|
|
MyWorksheet.WriteFormula(1, 1, MyFormula); // B2
|
2011-05-30 10:23:55 +00:00
|
|
|
//
|
2011-05-30 09:22:08 +00:00
|
|
|
SetLength(MyRPNFormula, 2);
|
2011-05-30 10:03:04 +00:00
|
|
|
MyRPNFormula[0].ElementKind := fekCellRange;
|
|
|
|
MyRPNFormula[0].Row := 1;
|
|
|
|
MyRPNFormula[0].Row2 := 4;
|
2011-05-30 10:23:55 +00:00
|
|
|
MyRPNFormula[0].Col := 4;
|
|
|
|
MyRPNFormula[0].Col2 := 4;
|
2011-05-30 10:03:04 +00:00
|
|
|
MyRPNFormula[1].ElementKind := fekOpSUM;
|
2011-05-30 10:07:41 +00:00
|
|
|
MyWorksheet.WriteRPNFormula(1, 2, MyRPNFormula); // C2
|
2011-05-30 10:23:55 +00:00
|
|
|
|
|
|
|
// Write the formula =ABS(E1)
|
|
|
|
MyWorksheet.WriteUTF8Text(2, 0, '=ABS(E1)'); // A3
|
|
|
|
//
|
|
|
|
SetLength(MyRPNFormula, 2);
|
|
|
|
MyRPNFormula[0].ElementKind := fekCell;
|
|
|
|
MyRPNFormula[0].Col := 4;
|
|
|
|
MyRPNFormula[0].Row := 0;
|
|
|
|
MyRPNFormula[1].ElementKind := fekABS;
|
|
|
|
MyWorksheet.WriteRPNFormula(2, 2, MyRPNFormula);
|
|
|
|
|
|
|
|
// Write the formula =4+5
|
|
|
|
MyWorksheet.WriteUTF8Text(3, 0, '=4+5'); // A4
|
|
|
|
//
|
|
|
|
SetLength(MyRPNFormula, 3);
|
|
|
|
MyRPNFormula[0].ElementKind := fekNum;
|
|
|
|
MyRPNFormula[0].DoubleValue := 4.0;
|
|
|
|
MyRPNFormula[1].ElementKind := fekNum;
|
|
|
|
MyRPNFormula[1].DoubleValue := 5.0;
|
|
|
|
MyRPNFormula[2].ElementKind := fekAdd;
|
|
|
|
MyWorksheet.WriteRPNFormula(3, 2, MyRPNFormula);
|
2011-05-30 09:22:08 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure WriteSecondWorksheet();
|
|
|
|
begin
|
|
|
|
{ MyWorksheet := MyWorkbook.AddWorksheet('Worksheet2');
|
|
|
|
|
|
|
|
// Write some cells
|
|
|
|
|
|
|
|
// Line 1
|
|
|
|
|
|
|
|
MyWorksheet.WriteUTF8Text(1, 1, 'Relatório');
|
|
|
|
MyCell := MyWorksheet.GetCell(1, 1);
|
|
|
|
MyCell^.Border := [cbNorth, cbWest, cbSouth];
|
|
|
|
MyCell^.BackgroundColor := scGrey20pct;
|
|
|
|
MyCell^.UsedFormattingFields := [uffBorder, uffBackgroundColor, uffBold];}
|
|
|
|
end;
|
|
|
|
|
2014-07-23 10:47:38 +00:00
|
|
|
const
|
|
|
|
TestFile='test_formula.xls';
|
2014-07-23 20:52:35 +00:00
|
|
|
|
|
|
|
{$R *.res}
|
|
|
|
|
2011-05-30 09:22:08 +00:00
|
|
|
begin
|
2014-07-23 10:47:38 +00:00
|
|
|
writeln('Starting program.');
|
2011-05-30 09:22:08 +00:00
|
|
|
MyDir := ExtractFilePath(ParamStr(0));
|
|
|
|
|
|
|
|
// Create the spreadsheet
|
|
|
|
MyWorkbook := TsWorkbook.Create;
|
|
|
|
|
|
|
|
WriteFirstWorksheet();
|
|
|
|
|
|
|
|
WriteSecondWorksheet();
|
|
|
|
|
|
|
|
// Save the spreadsheet to a file
|
2014-07-23 10:47:38 +00:00
|
|
|
MyWorkbook.WriteToFile(MyDir + TestFile, sfExcel8, True);
|
2011-05-30 09:22:08 +00:00
|
|
|
// MyWorkbook.WriteToFile(MyDir + 'test_formula.odt', sfOpenDocument, False);
|
|
|
|
MyWorkbook.Free;
|
2014-07-23 10:47:38 +00:00
|
|
|
writeln('Finished. Please open "'+Testfile+'" in your spreadsheet program.');
|
2011-05-30 09:22:08 +00:00
|
|
|
end.
|
|
|
|
|