Skip to content

Commit 85738f6

Browse files
committed
Insert serial dates
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.
1 parent 264a000 commit 85738f6

2 files changed

Lines changed: 25 additions & 9 deletions

File tree

lib/google_drive/worksheet.rb

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,16 +183,23 @@ def [](*args)
183183
# worksheet[1, 3] = "=A1+B1"
184184
def []=(*args)
185185
(row, col) = parse_cell_args(args[0...-1])
186-
value = args[-1].to_s
187-
validate_cell_value(value)
186+
value = args[-1]
187+
188188
reload_cells unless @cells
189+
if value.is_a?(Numeric)
190+
@numeric_values[[row, col]] = value
191+
else
192+
value = value.to_s
193+
validate_cell_value(value)
194+
@numeric_values[[row, col]] = nil
195+
end
196+
189197
@cells[[row, col]] = value
190198
@input_values[[row, col]] = value
191-
@numeric_values[[row, col]] = nil
192199
@modified.add([row, col])
193200
self.max_rows = row if row > @max_rows
194201
self.max_cols = col if col > @max_cols
195-
if value.empty?
202+
if value == ''
196203
@num_rows = nil
197204
@num_cols = nil
198205
else
@@ -558,7 +565,7 @@ def merge_cells(top_row, left_col, num_rows, num_cols, merge_type: 'MERGE_ALL')
558565
# Changes the formatting of a range of cells to match the given number format.
559566
# For example to change A1 to a percentage with 1 decimal point:
560567
# worksheet.set_number_format(1, 1, 1, 1, "##.#%")
561-
# Google API reference: https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets#numberformat
568+
# Google API reference: https://developers.google.com/sheets/api/guides/formats
562569
def set_number_format(top_row, left_col, num_rows, num_cols, pattern, type: "NUMBER")
563570
number_format = Google::Apis::SheetsV4::NumberFormat.new(type: type, pattern: pattern)
564571
format = Google::Apis::SheetsV4::CellFormat.new(number_format: number_format)

test/test_google_drive.rb

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,27 +44,36 @@ def test_spreadsheet_online
4444
ws[1, 2] = '5'
4545
ws[1, 3] = '=A1+B1'
4646
ws[1, 4] = 13
47+
48+
ws.set_number_format(1, 5, 1, 2, 'yyyy-mm-dd h:mm:ss')
49+
ws[1, 5] = 25569
50+
ws[1, 6] = 25570.5
51+
4752
assert { ws.max_rows == 20 }
4853
assert { ws.max_cols == 10 }
4954
assert { ws.num_rows == 1 }
50-
assert { ws.num_cols == 4 }
55+
assert { ws.num_cols == 6 }
5156
assert { ws[1, 1] == '3' }
5257
assert { ws[1, 2] == '5' }
53-
assert { ws[1, 4] == '13' }
58+
assert { ws[1, 4] == 13 }
59+
assert { ws[1, 5] == 25569 }
60+
assert { ws[1, 6] == 25570.5 }
5461
ws.save
5562

5663
ws.reload
5764
assert { ws.max_rows == 20 }
5865
assert { ws.max_cols == 10 }
5966
assert { ws.num_rows == 1 }
60-
assert { ws.num_cols == 4 }
67+
assert { ws.num_cols == 6 }
6168
assert { ws[1, 1] == '3' }
6269
assert { ws[1, 2] == '5' }
6370
assert { ws[1, 3] == '8' }
6471
assert { ws[1, 4] == '13' }
72+
assert { ws[1, 5] == '1970-01-01 0:00:00' }
73+
assert { ws[1, 6] == '1970-01-02 12:00:00' }
6574
assert { ws[1, 1].encoding == Encoding::UTF_8 }
6675

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

7079
ss2 = session.spreadsheet_by_key(ss.key)

0 commit comments

Comments
 (0)