TXlsFile.GetSheetIndex for a sheet name returns a 1-based index, where TXlsFile.DeleteSheet expects a 0-based index. This is a bit confusing, since neither the documentation of GetSheetIndex nor the documentation of DeleteSheet mentions whether the index returned/passed is/should be 0- oder 1-based.
Hi,
All sheets in FlexCel are 1-based. I've just checked it, and DeleteSheet indeed uses a 1-based index, so I am not sure on why you see it as 0-based. I can think in 2 reasons:
- For historical reasons (I wouldn't do it like this today) DeleteSheet can be a little confusing. If you call DeleteSheet(3), it won't delete the sheet 3, but instead it will delete 3 sheets from the ActiveSheet. So to delete Sheet3 you either need to do:
xls.ActiveSheet := 3;
xls.DeleteSheet(1);
or
xls.DeleteSheet(3,1);
As said, I know this is a little confusing, but it I didn't realize 20 years ago, and we can't change it now without breaking existing code.
- If you are correctly calling DeleteSheet with 2 parameters, then maybe there is a hidden or very-hidden sheet that is confusing the count?
I've rechecked it here, also looked at the source code, and DeleteSheet should be 1-based. I've also updated the docs to say both are 1-based.
To be more specific, you shouldn't even be able to pass a sheet =0 to DeleteSheet. The code in DeleteSheet is:
....
CheckSheet(aSheet);
...
FWorkbook.DeleteSheets(aSheet - 1, aSheetCount);
....
where CheckSheet is:
procedure TXlsFile.CheckSheet(const sheet: Int32);
begin
if (sheet < 1) or (sheet > FWorkbook.Sheets.Count) then
XlsMessages.ThrowException(TXlsErr.ErrInvalidSheetNo, [sheet, 1, FWorkbook.Sheets.Count]);
end;
So CheckSheet shouldn't allow a sheet less than 1 or bigger than SheetCount (and CheckSheet is also used by all methods in FlexCel, so it is always the same). It then will call the actual method with "aSheet - 1", because internally we use 0-based indexes. But all public methods in FlexCel take 1-based sheet indexes.
If you can find a case where DeleteSheet(sheetIndex, count) is 0-based, please send a simple example here or to adrian@tmssoftware.com and I'll take a look.
Ok, I see it was my fault. Calling DeleteSheet(Integer) with a sheet index and observing the exceptions that were thrown led me somehow to the conclusion that DeleteSheet must expect a 0-based index. Now I realized that the parameter passed to DeleteSheet(Integer) is the number of sheets to be deleted starting with the active sheet. Sorry, and continue the good work.