lib/roo/google.rb in roo-0.9.1 vs lib/roo/google.rb in roo-0.9.2
- old
+ new
@@ -127,10 +127,22 @@
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 time with format HH:MM:SS?
+ def Google.time?(string)
+ return false if string.class == Float
+ return true if string.class == Date
+ return string.strip =~ /^([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
+
# Returns the content of a spreadsheet-cell.
# (1,1) is the upper left corner.
# (1,1), (1,'A'), ('A',1), ('a',1) all refers to the
# cell at the first line and first row.
def cell(row, col, sheet=nil)
@@ -161,10 +173,11 @@
# returns the type of a cell:
# * :float
# * :string,
# * :date
# * :percentage
+ # * :time
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}"]
@@ -239,10 +252,11 @@
def empty?(row, col, sheet=nil)
value = cell(row, col, sheet)
return true unless value
return false if value.class == Date # a date is never empty
return false if value.class == Float
+ return false if celltype(row,col,sheet) == :time
value.empty?
end
# returns all values in this column as an array
# column numbers are 1,2,3,... like in the spreadsheet
@@ -313,94 +327,10 @@
@first_row[sheet], @last_row[sheet], @first_column[sheet], @last_column[sheet] = @gs.oben_unten_links_rechts(sheet_no)
end
return @last_column[sheet]
end
- # find a row either by row number or a condition
- # Caution: this works only within the default sheet -> set default_sheet before you call this method
- # (experimental. see examples in the test_roo.rb file)
- #--
- # -----------------------------------------------------
- # !!!TODO: should be factored out to GenericSpreadsheet
- # also in Openofiffe
- # -----------------------------------------------------
- #++
- #def find(*args)
- # result_array = false
- # args.each {|arg,val|
- # if arg.class == Hash
- # arg.each { |hkey,hval|
- # if hkey == :array and hval == true
- # result_array = true
- # end
- # }
- # end
- # }
- # column_with = {}
- # 1.upto(last_column) do |col|
- # column_with[cell(@header_line,col)] = col
- # end
- # result = Array.new
- # #-- id
- # if args[0].class == Fixnum
- # rownum = args[0]
- # tmp = {}
- # 1.upto(self.row(rownum).size) {|j|
- # x = ''
- # column_with.each { |key,val|
- # if val == j
- # x = key
- # end
- # }
- # tmp[x] = cell(rownum,j)
- # }
- # result = [ tmp ] # row(rownum)
- # #-- :all
- # elsif args[0] == :all
- # if args[1].class == Hash
- # args[1].each {|key,val|
- # if key == :conditions
- # column_with = {}
- # 1.upto(last_column) do |col|
- # column_with[cell(@header_line,col)] = col
- # end
- # conditions = val
- # first_row.upto(last_row) do |i|
- # # are all conditions met?
- # found = 1
- # conditions.each { |key,val|
- # if cell(i,column_with[key]) == val
- # found *= 1
- # else
- # found *= 0
- # end
- # }
- # # p self.row(i) if found > 0
- # if found > 0
- # tmp = {}
- # 1.upto(self.row(i).size) {|j|
- # x = ''
- # column_with.each { |key,val|
- # if val == j
- # x = key
- # end
- # }
- # tmp[x] = cell(i,j)
- # }
- # if result_array
- # result << self.row(i)
- # else
- # result << tmp
- # end
- # end
- # end
- # end # :conditions
- # }
- # end
- # end
- # result
- #end
private
# read all cells in a sheet
def read_cells(sheet=nil)
sheet = @default_sheet unless sheet
@@ -428,9 +358,12 @@
elsif Google.date?(value)
ty = :date
elsif numeric?(value) # or o.class ???
ty = :float
value = value.to_f
+ elsif Google.time?(value)
+ ty = :time
+ value = Google.timestring_to_seconds(value)
else
ty = :string
end
key = "#{row},#{col}"
@cell[sheet] = {} unless @cell[sheet]