Recent

Author Topic: TsWorbookSource  (Read 649 times)

straetch

  • Jr. Member
  • **
  • Posts: 80
TsWorbookSource
« on: November 01, 2024, 11:54:47 am »
I have a workbook with one sheet.
I want to display items with a TsWorksheetGrid.
So I included the TsWorksheetGrid and a TsWorkbookSource.
The grid is linked to the WorkbookSource.
I am confused how to link the workbooksource to the workbook.

My code looks like this:
  BestandenWB := TsWorkbook.Create;
  BestandenWS := BestandenWB.AddWorksheet('Bestandenlijst');
  sWorkbookSource.LoadFromWorkbook(BestandenWB); // link to the grid
But I get an SIGSEGV on the last statement.
I experimented with TsWorkbookSource.SelectWorksheet. Does not work.

In the tutorial I do not find how to do that for newly created workbooks. For workbooks loaded from file it is clear.
I must be missing something trivial.

Using laz_fpspreadsheet_visual 1.14.0.0

wp

  • Hero Member
  • *****
  • Posts: 12457
Re: TsWorbookSource
« Reply #1 on: November 02, 2024, 07:01:14 pm »
Can you post a simple project to demonstrate the issue?

straetch

  • Jr. Member
  • **
  • Posts: 80
Re: TsWorbookSource
« Reply #2 on: November 03, 2024, 12:46:42 pm »
The project contains a sWorkbookSource and grid. The grid is linked to the sWorkbookSource.

Code: Pascal  [Select][+][-]
  1. var
  2.   BestandenWB: TsWorkBook;
  3.   BestandenWS: TsWorkSheet;
  4. begin
  5.   BestandenWSGrid: TsWorksheetGrid;
  6.   BestandenWB := TsWorkbook.Create;
  7.   BestandenWS := BestandenWB.AddWorksheet('Bestandenlijst');
  8.   sWorkbookSource.LoadFromWorkbook(BestandenWB);
  9.   BestandenWSGrid.Clear;
  10. end;
  11.  
The program generates a SIGSEGV on the statement BestandenWSGrid.Clear;
The last item on the stack points at fpspreadsheetgrid.pas(sourcevisual\) at row 6373, Function GETWORKBOOKSOURCE(0x0).
If I remove the last instruction, then the problem is gone.
Does that mean that, once the grid is linked to the workbooksource, I cannot work directly on the grid?

straetch

  • Jr. Member
  • **
  • Posts: 80
Re: TsWorbookSource
« Reply #3 on: November 03, 2024, 01:01:42 pm »
Thinking further.
I guess the behaviour of fpspreadsheet is correct.
The content of the grid follows the content of the active worksheet via the sWorkbooksource.
So I can only change the content of the grid by changing the content of the worksheet.
Correct?

wp

  • Hero Member
  • *****
  • Posts: 12457
Re: TsWorbookSource
« Reply #4 on: November 03, 2024, 02:08:16 pm »
Yes. Unlike TStringGrid, the WorksheetGrid does not contain any data. It reads and writes its data (and formatting) from/to the underlying worksheet.

If you only need a WorksheetGrid on your form and no other visual spreadsheet controls, you can skip the WorkbookSource because the Grid contains an internal WorkbookSource which becomes active when no other WorkbookSource is connected. So, you simply place a WorksheetGrid on the form, and let the user type in his/her data values and formatting. Or you pre-fill the grid by yourself by setting the WorksheetGrid.Cells[c, r]. At the end you simply save by calling WorksheetGrid.SaveToFile(_file_name_, _file_format_, true) (the "true" is for overwriting an already-existing file). Before that you may want to specify the name of the worksheet which contains the grid:

Code: Pascal  [Select][+][-]
  1. uses
  2.   [...], fpSpreadsheetGrid, fpsTypes, xlsxOOXML;
  3.  
  4. procedure TForm1.Button1Click(Sender: TObject);
  5. begin
  6.   sWorksheetGrid1.Autocalc := true;
  7.   sWorksheetGrid1.Cells[1, 1] := 'ABC';
  8.   sWorksheetGrid1.CellFont[0, 0].Style := [fsBold];
  9.   sWorksheetGrid1.Cells[1, 2] := '=sqrt(2)';      // requires sWorksheetGrid1.AutoCalc := true somewhere...
  10.   sWorksheetGrid1.Worksheet.Name := 'This is a test';
  11.   sWorksheetGrid1.SaveToSpreadsheetFile('test.xlsx', sfOOXML, true);
  12. end;    

straetch

  • Jr. Member
  • **
  • Posts: 80
Re: TsWorbookSource
« Reply #5 on: November 04, 2024, 10:51:44 am »
Interesting.
I want to remove the row and column headers from the worksheetgrid (which is linked to the workbooksource).
I can preset the option ShowHeaders to false at design time, but at runtime it shows the headers anyway.
If I set worksheetgrid.showheaders := false in the program, I get a SIGSEGV (fpspreadsheetgrid.pas at linen7134).
I understand from the above the content displayed in the grid follows the content of the worksheet.
But I presume the display attributes of the grid belong exclusively to the grid. So I did not expect a problem.

wp

  • Hero Member
  • *****
  • Posts: 12457
Re: TsWorbookSource
« Reply #6 on: November 04, 2024, 12:08:34 pm »
I can preset the option ShowHeaders to false at design time, but at runtime it shows the headers anyway.
There was a conflict in the communication between the workbooksource and the worksheetgrid. Although the grid had ShowHeader=false this setting was overwritten at runtime when the workbooksource created a new internal workbook in which the worksheets by default are formatted for ShowHeader=true. Should be fixed in the current ccr version, hopefully without side-effects...

If I set worksheetgrid.showheaders := false in the program, I get a SIGSEGV (fpspreadsheetgrid.pas at linen7134).
This should not happen, and it does not happen in my attached demo program. Maybe you can extract a standalone, simple test program from your project which you can attach here to demonstrate the issue.


I understand from the above the content displayed in the grid follows the content of the worksheet.
But I presume the display attributes of the grid belong exclusively to the grid. So I did not expect a problem.
[/quote]

 

TinyPortal © 2005-2018