Skip to content

Commit

Permalink
Access numericValue, from ListRow.to_hash(numeric)
Browse files Browse the repository at this point in the history
  • Loading branch information
robermorales committed Jun 8, 2012
1 parent c327785 commit be51420
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
5 changes: 3 additions & 2 deletions lib/google_drive/list.rb
Expand Up @@ -93,8 +93,9 @@ def to_hash_array()
return self.map(){ |r| r.to_hash() }
end

def get(index, key) #:nodoc:
return @worksheet[index + 2, key_to_col(key)]
def get(index, key, numeric = false ) #:nodoc:
( numeric && @worksheet.numeric_value( index + 2, key_to_col(key) ) ) ||
@worksheet [ index + 2, key_to_col(key) ]
end

def set(index, key, value) #:nodoc:
Expand Down
4 changes: 2 additions & 2 deletions lib/google_drive/list_row.rb
Expand Up @@ -60,10 +60,10 @@ def clear()
end
end

def to_hash()
def to_hash( numeric = false )
result = {}
for key in @list.keys
result[key] = self[key]
result[key] = @list.get(@index, key, numeric)
end
return result
end
Expand Down
22 changes: 22 additions & 0 deletions lib/google_drive/worksheet.rb
Expand Up @@ -26,6 +26,7 @@ def initialize(session, spreadsheet, cells_feed_url, title = nil) #:nodoc:

@cells = nil
@input_values = nil
@numeric_values = nil
@modified = Set.new()
@list = nil

Expand Down Expand Up @@ -86,6 +87,7 @@ def []=(*args)
reload() if !@cells
@cells[[row, col]] = value
@input_values[[row, col]] = value
@numeric_values[[row, col]] = value.to_f
@modified.add([row, col])
self.max_rows = row if row > @max_rows
self.max_cols = col if col > @max_cols
Expand Down Expand Up @@ -116,6 +118,24 @@ def input_value(*args)
return @input_values[[row, col]] || ""
end

# Returns the numeric value of the cell. Arguments must be either
# (row number, column number) or cell name. Top-left cell is [1, 1].
#
# Returns nil if there is no numeric value
#
# Example:
# worksheet[1, 3] #=> "3,0" # it depends on locale, currency...
# worksheet.numeric_value(1, 3) #=> 3.0 # 3.0:Float
#
# More (official reference):
# https://developers.google.com/google-apps/spreadsheets/#working_with_cell-based_feeds
#
def numeric_value(*args)
(row, col) = parse_cell_args(args)
reload() if !@cells
return @numeric_values[[row, col]].to_f || nil
end

# Row number of the bottom-most non-empty row.
def num_rows
reload() if !@cells
Expand Down Expand Up @@ -198,12 +218,14 @@ def reload()

@cells = {}
@input_values = {}
@numeric_values = {}
doc.css("feed > entry").each() do |entry|
cell = entry.css("gs|cell")[0]
row = cell["row"].to_i()
col = cell["col"].to_i()
@cells[[row, col]] = cell.inner_text
@input_values[[row, col]] = cell["inputValue"]
@numeric_values[[row, col]] = cell["numericValue"]
end
@modified.clear()
@meta_modified = false
Expand Down

0 comments on commit be51420

Please sign in to comment.