sheets_v4 0.7.0 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +18 -0
- data/README.md +41 -0
- data/Rakefile +3 -0
- data/lib/sheets_v4/google_extensions/sheet.rb +32 -0
- data/lib/sheets_v4/google_extensions/sheets_service.rb +99 -0
- data/lib/sheets_v4/google_extensions/spreadsheet.rb +104 -0
- data/lib/sheets_v4/google_extensions.rb +42 -0
- data/lib/sheets_v4/version.rb +1 -1
- data/lib/sheets_v4.rb +6 -0
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d8730b843169d2404228e744281e52a37624c61f33cb061b99e38178faab2cc9
|
4
|
+
data.tar.gz: 4f156d01ec986dd39eb0c471f9838b1670d16c33240843dbec7b873489e10c68
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3b6558bcfc332037e0bf21a1b9605e94a629bdd89bc7621a6afe455bb4451395b1178e4ba92c23ba61e76adef6fd6a9ec4a9f8abc326f4068fcc505c97172ecc
|
7
|
+
data.tar.gz: 5fdce2afd7f3063452bbe79cc7ce35311d87d415c44027c566dbd27099e2ac748777b497a3a4f1e46a3e044e21463c4299fb58bf83df30f048d43a6f29c2529c
|
data/CHANGELOG.md
CHANGED
@@ -4,6 +4,24 @@ Changes for each release are listed in this file.
|
|
4
4
|
|
5
5
|
This project adheres to [Semantic Versioning](https://semver.org/) for its releases.
|
6
6
|
|
7
|
+
## v0.9.0 (2023-10-16)
|
8
|
+
|
9
|
+
[Full Changelog](https://github.com/main-branch/sheets_v4/compare/v0.8.0..v0.9.0)
|
10
|
+
|
11
|
+
Changes since v0.8.0:
|
12
|
+
|
13
|
+
* 8fd7ec5 Add convenience methods to Spreadsheet to access Sheets (#28)
|
14
|
+
|
15
|
+
## v0.8.0 (2023-10-15)
|
16
|
+
|
17
|
+
[Full Changelog](https://github.com/main-branch/sheets_v4/compare/v0.7.0..v0.8.0)
|
18
|
+
|
19
|
+
Changes since v0.7.0:
|
20
|
+
|
21
|
+
* d8f695c Add extensions to Google::Apis::SheetsV4 classes (#26)
|
22
|
+
* ed2dc0e Show the cop names on Rubocop offenses when Rubocop is run from Rake (#25)
|
23
|
+
* c5bfcc1 Group the SheetsV4 methods in the Yard Docs to make them easier to find (#24)
|
24
|
+
|
7
25
|
## v0.7.0 (2023-10-08)
|
8
26
|
|
9
27
|
[Full Changelog](https://github.com/main-branch/sheets_v4/compare/v0.6.0..v0.7.0)
|
data/README.md
CHANGED
@@ -25,6 +25,10 @@ Unofficial helpers for the Google Sheets V4 API
|
|
25
25
|
* [Method 2: constructing requests using hashes](#method-2-constructing-requests-using-hashes)
|
26
26
|
* [Which method should be used?](#which-method-should-be-used)
|
27
27
|
* [Validating requests](#validating-requests)
|
28
|
+
* [Google Extensions](#google-extensions)
|
29
|
+
* [SheetsService Extensions](#sheetsservice-extensions)
|
30
|
+
* [Spreadsheet Extensions](#spreadsheet-extensions)
|
31
|
+
* [Sheet Extensions](#sheet-extensions)
|
28
32
|
* [Working with dates and times](#working-with-dates-and-times)
|
29
33
|
* [Colors](#colors)
|
30
34
|
* [Development](#development)
|
@@ -274,6 +278,43 @@ request:
|
|
274
278
|
SheetsV4.validate_api_object(schema: 'batch_update_spreadsheet_request', object: requests)
|
275
279
|
```
|
276
280
|
|
281
|
+
### Google Extensions
|
282
|
+
|
283
|
+
The `SheetsV4::GoogleExtensions` module provides extensions to the `Google::Apis::SheetsV4`
|
284
|
+
classes to simplify use of the SheetsV4 API.
|
285
|
+
|
286
|
+
These extensions are not loaded by default and are not required to use other parts
|
287
|
+
of this Gem. To enable these extension, you must:
|
288
|
+
|
289
|
+
```Ruby
|
290
|
+
require 'sheets_v4/google_extensions'
|
291
|
+
```
|
292
|
+
|
293
|
+
#### SheetsService Extensions
|
294
|
+
|
295
|
+
Functionality is added to `get_spreadsheet` to set the `sheets_service` attribute on
|
296
|
+
the returned spreadsheet and set the `sheets_service` and `spreadsheet` attributes
|
297
|
+
on the sheets contained in the spreadsheet.
|
298
|
+
|
299
|
+
This can simplify complex spreadsheet updates because you won't have to pass a
|
300
|
+
sheets_service, spreadsheet, and sheet objects separately.
|
301
|
+
|
302
|
+
#### Spreadsheet Extensions
|
303
|
+
|
304
|
+
The `sheets_service` attribute is added and is set by `SheetsService#get_spreadsheet`.
|
305
|
+
|
306
|
+
Convenience methods for getting sheets within the spreadsheet are added:
|
307
|
+
|
308
|
+
* `sheet(id_or_title)`: returns the sheet matching the id or title given
|
309
|
+
* `sheet_id(title)`: returns the ID for the sheet matching the title given
|
310
|
+
* `each_sheet(ids_or_titles)`: enumerates the sheets within a spreadsheet matching
|
311
|
+
the given IDs or titles.
|
312
|
+
|
313
|
+
#### Sheet Extensions
|
314
|
+
|
315
|
+
The `sheets_service` and `spreadsheet` attributes are added. Both are set when the
|
316
|
+
sheet's spreadsheet is loaded by `SheetsService#get_spreadsheet`.
|
317
|
+
|
277
318
|
### Working with dates and times
|
278
319
|
|
279
320
|
Google Sheets, similar to other spreadsheet programs, stores dates and date-time
|
data/Rakefile
CHANGED
@@ -0,0 +1,32 @@
|
|
1
|
+
# Copyright (c) 2022 Yahoo
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'google/apis/sheets_v4'
|
5
|
+
require 'googleauth'
|
6
|
+
|
7
|
+
module SheetsV4
|
8
|
+
module GoogleExtensions
|
9
|
+
# The SheetsService class implements handling credentials on top of the
|
10
|
+
# Google::Apis::SheetsV4::SheetsService class.
|
11
|
+
#
|
12
|
+
# @api public
|
13
|
+
#
|
14
|
+
module Sheet
|
15
|
+
# The sheets_service object used to create this sheet
|
16
|
+
#
|
17
|
+
# @example
|
18
|
+
# sheets_service = sheet.sheets_service
|
19
|
+
#
|
20
|
+
# @return [Google::Apis::SheetsV4::SheetsService]
|
21
|
+
attr_reader :sheets_service
|
22
|
+
|
23
|
+
# The spreadsheet object that contains this sheet
|
24
|
+
#
|
25
|
+
# @example
|
26
|
+
# spreadsheet = sheet.spreadsheet
|
27
|
+
#
|
28
|
+
# @return [Google::Apis::SheetsV4::Spreadsheet]
|
29
|
+
attr_reader :spreadsheet
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
# Copyright (c) 2022 Yahoo
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
module SheetsV4
|
5
|
+
module GoogleExtensions
|
6
|
+
# This module extends the `Google::Apis::SheetsV4::SheetsService` class to add
|
7
|
+
# attributes to the `Google::Apis::SheetsV4::Spreadsheet` and `Google::Apis::SheetsV4::Sheet`
|
8
|
+
# classes that reference the `SheetsService` instance used to retrieve them.
|
9
|
+
#
|
10
|
+
# Similarly, an attribute is added to the `Google::Apis::SheetsV4::Sheet` class
|
11
|
+
# that references the `Google::Apis::SheetsV4::Spreadsheet` instance that contains it.
|
12
|
+
#
|
13
|
+
# This allows getting the `SheetsService` object from a spreadsheet or sheet object,
|
14
|
+
# making it unnecessary to pass the `SheetsService` object around with the spreadsheet
|
15
|
+
# and its sheets.
|
16
|
+
#
|
17
|
+
# @example
|
18
|
+
# require 'sheets_v4/google_extensions'
|
19
|
+
# sheets_service = SheetsV4::SheetsService.new
|
20
|
+
# spreadsheet = sheets_service.get_spreadsheet('1nT_q0TrQzC3dLZXuI3K9V5P3mArBVZpVd_vRsOpvcyk')
|
21
|
+
#
|
22
|
+
# spreadsheet.sheets_service == sheets_service # => true
|
23
|
+
# spreadsheet.sheets.each do |sheet|
|
24
|
+
# sheet.sheets_service == sheets_service # => true
|
25
|
+
# sheet.spreadsheet == spreadsheet # => true
|
26
|
+
# end
|
27
|
+
#
|
28
|
+
# @api public
|
29
|
+
#
|
30
|
+
module SheetsService
|
31
|
+
# Replace the prepending class's `get_spreadsheet` implementation
|
32
|
+
#
|
33
|
+
# When this module is prepended to a class, class's `get_spreadsheet` method
|
34
|
+
# is replaced wity `new_get_spreadsheet` method from this module. The class's
|
35
|
+
# original `get_spreadsheet` method is renamed to `original_get_spreadsheet`.
|
36
|
+
#
|
37
|
+
# @example
|
38
|
+
# Google::Apis::SheetsV4::SheetsService.prepend(
|
39
|
+
# SheetsV4::GoogleExtensions::SheetsService
|
40
|
+
# )
|
41
|
+
#
|
42
|
+
# @return [void]
|
43
|
+
#
|
44
|
+
# @private
|
45
|
+
#
|
46
|
+
def self.prepended(prepended_to_class)
|
47
|
+
prepended_to_class.send(:alias_method, :original_get_spreadsheet, :get_spreadsheet)
|
48
|
+
prepended_to_class.send(:remove_method, :get_spreadsheet)
|
49
|
+
prepended_to_class.send(:alias_method, :get_spreadsheet, :new_get_spreadsheet)
|
50
|
+
end
|
51
|
+
|
52
|
+
# @!method get_spreadsheet(spreadsheet_id, include_grid_data, ranges, fields, quota_user, options, &block)
|
53
|
+
#
|
54
|
+
# @api public
|
55
|
+
#
|
56
|
+
# Gets an existing spreadsheet
|
57
|
+
#
|
58
|
+
# Creates a spreadsheet object by calling the original
|
59
|
+
# Google::Apis::SheetsV4::SheetsService#get_spreadsheet method and then does
|
60
|
+
# the following:
|
61
|
+
#
|
62
|
+
# * Sets the `sheets_service` attribute for the returned spreadsheet.
|
63
|
+
# * Sets the `sheets_service` and `spreadsheet` attributes all the sheets contained in the spreadsheet.
|
64
|
+
#
|
65
|
+
# See the documentation for Google::Apis::SheetsV4::SheetsService#get_spreadsheet for
|
66
|
+
# details on the parameters and return value.
|
67
|
+
#
|
68
|
+
# @example Get a spreadsheet object and output new attributes:
|
69
|
+
# require 'sheets_v4'
|
70
|
+
# require 'sheets_v4/google_extensions'
|
71
|
+
#
|
72
|
+
# sheets_service = SheetsV4::SheetsService.new
|
73
|
+
# spreadsheet_id = '1nT_q0TrQzC3dLZXuI3K9V5P3mArBVZpVd_vRsOpvcyk'
|
74
|
+
#
|
75
|
+
# spreadsheet = sheets_service.get_spreadsheet(spreadsheet_id)
|
76
|
+
#
|
77
|
+
# @return [Google::Apis::SheetsV4::Spreadsheet] the spreadsheet whose ID is `spreadsheet_id`
|
78
|
+
|
79
|
+
# Replaces the `get_spreadsheet` method implementation in the prepended class
|
80
|
+
#
|
81
|
+
# @example
|
82
|
+
# spreadsheet = sheets_service.new_get_spreadsheet(spreadsheet_id)
|
83
|
+
#
|
84
|
+
# @private
|
85
|
+
#
|
86
|
+
# @return [Google::Apis::SheetsV4::Spreadsheet]
|
87
|
+
#
|
88
|
+
def new_get_spreadsheet(...)
|
89
|
+
original_get_spreadsheet(...)&.tap do |spreadsheet|
|
90
|
+
spreadsheet.instance_variable_set(:@sheets_service, self)
|
91
|
+
spreadsheet.sheets.each do |sheet|
|
92
|
+
sheet.instance_variable_set(:@sheets_service, self)
|
93
|
+
sheet.instance_variable_set(:@spreadsheet, spreadsheet)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
# Copyright (c) 2022 Yahoo
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'google/apis/sheets_v4'
|
5
|
+
require 'googleauth'
|
6
|
+
|
7
|
+
module SheetsV4
|
8
|
+
module GoogleExtensions
|
9
|
+
# The SheetsService class implements handling credentials on top of the
|
10
|
+
# Google::Apis::SheetsV4::SheetsService class.
|
11
|
+
#
|
12
|
+
# @api public
|
13
|
+
#
|
14
|
+
module Spreadsheet
|
15
|
+
# The sheets_service object used to create this spreadsheet
|
16
|
+
#
|
17
|
+
# @example
|
18
|
+
# sheets_service = spreadsheet.sheets_service
|
19
|
+
#
|
20
|
+
# @return [Google::Apis::SheetsV4::SheetsService]
|
21
|
+
attr_reader :sheets_service
|
22
|
+
|
23
|
+
# Return the matching sheet object or nil
|
24
|
+
#
|
25
|
+
# If `id_or_title` is an Integer, it is assumed to be the sheet ID. Otherwise,
|
26
|
+
# it is assumed to be the sheet title.
|
27
|
+
#
|
28
|
+
# @example Get the sheet whose title is 'Sheet1'
|
29
|
+
# sheet = spreadsheet.sheet('Sheet1')
|
30
|
+
#
|
31
|
+
# @example Get the sheet whose title is '2023-03-15'
|
32
|
+
# date = Date.new(2023, 3, 15)
|
33
|
+
# sheet = spreadsheet.sheet(date)
|
34
|
+
#
|
35
|
+
# @example Get the sheet whose ID is 123456
|
36
|
+
# sheet = spreadsheet.sheet(123456)
|
37
|
+
#
|
38
|
+
# @param id_or_title [Integer, #to_s] the ID or title of the sheet to return
|
39
|
+
#
|
40
|
+
# @return [Google::Apis::SheetsV4::Sheet, nil]
|
41
|
+
#
|
42
|
+
def sheet(id_or_title)
|
43
|
+
if id_or_title.is_a?(Integer)
|
44
|
+
sheets.find { |sheet| sheet.properties.sheet_id == id_or_title }
|
45
|
+
else
|
46
|
+
title = id_or_title.to_s
|
47
|
+
sheets.find { |sheet| sheet.properties.title == title }
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# Return the ID of the matching sheet
|
52
|
+
#
|
53
|
+
# @example
|
54
|
+
# id = spreadsheet.sheet_id('Sheet1')
|
55
|
+
#
|
56
|
+
# @param id_or_title [Integer, #to_s] the ID or title of the sheet
|
57
|
+
#
|
58
|
+
# @return [Integer]
|
59
|
+
#
|
60
|
+
def sheet_id(id_or_title)
|
61
|
+
sheet(id_or_title)&.properties&.sheet_id
|
62
|
+
end
|
63
|
+
|
64
|
+
# Iterate over sheets in a spreadsheet
|
65
|
+
#
|
66
|
+
# If `ids_or_titles` is not given or is nil, all sheets are enumerated. Otherwise,
|
67
|
+
# only the sheets whose IDs or titles are in `sheets` are enumerated.
|
68
|
+
#
|
69
|
+
# @example Enumerate all sheets
|
70
|
+
# spreadsheet.each_sheet { |sheet| puts sheet.properties.title }
|
71
|
+
#
|
72
|
+
# @example Enumerate sheets whose IDs are 123456 and 789012
|
73
|
+
# sheets = [123456, 789012]
|
74
|
+
# spreadsheet.each_sheet(sheets).with_index do |sheet, index|
|
75
|
+
# puts "#{index}: #{sheet.properties.title} (#{sheet.properties.sheet_id})"
|
76
|
+
# end
|
77
|
+
#
|
78
|
+
# @param ids_or_titles [Array<Integer, #to_s>] an array of sheet IDs and/or titles
|
79
|
+
#
|
80
|
+
# An Integer in this array is match to the sheet ID. Anything else is matched
|
81
|
+
# to the sheet title after calling `#to_s` on it.
|
82
|
+
#
|
83
|
+
# @return [void]
|
84
|
+
#
|
85
|
+
# @yield each matching sheet
|
86
|
+
# @yieldparam [Google::Apis::SheetsV4::Sheet] the matching sheet
|
87
|
+
#
|
88
|
+
# @raise [RuntimeError] if one of the sheets does not exist
|
89
|
+
# @raise [RuntimeError] if a block is not given
|
90
|
+
#
|
91
|
+
def each_sheet(ids_or_titles = sheets.map { |sheet| sheet.properties.sheet_id }, &block)
|
92
|
+
return enum_for(:each_sheet, ids_or_titles) unless block
|
93
|
+
|
94
|
+
matching_sheets = ids_or_titles.map do |id_or_title|
|
95
|
+
sheet(id_or_title) || raise("Could not find sheet '#{id_or_title}'")
|
96
|
+
end
|
97
|
+
|
98
|
+
matching_sheets.each { |sheet| block[sheet] }
|
99
|
+
|
100
|
+
self
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SheetsV4
|
4
|
+
# The Google extensions are additions directly to Google::Apis::SheetsV4 classes
|
5
|
+
#
|
6
|
+
# These additions are optional and provide convenience methods and attributes
|
7
|
+
# that simplify use of the Google Sheets API.
|
8
|
+
#
|
9
|
+
# To use these extensions, require the `sheets_v4/google_extensions` file.
|
10
|
+
#
|
11
|
+
# @example
|
12
|
+
# require 'sheets_v4/google_extensions'
|
13
|
+
#
|
14
|
+
module GoogleExtensions; end
|
15
|
+
end
|
16
|
+
|
17
|
+
require_relative 'google_extensions/sheets_service'
|
18
|
+
require_relative 'google_extensions/spreadsheet'
|
19
|
+
require_relative 'google_extensions/sheet'
|
20
|
+
|
21
|
+
# @private
|
22
|
+
module Google
|
23
|
+
module Apis
|
24
|
+
# Add SheetsV4 extensions to Google::Apis::SheetsV4 classes
|
25
|
+
module SheetsV4
|
26
|
+
# Add SheetsV4 extensions to Google::Apis::SheetsV4::SheetsService
|
27
|
+
class SheetsService
|
28
|
+
prepend ::SheetsV4::GoogleExtensions::SheetsService
|
29
|
+
end
|
30
|
+
|
31
|
+
# Add SheetsV4 extensions to Google::Apis::SheetsV4::Spreadsheet
|
32
|
+
class Spreadsheet
|
33
|
+
prepend ::SheetsV4::GoogleExtensions::Spreadsheet
|
34
|
+
end
|
35
|
+
|
36
|
+
# Add SheetsV4 extensions to Google::Apis::SheetsV4::Sheet
|
37
|
+
class Sheet
|
38
|
+
prepend ::SheetsV4::GoogleExtensions::Sheet
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/lib/sheets_v4/version.rb
CHANGED
data/lib/sheets_v4.rb
CHANGED
@@ -57,6 +57,8 @@ module SheetsV4
|
|
57
57
|
end
|
58
58
|
end
|
59
59
|
|
60
|
+
# @!group Validation
|
61
|
+
|
60
62
|
# Validate the object using the named JSON schema
|
61
63
|
#
|
62
64
|
# The JSON schemas are loaded from the Google Disocvery API. The schemas names are
|
@@ -90,6 +92,8 @@ module SheetsV4
|
|
90
92
|
SheetsV4::ApiObjectValidation::LoadSchemas.new(logger:).call.keys.sort
|
91
93
|
end
|
92
94
|
|
95
|
+
# @!group Colors
|
96
|
+
|
93
97
|
# Given the name of the color, return a Google Sheets API color object
|
94
98
|
#
|
95
99
|
# Available color names are listed using `SheetsV4.color_names`.
|
@@ -117,6 +121,8 @@ module SheetsV4
|
|
117
121
|
#
|
118
122
|
def color_names = SheetsV4::Color::COLORS.keys
|
119
123
|
|
124
|
+
# @!group Date and DateTime Conversions
|
125
|
+
|
120
126
|
# @!attribute [rw] default_spreadsheet_tz
|
121
127
|
#
|
122
128
|
# Set the default time zone for date and time conversions
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sheets_v4
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.9.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Couball
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-10-
|
11
|
+
date: 2023-10-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler-audit
|
@@ -268,6 +268,10 @@ files:
|
|
268
268
|
- lib/sheets_v4/color.rb
|
269
269
|
- lib/sheets_v4/convert_dates_and_times.rb
|
270
270
|
- lib/sheets_v4/create_credential.rb
|
271
|
+
- lib/sheets_v4/google_extensions.rb
|
272
|
+
- lib/sheets_v4/google_extensions/sheet.rb
|
273
|
+
- lib/sheets_v4/google_extensions/sheets_service.rb
|
274
|
+
- lib/sheets_v4/google_extensions/spreadsheet.rb
|
271
275
|
- lib/sheets_v4/validate_api_objects.rb
|
272
276
|
- lib/sheets_v4/validate_api_objects/validate_api_object.rb
|
273
277
|
- lib/sheets_v4/version.rb
|