2014-01-16 39 views
0

我正在使用ruby watir黃瓜框架。我從Excel中讀取值,我的方案是如何停止從特定行的excel中讀取數據並繼續閱讀相同的紅寶石excel

Given i am logged in to the console 
When i navigate to the "report_link" Report page 
When i navigate to the "alert_link" Alerts page 

我一步定義

When(/^i navigate to the "(.*)" Report page$/) do |report_link| 
    book = Spreadsheet.open 'Test.xls' 
    sheet1 = book.worksheet 0 
    sheet1.each do |row| 
    break if row[0].nil? 
    puts row.join(',') 
    @browser.link(:href => row[0]).when_present.click 
    end 
end 

When(/^i navigate to the "(.*)" Alerts page$/) do |alert_link| 
    book = Spreadsheet.open 'Test.xls' 
    sheet1 = book.worksheet 0 
    sheet1.each 4 do |row| 
    break if row[5].nil? 
    puts row.join(',') 
    @browser.link(:href => row[5]).when_present.click 
    end 
end 

有我的excel兩組HREF數據。我如何從同一個Excel中讀取它?

+1

電子表格的外觀如何 - 例如,您如何知道要讀取哪些行? –

+0

該電子表格有一個有多行的列。第五個單元格是空的。我有一組hrefs從單元格1到4和一組hrefs從單元格6到8 –

+0

難道這聽起來難以維護嗎?爲什麼不使用列來標識鏈接類型或使用多個電子表格? –

回答

0

而不是使用行將列表分解爲空行,它可能會更容易使用列。例如,列A具有報告鏈接,列B具有警報鏈接。

Spreadsheet允許您訪問類似於使用行的列。例如,下面會給你的第一列:

sheet1.column(0) 

要獲得所有值從柱,你可以做到以下幾點:

sheet1.column(0).to_a.compact 
#=> Returns an array of cell values 

行(即長度數該列)基於最長的列。換句話說,較短的列將會有額外的空白值。 compact被添加刪除這些。

上面的可以在你的腳步像使用:

When(/^i navigate to the "(.*)" Report page$/) do |report_link| 
    book = Spreadsheet.open 'Test.xls' 
    sheet1 = book.worksheet 0 

    urls = sheet1.column(0).to_a.compact 
    urls.each do |url| 
    @browser.link(:href => url).when_present.click 
    end 
end 

警報的步驟將是相似 - 只是改變了列索引。