lib/roo/openoffice.rb in roo-0.1.1 vs lib/roo/openoffice.rb in roo-0.1.2

- old
+ new

@@ -4,12 +4,19 @@ # require 'matrix' require 'fileutils' require 'zip/zipfilesystem' require 'date' +class Fixnum + def as_letter + "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[self-1,1] + end +end + class Openoffice + def initialize(filename) @cells_read = false @filename = filename @tmpdir = "oo_"+$$.to_s unless File.exists?(@tmpdir) @@ -39,11 +46,11 @@ else raise FormatError end end if col.class == String - col = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".index(col.upcase)+1 + col = Openoffice.letter_to_number(col) end read_cells unless @cells_read @cell["#{row},#{col}"] end @@ -165,10 +172,22 @@ def last_column_as_letter number_to_letter(last_column) end + def first_row_as_letter + number_to_letter(first_row) + end + + def last_row_as_letter + number_to_letter(last_row) + end + + def as_letter(n) + number_to_letter(last_row) + end + private def number_to_letter(n) "ABCDEFGHIJKLMNOPQRSTUVWXYZ"[n-1,1] end @@ -238,40 +257,31 @@ end y += 1 x = 1 end end - # p se.attributes['name'] - # return_sheets << se.attributes['name'] - end # richtiges sheet + end # sheet end end end end end end - # puts oo_element_count.to_s+" oo_element_count " end - # puts oo_document_count.to_s+" oo_document_count " - # p @cell @cells_read = true end def process_zipfile(zip, path='') if zip.file.file? path - # puts %{#{path}: "#{zip.read(path)}"} - # puts %{#{path}:} - if path == "content.xml" open(@tmpdir+'/'+'roo_content.xml','w') {|f| f << zip.read(path) } end else unless path.empty? path += '/' - # puts path end zip.dir.foreach(path) do |filename| process_zipfile(zip, path+filename) end end @@ -280,8 +290,19 @@ def extract_content Zip::ZipFile.open(@filename) do |zip| process_zipfile(zip) end + end + + def Openoffice.letter_to_number(letters) + result = 0 + while letters && letters.length > 0 + character = letters[0,1].upcase + num = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".index(character)+1 + result = result * 26 + num + letters = letters[1..-1] + end + result end end