Recent

Author Topic: [SOLVED] search "while FindNext", does not return the row column correctly  (Read 1370 times)

rca

  • Jr. Member
  • **
  • Posts: 84
I need to find the first and the last, for that I have performed the following functions:
Code: Pascal  [Select][+][-]
  1. function TForm1.FindLastTxt(searchtxt:string) : boolean;
  2. var
  3.   foundWorksheet: TsWorksheet;
  4.   MySearchParams: TsSearchParams;
  5.   tmpfind: Boolean;
  6.  
  7. begin
  8.   foundRow:=0;
  9.   foundCol:=0;
  10.  
  11.   tmpfind:=False;
  12.  
  13.   MySearchParams.SearchText := searchtxt;
  14.   MySearchParams.Options := [soCompareEntireCell,soEntireDocument];
  15.   MySearchParams.Within := swWorksheet;
  16.  
  17.   with TsSearchEngine.Create(sWGrid.Workbook) do begin
  18.     if FindFirst(MySearchParams, foundWorksheet, foundRow, foundCol) then
  19.     begin
  20.       while FindNext(MySearchParams, foundWorksheet, foundRow, foundCol) do
  21.       begin
  22.         tmpfind:=True;
  23.       end;
  24.     end;
  25.     Free;
  26.   end;
  27.  
  28.   Result:=tmpfind;
  29. end;
  30.  

Code: Pascal  [Select][+][-]
  1. function TForm1.FindFirstTxt(searchtxt:string) : boolean;
  2. var
  3.   foundWorksheet: TsWorksheet;
  4.   MySearchParams: TsSearchParams;
  5.   tmpfind: Boolean;
  6.  
  7. begin
  8.   foundRow:=0;
  9.   foundCol:=0;
  10.  
  11.   tmpfind:=False;
  12.  
  13.   MySearchParams.SearchText := searchtxt;
  14.   MySearchParams.Options := [soCompareEntireCell,soEntireDocument];
  15.   MySearchParams.Within := swWorksheet;
  16.  
  17.   with TsSearchEngine.Create(sWGrid.Workbook) do begin
  18.     if FindFirst(MySearchParams, foundWorksheet, foundRow, foundCol) then
  19.     begin
  20.       tmpfind:=True;
  21.     end;
  22.     Free;
  23.   end;
  24.  
  25.   Result:=tmpfind;
  26. end;
  27.  

Where,
Code: Pascal  [Select][+][-]
  1. var
  2.   Form1: TForm1;
  3.   foundRow, foundCol: Cardinal;


With FindFirstTxt: Successfully selects the first cell found and returns foundRow and foundCol successfully.
With FindLastTxt: selects the last found cell correctly, but returns foundRow and foundCol with incorrect values.

I attach an image and the code of the test program.

I appreciate the help you can give me.
« Last Edit: August 09, 2024, 11:31:55 am by rca »

paweld

  • Hero Member
  • *****
  • Posts: 1217
Re: search "while FindNext", does not return the row column correctly
« Reply #1 on: August 09, 2024, 08:05:24 am »
Code: Pascal  [Select][+][-]
  1. function TForm1.FindLastTxt(searchtxt: String): Boolean;
  2. var
  3.   foundWorksheet: TsWorksheet;
  4.   MySearchParams: TsSearchParams;
  5.   tmprow, tmpcol: Cardinal;
  6. begin
  7.   foundRow := 0;
  8.   foundCol := 0;
  9.  
  10.   MySearchParams.SearchText := searchtxt;
  11.   MySearchParams.Options := [soCompareEntireCell, soEntireDocument];
  12.   MySearchParams.Within := swWorksheet;
  13.  
  14.   with TsSearchEngine.Create(sWGrid.Workbook) do
  15.   begin
  16.     Result := FindFirst(MySearchParams, foundWorksheet, foundRow, foundCol);
  17.     tmprow := foundRow;
  18.     tmpcol := foundCol;
  19.     while Result and FindNext(MySearchParams, foundWorksheet, tmprow, tmpcol) do
  20.     begin
  21.       foundRow := tmprow;
  22.       foundCol := tmpcol;
  23.     end;
  24.     Free;
  25.   end;
  26. end;      
Best regards / Pozdrawiam
paweld

rca

  • Jr. Member
  • **
  • Posts: 84
Re: search "while FindNext", does not return the row column correctly
« Reply #2 on: August 09, 2024, 11:31:10 am »
@paweld

Now it works.

Thanks for your help.

wp

  • Hero Member
  • *****
  • Posts: 12361
The other solution, built into FPSpreadsheet, is:
  • Add option "boBackward" to MySearchParam.Options of the "FindLastTxt" procedure
  • Remove the "while FindNext" loop from "FindLastTxt" so that the Searchengine does the same as in "FindFirstTxt"
Code: Pascal  [Select][+][-]
  1. function TForm1.FindLastTxt(searchtxt:string) : boolean;
  2. var
  3.   foundWorksheet: TsWorksheet;
  4.   MySearchParams: TsSearchParams;
  5.   tmpfind: Boolean;
  6.  
  7. begin
  8.   foundRow:=0;
  9.   foundCol:=0;
  10.  
  11.   tmpfind:=False;
  12.  
  13.   MySearchParams.SearchText := searchtxt;
  14.   MySearchParams.Options := [soBackward, soCompareEntireCell,soEntireDocument];
  15.   MySearchParams.Within := swWorksheet;
  16.  
  17.   with TsSearchEngine.Create(sWGrid.Workbook) do begin
  18.     if FindFirst(MySearchParams, foundWorksheet, foundRow, foundCol) then
  19.     begin
  20.       tmpfind:=True;
  21.     end;
  22.     Free;
  23.   end;
  24.  
  25.   Result:=tmpfind;
  26. end;
Your issue probably is that SearchEngine.FindFirst does not mean "Find first (top,left) cell in search range", but "Find first cell which meets the search criteria."; the search direction is determined by the options,

rca

  • Jr. Member
  • **
  • Posts: 84
@wp

Tested and it works too!

Thanks for your explanation and help.

 

TinyPortal © 2005-2018