Open jhwinters opened 4 years ago
Interesting, thank you for this report. Why do you want to create a file like this?
On 10/10/2020 14:25, Zeno R.R. Davatz wrote:
Interesting, thank you for this report. Why do you want to create a file like this?
To be able to pass the generated file into an existing module of the application which extends Tempfile.
What I really want to be able to do is:
output = Tempfile.new book.write(output) etc.
I came up with the second option (writing to a temporary StringIO object) as a workaround, but it seems to me I should be able to write direct to the Tempfile.
Cheers, John
-- Xronos Scheduler - https://xronos.uk/ All your school's schedule information in one place. Timetable, activities, homework, public events - the lot Live demo at https://schedulerdemo.xronos.uk/
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.
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.
that is possible. Can you share your produtive code here?
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.
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.
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.
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.
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.
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.
so spreadsheet creates the XLS file?
Yes.
Have you tried to write a simple function to create a new XLS file with spreadsheet, separate from your application?
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.
How many MB is the file that you try to write but fail?
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:
This also works:
but this does not work:
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.