Recent

Author Topic: FPSpreadsheet - modifing cells causes advance to next row down  (Read 13164 times)

dodgebros

  • Full Member
  • ***
  • Posts: 161
Here is my code and btw rowNum has the value of 260 going into this code:
Code: [Select]
            //write TOTALS row
            //rowNum:=rowNum+1;  <--- LINE A
            SLCount:=(SLCount + 5);
            strCell:= 'A'+IntToStr(rowNum)+Chr(58)+'E'+IntToStr(rowNum);
            sWorksheetGrid_Genco.Worksheet.MergeCells(strCell); <----LINE B
            While c < SLCount do
              begin
                for i:=0 to 2 do
                  begin
                    strText:= fGetTextBold(SL[c]);
                    //sWorksheetGrid_Genco.Worksheet.WriteFontStyle(rowNum,i,[fssBold]);  <--- LINE C
                    sWorksheetGrid_Genco.Worksheet.WriteUTF8Text(rowNum,i,strText); <--- LINE D
                    c:=c+1;
                  end;
              end;
                                                                                         

OK, going into the While Do loop above, rowNum shows 260 and strCell shows A260:E260, if you hover over them while stepping though the code.

The attached screen capture shows the merged cells so that is working.

The code within the While Do loop is writing the correct values to the correct cells in the row but one row below where they should be.  The word "Total", in the screen capture, should be printed inside the merged cell. 

rowNum is the row number in the spreadsheet and the maximum number of rows is 261.

If I un-comment LINE A then I get an error saying rowNum is out of bounds with a value 262 as mystically rowNum gets increased by 1. 

The problem is that LINES B or C cause the actual cursor? to advance to the next row.

I can hover over rowNum and see that it's value shows 260 in LINE D but text ends up getting written to line 261 somehow.

Help!
TD
« Last Edit: July 31, 2015, 09:07:29 pm by dodgebros »

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: FPSpreadsheet - modifing cells causes adance to next row down
« Reply #1 on: July 30, 2015, 05:36:27 pm »
Did you consider that row index values begin at 0? This is different from the row index used by the cell string notations where it starts at 1. This means that the left/top cell of a worksheet, "A1" is in row 0 (not 1).

Since your code uses row and column indexes anyway I would not convert them into a cell string since that will be converted back to indexes in the end. In order to merge cells A260:E260 I would call "worksheet.MergeCells(259, 0, 259, 4)".

dodgebros

  • Full Member
  • ***
  • Posts: 161
Re: FPSpreadsheet - modifing cells causes adance to next row down
« Reply #2 on: July 30, 2015, 09:36:18 pm »
I went back and changed the code to make it easier to see what is happening ( see code below ).  Thanks to wp I now have the correct text printing in the correct cell on the correct row. 

However, you'll notice I have the lines that set the font style to bold
commented out because I still get the "out of bounds" error if they are used.

I have even tried replacing the variable "rowNum" with the actual row number of 260 but I still get this error.   I have checked and I think I have the syntax correct for doing this and because much further up in the same procedure I use this same code and it works fine.  Any ideas what is causing the error?

TD

Code: [Select]
            //write TOTALS row
            SLCount:=(SLCount + 5);

            //first value
            sWorksheetGrid_Genco.Worksheet.MergeCells(rowNum,0,rowNum,4);
             //sWorksheetGrid_Genco.Worksheet.WriteFontStyle(rowNum,1,[fssBold]);
            strText:= fGetTextBold(SL[c]);
            sWorksheetGrid_Genco.Worksheet.WriteUTF8Text(rowNum,0,strText);

            //second value
             //sWorksheetGrid_Genco.Worksheet.WriteFontStyle(rowNum,7,[fssBold]);
            strText:= fGetTextBold(SL[c+1]);
            sWorksheetGrid_Genco.Worksheet.WriteUTF8Text(rowNum,6,strText);

            //third value
            //sWorksheetGrid_Genco.Worksheet.WriteFontStyle(rowNum,7,[fssBold]);
            strText:= fGetTextBold(SL[c+2]);
            sWorksheetGrid_Genco.Worksheet.WriteUTF8Text(rowNum,7,strText);
 

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: FPSpreadsheet - modifing cells causes adance to next row down
« Reply #3 on: July 30, 2015, 10:06:47 pm »
I think you have to show more code:
What is "fGetTextBold"?
What is "SL"? A stringlist?
What is the "SLCount" good for? The count of items in the string list? You cannot add items to a stringlist by incrementing a count variable, only by calling "Add" or "Insert" of the "items" of the stringlist.

