Skip to content

Commit

Permalink
Update Excel to use new parseexcel and read font info
Browse files Browse the repository at this point in the history
  • Loading branch information
--unset committed Feb 10, 2009
1 parent 3fe077c commit 848cb27
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
35 changes: 32 additions & 3 deletions lib/roo/excel.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'rubygems'
gem 'parseexcel', '>= 0.5.2'
gem 'parseexcel', '>= 0.5.3'
require 'parseexcel'
CHARGUESS = false
require 'charguess' if CHARGUESS
Expand Down Expand Up @@ -57,6 +57,7 @@ def initialize(filename, packed = nil, file_warning = :error)
@last_column = Hash.new
@header_line = 1
@cells_read = Hash.new
@fonts = Hash.new
end

# returns an array of sheet names in the spreadsheet
Expand Down Expand Up @@ -169,6 +170,31 @@ def formulas(sheet=nil)
raise EXCEL_NO_FORMULAS
end

# Given a cell, return the cell's style name
def cell_font(row, col, sheet=nil)
sheet = @default_sheet unless sheet
read_cells(sheet) unless @cells_read[sheet]
row,col = normalize(row,col)
@fonts[sheet][[row,col]]
end
private :cell_font

# true if the cell style is bold
def bold?(*args)
cell_font(*args)[:bold]
end

# true if the cell style is italic
def italic?(*args)
cell_font(*args)[:italic]
end

# true if the cell style is underline
def underlined?(*args)
cell_font(*args)[:underline]
end


# shows the internal representation of all cells
# mainly for debugging purposes
def to_s(sheet=nil)
Expand Down Expand Up @@ -291,14 +317,17 @@ def remove_every_second_null(str)
end

# helper function to set the internal representation of cells
def set_cell_values(sheet,x,y,i,v,vt,formula,tr,str_v)
def set_cell_values(sheet,x,y,i,v,vt,formula,tr,str_v, font)
#key = "#{y},#{x+i}"
key = [y,x+i]
@cell_type[sheet] = {} unless @cell_type[sheet]
@cell_type[sheet][key] = vt
@formula[sheet] = {} unless @formula[sheet]
@formula[sheet][key] = formula if formula
@cell[sheet] = {} unless @cell[sheet]
@fonts[sheet] = {} unless @fonts[sheet]
@fonts[sheet][key] = font

case vt # @cell_type[sheet][key]
when :float
@cell[sheet][key] = v.to_f
Expand Down Expand Up @@ -378,7 +407,7 @@ def read_cells(sheet=nil)
v = nil
end # case
formula = tr = nil #TODO:???
set_cell_values(sheet,x,y,i,v,vt,formula,tr,str_v)
set_cell_values(sheet,x,y,i,v,vt,formula,tr,str_v, cell.font)
end # if cell

x += 1
Expand Down
4 changes: 4 additions & 0 deletions test/test_roo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4908,6 +4908,10 @@ def test_cell_styles
oo = Excelx.new(File.join(TESTDIR,"style.xlsx"))
verify_cell_fonts(oo)
end
if EXCEL
oo = Excel.new(File.join(TESTDIR,"style.xls"))
verify_cell_fonts(oo)
end
end

end # class

0 comments on commit 848cb27

Please sign in to comment.