-
-
Notifications
You must be signed in to change notification settings - Fork 240
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
Comments
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:
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.
The text was updated successfully, but these errors were encountered: