I'm still migrating code from XLSReadWrite and XLSReadWriteII 2.0 to XLSReadWriteII 5.0, and I came across a very difficult error to debug. After struggling for 6-8 hours, I believe I found the general cause, which appears to be due to the use of global variables.
Our code in question makes use of 3 different TXLSReadWriteII5 components:
1) One that is responsible for writing the main output spreadsheet.
2) One that is responsible for reading the list of OrderIDs that the user wants extracted.
3) One that is responsible for writing a single order's "Research" to a spreadsheet that we store into a sub directory of the main output spreadsheet's directory. For each order in the main output spreadsheet, we put a hyperlink to its "Research" spreadsheet.
Shortly after the 1st "Research" spreadsheet is written, when writing a normal cell to the output spreadsheet, I get a "List Index Out of Bounds" in TXc12XFEditor.UseStyleStyle... The AIndex is some large number, when I can see FStyles.StyleXFs only contains 3 elements.
If I comment out the code that writes the "Research", and simply put a hyperlink to a non-existent file, then it writes the output spreadsheet with no issue.
While I wasn't able to reproduce this exact error in a test application, I was able to produce what I believe to be a related symptom of the global variables that I see:
Code: Select all
procedure TForm1.FormShow(Sender: TObject);
var
XLS1, XLS2: TXLSReadWriteII5;
procedure WriteSomeData(x: TXLSReadWriteII5; const ARowStart: Integer);
var
iCol, iRow: Integer;
begin
for iRow := ARowStart to ARowStart+10 do
for iCol := 0 to 10 do
x[0].AsString[iCol, iRow] := 'A';
end;
begin
XLS1 := TXLSReadWriteII5.Create(nil);
try
XLS1.FileName := 'C:\Temp\XLS1.xlsx';
WriteSomeData(XLS1, 0);
XLS2 := TXLSReadWriteII5.Create(nil);
try
XLS2.FileName := 'C:\Temp\XLS2.xlsx';
WriteSomeData(XLS2, 0);
XLS2.Write;
finally
XLS2.Free;
end;
WriteSomeData(XLS1, 10);
XLS1.Write;
finally
XLS1.Free;
end;
Close;
end;
Can you somehow refactor the code to not utilize global variables? I'm not sure how I would be able to refactor our code without a lot of complex logic and inefficiencies... Any help would be very appreciated.
Thanks,
Mark