lib/roo/excel.rb in roo-0.4.1 vs lib/roo/excel.rb in roo-0.5.0

- old
+ new

@@ -11,42 +11,48 @@ @workbook = Spreadsheet::ParseExcel.parse(filename) @default_sheet = nil @first_row = @last_row = @first_column = @last_column = nil end - # TODO: waiting for - # ich glaube, parseexcel kann keine namen der sheets ??? def sheets - if DateTime.now < Date.new(2007,6,10) - return ["Tabelle1", "Name of Sheet 2", "Sheet3"] - else - #worksheet = @workbook.worksheet(0) - # p @workbook - # p @workbook.worksheet(0) - ["aaa","bbb","ccc"] - end + #if DateTime.now < Date.new(2007,6,10) + # return ["Tabelle1", "Name of Sheet 2", "Sheet3"] + #else + result = [] + 0.upto(@workbook.sheet_count - 1) do |i| + # TODO: is there a better way to do conversion? + result << Iconv.new('utf-8','unicode').iconv( + @workbook.worksheet(i).name + ) + end + return result + #end end # sets the working sheet (1,2,3,..) #-- # im Excel-Bereich muesste man wahrscheinlich intern mit Nummern arbeiten # von aussen arbeite ich mit (1,2,3... intern wird Index 0,1,2,... # verwendet. def default_sheet=(n) - unless n.kind_of?(Fixnum) - fail ArgumentError.new("Number expected") + if DateTime.now < Date.new(2007,7,19) + unless n.kind_of?(Fixnum) + fail ArgumentError.new("Number expected") + end + @default_sheet = n-1 + else + # parseexcel supports now the name of a sheet + @default_sheet = n end - @default_sheet = n-1 @first_row = @last_row = @first_column = @last_column = nil @cells_read = false end # returns the content of a cell. The upper left corner is (1,1) or ('A',1) def cell(row,col) row,col = normalize(row,col) - default_sheet_check - worksheet = @workbook.worksheet(@default_sheet) + worksheet = @workbook.worksheet(sheet_no(@default_sheet)) skip = 0 line = 1 worksheet.each(skip) { |row_par| if line == row if row_par == nil @@ -68,32 +74,32 @@ # returns the type of a cell: "float", "string", "date" def celltype(row,col) row,col = normalize(row,col) default_sheet_check - worksheet = @workbook.worksheet(@default_sheet) + worksheet = @workbook.worksheet(sheet_no(@default_sheet)) skip = 0 line = 1 worksheet.each(skip) { |row_par| if line == row cell = row_par.at(col-1) return nil unless cell case cell.type - when :numeric then return "float" - when :text then return "string" - when :date then return "date" - else return cell.type + when :numeric then return :float + when :text then return :string + when :date then return :date + else return cell.type.to_sym end end line += 1 } end # return this row a an array off cells def row(rownumber) default_sheet_check - worksheet = @workbook.worksheet(@default_sheet) + worksheet = @workbook.worksheet(sheet_no(@default_sheet)) therow = worksheet.row(rownumber-1) result = [] therow.each {|cell| case cell.type when :numeric then result << cell.to_i @@ -155,11 +161,11 @@ # determine the first and last boundaries def get_firsts_lasts fr = fc = 999_999 lr = lc = -999_999 - worksheet = @workbook.worksheet(@default_sheet) + worksheet = @workbook.worksheet(sheet_no(@default_sheet)) skip = 0 line = 1 worksheet.each(skip) { |row_par| if row_par row_par.each_with_index {|cell,i| @@ -184,6 +190,18 @@ @first_column = fc @last_column = lc return fr, lr, fc, lc end + + # converts name of a sheet to index (0,1,2,..) + def sheet_no(name) + return name-1 if name.kind_of?(Fixnum) + 0.upto(@workbook.sheet_count - 1) do |i| + # TODO: is there a better way to do conversion? + return i if name == Iconv.new('utf-8','unicode').iconv( + @workbook.worksheet(i).name + ) + end + raise StandardError, "sheet '#{name}' not found" + end end