# Read spreadsheet from internet

**URL:** <https://www.ruby-forum.com/t/read-spreadsheet-from-internet/202066>\
**Category:** Ruby\
**Created:** [January 20, 2011, 11:19pm UTC](https://www.ruby-forum.com/t/read-spreadsheet-from-internet/202066 "2011-01-20T23:19:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [January 20, 2011, 11:19pm UTC](https://www.ruby-forum.com/t/read-spreadsheet-from-internet/202066/1 "2011-01-20T23:19:10Z")

</div>

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

> <https://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

---

<div class="post-metadata">

**Author:** ![osshcs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/7c8e57/32.png) [@osshcs](https://www.ruby-forum.com/u/osshcs)\
**Post date:** [January 21, 2011, 12:16am UTC](https://www.ruby-forum.com/t/read-spreadsheet-from-internet/202066/2 "2011-01-21T00:16:40Z")

</div>

On Thu, Jan 20, 2011 at 4:19 PM, Henry O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> book = Spreadsheet.open web\_contents

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](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

---

<div class="post-metadata">

**Author:** ![osshcs](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/7c8e57/32.png) [@osshcs](https://www.ruby-forum.com/u/osshcs)\
**Post date:** [January 21, 2011, 12:39am UTC](https://www.ruby-forum.com/t/read-spreadsheet-from-internet/202066/3 "2011-01-21T00:39:42Z")

</div>

Thanks. Luckily I am incapable of embarrassment.
