From d0e1850e2c5f8f292068551bea853af59dfa8d1c Mon Sep 17 00:00:00 2001 From: Vincent Robert Date: Tue, 27 Aug 2019 12:14:10 +0200 Subject: [PATCH 1/7] Add Spreadsheet#append using the dedicated Google Drive API This allows to append data efficiently to an existing spreadsheet without having to load and submit the whole worksheet everytime we want to add a new row. --- lib/google_drive/spreadsheet.rb | 13 ++++++++++ test/test_google_drive.rb | 46 +++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/lib/google_drive/spreadsheet.rb b/lib/google_drive/spreadsheet.rb index d5e22ad5..7f9b41fd 100644 --- a/lib/google_drive/spreadsheet.rb +++ b/lib/google_drive/spreadsheet.rb @@ -131,5 +131,18 @@ def batch_update(requests) @session.sheets_service.batch_update_spreadsheet(id, batch_request) batch_response.replies end + + # Append values to a spreadsheet by first searching for a data table at a range, + # then appending the specified values at the end of this data table. + def append(range_name, values, override_params = {}) + value_range = Google::Apis::SheetsV4::ValueRange.new(values: values) + default_params = { + value_input_option: 'USER_ENTERED', + insert_data_option: 'INSERT_ROWS', + } + request_body = default_params.merge(override_params) + result = @session.sheets_service.append_spreadsheet_value(id, range_name, value_range, request_body) + result.updates + end end end diff --git a/test/test_google_drive.rb b/test/test_google_drive.rb index a773ea8d..9d2d52c2 100644 --- a/test/test_google_drive.rb +++ b/test/test_google_drive.rb @@ -17,6 +17,52 @@ class TestGoogleDrive < Test::Unit::TestCase @@session = nil + def test_spreadsheet_append + session = get_session + + ss_title = "#{PREFIX}spreadsheet-append" + ss_copy_title = "#{PREFIX}spreadsheet-append-copy" + + # Removes test spreadsheets in the previous run in case the previous run + # failed. + for ss in session.files('title' => ss_title, 'title-exact' => 'true') + delete_test_file(ss, true) + end + for ss in session.files('title' => ss_copy_title, 'title-exact' => 'true') + delete_test_file(ss, true) + end + + ss = session.create_spreadsheet(ss_title) + assert { ss.title == ss_title } + ws = ss.worksheets[0] + + ss.append("A1", [ %w[abc def ghi], %w[jkl mno pqr] ]) + ws.reload + assert { ws.max_rows == 1002 } + assert { ws.max_cols == 26 } + assert { ws.num_rows == 2 } + assert { ws.num_cols == 3 } + assert { ws[1, 1] == 'abc' } + assert { ws[1, 2] == 'def' } + assert { ws[1, 3] == 'ghi' } + assert { ws[2, 1] == 'jkl' } + assert { ws[2, 2] == 'mno' } + assert { ws[2, 3] == 'pqr' } + + ss.append("A1", [ %w[stu vwx yz], %w[123 456 789] ]) + ws.reload + assert { ws.max_rows == 1004 } + assert { ws.max_cols == 26 } + assert { ws.num_rows == 4 } + assert { ws.num_cols == 3 } + assert { ws[3, 1] == 'stu' } + assert { ws[3, 2] == 'vwx' } + assert { ws[3, 3] == 'yz' } + assert { ws[4, 1] == '123' } + assert { ws[4, 2] == '456' } + assert { ws[4, 3] == '789' } + end + def test_spreadsheet_online session = get_session From 0bab64ba2787c6446b463b29d5d1e6cdf09f1256 Mon Sep 17 00:00:00 2001 From: Vincent Robert Date: Tue, 10 Sep 2019 15:41:32 +0200 Subject: [PATCH 2/7] Rename method --- lib/google_drive/spreadsheet.rb | 2 +- test/test_google_drive.rb | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/google_drive/spreadsheet.rb b/lib/google_drive/spreadsheet.rb index 7f9b41fd..dc72ba7c 100644 --- a/lib/google_drive/spreadsheet.rb +++ b/lib/google_drive/spreadsheet.rb @@ -134,7 +134,7 @@ def batch_update(requests) # Append values to a spreadsheet by first searching for a data table at a range, # then appending the specified values at the end of this data table. - def append(range_name, values, override_params = {}) + def append_values(range_name, values, override_params = {}) value_range = Google::Apis::SheetsV4::ValueRange.new(values: values) default_params = { value_input_option: 'USER_ENTERED', diff --git a/test/test_google_drive.rb b/test/test_google_drive.rb index 9d2d52c2..cd21ceda 100644 --- a/test/test_google_drive.rb +++ b/test/test_google_drive.rb @@ -17,11 +17,11 @@ class TestGoogleDrive < Test::Unit::TestCase @@session = nil - def test_spreadsheet_append + def test_spreadsheet_append_values session = get_session - ss_title = "#{PREFIX}spreadsheet-append" - ss_copy_title = "#{PREFIX}spreadsheet-append-copy" + ss_title = "#{PREFIX}spreadsheet-append-values" + ss_copy_title = "#{PREFIX}spreadsheet-append-values-copy" # Removes test spreadsheets in the previous run in case the previous run # failed. @@ -36,7 +36,7 @@ def test_spreadsheet_append assert { ss.title == ss_title } ws = ss.worksheets[0] - ss.append("A1", [ %w[abc def ghi], %w[jkl mno pqr] ]) + ss.append_values("A1", [ %w[abc def ghi], %w[jkl mno pqr] ]) ws.reload assert { ws.max_rows == 1002 } assert { ws.max_cols == 26 } @@ -49,7 +49,7 @@ def test_spreadsheet_append assert { ws[2, 2] == 'mno' } assert { ws[2, 3] == 'pqr' } - ss.append("A1", [ %w[stu vwx yz], %w[123 456 789] ]) + ss.append_values("A1", [ %w[stu vwx yz], %w[123 456 789] ]) ws.reload assert { ws.max_rows == 1004 } assert { ws.max_cols == 26 } From 34c2ccba06a1d92871af2fbd1fd65dd2e13ffe66 Mon Sep 17 00:00:00 2001 From: Vincent Robert Date: Tue, 10 Sep 2019 15:48:40 +0200 Subject: [PATCH 3/7] Document all parameters --- lib/google_drive/spreadsheet.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/google_drive/spreadsheet.rb b/lib/google_drive/spreadsheet.rb index dc72ba7c..55843d20 100644 --- a/lib/google_drive/spreadsheet.rb +++ b/lib/google_drive/spreadsheet.rb @@ -134,6 +134,20 @@ def batch_update(requests) # Append values to a spreadsheet by first searching for a data table at a range, # then appending the specified values at the end of this data table. + # + # +range+ The A1 notation of a range to search for a logical table of data. + # Values will be appended after the last row of the table. + # +values+ Array (rows) of Array (columns) of values to append to the spreadsheet. + # +override_params+ allows you to control how the values will be inserted. + # By default, the values will be interpreted as if typed by a user, + # and will add new rows instead of ovewriting existing ones. + # + # You can read the Google documentation for more information: + # https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/append + # + # Example: + # sheet.append_values "A1", [ [ 10, 11, 12 ], [ 20, 21, 22 ] ] + # def append_values(range_name, values, override_params = {}) value_range = Google::Apis::SheetsV4::ValueRange.new(values: values) default_params = { From 2b05d3a95e3ee20ae8746d1a6c15494b03c3d544 Mon Sep 17 00:00:00 2001 From: Vincent Robert Date: Tue, 10 Sep 2019 16:04:16 +0200 Subject: [PATCH 4/7] Add type of the return value --- lib/google_drive/spreadsheet.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/google_drive/spreadsheet.rb b/lib/google_drive/spreadsheet.rb index 55843d20..8212a49a 100644 --- a/lib/google_drive/spreadsheet.rb +++ b/lib/google_drive/spreadsheet.rb @@ -141,6 +141,9 @@ def batch_update(requests) # +override_params+ allows you to control how the values will be inserted. # By default, the values will be interpreted as if typed by a user, # and will add new rows instead of ovewriting existing ones. + # + # Returns an object +UpdateValuesResponse+ that documents the modifications done + # to your spreadsheet. # # You can read the Google documentation for more information: # https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/append From a9d9b81e6cfb423d73d9009b65c6fef0b205ba24 Mon Sep 17 00:00:00 2001 From: Vincent Robert Date: Wed, 26 Aug 2020 09:42:49 +0200 Subject: [PATCH 5/7] Use single quotes for consistency --- test/test_google_drive.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_google_drive.rb b/test/test_google_drive.rb index cd21ceda..283b0531 100644 --- a/test/test_google_drive.rb +++ b/test/test_google_drive.rb @@ -36,7 +36,7 @@ def test_spreadsheet_append_values assert { ss.title == ss_title } ws = ss.worksheets[0] - ss.append_values("A1", [ %w[abc def ghi], %w[jkl mno pqr] ]) + ss.append_values('A1', [ %w[abc def ghi], %w[jkl mno pqr] ]) ws.reload assert { ws.max_rows == 1002 } assert { ws.max_cols == 26 } From 37ab3532d1b26382dcb8c97d3f7de9836c6e71ae Mon Sep 17 00:00:00 2001 From: Vincent Robert Date: Wed, 26 Aug 2020 09:46:58 +0200 Subject: [PATCH 6/7] More specific in documentation of override_params --- lib/google_drive/spreadsheet.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/google_drive/spreadsheet.rb b/lib/google_drive/spreadsheet.rb index 8212a49a..1cf4893f 100644 --- a/lib/google_drive/spreadsheet.rb +++ b/lib/google_drive/spreadsheet.rb @@ -141,6 +141,8 @@ def batch_update(requests) # +override_params+ allows you to control how the values will be inserted. # By default, the values will be interpreted as if typed by a user, # and will add new rows instead of ovewriting existing ones. + # So default value is `{ value_input_option: 'USER_ENTERED', insert_data_option: 'INSERT_ROWS' }` + # See https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/append#query-parameters for more information # # Returns an object +UpdateValuesResponse+ that documents the modifications done # to your spreadsheet. From eeff55506d40f47c8e4d176f37f03988cd649fdd Mon Sep 17 00:00:00 2001 From: Vincent Robert Date: Wed, 26 Aug 2020 09:49:21 +0200 Subject: [PATCH 7/7] Remove inconsistent assertions --- test/test_google_drive.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/test_google_drive.rb b/test/test_google_drive.rb index 283b0531..d0dae4f2 100644 --- a/test/test_google_drive.rb +++ b/test/test_google_drive.rb @@ -51,8 +51,6 @@ def test_spreadsheet_append_values ss.append_values("A1", [ %w[stu vwx yz], %w[123 456 789] ]) ws.reload - assert { ws.max_rows == 1004 } - assert { ws.max_cols == 26 } assert { ws.num_rows == 4 } assert { ws.num_cols == 3 } assert { ws[3, 1] == 'stu' }