The project below which is essentially your code runs without issues:
Code: [Select]
program Project1;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes, laz_fpspreadsheet, fpstypes, fpspreadsheet, xlsbiff8;

var
  workbook: TsWorkbook;
  worksheet: TsWorksheet;
  rownum: Integer;
  strText: String;

begin
  workbook := TsWorkbook.Create;
  try
    worksheet := workbook.AddWorksheet('test');
    rownum := 2;

    worksheet.MergeCells(rowNum,0,rowNum,4);
    worksheet.WriteFontStyle(rowNum,1,[fssBold]);
    //strText:= fGetTextBold(SL[c]);
    strText := 'abc';
    worksheet.WriteUTF8Text(rowNum,0,strText);

    workbook.WriteToFile('test.xls', sfExcel8, true);

  finally
    workbook.Free;
  end;
end. 

Since you are using merged cell ranges I should note that only the top/left cell of a merged range can accept data and formats, the other cells of the range are ignored. Assuming for simplicity that your rownum is 0 then you merge the cells A1:E1. You write the strText into cell A1 - this is fine because this is the top-left cell. But in the commented part you write a format (bold font) to cell B1 - this is a cell within the merged range. The format will be neglected because the cell is not the top/left cell of the range. In order to set the entire merged range to bold you have to write the fontstyle to cell A1.

But this does not cause an out-of-bounds error. Assuming that SL is a stringlist I'd guess that "c" is too large, it must be less than the count of the stringlist items.

Learn how to use the debugger, how to set breakpoints and how to query the value of variables.

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: FPSpreadsheet - modifing cells causes adance to next row down
« Reply #4 on: July 30, 2015, 10:52:24 pm »
Code: [Select]
    worksheet.WriteFontStyle(rowNum,1,[fssBold]);
    worksheet.WriteUTF8Text(rowNum,0,strText);
Shouldn't it be both 0 then ???
Code: [Select]
    worksheet.WriteFontStyle(rowNum,0,[fssBold]);
    worksheet.WriteUTF8Text(rowNum,0,strText);

In the original code the numbers are also off:
Code: [Select]
            //first value
             //sWorksheetGrid_Genco.Worksheet.WriteFontStyle(rowNum,1,[fssBold]);
            sWorksheetGrid_Genco.Worksheet.WriteUTF8Text(rowNum,0,strText);

            //second value
             //sWorksheetGrid_Genco.Worksheet.WriteFontStyle(rowNum,7,[fssBold]);
            sWorksheetGrid_Genco.Worksheet.WriteUTF8Text(rowNum,6,strText);

            //third value
            //sWorksheetGrid_Genco.Worksheet.WriteFontStyle(rowNum,7,[fssBold]);
            sWorksheetGrid_Genco.Worksheet.WriteUTF8Text(rowNum,7,strText);
The last one is 7 and 7 while the others are 1/0 and 7/6. (So not consistent. One of these is wrong)

B.T.W. are you really sure WriteFontStyle gives the out of range error. I don't think it can. For every value of row and column WriteFontStyle just expands the sheet. Did you trace through the code ??

dodgebros

  • Full Member
  • ***
  • Posts: 161
Re: FPSpreadsheet - modifing cells causes adance to next row down
« Reply #5 on: July 31, 2015, 03:15:19 am »
Quote
The last one is 7 and 7 while the others are 1/0 and 7/6. (So not consistent. One of these is wrong)
OK, here is the cleaned up version; I had been trying different things with the commented out lines and hadn't return them to there original code:
Code: [Select]

          //write TOTALS row
          SLCount:=(SLCount + 5);
          //first value
          sWorksheetGrid_Genco.Worksheet.MergeCells(rowNum,0,rowNum,4);
          //sWorksheetGrid_Genco.Worksheet.WriteFontStyle(rowNum,0,[fssBold]);
          strText:= fGetTextBold(SL[c]); <-- LINE A
          sWorksheetGrid_Genco.Worksheet.WriteUTF8Text(rowNum,0,strText);
          //second value
          //sWorksheetGrid_Genco.Worksheet.WriteFontStyle(rowNum,7,[fssBold]);
          strText:= fGetTextBold(SL[c+1]); <-- LINE B
          sWorksheetGrid_Genco.Worksheet.WriteUTF8Text(rowNum,6,strText);
          //third value
          //sWorksheetGrid_Genco.Worksheet.WriteFontStyle(rowNum,8,[fssBold]);
          strText:= fGetTextBold(SL[c+2]); <-- LINE C
          sWorksheetGrid_Genco.Worksheet.WriteUTF8Text(rowNum,7,strText);
     

