Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot write spreadsheet to an already opened file #254

Open
jhwinters opened this issue Oct 10, 2020 · 15 comments
Open

Cannot write spreadsheet to an already opened file #254

jhwinters opened this issue Oct 10, 2020 · 15 comments

Comments

@jhwinters
Copy link

If I understand the documentation correctly, one should be able to pass an IO object to the spreadsheet.write() method, and indeed this does seem to produce some output, but...

This works:

book.write('timetable1.xls')

This also works:

buffer = StringIO.new
book.write(buffer)

buffer.rewind

File.open("timetable2.xls", "w:ASCII-8BIT") do |file|
  file.write(buffer.read)
end

but this does not work:

File.open("timetable3.xls", "a+:ASCII-8BIT") do |file|
  book.write(file)
end

A file is produced but it isn't recognized by anything as being an XLS file. The "a+" is necessary because without it the call on "book.write()" produces an exception because it can't read (!) from the file. The ASCII-8BIT I tried adding because it seems to be necessary on the second working version in order to produce output identical to the first version. However, it doesn't seem to help.

@zdavatz
Copy link
Owner

zdavatz commented Oct 10, 2020

Interesting, thank you for this report. Why do you want to create a file like this?

@jhwinters
Copy link
Author

jhwinters commented Oct 10, 2020 via email

@jhwinters
Copy link
Author

Perhaps I should also mention that the output files produced by the first two methods are bit-for-bit identical. The third file is exactly 1024 bytes larger.

@jhwinters
Copy link
Author

Further information point. The code above is not my original code, it is a reduced version which I produced to see how small a piece of code could demonstrate the problem. The spreadsheet which it is trying to save is a trivial one.

I did a bit more experimenting this morning with my real data and all three of the approaches shown above. The first and second both work and produce identical output files. The third one doesn't complete when faced with real data and produces the following exception:

Traceback (most recent call last):
	13: from lib/utils/timetabletoxls.rb:117:in `<main>'
	12: from lib/utils/timetabletoxls.rb:117:in `open'
	11: from lib/utils/timetabletoxls.rb:118:in `block in <main>'
	10: from /home/john/.rvm/gems/ruby-2.6.6@scheduler/gems/spreadsheet-1.2.6/lib/spreadsheet/workbook.rb:129:in `write'
	 9: from /home/john/.rvm/gems/ruby-2.6.6@scheduler/gems/spreadsheet-1.2.6/lib/spreadsheet/writer.rb:12:in `write'
	 8: from /home/john/.rvm/gems/ruby-2.6.6@scheduler/gems/spreadsheet-1.2.6/lib/spreadsheet/excel/writer/workbook.rb:647:in `write_workbook'
	 7: from /home/john/.rvm/gems/ruby-2.6.6@scheduler/gems/spreadsheet-1.2.6/lib/spreadsheet/excel/writer/workbook.rb:457:in `write_from_scratch'
	 6: from /home/john/.rvm/gems/ruby-2.6.6@scheduler/gems/ruby-ole-1.2.12.2/lib/ole/storage/base.rb:85:in `open'
	 5: from /home/john/.rvm/gems/ruby-2.6.6@scheduler/gems/ruby-ole-1.2.12.2/lib/ole/storage/base.rb:85:in `new'
	 4: from /home/john/.rvm/gems/ruby-2.6.6@scheduler/gems/ruby-ole-1.2.12.2/lib/ole/storage/base.rb:79:in `initialize'
	 3: from /home/john/.rvm/gems/ruby-2.6.6@scheduler/gems/ruby-ole-1.2.12.2/lib/ole/storage/base.rb:112:in `load'
	 2: from /home/john/.rvm/gems/ruby-2.6.6@scheduler/gems/ruby-ole-1.2.12.2/lib/ole/storage/base.rb:112:in `new'
	 1: from /home/john/.rvm/gems/ruby-2.6.6@scheduler/gems/ruby-ole-1.2.12.2/lib/ole/storage/base.rb:370:in `initialize'
/home/john/.rvm/gems/ruby-2.6.6@scheduler/gems/ruby-ole-1.2.12.2/lib/ole/storage/base.rb:378:in `validate!': OLE2 signature is invalid (Ole::Storage::FormatError)

It would seem that the processing the library does when passed an open file is different from what it does for a file name or StringIO object.

@zdavatz
Copy link
Owner

zdavatz commented Oct 11, 2020

that is possible. Can you share your produtive code here?

@jhwinters
Copy link
Author

Sorry, no - it's 43,000 lines of Ruby code. Generating the spreadsheet involves hundreds of d/b queries. I will however try to produce a standalone but complete script which demonstrates the problem.

@zdavatz
Copy link
Owner

zdavatz commented Oct 11, 2020

Ok great. I think it is a problem in your script. So it is good if you create a standalone example that replicates your problem. Looking forward to seeing it.

@jhwinters
Copy link
Author

It's difficult to see how it could be a problem in my script (although I rule nothing out). The lines I quoted above were lifted straight out of the body of the script. If I had somehow corrupted the spreadsheet's internal data structures then one would expect all the writes to fail, rather than two succeeding and one failing.

Specific observations.

  1. The gem attempts to read from a file it's meant to be writing to.
  2. Given a less trivial spreadsheet, the gem manages to write it out via method 1, but then triggers an exception in the OLE code when invoked by method 3.

If my code had corrupted the gem's data structures then surely it should produce the same result for both methods?

The only thing I can think is that I am using it within a Rails application, so perhaps there is an interference between the Gem and something which Rails has done to file handling.

I'll try for that standalone script.

@zdavatz
Copy link
Owner

zdavatz commented Oct 11, 2020

Are you adding information to an existing file or are you creating a new file with your spreadsheet script? Yes, and if you are using Rails, you should try to separate the spreadsheet logic from the Rails logic.

@jhwinters
Copy link
Author

I'm creating a new file.

I'm not sure what you mean by "separate the spreadsheet logic from the Rails logic". The script reads a lot of records from the Rails database and puts selected information into cells of the newly created spreadsheet. All that works fine, but the problem comes when trying to write the spreadsheet out to a file.

@zdavatz
Copy link
Owner

zdavatz commented Oct 12, 2020

so spreadsheet creates the XLS file?

@jhwinters
Copy link
Author

Yes.

@zdavatz
Copy link
Owner

zdavatz commented Oct 12, 2020

Have you tried to write a simple function to create a new XLS file with spreadsheet, separate from your application?

@jhwinters
Copy link
Author

Yes. For a trivial spreadsheet that seems to work. I'm currently trying to produce a minimal standalone program which demonstrates the problem, however I've been called off to fix a production problem so that's on hold for the moment.

@zdavatz
Copy link
Owner

zdavatz commented Oct 13, 2020

How many MB is the file that you try to write but fail?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants