Skip to content

Instantly share code, notes, and snippets.

@basuneko
Created June 4, 2011 16:49
Show Gist options
  • Save basuneko/1008050 to your computer and use it in GitHub Desktop.
Save basuneko/1008050 to your computer and use it in GitHub Desktop.
Parsing a .xls price list with several categories using Spreadsheet gem
def create
require 'spreadsheet'
#for debugging purposes the file path is hardcoded
book = Spreadsheet.open "#{Rails.root}/public/price.xls"
sheet = book.worksheet 0
sheet.each do |row|
font = row.format(0).font # read font data of the first cell in the row
# My spreadsheet has 2 levels of categories, but I only need the second
if font.weight == 400 # subcategory or unit
if font.size == 11 # subcategory
@category.units.create(@units) if !@category.nil?
@units = [] # (re)initialize the array
begin
@category = Category.find_by_title row[0]
rescue # in case there is no such category
@category = nil
end
else # unit
if !@category.nil?
@units.push({ :title => row[0], :wholesale => row[1], #:retail => row[2] })
end
end
end
end
end
@zdavatz
Copy link

zdavatz commented Jun 4, 2011

This recognizes the font size and by doing so distinguishes between the colors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment