You've already forked lazarus-ccr
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:
79
components/fpspreadsheet/examples/other/demo_search.lpi
Normal file
79
components/fpspreadsheet/examples/other/demo_search.lpi
Normal 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>
|
64
components/fpspreadsheet/examples/other/demo_search.pas
Normal file
64
components/fpspreadsheet/examples/other/demo_search.pas
Normal 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.
|
||||||
|
|
@ -109,7 +109,7 @@ begin
|
|||||||
if not (soMatchCase in FSearchParams.Options) then
|
if not (soMatchCase in FSearchParams.Options) then
|
||||||
Include(flags, rfIgnoreCase);
|
Include(flags, rfIgnoreCase);
|
||||||
s := UTF8StringReplace(s, FSearchparams.SearchText, FReplaceParams.ReplaceText, flags);
|
s := UTF8StringReplace(s, FSearchparams.SearchText, FReplaceParams.ReplaceText, flags);
|
||||||
AWorksheet.WritecellValueAsString(ARow, ACol, s);
|
AWorksheet.WriteCellValueAsString(ARow, ACol, s);
|
||||||
// to do: RegEx to be added
|
// to do: RegEx to be added
|
||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
@ -451,22 +451,27 @@ end;
|
|||||||
function TsSearchEngine.Matches(AWorksheet: TsWorksheet; ARow, ACol: Cardinal): Boolean;
|
function TsSearchEngine.Matches(AWorksheet: TsWorksheet; ARow, ACol: Cardinal): Boolean;
|
||||||
var
|
var
|
||||||
cell: PCell;
|
cell: PCell;
|
||||||
celltxt: String;
|
txt: String;
|
||||||
begin
|
begin
|
||||||
cell := AWorksheet.FindCell(ARow, ACol);
|
cell := AWorksheet.FindCell(ARow, ACol);
|
||||||
|
txt := '';
|
||||||
if cell <> nil then
|
if cell <> nil then
|
||||||
celltxt := AWorksheet.ReadAsText(cell) else
|
begin
|
||||||
celltxt := '';
|
if (soSearchInComment in FSearchParams.Options) then
|
||||||
|
txt := AWorksheet.ReadComment(cell)
|
||||||
|
else
|
||||||
|
txt := AWorksheet.ReadAsText(cell);
|
||||||
|
end;
|
||||||
|
|
||||||
if soRegularExpr in FSearchParams.Options then
|
if soRegularExpr in FSearchParams.Options then
|
||||||
Result := FRegEx.Exec(celltxt)
|
Result := FRegEx.Exec(txt)
|
||||||
else
|
else
|
||||||
begin
|
begin
|
||||||
if not (soMatchCase in FSearchParams.Options) then
|
if not (soMatchCase in FSearchParams.Options) then
|
||||||
celltxt := UTF8Lowercase(celltxt);
|
txt := UTF8Lowercase(txt);
|
||||||
if soCompareEntireCell in FSearchParams.Options then
|
if soCompareEntireCell in FSearchParams.Options then
|
||||||
exit(celltxt = FSearchText);
|
exit(txt = FSearchText);
|
||||||
if UTF8Pos(FSearchText, celltxt) > 0 then
|
if pos(FSearchText, txt) > 0 then
|
||||||
exit(true);
|
exit(true);
|
||||||
Result := false;
|
Result := false;
|
||||||
end;
|
end;
|
||||||
|
@ -830,7 +830,7 @@ type
|
|||||||
|
|
||||||
{@@ Search option }
|
{@@ Search option }
|
||||||
TsSearchOption = (soCompareEntireCell, soMatchCase, soRegularExpr, soAlongRows,
|
TsSearchOption = (soCompareEntireCell, soMatchCase, soRegularExpr, soAlongRows,
|
||||||
soBackward, soWrapDocument, soEntireDocument);
|
soBackward, soWrapDocument, soEntireDocument, soSearchInComment);
|
||||||
|
|
||||||
{@@ A set of search options }
|
{@@ A set of search options }
|
||||||
TsSearchOptions = set of TsSearchOption;
|
TsSearchOptions = set of TsSearchOption;
|
||||||
|
Reference in New Issue
Block a user