使用Roo导入Excel文件,错误:不赞成将“packed”或“file_warning”作为单独的参数提供给“Roo:



导入代码:

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]
    region = find_by_id(row["id"]) || new
    region.attributes = row.to_hash.slice(*row.to_hash.keys)
    region.save!
  end
end
def self.open_spreadsheet(file)
  case File.extname(file.original_filename)
  when ".csv" then Roo::Csv.new(file.path, nil, :ignore)
  when ".xls" then Roo::Excel.new(file.path, nil, :ignore)
  when ".xlsx" then Roo::Excelx.new(file.path, nil, :ignore)
  else raise "Unknown file type: #{file.original_filename}"
  end
end

错误:

Supplying `packed` or `file_warning` as separate arguments to `Roo::Excel.new` is deprecated. Use an options hash instead.

我知道我需要在某个地方通过options = {}考试,但我不确定在哪里。我在谷歌上搜索过,看到了一些示例代码,但它们的用途完全不同。

谢谢你的帮助!

尝试更改:

when ".csv" then Roo::Csv.new(file.path, nil, :ignore)
when ".xls" then Roo::Excel.new(file.path, nil, :ignore)
when ".xlsx" then Roo::Excelx.new(file.path, nil, :ignore)

when ".csv" then Roo::Csv.new(file.path, packed: nil, file_warning: :ignore)
when ".xls" then Roo::Excel.new(file.path, packed: nil, file_warning: :ignore)
when ".xlsx" then Roo::Excelx.new(file.path, packed: nil, file_warning: :ignore)

看起来您当前拥有的是Roo::方法调用的旧版本。

当前版本的roo的initialize方法需要一个filename&选项参数。将file.path, packed: nil, file_warning: :ignore设置为params将使其使用第一个param作为文件名,其余params作为选项的散列。

最新更新