0

I'm trying to open a file from a URL using roo (1.11.0)

Foobazs-iMac:pipeline foobazlabs$ irb
irb(main):001:0> require 'roo'
require '=> true
irb(main):002:0> require 'open-uri'
=> true
irb(main):003:0> Roo::Excelx.new(open("https://www.filepicker.io/api/file/xPxn6cu3RZuQdgrx4y72"))
NoMethodError: undefined method `start_with?' for #<Tempfile:0x007ffd091d25c8>

but it looks like there isn't support for this using roo (it accepts only path names)

Can't use a temp file path name either

irb(main):004:0> t = Tempfile.new('temp.xlsx')
=> #<File:/var/folders/q_/q738l9cj3xl90yfs1g8wc4200000gn/T/temp.xlsx20130317-2753-1p3l5l6>
irb(main):005:0> t.binmode
irb(main):006:0> open("https://www.filepicker.io/api/file/xPxn6cu3RZuQdgrx4y72") { |data| t.write   data.read }
=> 13494
irb(main):007:0> Roo::Excelx.new(t.path)
use Roo::Excelx.new to handle .xlsx spreadsheet files. This has .xlsx20130317-2753-1p3l5l6
TypeError: /var/folders/q_/q738l9cj3xl90yfs1g8wc4200000gn/T/temp.xlsx20130317-2753-1p3l5l6 is not an Excel-xlsx file

Any Ruby library/gem that I can use for this purpose? I only need to read xls and xlsx files. I don't need to write. Since I'm on Heroku, I can only use Tempfiles not actual Files.

Robin Rosicky
  • 325
  • 4
  • 9
  • 1
    Have you tried passing in the url without `open`? According to the documentation, the way you're supposed to instantiate a new `Excel` object is as follows: `oo = Excel.new("http://www.somedomain.com/simple_spreadsheet.xls")` – fbonetti Mar 17 '13 at 16:57
  • I fixed it by removing the check for .xlsx extension in roo. Of course, the use case ensures that it is always an .xlsx file so it works for my case. – Robin Rosicky Apr 01 '13 at 08:46

4 Answers4

1

This might not be what you have in mind, but I use two packages to read xls and xlsx files, which are more common than I would like.

Andre Silva
  • 4,782
  • 9
  • 52
  • 65
DKB at NYU
  • 427
  • 6
  • 6
1

You can use RemoteTable for XLSX, XLS, CSV, and other formats:

require 'remote_table'
RemoteTable.new('https://www.filepicker.io/api/file/xPxn6cu3RZuQdgrx4y72', format: :xlsx).each do |row|
  puts row
end

Gives you:

{"Name"=>"Kristina H. Chung", "E-mail"=>"cheenu.madan@gmail.com", "Phone"=>"8032696336.0", "Org"=>"ABCD"}
{"Name"=>"Paige H. Chen", "E-mail"=>"cheenu.madan@gmail.com", "Phone"=>"7502167067.0", "Org"=>"ABCD"}
{"Name"=>"Sherri E. Melton", "E-mail"=>"cheenu.madan@gmail.com", "Phone"=>"7044576998.0", "Org"=>"ABCD"}
{"Name"=>"Gretchen I. Hill", "E-mail"=>"cheenu.madan@gmail.com", "Phone"=>"7967784377.0", "Org"=>"ABCD"}
{"Name"=>"Karen U. Puckett", "E-mail"=>"cheenu.madan@gmail.com", "Phone"=>"9151299999.0", "Org"=>"ABCD"}
[...]
Seamus Abshere
  • 8,326
  • 4
  • 44
  • 61
0

I was able to get ride of the NoMethod error (undefined method start_with?) by adding an additional to_s string conversion to the path:

Roo::Excelx.new(path.to_s)

In my case it was an uploaded Excel file which I tried to process with the Roo gem, though

0x4a6f4672
  • 27,297
  • 17
  • 103
  • 140
0

Nothing here works for me, but this yes(for xlsx):

file =  Roo::Excelx.new(params[:file].path,nil, :ignore)

from: http://railscasts.com/episodes/396-importing-csv-and-excel?view=asciicast

overallduka
  • 1,520
  • 19
  • 27