Problem with references when writing xls

Questions and answers on how to use XLSReadWriteII 5.
Post Reply
Martin
Posts: 3
Joined: Tue Dec 02, 2014 11:29 am
Location: Denmark

Problem with references when writing xls

Post by Martin »

Our customers experience problems with references written to xls files.
I have tried to recreate the problem in a small example program.
When I write to an xls-file the references are ok the first time when I create them, but when I overwrite the definition it shows up i Excel as eg.: =#REFERENCE!$F$12:$H$44
When I use an Xlsx-file the references are ok both the first write and the following writes.
The code is migrated from XlsReadWriteII 4, and likely ealier versions before that. The customers tells me that the problem is new since the upgrade to version 5; before they could only use xls-files.
Here is the small program that recreated the problem:

Code: Select all

FXLSWriter: TXLSReadWriteII5;
....
procedure TForm49.MakeRef(AName, ADef: string);
var
  Existing: TXLSName;
begin
  Existing := FXLSWriter.Names.Find(AName);
  if Assigned(Existing) then
  begin
    Existing.Definition := ADef;
  end
  else
    FXLSWriter.Names.Add(AName, ADef);
end;

procedure TForm49.MakeSheet(AName: string);
var
  SheetIndex: Integer;
begin
  if Assigned(FXLSWriter.SheetByName(AName)) then
  begin
    SheetIndex := FXLSWriter.SheetByName(AName).Index;
    FXLSWriter.Delete(SheetIndex);
  end;
  FXLSWriter.Add;
  FXLSWriter.Sheets[FXLSWriter.Count-1].Name := AName;
end;

procedure TForm49.ResetReferences(ASheetName: string);
var
  I: Integer;
begin
  for I := FXLSWriter.Names.Count-1 downto 0 do
    if (copy(FXLSWriter.Names[I].Name, 1, length(ASheetName)+1) = ASheetName + '_') and
      not(FXLSWriter.Names[I].VBProcedure) then
        FXLSWriter.Names[I].Definition := FXLSWriter.SheetByName(ASheetName).name + '!$A$1';
end;

procedure TForm49.WriteExcel(AFilename: string);

begin
  FXLSWriter := TXLSReadWriteII5.Create(nil);
  FXLSWriter.Filename := AFileName;
  FXLSWriter.Read;
  MakeSheet('Diverse');
  MakeSheet('Andet');
  ResetReferences('Diverse');
  ResetReferences('Andet');
  MakeRef('Diverse_A', 'Diverse!$B$5:$B$38');
  MakeRef('Andet_omr', 'Andet!$F$12:$H$44');
  FXLSWriter.Write;
  FXLSWriter.Free;
end;
Martin
Posts: 3
Joined: Tue Dec 02, 2014 11:29 am
Location: Denmark

Re: Problem with references when writing xls

Post by Martin »

Had another customer ask for a fix.
Can you confirm it is a bug and give a time horizon for a fix on your end?
Or tell me what is missing to make the code save xls files.

The customers also complain that macros are broken, but I cannot tell if it is just because of the broken references or something more.
larsa
Site Admin
Posts: 926
Joined: Mon Jun 27, 2005 9:30 pm

Re: Problem with references when writing xls

Post by larsa »

Hello

I can confirm the bug. Hope to have a fix ready by the end of this week.
Lars Arvidsson, Axolot Data
larsa
Site Admin
Posts: 926
Joined: Mon Jun 27, 2005 9:30 pm

Re: Problem with references when writing xls

Post by larsa »

Hello

In order to solve the problem, add this line of code after Read:

Code: Select all

FXLSWriter.Read;
FXLSWriter.BIFF.FormulaHandler.ExternalNames.Clear;
Lars Arvidsson, Axolot Data
Martin
Posts: 3
Joined: Tue Dec 02, 2014 11:29 am
Location: Denmark

Re: Problem with references when writing xls

Post by Martin »

Hi Lars

Thanks for the answer, I still cannot get it working though.
Now the references on pages our program does not write to goes broken.
That is the references the customer has made, eg.
=BDH(B5;PX_LAST;$D$4;$D$4;"QtTyp=P")
becomes
=eurobonds.xls!_xlfn.IFERROR(B5;PX_LAST;$D$4;$D$4;"QtTyp=P").

As far as i can tell is it the line
FXLSWriter.BIFF.FormulaHandler.ExternalNames.Clear;
that causes this, since that part was not a problem before.
larsa
Site Admin
Posts: 926
Joined: Mon Jun 27, 2005 9:30 pm

Re: Problem with references when writing xls

Post by larsa »

Hello

The code I showed you can work in some situations, but not always.

I think you have to redesign your code. You must be aware of:

1. When you delete a worksheet, all names that refers to that worksheet will be tagged invalid (#REF! error).

2. You can't change the definition of a name. You must first delete it and then create a new.
Lars Arvidsson, Axolot Data
Post Reply