Quote
Did you trace through the code ??
Not sure what you mean by trace.  I have set a breakpoint at the beginning of this code and checked the values of everything but the values appear to be correct.  At the start of this code "rowNum" shows a value of 260 which is correct.  The total number of lines in SL is 2607.  At LINE A the variable "c" shows a value of 2602, at LINE B, it is 2603, and at LINE C it is 2604.

Quote
B.T.W. are you really sure WriteFontStyle gives the out of range error. I don't think it can. For every value of row and column WriteFontStyle just expands the sheet. Did you trace through the code ??
All I know is when I uncommented the line above LINE A above I got the error message in the attached screen capture.

Below is all the code in this unit:
Code: [Select]

unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ComCtrls,
  StdCtrls, Grids, Buttons, fpspreadsheetgrid, fpspreadsheet, fpsallformats,
  fpspreadsheetctrls, fpstypes, StrUtils, httpsend;

type

  { TForm1 }

  TForm1 = class(TForm)
    btnLoadFile: TButton;
    btnProcessFile: TButton;
    Edit1: TEdit;
    Label1: TLabel;
    OpenDialog1: TOpenDialog;
    PageControl1: TPageControl;
    ProgressBar1: TProgressBar;
    sWorkbookSource_Enco: TsWorkbookSource;
    sWorkbookTabControl_Enco: TsWorkbookTabControl;
    sWorksheetGrid_Enco: TsWorksheetGrid;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    procedure btnLoadFileClick(Sender: TObject);
    procedure btnProcessFileClick(Sender: TObject);
  private
    { private declarations }
    function fDownloadHTTP(URL: string; Response: TStringList): boolean;
    function fIsEncoFile(): boolean;
    function fGetText(strInput: string):string;
    function fGetTextBold(strInput: string):string;
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }


function TForm1.fDownloadHTTP(URL: string; Response: TStringList): boolean;
var
  HTTPGetResult: boolean;
  HTTPSender: THTTPSend;
begin
  Response.Clear;
  Result := False;
  HTTPSender := THTTPSend.Create;
  try
    HTTPGetResult := HTTPSender.HTTPMethod('GET', URL);
    if (HTTPSender.ResultCode >= 100) and (HTTPSender.ResultCode <= 299) then
    begin
      Response.LoadFromStream(HTTPSender.Document); // <-- this will write the result to your tstringlist
      Result := True;
    end;
  finally
    HTTPSender.Free;
  end;
end;

function TForm1.fIsEncoFile(): boolean;
var
  strContent: ansistring;
  strSearchFor: ansistring;
  cell: PCell;
begin
  Result:= false;
  strSearchFor:= 'Enco'; //need to add to xml setup file later
    sWorkbookTabControl_Enco.Workbook.GetFirstWorksheet;
    for cell in sWorksheetGrid_Enco.Worksheet.Cells do
      begin
        strContent:= sWorksheetGrid_Enco.Worksheet.ReadAsUTF8Text(cell);
          if AnsiContainsText(strContent,strSearchFor) then
            begin
              fIsEncoFile:= true;
            end;
     end;
end;

function TForm1.fGetText(strInput: string):string;
var
  intBegin: integer;
  intEnd: integer;
begin
     intBegin:= (AnsiPOS('>',strInput) + 1);
     intEnd:= AnsiPOS('</',strInput);
     fGetText:= AnsiMidStr(strInput,intBegin,(intEnd - intBegin));
end;

function TForm1.fGetTextBold(strInput: string):string;
var
  intBegin: integer;
  intEnd: integer;
