0

セルが空白のときに 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')

エクセルファイル:

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

エクセルの説明:

エクセルの説明

モデル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 
4

1 に答える 1