lib/roo/excel.rb in roo-1.9.6 vs lib/roo/excel.rb in roo-1.9.7
- old
+ new
@@ -98,50 +98,32 @@
# Creates a new Excel spreadsheet object.
# Parameter packed: :zip - File is a zip-file
def initialize(filename, packed = nil, file_warning = :error)
super()
@file_warning = file_warning
- file_type_check(filename,'.xls','an Excel',packed)
- # @tmpdir = "oo_"+$$.to_s
+ file_type_check(filename,'.xls','an Excel',packed)
@tmpdir = GenericSpreadsheet.next_tmpdir
@tmpdir = File.join(ENV['ROO_TMP'], @tmpdir) if ENV['ROO_TMP']
unless File.exists?(@tmpdir)
FileUtils::mkdir(@tmpdir)
end
filename = open_from_uri(filename) if filename[0,7] == "http://"
filename = open_from_stream(filename[7..-1]) if filename[0,7] == "stream:"
filename = unzip(filename) if packed and packed == :zip
- #begin
- #begin
- # file_type_check(filename,'.xls','an Excel')
- #rescue TypeError
- # #FileUtils::rm_r @tmpdir
- # raise
- #end
- @filename = filename
- unless File.file?(@filename)
- FileUtils::rm_r(@tmpdir)
- raise IOError, "file #{@filename} does not exist"
- end
- begin
- @workbook = Spreadsheet.open(filename)
- rescue Ole::Storage::FormatError
- FileUtils::rm_r(@tmpdir)
- raise # nach aussen weiterhin sichtbar
- end
- @default_sheet = self.sheets.first
- #ensure
- #if ENV["roo_local"] != "thomas-p"
- # ich glaube ich darf hier noch nicht die temporaere
- # Datei loeschen, weil
- #to do
- #"Loeschen temp. Directory anpassen"
- #end
- #end
- #if fremdrechner?
+ @filename = filename
+ unless File.file?(@filename)
FileUtils::rm_r(@tmpdir)
- #end
+ raise IOError, "file #{@filename} does not exist"
+ end
+ begin
+ @workbook = Spreadsheet.open(filename)
+ rescue Ole::Storage::FormatError
+ FileUtils::rm_r(@tmpdir)
+ raise # nach aussen weiterhin sichtbar
+ end
+ @default_sheet = self.sheets.first
+ FileUtils::rm_r(@tmpdir)
@cell = Hash.new
@cell_type = Hash.new
@formula = Hash.new
@first_row = Hash.new
@last_row = Hash.new
@@ -152,15 +134,11 @@
@fonts = Hash.new
end
# returns an array of sheet names in the spreadsheet
def sheets
- result = []
- @workbook.worksheets.each do |worksheet|
- result << normalize_string(worksheet.name)
- end
- return result
+ @workbook.worksheets.collect {|worksheet| normalize_string(worksheet.name)}
end
# returns the content of a cell. The upper left corner is (1,1) or ('A',1)
def cell(row,col,sheet=nil)
sheet = @default_sheet unless sheet
@@ -173,11 +151,10 @@
return Date.new(yyyy.to_i,mm.to_i,dd.to_i)
end
if celltype(row,col,sheet) == :string
return platform_specific_iconv(@cell[sheet][[row,col]])
else
- #return @cell[sheet][[row,col]]
if @cell[sheet] and @cell[sheet][[row,col]]
return @cell[sheet][[row,col]]
else
return nil
end
@@ -198,11 +175,10 @@
row,col = normalize(row,col)
begin
if @formula[sheet] and @formula[sheet][[row,col]]
return :formula
else
- # @cell_type[sheet][[row,col]]
if @cell_type[sheet] and @cell_type[sheet][[row,col]]
return @cell_type[sheet][[row,col]]
else
return nil
end
@@ -352,11 +328,11 @@
@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]
+ case vt
when :float
@cell[sheet][key] = v.to_f
when :string
@cell[sheet][key] = v
when :date
@@ -386,11 +362,11 @@
row_index=1
worksheet.each(0) do |row|
(0..row.size).each do |cell_index|
cell = row.at(cell_index)
next if cell.nil? #skip empty cells
- next if cell.class == Spreadsheet::Formula && cell.value.nil? # skip empty formla cells
+ next if cell.class == Spreadsheet::Formula && cell.value.nil? # skip empty formula cells
if date_or_time?(row, cell_index)
vt, v = read_cell_date_or_time(row, cell_index)
else
vt, v = read_cell(row, cell_index)
end
@@ -482,20 +458,6 @@
end # case
return value_type, value
end
private :read_cell
- #TODO: testing only
- # def inject_null_characters(str)
- # if str.class != String
- # return str
- # end
- # new_str=''
- # 0.upto(str.size-1) do |i|
- # new_str += str[i,1]
- # new_str += "\000"
- # end
- # new_str
- # end
- #
-
end