begin
     intBegin:= (AnsiPOS('<b>',strInput) + 3);
     intEnd:= AnsiPOS('</b>',strInput);
     fGetTextBold:= AnsiMidStr(strInput,intBegin,(intEnd - intBegin));
end;

procedure TForm1.btnLoadFileClick(Sender: TObject);
  var
  strFile: string;
begin
  //locate excel file to process
  OpenDialog1.Execute;
  strFile:= OpenDialog1.FileName;

  //load file name into edit box
  Edit1.Text:= strFile;

  //load spreadsheet into grid
  sWorkbookSource_Enco.FileName:= strFile;
end;

procedure TForm1.btnProcessFileClick(Sender: TObject);
var
   baseSheet: TsWorksheet;
   strSpaces: string;
   strFile: string;
   strCell: string;
   strMsg: string;
   strTab: string;
   strURL: string;
   strText: ansistring;
   row, rowNum: Cardinal;
   cell: PCell;
   SL: TStringList;
   i, c, SLCount, numOfRows: Integer;

begin
  //initialize stringlist
  SL := TStringList.Create;

  //check if Enco file, if not exit
  if Not fIsEncoFile() then
    begin
      strSpaces:= StringOfChar(AnsiChar(32),20);
      strMsg:= strSpaces + 'THIS IS NOT A Enco FILE' + strSpaces;
      showmessage(strMsg);
      Exit;
    end;

  //get number of rows in spreadsheet
  numOfRows:= sWorksheetGrid_Enco.Worksheet.GetLastRowIndex;

  //setup progress bar
  ProgressBar1.Min := 0;
  ProgressBar1.Max := numOfRows;
  ProgressBar1.Position := 0;
  ProgressBar1.Visible := true;

  //begin processing webpages
  baseSheet:= sWorkbookTabControl_Enco.Workbook.GetFirstWorksheet;

  for row:=1 to numOfRows do
    begin
      //get new tab name
      cell:= baseSheet.FindCell(row,2);
      strTab:= baseSheet.ReadAsUTF8Text(cell);

      //get webpage URL
      cell:= baseSheet.FindCell(row,3);
      strURL:= baseSheet.ReadAsUTF8Text(cell);

      //create new tab
      sWorkbookSource_Enco.Workbook.AddWorksheet(strTab);

      //retrieve webpage html
      if fDownloadHTTP(strURL,SL) then
        begin
          //begin adding webpage data to spreadsheet tab
          SLCount:=SL.Count;

          //webpage header
          strText:= '';
          rowNum:=0;
          i:=0;
          for i:=2 to 7 do
            begin
              strText:= strText + fGetText(SL[i]);
              if i in [3,5] then
                begin
                  strText:= strText + StringOfChar(AnsiChar(32),5);
                end;
            end;
            sWorksheetGrid_Enco.Worksheet.MergeCells(0,0,0,7);
            sWorksheetGrid_Enco.Worksheet.WriteHorAlignment(rowNum,0,haCenter);
            sWorksheetGrid_Enco.Worksheet.WriteVertAlignment(rowNum,0,vaCenter);
            sWorksheetGrid_Enco.Worksheet.WriteFontStyle(rowNum,0,[fssBold]);
            sWorksheetGrid_Enco.Worksheet.WriteRowHeight(rowNum,1.75);
            sWorksheetGrid_Enco.Worksheet.WriteUTF8Text(rowNum,0,strText);

          //webpage column names
          strText:= '';
          rowNum:=1;
          i:=0;
          for i:=12 to 19 do
            begin
              strText:= fGetText(SL[i]);
              c:=i-12;
              sWorksheetGrid_Enco.Worksheet.WriteHorAlignment(rowNum,c,haCenter);
              sWorksheetGrid_Enco.Worksheet.WriteVertAlignment(rowNum,c,vaCenter);
              sWorksheetGrid_Enco.Worksheet.WriteFontStyle(rowNum,c,[fssBold]);
              sWorksheetGrid_Enco.Worksheet.WriteRowHeight(rowNum,1.75);
              sWorksheetGrid_Enco.Worksheet.WriteUTF8Text(rowNum,c,strText);
            end;

          //webpage data
          strText:= '';
          rowNum:=2;
          SLCount:=(SLCount - 8);
          i:=0;
          c:=22;
          //write standard rows
          While c < SLCount do
            begin
              For i:=0 to 7 do
                begin
                  strText:= fGetText(SL[c]);
                  sWorksheetGrid_Enco.Worksheet.WriteUTF8Text(rowNum,i,strText);
                  c:=c+1;
                end;
            c:=c+2;
            rowNum:=rowNum+1;
            end;

          //write TOTALS row
          SLCount:=(SLCount + 5);
          //first value
          sWorksheetGrid_Enco.Worksheet.MergeCells(rowNum,0,rowNum,4);
          //sWorksheetGrid_Enco.Worksheet.WriteFontStyle(rowNum,0,[fssBold]);
          strText:= fGetTextBold(SL[c]);
          sWorksheetGrid_Enco.Worksheet.WriteUTF8Text(rowNum,0,strText);
          //second value
          //sWorksheetGrid_Enco.Worksheet.WriteFontStyle(rowNum,7,[fssBold]);
          strText:= fGetTextBold(SL[c+1]);
          sWorksheetGrid_Enco.Worksheet.WriteUTF8Text(rowNum,6,strText);
          //third value
          //sWorksheetGrid_Enco.Worksheet.WriteFontStyle(rowNum,8,[fssBold]);
          strText:= fGetTextBold(SL[c+2]);
          sWorksheetGrid_Enco.Worksheet.WriteUTF8Text(rowNum,7,strText);

          SL.Clear;
          ProgressBar1.Position := row;
          Sleep(1000);   //needs to be stored in external xml file
        end
      else
        begin
          showmessage('UNABLE TO DOWNLOAD WEBPAGE');
        end;
      sWorkbookSource_Enco.SelectWorksheet(baseSheet);
   end;

  //save changes to existing excel file
  SL.Free;
  strFile:= Edit1.Text;
  sWorkbookSource_Enco.SaveToSpreadsheetFile(strFile,true);
