Recent

Author Topic: Invalid font index error  (Read 1676 times)

Bert_Plitt

  • New Member
  • *
  • Posts: 24
Invalid font index error
« on: March 06, 2019, 05:38:52 pm »
I want to add the ability to change the font of a selected cell or group of cells using a Font Dialog Box in a spreadsheet project I am working on.  As practice, I tried doing this with one of the demo projects in fpspreadsheet-1.10.1, but keep getting an error when I try to change the font.

To test my method, I opened project demo_ctrls.lpi contained in ...\fpspreadsheet-1.10.1\examples\visual\fpsctrls.  I added sFontDialogAction1 to ActionList1 and a glyph for the action to ImageList1.  I assigned the action's WorkbookSource property to sWorkbookSource1.  I added a ToolButton (ToolButton4) to ToolBar1 and assigned its Action property to sFontDialogAction1 in the properties window.  All other properties of all added items were left at their default values. 

The project compiles and runs OK.  But when I try to change the font using the new ToolButton, I get the error message "Invalid font index.  Press OK to ignore and risk data corruption.  Press Abort to kill the program."  What am I doing wrong?  I must be missing something.  Any help would be appreciated.

wp

  • Hero Member
  • *****
  • Posts: 6147
Re: Invalid font index error
« Reply #1 on: March 06, 2019, 05:57:46 pm »
You should probably post the project here. Pack the .pas, .lfm, .lpi, .lpr files to a common zip which you can upload here (--> "Attachments and other options"). If required add the spreadsheet file too.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

Bert_Plitt

  • New Member
  • *
  • Posts: 24
Re: Invalid font index error
« Reply #2 on: March 06, 2019, 11:36:11 pm »
OK, here's the files.

wp

  • Hero Member
  • *****
  • Posts: 6147
Re: Invalid font index error
« Reply #3 on: March 07, 2019, 12:19:30 am »
A left-over from the mysterious Font #4 which does not exist in the old Excel files...

Please open fpspreadsheet.pas, find the implementation of procedure TsWorksheet.WriteFont and remove the "or (AFontIndex = 4)". The correct method should be like this:

Code: Pascal  [Select]
  1. procedure TsWorksheet.WriteFont(ACell: PCell; AFontIndex: Integer);
  2. var
  3.   fmt: TsCellFormat;
  4. begin
  5.   if ACell = nil then
  6.     exit;
  7.  
  8.   if (AFontIndex < 0) or (AFontIndex >= Workbook.GetFontCount) then
  9.     raise EFPSpreadsheet.Create(rsInvalidFontIndex);
  10.  
  11.   fmt := Workbook.GetCellFormat(ACell^.FormatIndex);
  12.   Include(fmt.UsedFormattingFields, uffFont);
  13.   fmt.FontIndex := AFontIndex;
  14.   ACell^.FormatIndex := Workbook.AddCellFormat(fmt);
  15.  
  16.   ChangedFont(ACell^.Row, ACell^.Col);
  17. end;  

Recompile the package laz_fpspreadsheet.lpk, and the bug should be gone.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

Bert_Plitt

  • New Member
  • *
  • Posts: 24
Re: Invalid font index error
« Reply #4 on: March 07, 2019, 01:32:18 am »
Yes, the bug is gone!  Thanks.