使用 roo 导入 excel 文件时如何保存空白字符?
How can I save blank characters when importing excel file using 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 描述:
型号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
不要在导入方法中转置以将 excel 行转换为以 header 值作为键的散列,
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
当单元格为空时导入 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 描述:
型号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
不要在导入方法中转置以将 excel 行转换为以 header 值作为键的散列,
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