获取excel数据并在Ruby中创建嵌套的默认数据哈希



我想我正在做的是试图创建一个嵌套散列。我试图用excel电子表格中的数据替换基于文件的默认数据。我需要将DEFAULT_DATA中的键分配给data_fill[]散列中的适当值。即:insured_name_first => data_fill['fname']

我只是尝试将data_fill散列放在populate方法中,但我希望能够在必要的地方传递对默认值的更改

当它在测试运行开始加载文件时,我得到:

undefined local variable or method `data_fill' for InsuredInformationPage:Class (NameError)

这是我使用的代码…

require 'gainweb_chrome'
require 'win32ole'
require 'roo'
require 'cgi'
class InsuredInformationPage
   include WatirHelper
   include GainwebChrome
  def initialize(browser)
    @browser = browser
    @state = get_state
    key = $excel.row(1)
    state_data = $excel.find(:all, :conditions => {'Policy_State' => "#{@state}"},:array => true)
    data_fill = Hash[key.zip state_data[0]]
  end
def populate(data = {})
page_data = DEFAULT_DATA.merge(data)
self.effective_date_value = page_data[:effective_date] if data.has_key?(:effective_date)
self.insured_name_first_enabled_check = page_data[:insured_name_first]
self.insured_name_middle_initial_enabled_check = page_data[:insured_name_middle_initial]
self.insured_name_last_enabled_check = page_data[:insured_name_last]
self.coinsured_name_first_enabled_check = page_data[:coinsured_name_first]
self.coinsured_name_middle_initial_enabled_check = page_data[:coinsured_name_middle_initial]
self.coinsured_name_last_enabled_check = page_data[:coinsured_name_last]
self.insured_birth_date_value = page_data[:insured_birth_date]
self.insured_gender_enabled_check = page_data[:insured_gender]
self.check_address_override if page_data[:address_override]=~/checked/i
self.address_line_one_enabled_check = page_data[:address_line_1]
self.address_line_two_enabled_check = page_data[:address_line_2]
self.city_enabled_check = page_data[:city]
self.state_enabled_check = page_data[:state]
self.zip5_enabled_check = page_data[:zip_code]
if not page_data[:has_former_address].empty?
  self.check_has_former_address if page_data[:has_former_address]=~/checked/i
  self.former_address = page_data[:former_address]
  self.former_address2 = page_data[:former_address2]
  self.former_city = page_data[:former_city]
  self.former_state = page_data[:former_state] unless page_data[:former_state].to_s.empty? 
  self.former_zip = page_data[:former_zip]
end
self.phone_number_areacode = page_data[:phone_number_areacode]
self.phone_number_prefix = page_data[:phone_number_prefix]
self.phone_number_suffix = page_data[:phone_number_suffix]
self.email_address = page_data[:email_address]
self.client_id = page_data[:client_id]
$policy_id = self.policy_id_element.value.to_s
end
def next_page
wait_for_elements(:next_button)
next_button
PageFactory.new(@browser).construct :policy_information_page
end
text_field(:effective_date,               :id => /txtEffectiveDate/)
element(:effective_date_compare,          :id => /lblPolEffDateCompare/)
text_field(:insured_name_first,           :id => /txtInsuredName_First/)
text_field(:insured_name_middle_initial,  :id => /txtInsuredName_Middle/)
text_field(:insured_name_last,            :id => /txtInsuredName_Last/)
text_field(:insured_birth_date,           :id => /txtBirthDate/)
select_list(:insured_gender,              :id => /lstGender/)
text_field(:coinsured_name_first,         :id => /txtCoinsuredName_First/)
text_field(:coinsured_name_middle_initial,:id => /txtCoinsuredName_Middle/)
text_field(:coinsured_name_last,          :id => /txtCoinsuredName_Last/)
text_field(:address_line_one,             :id => /StreetAddress/)
text_field(:address_line_two,             :id => /AddressLine2/)
text_field(:city,                         :id => /City/)
select_list(:state,                       :id => /State/)
text_field(:zip5,                         :id => /MainContent_AddressScrub_AS_Zipcode/)
text_field(:zip4,                         :id => /Zip4Textbox/)
button(:street_zip_lookup,                :id => /StreetZipButton/)
text_field(:validated,                    :id => /ValidatedTextbox/)
checkbox(:address_override,               :id => /AddressScrub_cbxOverride/)
image(:address_override_help,             :id => /AddressScrub_hbOverride/)
div(:address_override_help_div,           :id => /hbOverride_answer/)
checkbox(:has_former_address,             :id => /chkFormerAddress/)
image(:has_former_address_help,           :id => /AddressScrub_hbOverride/)
div(:has_former_address_help_div,         :id => /hbFormerAddress_answer/)
text_field(:former_address,               :id => /txtFormerAddress/)
text_field(:former_address2,              :id => /txtFormerAddressLine2/)
text_field(:former_city,                  :id => /txtFormerCity/)
select_list(:former_state,                :id => /FormerState/)
text_field(:former_zip,                   :id => /txtFormerZip/)
text_field(:phone_number_areacode,        :id => /txtAreaCode/)
text_field(:phone_number_prefix,          :id => /txtPrefix/)
text_field(:phone_number_suffix,          :id => /txtSufix/) #suffix is misspelled on the page
text_field(:email_address,                :id => /txtEmailAddress/)
text_field(:client_id,                    :id => /txtClientId/)
image(:client_id_help,                    :id => /hbClientID/)
div(:client_id_help_div,                  :id => /hbClientID_answer/)
text_field(:agent_notes,                  :id => /txtPolicyNotes/)
image(:agent_notes_help,                  :id => /hbAgentNotes/)
div(:agent_notes_help_div,                :id => /hbAgentNotes_answer/)
image(:close_button,                      :title => /Click to close/)
element(:policy_id,                       :id => /hidAS_PolicyID/)
DEFAULT_DATA ={
:effective_date => '',
:insured_name_first => 'Wile',
:insured_name_middle_initial => 'e',
:insured_name_last => 'Coyote',
:insured_birth_date => '03/24/1947',
:insured_gender => 'Male',
:coinsured_name_first => '',
:coinsured_name_middle_initial => '',
:coinsured_name_last => '',
:address_line_1 => '671 S High Street',
:address_line_2 => 'Apt C',
:city => 'Columbus',
:state => 'Ohio',
:zip_code => '43206',
:address_override => '',
:has_former_address => '',
:former_address => '212 Summitview Drive',
:former_address2 => 'Apt C',
:former_city => 'Lancaster',
:former_state => 'Ohio',
:former_zip => '43130',
:phone_number_areacode => '614',
:phone_number_prefix => '445',
:phone_number_suffix => '2593',
:email_address => '',
:client_id => 'wilddog1234',
:agent_notes => 'this space for rent',
:policy_id => 'foo'
}

这是一个方便的方法,可以将电子表格中任何选项卡上的数据转换为散列,第一行作为键值。

def self.excel_to_hash(folder_name, file_name, tab_name)
    # Takes an excel file name and a tab name, and returns an array of stripped, transposed rows
    # Sample call:  @@models = excel_to_hash File.join(Rails.root,'db/meta/model_headers.xlsx'), 'models'
    rows = []
    file = File.open(File.join(folder_name, file_name), mode = 'r')
    excel = Excelx.new(file.path, nil, :ignore)
    excel.default_sheet = excel.sheets.index(tab_name) + 1
    header = excel.row(1)
    (2..excel.last_row).each do |i|
      next unless excel.row(i)[0]
      row = Hash[[header, excel.row(i)].transpose]      
      row.each_key{|x| row[x] = row[x].to_s.strip if row[x]}
      rows << row
    end
    return rows
  end

然后叫它…

    data_folder = File.join(Rails.root, 'vendor','gems','omni','db','data')
    data_file = "my_data_spreadsheet"
    tab_name = "sheet1"
    my_data_in_a_hash = excel_to_hash data_folder, data_file, tab_name

这已经被验证到Roo gem 1.10.2

我明白了…

我将数据加载移动到填充函数,并更改电子表格标题行以匹配键名,然后将字符串值转换为符号并完全消除default_data散列。

该部分的新代码如下所示:

def populate(data = {})
  @state = get_state
  key = $excel.row(1)
  get_data = $excel.find(:all, :conditions => {'Policy_State' => "#{@state}"},:array => true)
  state_data = Hash[key.zip get_data[0]]
  # Convert column header strings to keys
  state_data = state_data.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
  page_data = state_data.merge(data)

最新更新