2016-05-13 56 views
0

我得到錯誤,當我導入Excel文件時,細胞是空白的,我需要能夠保存 「」如何使用roo導入excel文件時保存空白字符?

select * from clients where doc_nat = '1234' 
Insert into clients (doc_nat,name_nat,doc_jur,name_jur) 
values ('1234','cr7','','') 

select * from clients where doc_jur = '1234' 
Insert into clients (doc_nat,name_nat,doc_jur,name_jur) 
values ('','','5678','Messi') 

Excel文件:

|doc_nat| |name_nat| |doc_jur| |name_jur| 
1234   Cr7   
         56789  Messi 

Excel中的描述:

Excel description

型號Client.rb

COLUMNS_TO_STRING = ["doc_nat","name_nat", "doc_jur","name_jur" ] 

def self.import(file) 
    spreadsheet = open_spreadsheet(file) 
    header = spreadsheet.row(1) 
    (2..spreadsheet.last_row).each do |i| 
    row = Hash[[header, spreadsheet.row(i)].transpose] 
    row = clean_for row, COLUMNS_TO_STRING 

    if row["doc_nat"].present? 
     record = find_or_create_by(row["doc_nat"]) || new 
    else 
     record = find_by_doc_jur(row["doc_jur"]) || new 
    end  

    record.attributes = row.to_hash.slice(*row.to_hash.keys) 
    record.save! 
    end 
end 

def self.clean_for row_as_hash, string_columns_array 
    row_as_hash.each do |key, value| 
    if string_columns_array.include?key 
     row_as_hash[key] = value.to_i.to_s 
    end 
    end 
end 

def self.open_spreadsheet(file) 
    case File.extname(file.original_filename) 
    when ".csv" then Csv.new(file.path, nil, :ignore) 
    when ".xls" then Excel.new(file.path, nil, :ignore) 
    when ".xlsx" then Excelx.new(file.path, nil, :ignore) 
    else raise "Unknown format: #{file.original_filename}" 
    end 
end 

我得到這個消息:

element size differs (2 should be 4) 

我的日誌:

Processing ClientController#import (for 127.0.0.1 at 2016-05-13 09:39:14) [POST] 
Parameters: {"authenticity_token"=>"test, "file"=>#<File:/tmp/RackMultipart20160513-4217-6ijga7-0>, "commit"=>"Import"} 

IndexError (element size differs (3 should be 4)): 
app/models/client.rb:38:in `transpose' 
app/models/client.rb:38:in `import' 
app/models/client.rb:37:in `each' 
app/models/client.rb:37:in `import' 

溶液添加在空格字符:

#excel file 
|doc_nat| |name_nat| |doc_jur| |name_jur| 
    1234   Cr7  .   . 
     .   .  56789  Messi 

#sql log description 
select * from clients where doc_nat = '1234' 
Insert into clients (doc_nat,name_nat,doc_jur,name_jur) 
values ('1234','cr7','0','0') 

select * from clients where doc_jur = '1234' 
Insert into clients (doc_nat,name_nat,doc_jur,name_jur) 
values ('0','0','5678','Messi') 

我要救像這樣或者可以保存空白字符:

if row["doc_nat"].present? 
     record = find_or_create_by(row["doc_nat"]) || new 
     record = record.doc_jur = "" 
     record = record.num_jur = "" 
    else 
     record = find_by_doc_jur(row["doc_jur"]) || new 
     record = record.doc_nat = "" 
     record = record.num_nat = "" 
    end  

而且我也試過:

if row["doc_nat"].present? 
     record = find_or_create_by(row["doc_nat"]) || new 
     record = record.doc_jur = row["doc_jur"] 
     record = record.num_jur = row["num_jur"] 
    else 
     record = find_by_doc_jur(row["doc_jur"]) || new 
     record = record.doc_nat = row["doc_nat"] 
     record = record.num_nat = row["num_nat"] 
    end 
+0

您是否介意向'放row.inspect'立即一行之後'=散列[[報頭,spreadsheet.row(I)]。移調]'和後的輸出這裏? – mudasobwa

+0

row = Hash [[header,spreadsheet.row(i)]。轉置] puts row.inspect我得到相同的日誌,我張貼:處理ClientController#導入(對於127.0.0.1在2016-05-13 09:39 :14)[POST] 參數:{「authenticity_token」=>「test,」file「=>#」commit「=>」導入「} IndexError(元素大小不同(3應爲4)): app/models/client.rb:38:in'transpose' app/models/client.rb:38:in'import' app/models/client。 rb:37:in'each' app/models/client.rb:37:in'import' –

+0

嘗試使用'row = header.zip(spreadsheet.row(i))。to_h'而不是'row = Hash [[header,spreadsheet.row(i)]。轉置]'。 – mudasobwa

回答

1

而是在轉換Excel中的行與標頭值作爲鍵哈希您的導入方法轉置,

row = Hash[[header, spreadsheet.row(i)].transpose] 

做,

row_data = hash[header.zip spreadsheet.row(i)] 

它會處理缺失的值

2.1.0:005> Hash[['a','b'].zip [1]] 
=> {"a"=>1, "b"=>nil} 

其中作爲

2.1.0 :007 > Hash[[['a','b'],[1]].transpose] 
=> IndexError: element size differs (1 should be 2) 
from (irb):7:in `transpose' 
from (irb):7 
+0

我現在有這個錯誤:無法將數組轉換爲整數 –

+0

Thaks it works row = Hash [header.zip spreadsheet.row(i)] –

相關問題