fpspreadsheet: Add searching in comments. Add demo for searching.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@5965 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2017-06-27 09:18:34 +00:00
parent d6b78be9b6
commit 3aba9136ea
4 changed files with 157 additions and 9 deletions

View File

@ -0,0 +1,79 @@
<?xml version="1.0" encoding="UTF-8"?>
<CONFIG>
<ProjectOptions>
<Version Value="10"/>
<PathDelim Value="\"/>
<General>
<Flags>
<MainUnitHasCreateFormStatements Value="False"/>
<MainUnitHasTitleStatement Value="False"/>
</Flags>
<SessionStorage Value="InProjectDir"/>
<MainUnit Value="0"/>
<Title Value="demo_search"/>
<UseAppBundle Value="False"/>
<ResourceType Value="res"/>
</General>
<i18n>
<EnableI18N LFM="False"/>
</i18n>
<BuildModes Count="1">
<Item1 Name="Default" Default="True"/>
</BuildModes>
<PublishOptions>
<Version Value="2"/>
</PublishOptions>
<RunParams>
<local>
<FormatVersion Value="1"/>
</local>
</RunParams>
<RequiredPackages Count="2">
<Item1>
<PackageName Value="laz_fpspreadsheet"/>
</Item1>
<Item2>
<PackageName Value="LazUtils"/>
</Item2>
</RequiredPackages>
<Units Count="2">
<Unit0>
<Filename Value="demo_search.pas"/>
<IsPartOfProject Value="True"/>
</Unit0>
<Unit1>
<Filename Value="..\..\source\common\fpsexprparser.pas"/>
<IsPartOfProject Value="True"/>
</Unit1>
</Units>
</ProjectOptions>
<CompilerOptions>
<Version Value="11"/>
<PathDelim Value="\"/>
<Target>
<Filename Value="demo_search"/>
</Target>
<SearchPaths>
<IncludeFiles Value="$(ProjOutDir)"/>
<UnitOutputDirectory Value="..\lib\$(TargetCPU)-$(TargetOS)"/>
</SearchPaths>
<Linking>
<Debugging>
<UseExternalDbgSyms Value="True"/>
</Debugging>
</Linking>
</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,64 @@
program demo_search;
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
SysUtils, Classes, TypInfo,
fpsTypes, fpSpreadsheet, fpsSearch, fpsUtils, laz_fpspreadsheet;
var
workbook: TsWorkbook;
worksheet: TsWorksheet;
s: String;
searchParams: TsSearchParams;
RowFound, ColFound: Cardinal;
begin
workbook := TsWorkbook.Create;
try
worksheet := workbook.AddWorksheet('Test');
worksheet.WriteNumber(0, 0, 10); // A1
worksheet.WriteNumber(1, 0, 2); // A2
worksheet.WriteNumber(2, 0, 5); // A3 <---
worksheet.WriteNumber(3, 0, 1); // A4
worksheet.WriteNumber(4, 0, 5); // A5 <---
worksheet.WriteNumber(5, 0, 3); // A6
worksheet.WriteNumber(0, 1, 5); // B1 <---
worksheet.WriteComment(0, 0, '5');
worksheet.WriteComment(1, 0, '2');
searchParams := InitSearchParams('5', [soEntireDocument]);
// Create search engine and execute search
with TsSearchEngine.Create(workbook) do begin
if FindFirst(searchParams, worksheet, RowFound, ColFound) then begin
WriteLn('First "', searchparams.SearchText, '" found in cell ', GetCellString(RowFound, ColFound));
while FindNext(searchParams, worksheet, RowFound, ColFound) do
WriteLn('Next "', searchParams.SearchText, '" found in cell ', GetCellString(RowFound, ColFound));
end;
Free;
end;
// Now search in comments
Include(searchparams.Options, soSearchInComment);
with TsSearchEngine.Create(workbook) do begin
if FindFirst(searchParams, worksheet, RowFound, ColFound) then begin
WriteLn('First "', searchparams.SearchText, '" found in comment of cell ', GetCellString(RowFound, ColFound));
while FindNext(searchParams, worksheet, RowFound, ColFound) do
WriteLn('Next "', searchParams.SearchText, '" found in comment of cell ', GetCellString(RowFound, ColFound));
end;
Free;
end;
ReadLn;
finally
workbook.Free;
end;
end.

View File

@ -109,7 +109,7 @@ begin
if not (soMatchCase in FSearchParams.Options) then
Include(flags, rfIgnoreCase);
s := UTF8StringReplace(s, FSearchparams.SearchText, FReplaceParams.ReplaceText, flags);
AWorksheet.WritecellValueAsString(ARow, ACol, s);
AWorksheet.WriteCellValueAsString(ARow, ACol, s);
// to do: RegEx to be added
end;
end;
@ -451,22 +451,27 @@ end;
function TsSearchEngine.Matches(AWorksheet: TsWorksheet; ARow, ACol: Cardinal): Boolean;
var
cell: PCell;
celltxt: String;
txt: String;
begin
cell := AWorksheet.FindCell(ARow, ACol);
txt := '';
if cell <> nil then
celltxt := AWorksheet.ReadAsText(cell) else
celltxt := '';
begin
if (soSearchInComment in FSearchParams.Options) then
txt := AWorksheet.ReadComment(cell)
else
txt := AWorksheet.ReadAsText(cell);
end;
if soRegularExpr in FSearchParams.Options then
Result := FRegEx.Exec(celltxt)
Result := FRegEx.Exec(txt)
else
begin
if not (soMatchCase in FSearchParams.Options) then
celltxt := UTF8Lowercase(celltxt);
txt := UTF8Lowercase(txt);
if soCompareEntireCell in FSearchParams.Options then
exit(celltxt = FSearchText);
if UTF8Pos(FSearchText, celltxt) > 0 then
exit(txt = FSearchText);
if pos(FSearchText, txt) > 0 then
exit(true);
Result := false;
end;

View File

@ -830,7 +830,7 @@ type
{@@ Search option }
TsSearchOption = (soCompareEntireCell, soMatchCase, soRegularExpr, soAlongRows,
soBackward, soWrapDocument, soEntireDocument);
soBackward, soWrapDocument, soEntireDocument, soSearchInComment);
{@@ A set of search options }
TsSearchOptions = set of TsSearchOption;