I am trying to read an Excel file in Ruby On Rails.
I have done coding like this for reading the cell content from the Excel sheet.
def test
require 'rubygems'
require 'iconv'
require 'roo'
s = Excel.new("C:/Sites/hmmsapp/Book1.xls")
s.default_sheet = s.sheets.first
1.upto(4) do |line|
roll = s.cell(line,'A')
puts "#{roll} -------------"
end
end
But on running this it always gives me this error.
NameError in HostelController#test
uninitialized constant HostelController::Excel
I have also included iconv as per suggestions for this problem. But there is no change in error.
Please give some light to removing this error & to read the excel file properly.
Try Roo::Excel.new
Or Roo::Spreadsheet.new
Related
I'm using Selenium-Webdriver, OpenUri and Nokogiri to scrape a website. I want to download a particular image from said website to my Ubuntu computer. I tried a few different methods but each of them gives a different error message.
Here's my base code, which opens the website and gets the image url (everything after this I ran in my pry console):
require 'open-url'
require 'selenium-webdriver'
require 'nokogiri'
require 'uri'
url = "https://www.google.com/"
browser = Selenium::WebDriver.for :chrome
document = open(url).read
parsed_content = Nokogiri::HTML(content)
image = "https://www.google.com" + parsed_content.css('#hplogo').attr('src').value
binding.pry
1) Here's the first thing I tried to download the image:
download = open(image)
IO.copy_stream(download, '~/image.png')
For this, I got the following error:
Errno::ENOENT: No such file or directory # rb_sysopen - ~/image.png from (pry):44:in 'initialize'
As per this question, I tried adding a directory in the code:
FileUtils.mkdir_p(image) unless File.exist?(image)
But I got the same error.
2) Next I tried this:
open('image.png', 'wb') do |file|
file << open(image).read
end
and this returns
#<File:image.png (closed)
but the file isn't anywhere on my computer and I can't figure out what that message means.
3) Next I tried
IO.copy_stream(open(image), 'image.png')
which simply returned this:
5482
but again, I have no idea what that means and the file isn't anywhere.
4) Finally I tried
read_image = open(image).read
File.open(image, 'image.png') do |file|
file.puts read_image
end
which outputs
ArgumentError: invalid access mode image.png
from (pry):53:in 'initialize
What am I doing wrong? Was I close with any of my approaches?
File open second argument is mode for file openning.
read_image = open(image).read
File.open('image.png', 'w+') do |file|
file.write read_image
end
Your third variant works good.
5482 - length of file. File 'image.png' in same directory as your .rb file.
I am trying to parse GB2312 encoded page (http://news.qq.com/a/20140824/015032.htm), and this is my code.
I am not yet into the parsing part, just in the open and read, and I got error.
This is my code:
require 'open-uri'
open("http://news.qq.com/a/20140824/015032.htm").read
And this is the error:
Encoding::InvalidByteSequenceError: "\x8B" on GB2312
I am using Ruby 2.0.0p247
Any solution?
I don't know exactly why this happens when calling .read, but you can work around it if you are using Nokogiri. Just pass the file object directly to Nokogiri without calling .read:
require 'open-uri'
file = open("http://news.qq.com/a/20140824/015032.htm")
document = Nokogiri(file)
I cannot duplicate the error using 2.0.0p247,
require 'open-uri'
open("http://news.qq.com/a/20140824/015032.htm").read
Works fine.
However
require 'open-uri'
open("http://news.qq.com/a/20140824/015032.htm").read.encode('utf-8')
will raise the error
Encoding::InvalidByteSequenceError: "\x8B" on GB2312
Are you trying to do some encoding conversion?
you can try this
document = Nokogiri::HTML(open("http://news.qq.com/a/20140824/015032.htm"), nil, "GB18030")
When I open a file in xlsx format, using empact/roo gem, this line of code:
data = Roo::Spreadsheet.open("/Users/asd/Desktop/in_xlsx.xlsx", extensions: :xlsx)
or this line
data = Roo::Excelx.new("/Users/asd/Desktop/in_xlsx.xlsx")
works perfect! (at least this is what I think)
data is now a Roo::Excelx object with the columns and rows filled correctly.
But whenever I try to use a method like data.first_row or data.cell(1,1), I get this
NameError: uninitialized constant Spreadsheet::Link
from /Users/asd/.rvm/gems/ruby-2.0.0-p353#ch/gems/roo-1.13.2/lib/roo/excelx.rb:379:in `set_cell_values'
Additional info:
MacOS 10.9.1
Rails 4.0.2
Ruby 2.0.0-p353
Roo (1.13.2)
Any help is really appreciated!
Try this :
require 'rubygems'
require 'roo'
For more information http://roo.rubyforge.org/
I am very new to ruby, i wanted to learn the ruby code. I have basic knowledge.
I have list of pages(URL), I saved in an excel. I wanted to get those pages and needs to load in browser. Please find below basic script which I wrote for page loading.
require 'rubygems'
require 'watir'
ie = Watir::IE.new
ie.goto("http://google.com")
ie.goto("http://www.softwaretestinghelp.com/")
ie.goto("http://www.onestoptesting.com/manual-testing/")
ie.goto("https://facebook.com")
Please help to get it done.
Thanks and Regards,
Vinayaka M N
You can do it with Nokogiri gem and xml/ json file.
For exmaple:
require 'watir-webdriver'
require 'nokogiri'
browser = Watir::Browser.new :firefox
doc = Nokogiri::XML(File.open('your_file.xml'))
url = doc.at('//url').text
browser.goto(url)
And you must have xml like this:
<url>http://google.com</url>
In development mode iam using (rails3 and ruby 1.9.2p136) and CSV is working good,
but,
In production mode iam using (rails3 and ruby 1.9.2p0) and CSV throws error
(wrong number of arguments (0 for 1) , in generate)
please can u suggest me what could be the problem.
thanks
I encountered exact same problem on Fedora15 with ruby 1.8.7 .
Code below fixed the CSV.generate problem.
if RUBY_VERSION < "1.9"
require "rubygems"
require "faster_csv"
CSV = FCSV
else
require "csv"
end