bcflow
bcflow

Reputation: 31

Importing Excel file using Roo, error: Supplying `packed` or `file_warning` as separate arguments to `Roo::Excel.new` is deprecated

Import Code:

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

Error:

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

I know I need to pass an options = {} somewhere, but I'm not sure where. I've googled this and saw some example codes but they are for completely different uses.

Thanks for your help!

Upvotes: 0

Views: 1653

Answers (1)

Prakash Murthy
Prakash Murthy

Reputation: 13067

Try changing:

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)

to

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)

Looks like what you have currently is an older version of the Roo:: method calls.

The initialize method for the current version of roo expects a filename & an options parameter. Setting file.path, packed: nil, file_warning: :ignore as the params will make it use the first param as the filename, and the remaining params as a hash of options.

Upvotes: 4

Related Questions