From: Josh Cheek Date: 2011-01-21T08:15:49+09:00 Subject: Re: Read spreadsheet from internet --00163630f5f33ef76d049a4f50e8 Content-Type: text/plain; charset=ISO-8859-1 On Thu, Jan 20, 2011 at 4:19 PM, Henry Oss wrote: > I am probably no longer justified in claiming to a newbie, but I suspect > this is a newbie question. > > I am using the spreadsheet gem and I need to open a spreadsheet for > which I have a URL. Following > > http://stackoverflow.com/questions/263536/open-an-io-stream-from-a-local-file-or-url > I have done the following: > > web_contents = open(url) {|f| f.read } > book = Spreadsheet.open web_contents > > And I am getting: > > /usr/local/lib/ruby/gems/1.9.1/gems/spreadsheet-0.6.5.1/lib/spreadsheet.rb:68:in > `initialize': string contains null byte (ArgumentError) > from > > /usr/local/lib/ruby/gems/1.9.1/gems/spreadsheet-0.6.5.1/lib/spreadsheet.rb:68:in > `open' > from > > /usr/local/lib/ruby/gems/1.9.1/gems/spreadsheet-0.6.5.1/lib/spreadsheet.rb:68:in > `open' > > What am I doing wrong? > > Henry > > -- > Posted via http://www.ruby-forum.com/. > > I'm not familiar with the gem, but your web_contents looks like it is a string of the actual contents of the file, ie the file in memory in a variable, as a string. The Spreadsheet.open, however, looks like it expects maybe a path to the spreadsheet to open. That is probably where the issue lies, so we take a quick glance at the docs ( http://spreadsheet.rubyforge.org/Spreadsheet.html#method-c-open), which reveals that it takes an IO object, or a string as a path to the spreadsheet. You give it a string, so it thinks that is the path to the file, but it is actually the contents of the document. How to resolve this? Instead of reading the contents of f, pass f (an IO object) to Spreadsheet.open. So, if I've analyzed this correctly, I would expect this code to resolve the issue: book = nil open url do |f| book = Spreadsheet.open f end --00163630f5f33ef76d049a4f50e8--