lib/roo/excel.rb in roo-1.1.0 vs lib/roo/excel.rb in roo-1.2.0
- old
+ new
@@ -22,34 +22,30 @@
# Parameter packed: :zip - File is a zip-file
def initialize(filename, packed = nil, file_warning = :error)
super()
@file_warning = file_warning
@tmpdir = "oo_"+$$.to_s
+ @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 = unzip(filename) if packed and packed == :zip
- #if File.extname(filename).downcase != ".xls"
- # warn "are you sure, this is an excel file?"
- #end
- #@file_warning = :error
- file_type_check(filename,'.xls','an Excel')
- @filename = filename
begin
+ file_type_check(filename,'.xls','an Excel')
+ @filename = filename
unless File.file?(@filename)
raise IOError, "file #{@filename} does not exist"
end
@workbook = Spreadsheet::ParseExcel.parse(filename)
@default_sheet = nil
# no need to set default_sheet if there is only one sheet in the document
if self.sheets.size == 1
@default_sheet = self.sheets.first
end
- # @first_row = @last_row = @first_column = @last_column = nil
- #if ENV["roo_local"] != "thomas-p"
ensure
+ #if ENV["roo_local"] != "thomas-p"
FileUtils::rm_r(@tmpdir)
#end
end
@cell = Hash.new
@cell_type = Hash.new
@@ -80,23 +76,10 @@
end
end
return result
end
- #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
-
# 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
read_cells(sheet) unless @cells_read[sheet]
row,col = normalize(row,col)
@@ -173,11 +156,11 @@
end
private
# determine the first and last boundaries
- def get_firsts_lasts(sheet=nil)
+ def get_firsts_lasts_parseexcel(sheet=nil)
sheet = @default_sheet unless sheet
fr = fc = 999_999
lr = lc = -999_999
worksheet = @workbook.worksheet(sheet_no(sheet))
unless worksheet.min_row
@@ -210,10 +193,50 @@
while row(lr,sheet) == nil || empty_row?(row(lr,sheet))
lr -= 1
end
end
# 2007-11-05 END
+
+ if lc
+ letzte_spalte_leer = true
+ until ! letzte_spalte_leer
+ worksheet.each(0) {|reihe|
+ if reihe
+ cell = reihe.at(lc-1)
+ if cell
+ case cell.type
+ when :numeric, :date
+ letzte_spalte_leer = false
+ when :text
+ letzte_spalte_leer = false if cell.to_s != ""
+ end
+ end
+ end
+ }
+ lc -= 1 if letzte_spalte_leer
+ #puts "letzte Spalte auf #{lc} verringert" if letzte_spalte_leer
+ end
+ end
+
+ return fr, lr, fc, lc
+ end
+
+ # determine the first and last boundaries
+ def get_firsts_lasts(sheet=nil)
+ fr,lr,fc,lc = get_firsts_lasts_parseexcel(sheet)
+ if lr
+ # 2007-11-05 BEGIN
+ # parsexcel liefert (mir unverstaendlich) eine Zeile als letzte Zeile
+ # zurueck, die aber leer ist. Deshalb Korrekturfunktion, die wirklich
+ # die letzte nicht leere Zeile liefert.
+ # 2008-07-23 meine Loesung funtionierte auch noch nicht unter allen
+ # Umstaenden row() == nil ergaenzt
+ while row(lr,sheet) == nil || empty_row?(row(lr,sheet))
+ lr -= 1
+ end
+ end
+ # 2007-11-05 END
@first_row[sheet] = fr
@last_row[sheet] = lr
@first_column[sheet] = fc
@last_column[sheet] = lc
return fr, lr, fc, lc
@@ -241,10 +264,20 @@
end
}
! content
end
+ def empty_column?(col)
+ content = false
+ col.compact.each {|elem|
+ if elem != ''
+ content = true
+ end
+ }
+ ! content
+ end
+
def platform_specific_iconv(value)
case RUBY_PLATFORM.downcase
when /darwin/
result = Iconv.new('utf-8','utf-8').iconv(value)
when /solaris/
@@ -357,7 +390,21 @@
end
y += 1
}
@cells_read[sheet] = true
end
+
+ #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