end;

end.


I really appreciate you guys help.
TD
« Last Edit: July 31, 2015, 03:19:58 am by dodgebros »

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: FPSpreadsheet - modifing cells causes adance to next row down
« Reply #6 on: July 31, 2015, 11:51:56 am »
Code: [Select]
          //write TOTALS row
          SLCount:=(SLCount + 5);
Not sure what this does. You don't use SLCount from this point forward anymore.

Quote
Code: [Select]
          //second value
          //sWorksheetGrid_Enco.Worksheet.WriteFontStyle(rowNum,7,[fssBold]);
          strText:= fGetTextBold(SL[c+1]);
          sWorksheetGrid_Enco.Worksheet.WriteUTF8Text(rowNum,6,strText);
          //third value
          //sWorksheetGrid_Enco.Worksheet.WriteFontStyle(rowNum,8,[fssBold]);
          strText:= fGetTextBold(SL[c+2]);
          sWorksheetGrid_Enco.Worksheet.WriteUTF8Text(rowNum,7,strText);
You still have col 7 for FontStyle and col 6 for text, and col 8 for font and col 7 for text. It's not the cause of the error but I think the numbers need to match.

It's really difficult to say what's going wrong without actually being able to run this code. Tracing through the code (debugging like your did) is the only way to find this error. The error says it has a list index out of bounds with number 261. All the functions you use in fpspreadsheet expand the sheet automatically if you try to access a cell which would be out of bounds. So they shouldn't give you an error.
« Last Edit: July 31, 2015, 11:53:48 am by rvk »

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: FPSpreadsheet - modifing cells causes adance to next row down
« Reply #7 on: July 31, 2015, 11:54:09 am »
Hold on a minute...

Some code for wp :)

This simple code also gives me the error:
Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
var
  rowNum: Integer;
begin
  rowNum := 260;
  sWorksheetGrid_Enco.Worksheet.MergeCells(rowNum,0,rowNum,4);
  sWorksheetGrid_Enco.Worksheet.WriteFontStyle(rowNum,0,[fssBold]);
end;

It should expand the sheet but I also get a list out of bounds 261.


B.T.W. This is one of the reasons I would do this with a "clean" TsWorksheet instead of a TsWorksheetGrid. In the TsWorksheetGrid there are things done to resize cells etc. This is where this error occurs. Although it appears to be a "bug" with a TsWorksheet it wouldn't have happend. After reading everything in you could present the resulting sheet via a TsWorksheetGrid.

