fpspreadsheet: Add modified version of fpexprpars.pp as formula expression parser. Demo_expression_parser (in examples/other) runs fine. Currently only basic functionality, no function support yet. Not yet integratated into the framework.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@3500 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2014-08-22 07:27:54 +00:00
parent 257fd65a2c
commit e122d42652
3 changed files with 3778 additions and 0 deletions

View File

@ -0,0 +1,72 @@
<?xml version="1.0" encoding="UTF-8"?>
<CONFIG>
<ProjectOptions>
<Version Value="9"/>
<PathDelim Value="\"/>
<General>
<Flags>
<MainUnitHasCreateFormStatements Value="False"/>
<MainUnitHasTitleStatement Value="False"/>
</Flags>
<SessionStorage Value="InProjectDir"/>
<MainUnit Value="0"/>
<Title Value="demo_expression_parser"/>
<UseAppBundle Value="False"/>
<ResourceType Value="res"/>
</General>
<i18n>
<EnableI18N LFM="False"/>
</i18n>
<VersionInfo>
<StringTable ProductVersion=""/>
</VersionInfo>
<BuildModes Count="1">
<Item1 Name="Default" Default="True"/>
</BuildModes>
<PublishOptions>
<Version Value="2"/>
</PublishOptions>
<RunParams>
<local>
<FormatVersion Value="1"/>
</local>
</RunParams>
<RequiredPackages Count="1">
<Item1>
<PackageName Value="LazUtils"/>
</Item1>
</RequiredPackages>
<Units Count="1">
<Unit0>
<Filename Value="demo_expression_parser.pas"/>
<IsPartOfProject Value="True"/>
<UnitName Value="demo_expression_parser"/>
</Unit0>
</Units>
</ProjectOptions>
<CompilerOptions>
<Version Value="11"/>
<PathDelim Value="\"/>
<Target>
<Filename Value="demo_expression_parser"/>
</Target>
<SearchPaths>
<IncludeFiles Value="$(ProjOutDir)"/>
<OtherUnitFiles Value="..\.."/>
<UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/>
</SearchPaths>
</CompilerOptions>
<Debugging>
<Exceptions Count="3">
<Item1>
<Name Value="EAbort"/>
</Item1>
<Item2>
<Name Value="ECodetoolError"/>
</Item2>
<Item3>
<Name Value="EFOpenError"/>
</Item3>
</Exceptions>
</Debugging>
</CONFIG>

View File

@ -0,0 +1,85 @@
program demo_expression_parser;
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
SysUtils, Classes
{ 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;
cell: PCell;
parser: TsExpressionParser;
res: TsExpressionResult;
formula: TsRPNFormula;
i: Integer;
begin
workbook := TsWorkbook.Create;
try
worksheet := workbook.AddWorksheet('Test');
worksheet.WriteNumber(0, 0, 2); // A1
worksheet.WriteNumber(0, 1, 2.5); // B1
{
worksheet.WriteUTF8Text(0, 0, 'Hallo'); // A1
worksheet.WriteUTF8Text(0, 1, 'Welt'); // B1
}
cell := worksheet.WriteFormula(1, 0, '=(A1+2)*3'); // A2
WriteLn('A1 = ', worksheet.ReadAsUTF8Text(0, 0));
WriteLn('B1 = ', worksheet.ReadAsUTF8Text(0, 1));
parser := TsExpressionParser.Create(worksheet);
try
parser.Builtins := [bcStrings, bcDateTime, bcMath, bcBoolean, bcConversion, bcData,
bcVaria, bcUser];
parser.Expression := Prepare(cell^.FormulaValue.FormulaStr);
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));
end;
WriteLn;
end;
finally
parser.Free;
end;
finally
workbook.Free;
end;
end.

File diff suppressed because it is too large Load Diff