lib/roo/google.rb in roo-1.2.1 vs lib/roo/google.rb in roo-1.2.2

- old
+ new

@@ -127,10 +127,18 @@ return false if string.class == Float return true if string.class == Date return string.strip =~ /^([0-9]+):([0-9]+):([0-9]+)$/ end + # is String a date+time with format DD/MM/YYYY HH:MM:SS + def Google.datetime?(string) + return false if string.class == Float + return true if string.class == Date + return string.strip =~ /^([0-9]+)\/([0-9]+)\/([0-9]+)\ ([0-9]+):([0-9]+):([0-9]+)$/ + end + + def Google.timestring_to_seconds(value) hms = value.split(':') hms[0].to_i*3600 + hms[1].to_i*60 + hms[2].to_i end @@ -141,30 +149,38 @@ def cell(row, col, sheet=nil) sheet = @default_sheet unless sheet check_default_sheet #TODO: 2007-12-16 read_cells(sheet) unless @cells_read[sheet] row,col = normalize(row,col) - if celltype(row,col,sheet) == :date yyyy,mm,dd = @cell[sheet]["#{row},#{col}"].split('-') begin return Date.new(yyyy.to_i,mm.to_i,dd.to_i) rescue ArgumentError raise "Invalid date parameter: #{yyyy}, #{mm}, #{dd}" end - end # celltype date + elsif celltype(row,col,sheet) == :datetime + begin + date_part,time_part = @cell[sheet]["#{row},#{col}"].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) + rescue ArgumentError + raise "Invalid date parameter: #{yyyy}, #{mm}, #{dd}, #{hh}, #{mi}, #{ss}" + end + end return @cell[sheet]["#{row},#{col}"] end # returns the type of a cell: # * :float # * :string # * :date # * :percentage # * :formula # * :time - # * :datetime + # * :datetime def celltype(row, col, sheet=nil) sheet = @default_sheet unless sheet read_cells(sheet) unless @cells_read[sheet] row,col = normalize(row,col) if @formula[sheet]["#{row},#{col}"] @@ -343,10 +359,12 @@ else value = numericvalue end elsif Google.date?(value) ty = :date + elsif Google.datetime?(value) + ty = :datetime elsif numeric?(value) # or o.class ??? ty = :float value = value.to_f elsif Google.time?(value) ty = :time @@ -357,9 +375,15 @@ key = "#{row},#{col}" @cell[sheet] = {} unless @cell[sheet] if ty == :date dd,mm,yyyy = value.split('/') @cell[sheet][key] = sprintf("%04d-%02d-%02d",yyyy.to_i,mm.to_i,dd.to_i) + elsif ty == :datetime + date_part,time_part = value.split(' ') + dd,mm,yyyy = date_part.split('/') + hh,mi,ss = time_part.split(':') + @cell[sheet][key] = sprintf("%04d-%02d-%02d %02d:%02d:%02d", + yyyy.to_i,mm.to_i,dd.to_i,hh.to_i,mi.to_i,ss.to_i) else @cell[sheet][key] = value unless value == "" or value == nil end @cell_type[sheet][key] = ty # Openoffice.oo_type_2_roo_type(vt) @formula[sheet] = {} unless @formula[sheet]