GetCellValue with sheet index

Hi Adrian

I'm not getting anything from GetCellValue when I use the overload with the sheet index. I'm using code like this to get it to work:

rng := FXlsFile.NamedRangeByName(cellName);   // -- This returns a Flexcel TXlsNamedRange.


activeSheetIndex := FXlsFile.ActiveSheet;
FXlsFile.ActiveSheet := rng.SheetIndex;
result := GetCellValue(rng.Top, rng.Left);
FXlsFile.ActiveSheet := activeSheetIndex;
//result := GetCellValue(rng.SheetIndex, rng.Top, rng.Left);

I.e. I'm having to set the active sheet before I get the value - with GetCellValue(rng.SheetIndex, rng.Top, rng.Left) the return TCellValue is empty.

Is this a bug or am I missing something?

Cheers, Bob

I think the problem is that there is no overload GetCellValue(integer, integer, integer), that won't compile.

(is that why you have it commented out?)

the overloads are:
GetCellValue(integer, integer, var integer), which will return the XF format index in the third parameter, or;
GetCellValue(integer, integer, integer, var integer), which takes the sheet as first parameter.

I've tried with:
  XF: integer;
  xls := TXlsFile.Create(2, true);
  xls.SetCellValue(1, 1, 1,'sheet 1', -1);
  xls.SetCellValue(2, 1, 1,'sheet 2', -1);

  ShowMessage(xls.GetCellValue(1, 1, 1, XF ).ToString);
  ShowMessage(xls.GetCellValue(2, 1, 1, XF).ToString);

And it works as expected. Can you see if you are doing something different?

Ok - my daft mistake. Because the two overloads both have integer params my three param version still compiles and runs. I overlooked the XF param so my first param wasn't the sheetindex but the row#.

Might be an idea to change the type of XF - I can see subtle bugs appearing with it as an integer.

Thanks, Bob

I agree the 3/4 parameters can be confusing, and actually yes, if we could change the overload of XF we would, but it isn't that simple since it would break lots of lines of existing code. The sheet overload was added much later in time than the original xls.GetCellValue(row, col, XF), which is there since version 0 (because it is what reports use to optimize and get the value and format in a single call).

Note that the third parameter must be a variable: so xls.GetCellValue(1,1,1) won't compile. Sadly xls.GetCellValue(sheet, row, col) will, and it will actually call xls.GetCellValue(row, col, var xf). I am not sure on a way to fix it though: as said we don't want to break existing code, and even if we wanted, we really need both overloads, one that returns XF and the other that takes the sheet and returns XF. You'll just have to be a little more careful when using it. I don't really thing it will cuase subtle bugs, since it is very evident that you aren't getting the right value if you do the wrong call. 

>You'll just have to be a little more careful when using it

Yes I think that's the simplest remedy <g>

Thanks again, Bob