You've already forked lazarus-ccr
fpspreadsheet: Redo handling for formulas:
- Allow processing of string formulas (conversion to/from rpn formulas, calculation). - Drop cell ContentType cctRPNFormula. - Drop field RPNFormulaValue of TCell record. - Remove all fekXXXX declarations for sheet functions. Function is specified by name now. - Complete registration mechanism for user-defined formulas. Adapt all demos Test cases working This commit does not yet support: shared formulas, formulas in ods. git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@3506 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
@ -10,14 +10,6 @@ uses
|
||||
{ you can add units after this },
|
||||
TypInfo, fpSpreadsheet, fpsUtils, fpsExprParser;
|
||||
|
||||
function Prepare(AFormula: String): String;
|
||||
begin
|
||||
if (AFormula <> '') and (AFormula[1] = '=') then
|
||||
Result := Copy(AFormula, 2, Length(AFormula)-1)
|
||||
else
|
||||
Result := AFormula;
|
||||
end;
|
||||
|
||||
var
|
||||
workbook: TsWorkbook;
|
||||
worksheet: TsWorksheet;
|
||||
@ -26,55 +18,89 @@ var
|
||||
res: TsExpressionResult;
|
||||
formula: TsRPNFormula;
|
||||
i: Integer;
|
||||
s: String;
|
||||
|
||||
begin
|
||||
workbook := TsWorkbook.Create;
|
||||
try
|
||||
worksheet := workbook.AddWorksheet('Test');
|
||||
{
|
||||
worksheet.WriteNumber(0, 0, 2); // A1
|
||||
|
||||
worksheet.WriteNumber(0, 0, 1); // A1
|
||||
worksheet.WriteNumber(0, 1, 2.5); // B1
|
||||
}
|
||||
|
||||
{
|
||||
worksheet.WriteUTF8Text(0, 0, 'Hallo'); // A1
|
||||
worksheet.WriteUTF8Text(0, 1, 'World'); // B1
|
||||
}
|
||||
//cell := worksheet.WriteFormula(1, 0, '=4+5'); // A2
|
||||
//cell := worksheet.WriteFormula(1, 0, 'AND(TRUE(), TRUE(), TRUE())');
|
||||
//cell := worksheet.WriteFormula(1, 0, 'SIN(A1+B1)');
|
||||
//cell := worksheet.WriteFormula(1, 0, '=TRUE()');
|
||||
//cell := worksheet.WriteFormula(1, 0, '=1-(4/2)^2*2-1'); // A2
|
||||
//cell := Worksheet.WriteFormula(1, 0, 'datedif(today(),Date(2014,1,1),"D")');
|
||||
//cell := Worksheet.WriteFormula(1, 0, 'Day(Date(2014, 1, 12))');
|
||||
//cell := Worksheet.WriteFormula(1, 0, 'SUM(1,2,3)');
|
||||
//cell := Worksheet.WriteFormula(1, 0, 'CELL("address",A1)');
|
||||
cell := Worksheet.WriteFormula(1, 0, 'ISBLANK(A1)');
|
||||
|
||||
//cell := worksheet.WriteFormula(1, 0, '=(A1+2)*3'); // A2
|
||||
cell := worksheet.WriteFormula(1, 0, 'A1&" "&B1');
|
||||
WriteLn('A1: ', worksheet.ReadAsUTF8Text(0, 0));
|
||||
WriteLn('B1: ', worksheet.ReadAsUTF8Text(0, 1));
|
||||
|
||||
WriteLn('A1 = ', worksheet.ReadAsUTF8Text(0, 0));
|
||||
WriteLn('B1 = ', worksheet.ReadAsUTF8Text(0, 1));
|
||||
|
||||
parser := TsExpressionParser.Create(worksheet);
|
||||
parser := TsSpreadsheetParser.Create(worksheet);
|
||||
try
|
||||
parser.Builtins := [bcStrings, bcDateTime, bcMath, bcBoolean, bcConversion, bcData,
|
||||
bcVaria, bcUser];
|
||||
parser.Expression := Prepare(cell^.FormulaValue.FormulaStr);
|
||||
res := parser.Evaluate;
|
||||
try
|
||||
parser.Expression := cell^.FormulaValue;
|
||||
res := parser.Evaluate;
|
||||
|
||||
Write('A2 = ', Prepare(cell^.FormulaValue.FormulaStr), ' = ');
|
||||
case res.ResultType of
|
||||
rtBoolean : WriteLn(BoolToStr(res.ResBoolean));
|
||||
rtFloat : WriteLn(FloatToStr(res.ResFloat));
|
||||
rtInteger : WriteLn(IntToStr(res.ResInteger));
|
||||
rtDateTime : WriteLn(FormatDateTime('c', res.ResDateTime));
|
||||
rtString : WriteLn(res.ResString);
|
||||
end;
|
||||
|
||||
WriteLn('Reconstructed string formula: ', parser.BuildFormula);
|
||||
|
||||
WriteLn('RPN formula:');
|
||||
formula := parser.BuildRPNFormula;
|
||||
for i:=0 to Length(formula)-1 do begin
|
||||
Write(' Item ', i, ': token ', GetEnumName(TypeInfo(TFEKind), ord(formula[i].ElementKind)));
|
||||
case formula[i].ElementKind of
|
||||
fekCell : Write(' / cell: ' +GetCellString(formula[i].Row, formula[i].Col, formula[i].RelFlags));
|
||||
fekNum : Write(' / number value: ', FloatToStr(formula[i].DoubleValue));
|
||||
fekString : Write(' / string value: "', formula[i].StringValue, '"');
|
||||
fekBool : Write(' / boolean value: ', BoolToStr(formula[i].DoubleValue <> 0));
|
||||
WriteLn('A2: ', parser.Expression);
|
||||
Write('Result: ');
|
||||
case res.ResultType of
|
||||
rtEmpty : WriteLn('--- empty ---');
|
||||
rtBoolean : WriteLn(BoolToStr(res.ResBoolean, true));
|
||||
rtFloat : WriteLn(FloatToStr(res.ResFloat));
|
||||
rtInteger : WriteLn(IntToStr(res.ResInteger));
|
||||
rtDateTime : WriteLn(FormatDateTime('c', res.ResDateTime));
|
||||
rtString : WriteLn(res.ResString);
|
||||
rtError : WriteLn(GetErrorValueStr(res.ResError));
|
||||
end;
|
||||
WriteLn;
|
||||
|
||||
WriteLn('Reconstructed string formula: ', parser.BuildStringFormula);
|
||||
formula := parser.RPNFormula;
|
||||
|
||||
for i:=0 to Length(formula)-1 do begin
|
||||
Write(' Item ', i, ': token ', GetEnumName(TypeInfo(TFEKind), ord(formula[i].ElementKind)), ' ', formula[i].FuncName);
|
||||
case formula[i].ElementKind of
|
||||
fekCell : Write(' / cell: ' +GetCellString(formula[i].Row, formula[i].Col, formula[i].RelFlags));
|
||||
fekNum : Write(' / float value: ', FloatToStr(formula[i].DoubleValue));
|
||||
fekInteger : Write(' / integer value: ', IntToStr(formula[i].IntValue));
|
||||
fekString : Write(' / string value: "', formula[i].StringValue, '"');
|
||||
fekBool : Write(' / boolean value: ', BoolToStr(formula[i].DoubleValue <> 0, true));
|
||||
end;
|
||||
WriteLn;
|
||||
end;
|
||||
finally
|
||||
parser.Free;
|
||||
end;
|
||||
|
||||
except on E:Exception do
|
||||
begin
|
||||
WriteLn('Parser/calculation error: ', E.Message);
|
||||
raise;
|
||||
end;
|
||||
end;
|
||||
|
||||
parser := TsSpreadsheetParser.Create(worksheet);
|
||||
try
|
||||
try
|
||||
parser.RPNFormula := formula;
|
||||
s := parser.BuildStringFormula;
|
||||
WriteLn('String formula, reconstructed from RPN formula: ', s);
|
||||
except on E:Exception do
|
||||
begin
|
||||
WriteLn('RPN/string formula conversion error: ', E.Message);
|
||||
raise;
|
||||
end;
|
||||
end;
|
||||
finally
|
||||
parser.Free;
|
||||
end;
|
||||
@ -82,5 +108,6 @@ begin
|
||||
finally
|
||||
workbook.Free;
|
||||
end;
|
||||
|
||||
end.
|
||||
|
||||
|
Reference in New Issue
Block a user