sheets_v4 0.8.0 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -0
- data/README.md +7 -0
- data/lib/sheets_v4/google_extensions/spreadsheet.rb +80 -0
- data/lib/sheets_v4/version.rb +1 -1
- metadata +1 -1
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,14 @@ 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
|
+
|
7
15
|
## v0.8.0 (2023-10-15)
|
8
16
|
|
9
17
|
[Full Changelog](https://github.com/main-branch/sheets_v4/compare/v0.7.0..v0.8.0)
|
data/README.md
CHANGED
@@ -303,6 +303,13 @@ sheets_service, spreadsheet, and sheet objects separately.
|
|
303
303
|
|
304
304
|
The `sheets_service` attribute is added and is set by `SheetsService#get_spreadsheet`.
|
305
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
|
+
|
306
313
|
#### Sheet Extensions
|
307
314
|
|
308
315
|
The `sheets_service` and `spreadsheet` attributes are added. Both are set when the
|
@@ -19,6 +19,86 @@ module SheetsV4
|
|
19
19
|
#
|
20
20
|
# @return [Google::Apis::SheetsV4::SheetsService]
|
21
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
|
22
102
|
end
|
23
103
|
end
|
24
104
|
end
|
data/lib/sheets_v4/version.rb
CHANGED