Skip to content

Commit

Permalink
Minor cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
Empact committed Jun 9, 2014
1 parent b131196 commit 066e627
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 50 deletions.
93 changes: 45 additions & 48 deletions lib/roo/excelx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ def initialize(filename, options = {}, deprecated_file_warning = :error)
unless File.file?(@filename)
raise IOError, "file #{@filename} does not exist"
end
@comments_files = Array.new
@rels_files = Array.new
@comments_files = []
@rels_files = []
extract_content(tmpdir, @filename)
@workbook_doc = load_xml(File.join(tmpdir, "roo_workbook.xml"))
@shared_table = []
Expand All @@ -94,7 +94,7 @@ def initialize(filename, options = {}, deprecated_file_warning = :error)
read_shared_strings(@sharedstring_doc)
end
@styles_table = []
@style_definitions = Array.new # TODO: ??? { |h,k| h[k] = {} }
@style_definitions = [] # TODO: ??? { |h,k| h[k] = {} }
if File.exist?(File.join(tmpdir, 'roo_styles.xml'))
@styles_doc = load_xml(File.join(tmpdir, 'roo_styles.xml'))
read_styles(@styles_doc)
Expand All @@ -104,14 +104,14 @@ def initialize(filename, options = {}, deprecated_file_warning = :error)
@rels_doc = load_xmls(@rels_files)
end
super(filename, options)
@formula = Hash.new
@excelx_type = Hash.new
@excelx_value = Hash.new
@s_attribute = Hash.new # TODO: ggf. wieder entfernen nur lokal benoetigt
@comment = Hash.new
@comments_read = Hash.new
@hyperlink = Hash.new
@hyperlinks_read = Hash.new
@formula = {}
@excelx_type = {}
@excelx_value = {}
@s_attribute = {} # TODO: ggf. wieder entfernen nur lokal benoetigt
@comment = {}
@comments_read = {}
@hyperlink = {}
@hyperlinks_read = {}
end

def method_missing(m,*args)
Expand All @@ -138,11 +138,12 @@ def cell(row, col, sheet=nil)
row,col = normalize(row,col)
if celltype(row,col,sheet) == :date
yyyy,mm,dd = @cell[sheet][[row,col]].split('-')
return Date.new(yyyy.to_i,mm.to_i,dd.to_i)
Date.new(yyyy.to_i,mm.to_i,dd.to_i)
elsif celltype(row,col,sheet) == :datetime
return create_datetime_from( @cell[sheet][[row,col]] )
create_datetime_from( @cell[sheet][[row,col]] )
else
@cell[sheet][[row,col]]
end
@cell[sheet][[row,col]]
end

# Returns the formula at (row,col).
Expand All @@ -162,8 +163,8 @@ def formulas(sheet=nil)
sheet ||= @default_sheet
read_cells(sheet)
if @formula[sheet]
@formula[sheet].each.collect do |elem|
[elem[0][0], elem[0][1], elem[1]]
@formula[sheet].map do |coord, formula|
[coord[0], coord[1], formula]
end
else
[]
Expand Down Expand Up @@ -210,7 +211,7 @@ def celltype(row,col,sheet=nil)
read_cells(sheet)
row,col = normalize(row,col)
if @formula[sheet][[row,col]]
return :formula
:formula
else
@cell_type[sheet][[row,col]]
end
Expand All @@ -224,7 +225,7 @@ def excelx_type(row,col,sheet=nil)
sheet ||= @default_sheet
read_cells(sheet)
row,col = normalize(row,col)
return @excelx_type[sheet][[row,col]]
@excelx_type[sheet][[row,col]]
end

# returns the internal value of an excelx cell
Expand All @@ -233,7 +234,7 @@ def excelx_value(row,col,sheet=nil)
sheet ||= @default_sheet
read_cells(sheet)
row,col = normalize(row,col)
return @excelx_value[sheet][[row,col]]
@excelx_value[sheet][[row,col]]
end

# returns the internal format of an excel cell
Expand Down Expand Up @@ -265,11 +266,11 @@ def to_s(sheet=nil)
def label(labelname)
read_labels
if @label.empty? || !@label.has_key?(labelname)
return nil,nil,nil
[nil,nil,nil]
else
return @label[labelname][1].to_i,
Roo::Base.letter_to_number(@label[labelname][2]),
@label[labelname][0]
[@label[labelname][1].to_i,
self.class.letter_to_number(@label[labelname][2]),
@label[labelname][0]]
end
end

Expand All @@ -282,7 +283,7 @@ def labels
@label.map do |label|
[ label[0], # name
[ label[1][1].to_i, # row
Roo::Base.letter_to_number(label[1][2]), # column
self.class.letter_to_number(label[1][2]), # column
label[1][0], # sheet
] ]
end
Expand All @@ -298,8 +299,7 @@ def hyperlink(row,col,sheet=nil)
sheet ||= @default_sheet
read_hyperlinks(sheet) unless @hyperlinks_read[sheet]
row,col = normalize(row,col)
return nil unless @hyperlink[sheet]
@hyperlink[sheet][[row,col]]
@hyperlink[sheet] && @hyperlink[sheet][[row,col]]
end

