diff --git a/components/fpspreadsheet/examples/other/demo_search.lpi b/components/fpspreadsheet/examples/other/demo_search.lpi new file mode 100644 index 000000000..a1335b3be --- /dev/null +++ b/components/fpspreadsheet/examples/other/demo_search.lpi @@ -0,0 +1,79 @@ + + + + + + + + + + + + + + <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> diff --git a/components/fpspreadsheet/examples/other/demo_search.pas b/components/fpspreadsheet/examples/other/demo_search.pas new file mode 100644 index 000000000..b82447952 --- /dev/null +++ b/components/fpspreadsheet/examples/other/demo_search.pas @@ -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. + diff --git a/components/fpspreadsheet/source/common/fpssearch.pas b/components/fpspreadsheet/source/common/fpssearch.pas index 080cb18e2..18cd26df8 100644 --- a/components/fpspreadsheet/source/common/fpssearch.pas +++ b/components/fpspreadsheet/source/common/fpssearch.pas @@ -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; diff --git a/components/fpspreadsheet/source/common/fpstypes.pas b/components/fpspreadsheet/source/common/fpstypes.pas index 5bcef293f..ebae23c29 100644 --- a/components/fpspreadsheet/source/common/fpstypes.pas +++ b/components/fpspreadsheet/source/common/fpstypes.pas @@ -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;