Recent

Author Topic: AOverwriteExisting unreliable?  (Read 2472 times)

namdoog

  • Newbie
  • Posts: 2
AOverwriteExisting unreliable?
« on: October 26, 2017, 12:45:36 am »
I just started using FPSpreadsheet to create simple Excel files, and the AOverwriteExisting parameter seems to be ignored.  Mostly, on my Win10 machine, the program always overwrites the file regardless of the true/false value I am passing.

Has anyone else had a similar issue?

I am using Lazarus 1.6.4 32-bit under Windows 10, but had the same results compiling with the 64-bit version.

Here's a relevant bit of my source:

Code: Pascal  [Select][+][-]
  1. try
  2.   workbook.WriteToFile('serial_numbers.xlsx',ssformat,true,[])
  3. except
  4.   err_memo.Color := clRed;
  5. end;
  6.  
  7. [...]
  8.  
  9. Str(qty,s);
  10. fname := 'sn_log\'+base+'_1_'+s+'.xlsx';
  11. try
  12.   workbook.WriteToFile(fname,ssformat,false,[])
  13. except
  14.   err_memo.Color := clRed;
  15. end;
  16.  

wp

  • Hero Member
  • *****
  • Posts: 11853
Re: AOverwriteExisting unreliable?
« Reply #1 on: October 26, 2017, 10:18:38 am »
Good catch. Should be fixed in fpspreadsheet-trunk now. Or, if you use a released version, you should replace method TsCustomSpreadWriter.WriteToFile in unit fpReaderWriter by this code:

Code: Pascal  [Select][+][-]
  1. procedure TsCustomSpreadWriter.WriteToFile(const AFileName: string;
  2.   const AOverwriteExisting: Boolean = False; AParams: TsStreamParams = []);
  3. var
  4.   OutputStream: TStream;
  5. begin
  6.   if not AOverwriteExisting and FileExists(AFileName) then
  7.     raise EFCreateError.CreateFmt(rsFileAlreadyExists, [AFileName]);
  8.  
  9.   if (boFileStream in FWorkbook.Options) then
  10.     OutputStream := TFileStream.Create(AFileName, fmCreate)
  11.   else
  12.   if (boBufStream in Workbook.Options) then
  13.     OutputStream := TBufStream.Create(AFileName, fmCreate)
  14.   else
  15.     OutputStream := TMemoryStream.Create;
  16.  
  17.   try
  18.     WriteToStream(OutputStream, AParams);
  19.     if OutputStream is TMemoryStream then
  20.       (OutputStream as TMemoryStream).SaveToFile(AFileName);
  21.   finally
  22.     OutputStream.Free;
  23.   end;
  24. end;

namdoog

  • Newbie
  • Posts: 2
Re: AOverwriteExisting unreliable?
« Reply #2 on: October 26, 2017, 02:14:27 pm »
Thanks.  Will patch my copy today.

 

TinyPortal © 2005-2018