From be514205e1b7c1df660da7fbd21208ef1638223b Mon Sep 17 00:00:00 2001 From: Rober Morales-Chaparro Date: Fri, 8 Jun 2012 12:06:52 +0200 Subject: [PATCH] Access numericValue, from ListRow.to_hash(numeric) --- lib/google_drive/list.rb | 5 +++-- lib/google_drive/list_row.rb | 4 ++-- lib/google_drive/worksheet.rb | 22 ++++++++++++++++++++++ 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/lib/google_drive/list.rb b/lib/google_drive/list.rb index 4948cdc..cd45451 100644 --- a/lib/google_drive/list.rb +++ b/lib/google_drive/list.rb @@ -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: diff --git a/lib/google_drive/list_row.rb b/lib/google_drive/list_row.rb index f2b4032..ef7f26e 100644 --- a/lib/google_drive/list_row.rb +++ b/lib/google_drive/list_row.rb @@ -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 diff --git a/lib/google_drive/worksheet.rb b/lib/google_drive/worksheet.rb index 4410ec5..071c07e 100644 --- a/lib/google_drive/worksheet.rb +++ b/lib/google_drive/worksheet.rb @@ -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 @@ -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 @@ -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 @@ -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