# returns the comment at (row/col)
Expand All @@ -309,8 +309,7 @@ def comment(row,col,sheet=nil)
#read_cells(sheet)
read_comments(sheet) unless @comments_read[sheet]
row,col = normalize(row,col)
return nil unless @comment[sheet]
@comment[sheet][[row,col]]
@comment[sheet] && @comment[sheet][[row,col]]
end

# true, if there is a comment
Expand Down Expand Up @@ -349,7 +348,7 @@ def set_cell_values(sheet,x,y,i,v,value_type,formula,
@cell_type[sheet] ||= {}
@cell_type[sheet][key] = value_type
@formula[sheet] ||= {}
@formula[sheet][key] = formula if formula
@formula[sheet][key] = formula if formula
@cell[sheet] ||= {}
@cell[sheet][key] =
case @cell_type[sheet][key]
Expand Down Expand Up @@ -417,7 +416,7 @@ def read_cells(sheet=nil)
value_type = :string
v = inlinestr_content
excelx_type = :string
y, x = Roo::Base.split_coordinate(c['r'])
y, x = self.class.split_coordinate(c['r'])
excelx_value = inlinestr_content #cell.content
set_cell_values(sheet,x,y,0,v,value_type,formula,excelx_type,excelx_value,s_attribute)
end
Expand Down Expand Up @@ -458,7 +457,7 @@ def read_cells(sheet=nil)
value_type = :float
cell.content
end
y, x = Roo::Base.split_coordinate(c['r'])
y, x = self.class.split_coordinate(c['r'])
set_cell_values(sheet,x,y,0,v,value_type,formula,excelx_type,excelx_value,s_attribute)
end
end
Expand Down Expand Up @@ -516,7 +515,7 @@ def read_comments(sheet=nil)
return unless @comments_doc[n] #>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
@comments_doc[n].xpath("//xmlns:comments/xmlns:commentList/xmlns:comment").each do |comment|
ref = comment.attributes['ref'].to_s
row,col = Roo::Base.split_coordinate(ref)
row,col = self.class.split_coordinate(ref)
comment.xpath('./xmlns:text/xmlns:r/xmlns:t').each do |text|
@comment[sheet] ||= {}
@comment[sheet][[row,col]] = text.text
Expand All @@ -536,7 +535,7 @@ def read_hyperlinks(sheet=nil)
end]
@sheet_doc[n].xpath("/xmlns:worksheet/xmlns:hyperlinks/xmlns:hyperlink[id]").each do |h|
if rel_element = rels[h.attribute('id').text]
row,col = Roo::Base.split_coordinate(h.attributes['ref'].to_s)
row,col = self.class.split_coordinate(h.attributes['ref'].to_s)
@hyperlink[sheet] ||= {}
@hyperlink[sheet][[row,col]] = rel_element.attribute('Target').text
end
Expand Down Expand Up @@ -662,27 +661,25 @@ def attribute2format(s)
end

def base_date
@base_date ||= read_base_date
end

# Default to 1900 (minus one day due to excel quirk) but use 1904 if
# it's set in the Workbook's workbookPr
# http://msdn.microsoft.com/en-us/library/ff530155(v=office.12).aspx
def read_base_date
base_date = Date.new(1899,12,30)
@workbook_doc.xpath("//xmlns:workbookPr").map do |workbookPr|
if workbookPr["date1904"] && workbookPr["date1904"] =~ /true|1/i
base_date = Date.new(1904,01,01)
@base_date ||=
begin
# Default to 1900 (minus one day due to excel quirk) but use 1904 if
# it's set in the Workbook's workbookPr
# http://msdn.microsoft.com/en-us/library/ff530155(v=office.12).aspx
@workbook_doc.xpath("//xmlns:workbookPr[date1904]").each do |workbookPr|
if workbookPr["date1904"] =~ /true|1/i
return Date.new(1904,01,01)
end
end
Date.new(1899,12,30)
end
end
base_date
end

def create_datetime_from(datetime_string)
date_part,time_part = round_time_from(datetime_string).split(' ')
yyyy,mm,dd = date_part.split('-')
hh,mi,ss = time_part.split(':')
return DateTime.civil(yyyy.to_i,mm.to_i,dd.to_i,hh.to_i,mi.to_i,ss.to_i)
DateTime.civil(yyyy.to_i,mm.to_i,dd.to_i,hh.to_i,mi.to_i,ss.to_i)
end

def round_time_from(datetime_string)
Expand All @@ -691,4 +688,4 @@ def round_time_from(datetime_string)
hh,mi,ss = time_part.split(':')
Time.new(yyyy.to_i, mm.to_i, dd.to_i, hh.to_i, mi.to_i, ss.to_r).round(0).strftime("%Y-%m-%d %H:%M:%S")
end
end # class
end
4 changes: 2 additions & 2 deletions lib/roo/openoffice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def label(labelname)
end
if @label.has_key? labelname
return @label[labelname][1].to_i,
Roo::Base.letter_to_number(@label[labelname][2]),
self.class.letter_to_number(@label[labelname][2]),
@label[labelname][0]
else
return nil,nil,nil
Expand All @@ -203,7 +203,7 @@ def labels(sheet=nil)
@label.map do |label|
[ label[0], # name
[ label[1][1].to_i, # row
Roo::Base.letter_to_number(label[1][2]), # column
self.class.letter_to_number(label[1][2]), # column
label[1][0], # sheet
] ]
end
Expand Down

0 comments on commit 066e627

Please sign in to comment.