Recent

Author Topic: FPspreadsheet - [Aligment]  (Read 581 times)

seghele0

  • Sr. Member
  • ****
  • Posts: 263
FPspreadsheet - [Aligment]
« on: June 07, 2025, 03:27:21 pm »
On Windows 11.
The code below (via this forum) works without errors.
But the following code appears to have no effect:
Code: Pascal  [Select][+][-]
  1. Worksheet.PageLayout.Options:=
  2.         Worksheet.Pagelayout.Options + [poHorCentered];
  3.  Worksheet.PageLayout.Options:=
  4.         Worksheet.Pagelayout.Options + [poVertCentered];
  5.  
Can you please offer a solution.
 :-[
The complete procedure:
Code: Pascal  [Select][+][-]
  1. // --> SAVE StringGrid TO XLSX
  2. procedure SAVEStringGridXLSX(StringGrid1: TStringGrid);
  3. const
  4.   HEADER_FONT_SIZE = 11;   // in Points
  5.   DEFAULT_FONT_SIZE = 13;  // in Points
  6.   NICE_ROWHEIGHT_FACTOR = 1.5;  // adds extra margin to the cell
  7. var
  8.   WORKBOOK: TsWorkbook;
  9.   WORKSHEET: TsWorksheet;
  10.   CELL: pCell;
  11.   Row: Integer;  // Row
  12.   Col: Integer;  // Column
  13.   tmpvalue: Single;
  14. begin
  15.   Workbook := TsWorkbook.Create;
  16.   try
  17.     // Add a worksheet to the workbook
  18.     Worksheet := Workbook.AddWorksheet('Export from grid');
  19.     // Set default font for the workbook
  20.     Workbook.SetDefaultFont('Dejavu Sans Mono', DEFAULT_FONT_SIZE);
  21.     Worksheet.WriteDefaultRowHeight(DEFAULT_FONT_SIZE * NICE_ROWHEIGHT_FACTOR,
  22.          suPoints);
  23.     // Set default column widths based on StringGrid column sizes
  24.     for Col := 0 to StringGrid1.ColCount - 1 do
  25.       Worksheet.WriteColWidth(Col, pxToPts(StringGrid1.ColWidths[Col],
  26.          Screen.PixelsPerInch), suPoints);
  27.     // Configure the header row
  28.     for Col := 0 to StringGrid1.ColCount - 1 do
  29.     begin
  30.       // Write header text
  31.       Cell := Worksheet.WriteText(0, Col, StringGrid1.Cells[Col, 0]);
  32.       Worksheet.WriteHorAlignment(Cell, haCenter);       // Center horizontally
  33.       Worksheet.WriteVertAlignment(Cell, vaCenter);      // Center vertically
  34.       Worksheet.WriteBorders(Cell, [cbNorth, cbSouth, cbEast, cbWest]); // Add borders
  35.       Worksheet.WriteFontStyle(Cell, [fssBold]);         // Bold font
  36.       Worksheet.WriteFontSize(Cell, HEADER_FONT_SIZE);   // Set font size
  37.       Worksheet.WriteFontName(Cell, 'Dejavu Sans Mono'); // Set font name
  38.       Worksheet.WriteBackgroundColor(Cell, scYellow);    // Set background color
  39.     end;
  40.     // Adjust the height for the header row
  41.     Worksheet.WriteRowHeight(0, HEADER_FONT_SIZE * NICE_ROWHEIGHT_FACTOR,
  42.        suPoints);
  43.     // Write data cells
  44.     for Row := 1 to StringGrid1.RowCount - 1 do
  45.     begin
  46.       for Col := 0 to StringGrid1.ColCount - 1 do
  47.       begin
  48.         // Check if cell contains a number
  49.         if TryStrToFloat(StringGrid1.Cells[Col, Row], TmpValue) then
  50.           Cell := Worksheet.WriteNumber(Row, Col, TmpValue)
  51.         else
  52.           Cell := Worksheet.WriteText(Row, Col, StringGrid1.Cells[Col, Row]);
  53.         // Add borders and center text
  54.         Worksheet.WriteBorders(Cell, [cbNorth, cbSouth, cbEast, cbWest]);
  55.         Worksheet.WriteHorAlignment(Cell, haCenter);
  56.         Worksheet.WriteVertAlignment(Cell, vaCenter);
  57.       end;
  58.     end;
  59.     // Adjust row heights based on content
  60.     for Row := 0 to Worksheet.GetLastRowIndex do
  61.       Worksheet.WriteRowHeight(Row, NICE_ROWHEIGHT_FACTOR *
  62.             Worksheet.CalcAutoRowHeight(Row), Workbook.Units);
  63. // --> !!!!  Set page layout: horizontally center the table
  64.       Worksheet.PageLayout.Options:=
  65.         Worksheet.Pagelayout.Options + [poHorCentered];
  66. // --> !!!!  Set page layout: Vertically center the table
  67.       Worksheet.PageLayout.Options:=
  68.         Worksheet.Pagelayout.Options + [poVertCentered];
  69.     // Save the workbook as an XLSX file
  70.       Workbook.WriteToFile('WIEZEN.xlsx', sfOOXML, True);
  71.   finally
  72.     Workbook.Free;
  73.   end;
  74. end;        
  75.  

wp

  • Hero Member
  • *****
  • Posts: 12859
Re: FPspreadsheet - [Aligment]
« Reply #1 on: June 07, 2025, 05:16:11 pm »
The attached project - just your code pasted into a GUI project - works correctly. As you can see in the screenshot of the Excel print preview the table is correctly centered. Or what else do you mean?

seghele0

  • Sr. Member
  • ****
  • Posts: 263
Re: FPspreadsheet - [Aligment]
« Reply #2 on: June 07, 2025, 06:07:28 pm »
Thanks wp. I will check again in the course of next week.
If there are any extra questions, I'll be sure to let you know.
 ;)

