Created
June 4, 2011 16:49
-
-
Save basuneko/1008050 to your computer and use it in GitHub Desktop.
Parsing a .xls price list with several categories using Spreadsheet gem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This recognizes the font size and by doing so distinguishes between the colors.