Nontheless, this error should not occur.

As far as I can see:
There is a OnChangeFont-event which points to TsCustomWorksheetGrid.ChangedFontHandler. Somewhere from that event it goes wrong.

Edit:
It appears to go wrong at the 100 mark:
Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
begin
  // this works
  sWorksheetGrid1.Worksheet.WriteFontStyle(99,0,[fssBold]);
  Showmessage('99 worked');

  // this doesn't
  sWorksheetGrid1.Worksheet.WriteFontStyle(100,0,[fssBold]);
  Showmessage('100 worked');
end;
That's because initially there are 101 rows in a default sWorksheetGrid1. So somewhere the RowHeights-array isn't expanded when there are rows added.

Is it the responsibility of the programmer to expand RowCount of sWorksheetGrid1 or should the underlying Worksheet do this automatically when there are rows added (i.e. with WriteFontStyle)?
« Last Edit: July 31, 2015, 12:50:05 pm by rvk »

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: FPSpreadsheet - modifing cells causes adance to next row down
« Reply #8 on: July 31, 2015, 01:46:35 pm »
rvk, you are absolutely right. The problem occurs because the OP tries to read data into a worksheet bound to a visual control. Although this should work flawlessly, of course, it is mostly untested terrain at the moment. To me, the worksheetGrid is mainly a test bed to see how well files are read, I only occasionally test manual input into the grid.

Compared with the worksheet, the worksheetGrid has an additional complexity: the RowCount and ColCount of the grid class which is different from the number of rows and columns in the worksheet. When a program with a WorksheetGrid linked to an empty Workbook is running, then in principle there should not be any rows and columns. From the experience with the big office applications, this is unexpected for the user. Therefore, the grid's RowCound is set to be 100 (+1 for the FixedRow), and the ColCount is 26 (for all alphabet characters, +1 for the FixedCol). If a new cell is added to the underlying worksheet, the worksheet is automatically expanded such that the new cell exists. The grid should pick up this information and expand itself as well if required. Obviously, this seems to be buggy at the moment.

Since my priority in fpspreadsheet is in other topics at the moment it may take some time to fix this. As a workaround I recommend (like rvk) to do all the steps for populating the worksheet in a separate workbook/worksheet, store it to a memory stream and load this into the workbook of the WorkbookControl. You'll need fpspreadsheet trunk due to recent improvement of the streaming methods; If you want to stick with the release version you'll have to use a temporary file.

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: FPSpreadsheet - modifing cells causes adance to next row down
« Reply #9 on: July 31, 2015, 01:53:41 pm »
Since my priority in fpspreadsheet is in other topics at the moment it may take some time to fix this. As a workaround I recommend (like rvk) to do all the steps for populating the worksheet in a separate workbook/worksheet, store it to a memory stream and load this into the workbook of the WorkbookControl. You'll need fpspreadsheet trunk due to recent improvement of the streaming methods; If you want to stick with the release version you'll have to use a temporary file.
Another option for now is setting sWorksheetGrid1.RowCount := 262; :)
Although I find working with a TsWorksheet much saver, faster, cleaner... If you really want to use the visual TsWorksheetGrid at the moment you can expand the grid yourself by increasing the RowCount.

This works for me:
Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
begin
  sWorksheetGrid1.RowCount := 262; // <-- set to 2 above the largest Rowindex

  // this works
  sWorksheetGrid1.Worksheet.WriteFontStyle(99, 0, [fssBold]);
  ShowMessage('99 worked');

  // Now this works too
  sWorksheetGrid1.Worksheet.WriteFontStyle(260, 0, [fssBold]);
  ShowMessage('260 worked');
end;

But I would go with a rewrite with just TsWorksheet.

dodgebros

  • Full Member
  • ***
  • Posts: 161
Re: FPSpreadsheet - modifing cells causes adance to next row down
« Reply #10 on: July 31, 2015, 06:12:31 pm »
WOW...I kept wondering if it was a bug in fpspreadsheet, only because it didn't make sense.  I am using the visual components, partly as eye candy for the client to watch what is happening, but also to allow the client to look through the spreadsheet after the routine runs to make sure everything got done, otherwise they would have to start up Excel to do this. 

This is my first time to use fpspreadsheet and it has been quite a learning experience!  I am really pushed for time to get this to the client, otherwise I would start over and use the non-visual components.  This time around I'll try expanding the row count as the solution.

Thanks guys for the wonderful help.  I actually have several more questions regarding this app that I will be posting.

Thanks again!!!
TD

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: FPSpreadsheet - modifing cells causes adance to next row down
« Reply #11 on: July 31, 2015, 06:30:55 pm »
r4234 contains a modified version of the TsWorksheetGrid which expands the grid's RowCount and ColCount after new cells are created in the underlying worksheet - this happens, for example, when Worksheet.WriteXXXX is called, or Worksheet.MergeCells. (Navigation with the arrow keys, however, is not yet handled in the same way).

But in essence. this new version should no longer require to adjust the RowCount manually.

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: FPSpreadsheet - modifing cells causes adance to next row down
« Reply #12 on: July 31, 2015, 08:53:13 pm »
Since my priority in fpspreadsheet is in other topics at the moment it may take some time to fix this.
I love your conception of "some time" :)

But I think your solution only works with a Workbooksource connected to the TsWorksheetGrid.

What about when you just drop a TsWorksheetGrid on a form and execute the following with a button (so no connected Workbooksource):
Code: [Select]

uses fpstypes;

procedure TForm1.Button1Click(Sender: TObject);
begin
  // this works
  sWorksheetGrid1.Worksheet.WriteFontStyle(99, 0, [fssBold]);
  ShowMessage('99 worked');

  // this doesn't
  sWorksheetGrid1.Worksheet.WriteFontStyle(100, 0, [fssBold]);
  ShowMessage('100 worked');
end;

If you do this you'll see it also hits the problem at cell 100.
This is because you only added the ListenerNotification for the WorkbookLink. But when the TsWorksheetGrid doesn't have a Workbooksource connected to it, it should still work. And it doesn't.

Is there no option for the TsWorksheet to signal the TsWorksheetGrid to do this. Or is this listening-method only implemented for Workbooksource?

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: FPSpreadsheet - modifing cells causes advance to next row down
« Reply #13 on: July 31, 2015, 09:43:14 pm »
I admit that the concept is not very consistent here due to historical reasons.

Initially there was only the WorksheetGrid which did all the file loading/saving. But I learned when writing "spready" that this is wrong somehow, and then I added the WorkbookSource to become the center of the communication: the workbook/worksheet report to the WorkbookSource, and this notifies the visual controls. This way the concept became very simple and easily exapandable.

In the long run, I am planning to remove the file access methods and some code duplications from the WorksheetGrid such that it is no longer possible to do what you are reporting.

As for your old question regarding "spready": Yes there should be a reorganization of the demos. I think "spready" will be removed from the fpspreadsheet package and will replace Felips's old, now out-of date "lazspreadsheet" in the "applications" folder of ccr, but the code of spready will be that of the "demo_ctrls" in "visual/fps_ctrls". "demo_ctrls" will be simplified to match the tutorial.

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: FPSpreadsheet - modifing cells causes advance to next row down
« Reply #14 on: July 31, 2015, 09:57:34 pm »
Initially there was only the WorksheetGrid which did all the file loading/saving. But I learned when writing "spready" that this is wrong somehow, and then I added the WorkbookSource to become the center of the communication: the workbook/worksheet report to the WorkbookSource, and this notifies the visual controls. This way the concept became very simple and easily exapandable.
Ok. In that case, wouldn't it be better for TsWorksheetGrid to create a dummy WorkbookSource? As it is now, it creates a dummy Worksheet (I think) and leaves Workbooksource blank/nil. But because that hasn't the correct notification-methods it should go through a dummy WorkbookSource object (which in turn creates a dummy Workbook and Worksheet). (Or am I not seeing this correctly?)

I don't think it's a priority at the moment. dodgebros already uses the RowCount increment so there is no rush. And when there is a Workbooksource present it would work correctly. It's only for new users who just want to drop a TsWorksheetGrid on a form and work with it directly.

Another option would be to disable the dummy creation of a worksheet and force the presence of the Workbooksource.

 

TinyPortal © 2005-2018