* * *

Author Topic: How to set default font size  (Read 780 times)

witenite

  • New member
  • *
  • Posts: 23
How to set default font size
« on: April 20, 2017, 12:41:11 pm »
Hi, quick question. I see that a couple of defaults can be asserted in fpspreadsheet (or an object of type TsWorksheet) such as columnwidth, but how do you set a default font size? So far I have used this:

tblProperties.CellFontsizes[0,0,tblProperties.ColCount-1,tblProperties.RowCount-1] := FontSize; // Set font size for the whole grid as per default

where tblProperties is of type TsWorksheetGrid. This however takes an extraordinary amount of time to complete, even for a very moderate sized table. Given my success with working at sheet level (IE type TsWorksheet) I feel there must be a way to set the default size, or else a means of cycling through rapidly to set the size for all cells.

wp

  • Hero Member
  • *****
  • Posts: 3800
Re: How to set default font size
« Reply #1 on: April 21, 2017, 12:47:03 pm »
The workbook contains a list of all fonts, and the font with index 0 is the default font. There is a function workbook.GetDefaultFont to return the parameters of this font, and a procedure workbook.SetDefaultFont to set its font name and size:

Code: Pascal  [Select]
  1. program project1;
  2.  
  3. uses
  4.   fpstypes, fpspreadsheet, fpsallformats;
  5.  
  6. var
  7.   book: TsWorkbook;
  8.   sheet: TsWorksheet;
  9.   fnt: TsFont;
  10.  
  11. begin
  12.   book := TsWorkbook.Create;
  13.   try
  14.     sheet := book.AddWorksheet('Sheet 1');
  15.     fnt := book.GetDefaultFont;
  16.     book.SetDefaultFont(fnt.FontName, 18);
  17.     sheet.WriteText(0, 0, 'ABC');
  18.     sheet.WriteRowHeight(0, 1.1, suLines);
  19.  
  20.     book.WriteToFile('def_font.xlsx', sfOOXML, true);
  21.     book.WriteToFile('def_font.xls', sfExcel8, true);
  22.     book.WriteToFile('def_font.ods', sfOpenDocument, true);
  23.   finally
  24.     book.Free;
  25.   end;
  26.  
  27. end.

This code modifies the size of the default font to 18pt and stores some dummy data. You may also need to adjust the row heights.

Please note that, for the Excel file formats, the current fpspreadsheet adds the default font from the file as a new item to the font list. Therefore, existing cells are formatted with the new font, but new cells will use the old default font. This issue is fixed in svn trunk of fpspreadsheet.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

witenite

  • New member
  • *
  • Posts: 23
Re: How to set default font size
« Reply #2 on: April 23, 2017, 11:12:10 am »
Many thanks WP, that worked a charm. Useful information on the font type too, which I have read about elsewhere online.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus