Recent

Author Topic: TsWorksheetGrid in fpspreadsheetgrid -has Cols[].indexOf('string') been removed?  (Read 3351 times)

PaulANormanNZ

  • Full Member
  • ***
  • Posts: 115
Hi,

I set up a GUI project a while ago (pre 2014) to interface with pdftohtmlEX (http://soft.rubypdf.com/software/pdf2htmlex-windows-version and https://github.com/coolwanglu/pdf2htmlEX/wiki/Command-Line-Options).

I use an sWorksheetGrid as an extended property sheet for the command line options (screenshot attached).

I recently updated everything Lazarus (CodePilot style) and now  TsWorksheetGrid no longer supplies a .Cols stringlist which you can .indexOf('any string') to get the needed row number.

I can't find anything under .Columns that does the same thing.
I realise that I can write a function to do what is needed, but kept thinking that as I use it even in other projects (which I will want to update soon as well) there must be something already inbuilt to replace this neat functionality.

var
sWorksheetGrid2: TsWorksheetGrid;  {from unit fpspreadsheetgrid;    }

...

ProgressBar1.Min:= strtoint(sWorksheetGrid2.Cells[2,  sWorksheetGrid2.Cols[0].IndexOf('  --first-page')]); 

... No longer works  %)

Any advice would be appreciated please as so far I cant find any write up about it any where,

Paul

wp

  • Hero Member
  • *****
  • Posts: 11858
Did TsWorksheetGrid ever work this way? I don't think so: TsWorksheetGrid always has inherited from TCustomDrawGrid which does not expose a property Cols -- this one belongs to TStringGrid which is NOT an ancestor of TsWorksheetGrid.

Looking at your code fragment I suppose you want to find the cell which contains a specific text in a given column. You can use the search functionality of fpspreadsheet - have a look at the example demo_search in folder examples/other of fpspreadsheet.

You code fragment would translate to this code (untested):
Code: Pascal  [Select][+][-]
  1. uses
  2.   fpstypes, fpssearch, fpspreadsheet;
  3.  
  4. function SearchInCol(Worksheet: TsWorksheet; ACol: Integer; AText: String): PCell;
  5. var
  6.   searchParams: TsSearchParams;
  7.   rowfound, colfound: Cardinal;
  8. begin
  9.   // Define the text to be searched for
  10.   searchParams.SearchText := AText;
  11.   // Search begins at current active cell --> put active cell to the top of the search column
  12.   searchParams.Options := [];
  13.   searchParams.Within := swColumn;
  14.   Worksheet.ActiveCellCol := ACol;
  15.   Worksheet.ActiveCellRow := 0;
  16.   // Do the search (supposing that the search text is unique)
  17.   with TSearchEngine.Create(Worksheet.Workbook) do begin
  18.     if FindFirst(searchParams, worksheet, rowfound, colfound) then
  19.       Result := Worksheet.FindCell(rowfound, colfound)
  20.     else
  21.       Result := nil;
  22.     Free;
  23.   end;
  24. end;
  25.  
  26. // To be called like this
  27.   Progressbar1.Min := sWorksheetGrid2.Cells[2, SearchInCol(sWorksheetGrid.Worksheet, 0, '  --first-page')^.Row+1];
  28.   // +1 must be omitted of there are not header rows
  29.  

P.S. Just to paint the text in grid cells in different colors and fonts, TsWorksheetGrid certainly is an overkill. Why don't you use a standard grid and define the fonts and colos in its OnPrepareCanvas event?
« Last Edit: April 01, 2017, 09:55:44 am by wp »

PaulANormanNZ

  • Full Member
  • ***
  • Posts: 115
Dear Wp,

Thanks for the suggested code.

Quote
Did TsWorksheetGrid ever work this way? I don't think so

As you can see from my previous message attachment (slide to the right) the exe has compiled nicely (2014), nicely until just recently, and now won't - raising the .Cols area as the problem.

That previously compiled app still works well.

So I guess there must historically have been some code in the component to achieve this previously :)

Thanks again,
paul

wp

  • Hero Member
  • *****
  • Posts: 11858
I looked at the old svn notes of 2014, and, yes, you are right: in the old days, when I joined this project, TsWorksheetGrid did inherit from TsCustomStringGrid, and therefore had a Cols property. Because of duplicate storage of cell strings this inheritance was changed to TsCustomDrawGrid.

So: you're out of luck. I won't bring this back because it requires permanant storage of cell texts in a stringlist although the texts are already stored in the underlying worksheet. Please use the proposed code, or more simpy, use a plain-old TStringGrid and implement a handler for the OnPreparecanvas event. I really don't see a good reason why you should pull spreadsheet capabilities into your program for just a minor modification of used fonts. From your screenshot, I think this should work:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.StringGrid1PrepareCanvas(sender: TObject; aCol, aRow: Integer;
  2.   aState: TGridDrawState);
  3. var
  4.   grid: TStringGrid;
  5. begin
  6.   grid := Sender as TStringGrid;
  7.   case ACol of
  8.     0: grid.Canvas.Font.Style := grid.Canvas.Font.Style + [fsBold];
  9.     2: grid.Canvas.Font.Color := clBlue;
  10.     3: begin
  11.          grid.Canvas.Font.Color := clGreen;
  12.          grid.Canvas.Font.Style := grid.Canvas.Font.Style + [fsItalic];
  13.        end;
  14.   end;
  15. end;
« Last Edit: April 01, 2017, 05:19:36 pm by wp »

 

TinyPortal © 2005-2018