seghele0

  • Sr. Member
  • ****
  • Posts: 263
Re: FPspreadsheet - [Aligment]
« Reply #3 on: June 08, 2025, 01:52:48 pm »
Hello wp,
I took the time to check your answer.
It turned out to be too exciting to wait any longer.
Apparently there is a problem between MS-Excel and LibreOffice-Calc.
I don’t use MS-Office.
As you can see, the layout is not adapted to the code in LO.
Is there a solution?
 ;)

wp

  • Hero Member
  • *****
  • Posts: 12859
Re: FPspreadsheet - [Aligment]
« Reply #4 on: June 08, 2025, 02:02:12 pm »
I cannot confirmed. When I open the xlsx file written by the previous demo in LO Calc, the print preview shows a correctly centered table. And when I save the workbook additionally in ods format, this file is opened in LO and Excel and has a centered table in the preview.

No idea what's wrong with your setup? Which LibreOffice version do you have? Mine is v25.2.3.2. And what is your operating system (mine: Windows 11).

seghele0

  • Sr. Member
  • ****
  • Posts: 263
Re: FPspreadsheet - [Aligment]
« Reply #5 on: June 08, 2025, 02:29:58 pm »
Very strange.
My operating system is Windows 11 - 24H2 and LO-25.2.4.3.
The problem remains in Linux-Mint, EXE with Wine, and LibreOffice-25.2.4.
The StringGrid is not centered
Hopeless ???  :(

paweld

  • Hero Member
  • *****
  • Posts: 1419
Re: FPspreadsheet - [Aligment]
« Reply #6 on: June 08, 2025, 02:48:19 pm »
Install the version from SVN: https://sourceforge.net/code-snapshots/svn/l/la/lazarus-ccr/svn/lazarus-ccr-svn-r9774-components-fpspreadsheet.zip
In the version from OPM the page layout does not work properly.

Best regards / Pozdrawiam
paweld

wp

  • Hero Member
  • *****
  • Posts: 12859
Re: FPspreadsheet - [Aligment]
« Reply #7 on: June 08, 2025, 04:24:39 pm »
paweld, you seem to know fpspreadsheet better than me. SVN logs tell me that I had fixed the xlsx page layout centering issue in November, and half a year later, I have forgotten about it...

seghele0

  • Sr. Member
  • ****
  • Posts: 263
Re: FPspreadsheet - [Aligment]
« Reply #8 on: June 08, 2025, 05:26:43 pm »
How do I replace the old component fpspreadsheet v1
16 with the new component ( link by paweld) in Lazarus 4?

wp

  • Hero Member
  • *****
  • Posts: 12859
Re: FPspreadsheet - [Aligment]
« Reply #9 on: June 08, 2025, 06:17:26 pm »
Either use svn to checkout from the ccr repository at sourceforce, or download the zipped snapshot from https://sourceforge.net/p/lazarus-ccr/svn/HEAD/tree/components/fpspreadsheet/. (Unzip, then load all lpk files into the IDE, install at least the "..._visual_dsgn" package, and if you need them, the "..export_visual" and ".._dataset" packages).

But note that the ccr repository is in development and may change in incompatible ways (but very certainly not regarding page layout) - active work is in the new charting feature. A new release for OPM will be made when this work is finished.

seghele0

  • Sr. Member
  • ****
  • Posts: 263
Re: FPspreadsheet - [Aligment]
« Reply #10 on: June 09, 2025, 12:17:45 pm »
wp,
I would like to thank you for your clear information.
After installing your modified component "fpspreadsheet", everything works for now.
May I hope that the next release of Lazarus (4.1) will indeed be equipped with this new component?

wp

  • Hero Member
  • *****
  • Posts: 12859
Re: FPspreadsheet - [Aligment]
« Reply #11 on: June 09, 2025, 01:28:07 pm »
wp,
I would like to thank you for your clear information.
After installing your modified component "fpspreadsheet", everything works for now.
May I hope that the next release of Lazarus (4.1) will indeed be equipped with this new component?
No, fpspreadsheet is independet of Lazarus. The current svn version of fpspreadsheet still needs some fine-polish, and when this is done, it will be available in OPM for easier installation.

 

TinyPortal © 2005-2018