Skip to content

Commit

Permalink
Insert serial dates
Browse files Browse the repository at this point in the history
Numeric values should added as-is and not converted to strings.
This is specially important if the user is trying to add dates
in their serial form.
  • Loading branch information
Drowze committed Jul 23, 2020
1 parent 264a000 commit 85738f6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
17 changes: 12 additions & 5 deletions lib/google_drive/worksheet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,16 +183,23 @@ def [](*args)
# worksheet[1, 3] = "=A1+B1"
def []=(*args)
(row, col) = parse_cell_args(args[0...-1])
value = args[-1].to_s
validate_cell_value(value)
value = args[-1]

reload_cells unless @cells
if value.is_a?(Numeric)
@numeric_values[[row, col]] = value
else
value = value.to_s
validate_cell_value(value)
@numeric_values[[row, col]] = nil
end

@cells[[row, col]] = value
@input_values[[row, col]] = value
@numeric_values[[row, col]] = nil
@modified.add([row, col])
self.max_rows = row if row > @max_rows
self.max_cols = col if col > @max_cols
if value.empty?
if value == ''
@num_rows = nil
@num_cols = nil
else
Expand Down Expand Up @@ -558,7 +565,7 @@ def merge_cells(top_row, left_col, num_rows, num_cols, merge_type: 'MERGE_ALL')
# Changes the formatting of a range of cells to match the given number format.
# For example to change A1 to a percentage with 1 decimal point:
# worksheet.set_number_format(1, 1, 1, 1, "##.#%")
# Google API reference: https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets#numberformat
# Google API reference: https://developers.google.com/sheets/api/guides/formats
def set_number_format(top_row, left_col, num_rows, num_cols, pattern, type: "NUMBER")
number_format = Google::Apis::SheetsV4::NumberFormat.new(type: type, pattern: pattern)
format = Google::Apis::SheetsV4::CellFormat.new(number_format: number_format)
Expand Down
17 changes: 13 additions & 4 deletions test/test_google_drive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,27 +44,36 @@ def test_spreadsheet_online
ws[1, 2] = '5'
ws[1, 3] = '=A1+B1'
ws[1, 4] = 13

ws.set_number_format(1, 5, 1, 2, 'yyyy-mm-dd h:mm:ss')
ws[1, 5] = 25569
ws[1, 6] = 25570.5

assert { ws.max_rows == 20 }
assert { ws.max_cols == 10 }
assert { ws.num_rows == 1 }
assert { ws.num_cols == 4 }
assert { ws.num_cols == 6 }
assert { ws[1, 1] == '3' }
assert { ws[1, 2] == '5' }
assert { ws[1, 4] == '13' }
assert { ws[1, 4] == 13 }
assert { ws[1, 5] == 25569 }
assert { ws[1, 6] == 25570.5 }
ws.save

ws.reload
assert { ws.max_rows == 20 }
assert { ws.max_cols == 10 }
assert { ws.num_rows == 1 }
assert { ws.num_cols == 4 }
assert { ws.num_cols == 6 }
assert { ws[1, 1] == '3' }
assert { ws[1, 2] == '5' }
assert { ws[1, 3] == '8' }
assert { ws[1, 4] == '13' }
assert { ws[1, 5] == '1970-01-01 0:00:00' }
assert { ws[1, 6] == '1970-01-02 12:00:00' }
assert { ws[1, 1].encoding == Encoding::UTF_8 }

assert { ss.export_as_string('csv') == '3,5,8,13' }
assert { ss.export_as_string('csv') == '3,5,8,13,1970-01-01 0:00:00,1970-01-02 12:00:00' }
assert { ss.available_content_types.empty? }

ss2 = session.spreadsheet_by_key(ss.key)
Expand Down

0 comments on commit 85738f6

Please sign in to comment.