RUBY/WATIR/RASTA: Pass the value from the excel/ra

2019-03-06 16:29发布

问题:

How to pass the value separated with comma from the excel/rasta to an array in Ruby.

html looks like this, ....

<li><input type="checkbox" name="order:1"  />Burger</li>
<li><input type="checkbox" name="order:2"  />Pasta</li>
<li><input type="checkbox" name="order:3"  />Fries</li>

...

EXCEL looks like this...

Orders

Burger, Pasta


this code doesnt work

attr_accessor :orders

order = [@orders]
order.each do |i|

.......

........

It should look like this in ruby...

attr_accessor :orders

orders = [ 'burger','pasta '] *#should pass data from excel in the array "**orders**"

orders.each do |i|
@browser.checkbox(:text => i).click
@browser.button(:name => 'save').click
end

So how would i do the passing of the value in excel to an array?

Sorry, I'm still learning Ruby :|

回答1:

There is a favored library called roo. In the linked page, it will tell you how to install the library, then use it to get values out of excel.

require 'rubygems'
require 'roo'

HOURLY_RATE = 123.45

oo = Openoffice.new("simple_spreadsheet.ods")
oo.default_sheet = oo.sheets.first
oo.first_row.upto(oo.last_row) do |line|
  @browser.check(:value => oo.cell(line,'A'))
  @browser.button(:name => 'save').click
end


回答2:

i can get the values in the excel but i don't know how will I drop them as array in Ruby

My code looks like this.. but it doesn't work

......
if array 

 arrayLabel = [:array]

    arrayLabel.each do |i|
    @browser.checkbox(:text => i).click 
    @browser.button(:name => 'save').set
            end


end 
..............

:array , is the column name where i get the value to be pass in ruby

And also How to do if when the data in the array is not found on the list of checkbos choices?



回答3:

Find some online tutorials for Ruby, or purchase and work your way through a book like Everyday Scripting With Ruby.

Everyone has to start someplace, but at the moment, it seems you understand so little, and are over-reaching your abilities to the extent that the people who are trying to help you can barely make sense out of your question or what you are trying to do.

Next, work in baby steps, so you can tell what is working and what is failing, and gradually work your way up to what you want to do.

For example, in your case, get the Roo gem installed, and figure out how to use it to read from your spreadsheet and just print stuff on the screen.

Once you can do that, Then try to read the infomation into an array, and print the array out to the screen (so you know stuff got where you need it to go)

Now, write come code using Watir that has values hardcoded to click things etc and fill out the form you are working on etc.

Once you know that is working, combine that with your code to read the spreadsheet into an array, and replace the hard-coded values with stuff pulled from the array that you made from the spreadsheet.