Read spreadsheet from internet

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


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

···

On Thu, Jan 20, 2011 at 4:19 PM, Henry Oss <oss.hcs@googlemail.com> 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

ruby - Open an IO stream from a local file or url - Stack Overflow
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/\.

Thanks. Luckily I am incapable of embarrassment.

···

--
Posted via http://www.ruby-forum.com/.