sheets_v4 0.8.0 → 0.10.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.commitlintrc.yml +16 -0
- data/.husky/commit-msg +1 -0
- data/.markdownlint.yml +1 -1
- data/.rubocop.yml +5 -35
- data/.yardopts +3 -1
- data/CHANGELOG.md +35 -0
- data/LICENSE.txt +1 -1
- data/README.md +109 -28
- data/Rakefile +2 -10
- data/examples/README.md +230 -47
- data/examples/create_spreadsheet +11 -0
- data/examples/{set_background_color → set_background_color1} +21 -8
- data/examples/set_background_color2 +11 -21
- data/lib/sheets_v4/color.rb +2 -2
- data/lib/sheets_v4/convert_dates_and_times.rb +0 -2
- data/lib/sheets_v4/google_extensions/sheet.rb +0 -1
- data/lib/sheets_v4/google_extensions/sheets_service.rb +0 -1
- data/lib/sheets_v4/google_extensions/spreadsheet.rb +80 -1
- data/lib/sheets_v4/version.rb +1 -1
- data/lib/sheets_v4.rb +0 -36
- data/package.json +11 -0
- metadata +65 -22
- data/lib/sheets_v4/api_object_validation/load_schemas.rb +0 -155
- data/lib/sheets_v4/api_object_validation/resolve_schema_ref.rb +0 -72
- data/lib/sheets_v4/api_object_validation/traverse_object_tree.rb +0 -82
- data/lib/sheets_v4/api_object_validation/validate_api_object.rb +0 -87
- data/lib/sheets_v4/validate_api_objects/validate_api_object.rb +0 -87
- data/lib/sheets_v4/validate_api_objects.rb +0 -20
data/examples/README.md
CHANGED
@@ -1,49 +1,232 @@
|
|
1
1
|
# Google Sheets Examples
|
2
2
|
|
3
|
-
* [
|
4
|
-
[
|
5
|
-
* [
|
6
|
-
* [
|
7
|
-
* [
|
8
|
-
* [
|
9
|
-
* [
|
10
|
-
|
11
|
-
|
12
|
-
* [
|
13
|
-
* [ ]
|
14
|
-
* [
|
15
|
-
* [
|
16
|
-
* [
|
17
|
-
* [
|
18
|
-
* [
|
19
|
-
* [
|
20
|
-
* [ ]
|
21
|
-
* [
|
22
|
-
* [
|
23
|
-
* [
|
24
|
-
* [
|
25
|
-
* [ ]
|
26
|
-
* [
|
27
|
-
* [ ]
|
28
|
-
* [
|
29
|
-
* [
|
30
|
-
* [
|
31
|
-
* [
|
32
|
-
* [
|
33
|
-
* [
|
34
|
-
* [
|
35
|
-
* [ ]
|
36
|
-
* [
|
37
|
-
* [
|
38
|
-
* [
|
39
|
-
* [
|
40
|
-
* [
|
41
|
-
* [
|
42
|
-
* [
|
43
|
-
* [ ]
|
44
|
-
* [
|
45
|
-
* [ ]
|
46
|
-
* [
|
47
|
-
|
48
|
-
|
49
|
-
|
3
|
+
* [✅︎ Creating a Google API service account](#︎-creating-a-google-api-service-account)
|
4
|
+
* [✅︎ Create a SheetsService instance](#︎-create-a-sheetsservice-instance)
|
5
|
+
* [✅︎ Set background color](#︎-set-background-color)
|
6
|
+
* [Creating Google Sheets files](#creating-google-sheets-files)
|
7
|
+
* [Writing data to a sheet](#writing-data-to-a-sheet)
|
8
|
+
* [Reading data from a sheet](#reading-data-from-a-sheet)
|
9
|
+
* [Data formatting basics](#data-formatting-basics)
|
10
|
+
* [Creating charts](#creating-charts)
|
11
|
+
* [Data validation](#data-validation)
|
12
|
+
* [Cut, copy, and paste](#cut-copy-and-paste)
|
13
|
+
* [Duplicate sheets](#duplicate-sheets)
|
14
|
+
* [List sheets in a spreadsheet](#list-sheets-in-a-spreadsheet)
|
15
|
+
* [Set column width and row height](#set-column-width-and-row-height)
|
16
|
+
* [Append rows and columns](#append-rows-and-columns)
|
17
|
+
* [Delete rows and columns](#delete-rows-and-columns)
|
18
|
+
* [Insert rows and columns](#insert-rows-and-columns)
|
19
|
+
* [Move rows and columns](#move-rows-and-columns)
|
20
|
+
* [Clear data](#clear-data)
|
21
|
+
* [Add and delete sheets](#add-and-delete-sheets)
|
22
|
+
* [Copy sheet from one spreadsheet to another](#copy-sheet-from-one-spreadsheet-to-another)
|
23
|
+
* [Add a new sheet to an existing spreadsheet](#add-a-new-sheet-to-an-existing-spreadsheet)
|
24
|
+
* [Export a sheet to a CSV file](#export-a-sheet-to-a-csv-file)
|
25
|
+
* [Sort sheets](#sort-sheets)
|
26
|
+
* [Add calculated fields into a pivot table](#add-calculated-fields-into-a-pivot-table)
|
27
|
+
* [Named ranges](#named-ranges)
|
28
|
+
* [Create a pivot table](#create-a-pivot-table)
|
29
|
+
* [Calculated pivot fields](#calculated-pivot-fields)
|
30
|
+
* [Delete a pivot table](#delete-a-pivot-table)
|
31
|
+
* [Add pivot fields](#add-pivot-fields)
|
32
|
+
* [Add pivot filters](#add-pivot-filters)
|
33
|
+
* [Collapse/expand pivot table groups](#collapseexpand-pivot-table-groups)
|
34
|
+
* [Extract pivot table metadata](#extract-pivot-table-metadata)
|
35
|
+
* [Filter views](#filter-views)
|
36
|
+
* [Locate the last row in a column](#locate-the-last-row-in-a-column)
|
37
|
+
* [Autofill](#autofill)
|
38
|
+
* [Rename a sheet](#rename-a-sheet)
|
39
|
+
* [Find and replace](#find-and-replace)
|
40
|
+
* [Add and delete sheets](#add-and-delete-sheets-1)
|
41
|
+
* [Sum across sheets](#sum-across-sheets)
|
42
|
+
* [Freeze rows / columns](#freeze-rows--columns)
|
43
|
+
* [Protected ranges](#protected-ranges)
|
44
|
+
* [Resize a sheet](#resize-a-sheet)
|
45
|
+
* [Retrying on error](#retrying-on-error)
|
46
|
+
* [Set a custom datetime or decimal format for a range](#set-a-custom-datetime-or-decimal-format-for-a-range)
|
47
|
+
|
48
|
+
Annotated examples written in Ruby.
|
49
|
+
|
50
|
+
Checked (✅︎) topics are completed. Topics without a check still need to be added.
|
51
|
+
|
52
|
+
### ✅︎ Creating a Google API service account
|
53
|
+
|
54
|
+
<sup>[1](https://www.youtube.com/watch?v=sAgWCbGMzTo&list=PL3JVwFmb_BnSee8RFaRPZ3nykuMRlaQp1&index=1)</sup>
|
55
|
+
|
56
|
+
<sup>[2](https://www.youtube.com/watch?v=sVURhxyc6jE&list=PL3JVwFmb_BnSee8RFaRPZ3nykuMRlaQp1&index=43)</sup>
|
57
|
+
|
58
|
+
See the [Getting started](https://github.com/main-branch/sheets_v4#getting-started)
|
59
|
+
section in the project README.md
|
60
|
+
|
61
|
+
### ✅︎ Create a SheetsService instance
|
62
|
+
|
63
|
+
See [Obtaining an authenticated SheetsService](https://github.com/main-branch/sheets_v4#obtaining-an-authenticated-sheetsservice)
|
64
|
+
section in the project README.md
|
65
|
+
|
66
|
+
### ✅︎ Set background color
|
67
|
+
|
68
|
+
[examples/set_background_color1](https://github.com/main-branch/sheets_v4/blob/main/examples/set_background_color1):
|
69
|
+
set background colors building requests using `Google::Apis::SheetsV4::*` objects.
|
70
|
+
See the [Building a request](https://github.com/main-branch/sheets_v4/blob/main/README.md#building-a-request)
|
71
|
+
section in the project README.md.
|
72
|
+
|
73
|
+
[examples/set_background_color2](https://github.com/main-branch/sheets_v4/blob/main/examples/set_background_color2):
|
74
|
+
set background colors building requests using `Hash` objects.
|
75
|
+
See the [Building a request](https://github.com/main-branch/sheets_v4/blob/main/README.md#building-a-request)
|
76
|
+
section in the project README.md.
|
77
|
+
|
78
|
+
### Creating Google Sheets files
|
79
|
+
|
80
|
+
<sup>[1](https://www.youtube.com/watch?v=JRUxeQ6ZCy0&list=PL3JVwFmb_BnSee8RFaRPZ3nykuMRlaQp1&index=2)</sup>
|
81
|
+
|
82
|
+
### Writing data to a sheet
|
83
|
+
|
84
|
+
<sup>[1](https://www.youtube.com/watch?v=YF7Ad-7pvks&list=PL3JVwFmb_BnSee8RFaRPZ3nykuMRlaQp1&index=3)</sup>
|
85
|
+
|
86
|
+
### Reading data from a sheet
|
87
|
+
|
88
|
+
<sup>[1](https://www.youtube.com/watch?v=gkglr8GID5E&list=PL3JVwFmb_BnSee8RFaRPZ3nykuMRlaQp1&index=4)</sup>
|
89
|
+
|
90
|
+
Reading data
|
91
|
+
|
92
|
+
Reading formulas
|
93
|
+
|
94
|
+
### Data formatting basics
|
95
|
+
|
96
|
+
<sup>[1](https://www.youtube.com/watch?v=R4EN3iPRris&list=PL3JVwFmb_BnSee8RFaRPZ3nykuMRlaQp1&index=5)</sup>
|
97
|
+
|
98
|
+
### Creating charts
|
99
|
+
|
100
|
+
<sup>[1](https://www.youtube.com/watch?v=xt3p5I8mNWE&list=PL3JVwFmb_BnSee8RFaRPZ3nykuMRlaQp1&index=6)</sup>
|
101
|
+
|
102
|
+
### Data validation
|
103
|
+
|
104
|
+
<sup>[1](https://www.youtube.com/watch?v=n_Z2565gu6Y&list=PL3JVwFmb_BnSee8RFaRPZ3nykuMRlaQp1&index=7)</sup>
|
105
|
+
|
106
|
+
### Cut, copy, and paste
|
107
|
+
|
108
|
+
<sup>[1](https://www.youtube.com/watch?v=r8GWH2E_ehw&list=PL3JVwFmb_BnSee8RFaRPZ3nykuMRlaQp1&index=8)</sup>
|
109
|
+
|
110
|
+
### Duplicate sheets
|
111
|
+
|
112
|
+
<sup>[1](https://www.youtube.com/watch?v=BgQoPcoOiGY&list=PL3JVwFmb_BnSee8RFaRPZ3nykuMRlaQp1&index=9)</sup>
|
113
|
+
|
114
|
+
### List sheets in a spreadsheet
|
115
|
+
|
116
|
+
<sup>[1](https://www.youtube.com/watch?v=BgQoPcoOiGY&list=PL3JVwFmb_BnSee8RFaRPZ3nykuMRlaQp1&index=9)</sup>
|
117
|
+
|
118
|
+
### Set column width and row height
|
119
|
+
|
120
|
+
<sup>[1](https://www.youtube.com/watch?v=H3uMEaPqTVE&list=PL3JVwFmb_BnSee8RFaRPZ3nykuMRlaQp1&index=11)</sup>
|
121
|
+
|
122
|
+
### Append rows and columns
|
123
|
+
|
124
|
+
<sup>[1](https://www.youtube.com/watch?v=txfiwEjb7sk&list=PL3JVwFmb_BnSee8RFaRPZ3nykuMRlaQp1&index=12)</sup>
|
125
|
+
|
126
|
+
### Delete rows and columns
|
127
|
+
|
128
|
+
<sup>[1](https://www.youtube.com/watch?v=w1jrCxWx7Tc&list=PL3JVwFmb_BnSee8RFaRPZ3nykuMRlaQp1&index=13)</sup>
|
129
|
+
|
130
|
+
### Insert rows and columns
|
131
|
+
|
132
|
+
<sup>[1](https://www.youtube.com/watch?v=FL7WSsO5EVs&list=PL3JVwFmb_BnSee8RFaRPZ3nykuMRlaQp1&index=14)</sup>
|
133
|
+
|
134
|
+
### Move rows and columns
|
135
|
+
|
136
|
+
<sup>[1](https://www.youtube.com/watch?v=YHk3305dkOc&list=PL3JVwFmb_BnSee8RFaRPZ3nykuMRlaQp1&index=15)</sup>
|
137
|
+
|
138
|
+
### Clear data
|
139
|
+
|
140
|
+
<sup>[1](https://www.youtube.com/watch?v=mvbnhfdDrro&list=PL3JVwFmb_BnSee8RFaRPZ3nykuMRlaQp1&index=16)</sup>
|
141
|
+
|
142
|
+
### Add and delete sheets
|
143
|
+
|
144
|
+
<sup>[1](https://www.youtube.com/watch?v=X9PVQQVoJFc&list=PL3JVwFmb_BnSee8RFaRPZ3nykuMRlaQp1&index=17)</sup>
|
145
|
+
|
146
|
+
### Copy sheet from one spreadsheet to another
|
147
|
+
|
148
|
+
<sup>[1](https://www.youtube.com/watch?v=aIEM7Ts4n-c&list=PL3JVwFmb_BnSee8RFaRPZ3nykuMRlaQp1&index=18)</sup>
|
149
|
+
|
150
|
+
### Add a new sheet to an existing spreadsheet
|
151
|
+
|
152
|
+
### Export a sheet to a CSV file
|
153
|
+
|
154
|
+
<sup>[1](https://www.youtube.com/watch?v=Dz22fsWsLhI&list=PL3JVwFmb_BnSee8RFaRPZ3nykuMRlaQp1&index=25)</sup>
|
155
|
+
|
156
|
+
### Sort sheets
|
157
|
+
|
158
|
+
<sup>[1](https://www.youtube.com/watch?v=qbBZX7uBM1M&list=PL3JVwFmb_BnSee8RFaRPZ3nykuMRlaQp1&index=27)</sup>
|
159
|
+
|
160
|
+
### Add calculated fields into a pivot table
|
161
|
+
|
162
|
+
<sup>[1](https://www.youtube.com/watch?v=VR8zOz5ATLU&list=PL3JVwFmb_BnSee8RFaRPZ3nykuMRlaQp1&index=32)</sup>
|
163
|
+
|
164
|
+
### Named ranges
|
165
|
+
|
166
|
+
<sup>[1](https://www.youtube.com/watch?v=LTPdfXS_LHA&list=PL3JVwFmb_BnSee8RFaRPZ3nykuMRlaQp1&index=42)</sup>
|
167
|
+
|
168
|
+
### Create a pivot table
|
169
|
+
|
170
|
+
<sup>[1](https://www.youtube.com/watch?v=preFnuL7ua0&list=PL3JVwFmb_BnSee8RFaRPZ3nykuMRlaQp1&index=28)</sup>
|
171
|
+
|
172
|
+
### Calculated pivot fields
|
173
|
+
|
174
|
+
<sup>[1](https://www.youtube.com/watch?v=QLssI4uvjk4&list=PL3JVwFmb_BnSee8RFaRPZ3nykuMRlaQp1&index=33)</sup>
|
175
|
+
|
176
|
+
### Delete a pivot table
|
177
|
+
|
178
|
+
### Add pivot fields
|
179
|
+
|
180
|
+
<sup>[1](https://www.youtube.com/watch?v=VR8zOz5ATLU&list=PL3JVwFmb_BnSee8RFaRPZ3nykuMRlaQp1&index=32)</sup>
|
181
|
+
|
182
|
+
### Add pivot filters
|
183
|
+
|
184
|
+
<sup>[1](https://www.youtube.com/watch?v=EKikw-eIcbY&list=PL3JVwFmb_BnSee8RFaRPZ3nykuMRlaQp1&index=29)</sup>
|
185
|
+
|
186
|
+
### Collapse/expand pivot table groups
|
187
|
+
|
188
|
+
<sup>[1](https://www.youtube.com/watch?v=-S9bs5-ZJ5E&list=PL3JVwFmb_BnSee8RFaRPZ3nykuMRlaQp1&index=31)</sup>
|
189
|
+
|
190
|
+
### Extract pivot table metadata
|
191
|
+
|
192
|
+
<sup>[1](https://www.youtube.com/watch?v=H1SGdqbaL4w&list=PL3JVwFmb_BnSee8RFaRPZ3nykuMRlaQp1&index=30)</sup>
|
193
|
+
|
194
|
+
### Filter views
|
195
|
+
|
196
|
+
<sup>[1](https://www.youtube.com/watch?v=GyRxsSlx0GU&list=PL3JVwFmb_BnSee8RFaRPZ3nykuMRlaQp1&index=34)</sup>
|
197
|
+
|
198
|
+
### Locate the last row in a column
|
199
|
+
|
200
|
+
<sup>[1](https://www.youtube.com/watch?v=NWWHleJll28&list=PL3JVwFmb_BnSee8RFaRPZ3nykuMRlaQp1&index=35)</sup>
|
201
|
+
|
202
|
+
### Autofill
|
203
|
+
|
204
|
+
<sup>[1](https://www.youtube.com/watch?v=guHGNmODdpM&list=PL3JVwFmb_BnSee8RFaRPZ3nykuMRlaQp1&index=36)</sup>
|
205
|
+
|
206
|
+
### Rename a sheet
|
207
|
+
|
208
|
+
<sup>[1](https://www.youtube.com/watch?v=iuiDUJ4NrQI&list=PL3JVwFmb_BnSee8RFaRPZ3nykuMRlaQp1&index=37)</sup>
|
209
|
+
|
210
|
+
### Find and replace
|
211
|
+
|
212
|
+
<sup>[1](https://www.youtube.com/watch?v=YaFR0bu5CrY&list=PL3JVwFmb_BnSee8RFaRPZ3nykuMRlaQp1&index=39)</sup>
|
213
|
+
|
214
|
+
### Add and delete sheets
|
215
|
+
|
216
|
+
<sup>[1](https://www.youtube.com/watch?v=gMD4v8F8vlc&list=PL3JVwFmb_BnSee8RFaRPZ3nykuMRlaQp1&index=38)</sup>
|
217
|
+
|
218
|
+
### Sum across sheets
|
219
|
+
|
220
|
+
<sup>[1](https://www.youtube.com/watch?v=7QNk-MXkPC4&list=PL3JVwFmb_BnSee8RFaRPZ3nykuMRlaQp1&index=44)</sup>
|
221
|
+
|
222
|
+
### Freeze rows / columns
|
223
|
+
|
224
|
+
### Protected ranges
|
225
|
+
|
226
|
+
### Resize a sheet
|
227
|
+
|
228
|
+
### Retrying on error
|
229
|
+
|
230
|
+
### Set a custom datetime or decimal format for a range
|
231
|
+
|
232
|
+
<sup>[1](https://developers.google.com/sheets/api/samples/formatting#set_a_custom_datetime_or_decimal_format_for_a_range)</sup>
|
@@ -0,0 +1,11 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'sheets_v4'
|
5
|
+
|
6
|
+
# Requires one of the following Auth scopes:
|
7
|
+
# * https://www.googleapis.com/auth/drive
|
8
|
+
# * https://www.googleapis.com/auth/drive.file
|
9
|
+
# * https://www.googleapis.com/auth/spreadsheets
|
10
|
+
|
11
|
+
SheetsV4.sheets_service.create_spreadsheet
|
@@ -17,38 +17,51 @@ require 'googleauth'
|
|
17
17
|
|
18
18
|
def name_rows
|
19
19
|
SheetsV4.color_names.map do |color_name|
|
20
|
-
|
20
|
+
Google::Apis::SheetsV4::RowData.new(
|
21
|
+
values: [
|
22
|
+
Google::Apis::SheetsV4::CellData.new(
|
23
|
+
user_entered_value: Google::Apis::SheetsV4::ExtendedValue.new(string_value: color_name.to_s)
|
24
|
+
)
|
25
|
+
]
|
26
|
+
)
|
27
|
+
# { values: [{ user_entered_value: { string_value: color_name.to_s } }] }
|
21
28
|
end
|
22
29
|
end
|
23
30
|
|
24
31
|
def write_names
|
25
32
|
rows = name_rows
|
26
33
|
fields = 'user_entered_value'
|
27
|
-
start =
|
28
|
-
|
34
|
+
start = Google::Apis::SheetsV4::GridCoordinate.new(sheet_id: 0, row_index: 1, column_index: 0)
|
35
|
+
Google::Apis::SheetsV4::Request.new(
|
36
|
+
update_cells: Google::Apis::SheetsV4::UpdateCellsRequest.new(rows:, fields:, start:)
|
37
|
+
)
|
29
38
|
end
|
30
39
|
|
31
40
|
def background_color_rows
|
32
41
|
SheetsV4.color_names.map { |color_name| SheetsV4.color(color_name) }.map do |color|
|
33
|
-
|
42
|
+
background_color = Google::Apis::SheetsV4::Color.new(**color)
|
43
|
+
user_entered_format = Google::Apis::SheetsV4::CellFormat.new(background_color:)
|
44
|
+
cell_data = Google::Apis::SheetsV4::CellData.new(user_entered_format:)
|
45
|
+
Google::Apis::SheetsV4::RowData.new(values: [cell_data])
|
34
46
|
end
|
35
47
|
end
|
36
48
|
|
37
49
|
def set_background_colors
|
38
50
|
rows = background_color_rows
|
39
51
|
fields = 'user_entered_format'
|
40
|
-
start =
|
41
|
-
|
52
|
+
start = Google::Apis::SheetsV4::GridCoordinate.new(sheet_id: 0, row_index: 1, column_index: 1)
|
53
|
+
update_cells = Google::Apis::SheetsV4::UpdateCellsRequest.new(rows:, fields:, start:)
|
54
|
+
Google::Apis::SheetsV4::Request.new(update_cells:)
|
42
55
|
end
|
43
56
|
|
44
|
-
def requests =
|
57
|
+
def requests = Google::Apis::SheetsV4::BatchUpdateSpreadsheetRequest.new(requests: [write_names, set_background_colors])
|
45
58
|
|
46
59
|
# OPTIONAL: validate the requests against the schema before sending them to the API.
|
47
60
|
#
|
48
61
|
# While not necessary, it can be helpful to identify errors since the API will return
|
49
62
|
# an error if the request is invalid but does not tell you where the problem is.
|
50
63
|
#
|
51
|
-
SheetsV4.validate_api_object(schema_name: 'batch_update_spreadsheet_request', object: requests)
|
64
|
+
SheetsV4.validate_api_object(schema_name: 'batch_update_spreadsheet_request', object: requests.to_h)
|
52
65
|
|
53
66
|
spreadsheet_id = '18FAcgotK7nDfLTOTQuGCIjKwxkJMAguhn1OVzpFFgWY'
|
54
67
|
SheetsV4.sheets_service.batch_update_spreadsheet(spreadsheet_id, requests)
|
@@ -1,6 +1,8 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
# frozen_string_literal: true
|
3
3
|
|
4
|
+
require 'discovery_v1'
|
5
|
+
require 'discovery_v1/google_extensions'
|
4
6
|
require 'sheets_v4'
|
5
7
|
require 'googleauth'
|
6
8
|
|
@@ -17,51 +19,39 @@ require 'googleauth'
|
|
17
19
|
|
18
20
|
def name_rows
|
19
21
|
SheetsV4.color_names.map do |color_name|
|
20
|
-
|
21
|
-
values: [
|
22
|
-
Google::Apis::SheetsV4::CellData.new(
|
23
|
-
user_entered_value: Google::Apis::SheetsV4::ExtendedValue.new(string_value: color_name.to_s)
|
24
|
-
)
|
25
|
-
]
|
26
|
-
)
|
27
|
-
# { values: [{ user_entered_value: { string_value: color_name.to_s } }] }
|
22
|
+
{ values: [{ user_entered_value: { string_value: color_name.to_s } }] }
|
28
23
|
end
|
29
24
|
end
|
30
25
|
|
31
26
|
def write_names
|
32
27
|
rows = name_rows
|
33
28
|
fields = 'user_entered_value'
|
34
|
-
start =
|
35
|
-
|
36
|
-
update_cells: Google::Apis::SheetsV4::UpdateCellsRequest.new(rows:, fields:, start:)
|
37
|
-
)
|
29
|
+
start = { sheet_id: 0, row_index: 1, column_index: 0 }
|
30
|
+
{ update_cells: { rows:, fields:, start: } }
|
38
31
|
end
|
39
32
|
|
40
33
|
def background_color_rows
|
41
34
|
SheetsV4.color_names.map { |color_name| SheetsV4.color(color_name) }.map do |color|
|
42
|
-
background_color
|
43
|
-
user_entered_format = Google::Apis::SheetsV4::CellFormat.new(background_color:)
|
44
|
-
cell_data = Google::Apis::SheetsV4::CellData.new(user_entered_format:)
|
45
|
-
Google::Apis::SheetsV4::RowData.new(values: [cell_data])
|
35
|
+
{ values: [{ user_entered_format: { background_color: color } }] }
|
46
36
|
end
|
47
37
|
end
|
48
38
|
|
49
39
|
def set_background_colors
|
50
40
|
rows = background_color_rows
|
51
41
|
fields = 'user_entered_format'
|
52
|
-
start =
|
53
|
-
update_cells
|
54
|
-
Google::Apis::SheetsV4::Request.new(update_cells:)
|
42
|
+
start = { sheet_id: 0, row_index: 1, column_index: 1 }
|
43
|
+
{ update_cells: { rows:, fields:, start: } }
|
55
44
|
end
|
56
45
|
|
57
|
-
def requests =
|
46
|
+
def requests = { requests: [write_names, set_background_colors] }
|
58
47
|
|
59
48
|
# OPTIONAL: validate the requests against the schema before sending them to the API.
|
60
49
|
#
|
61
50
|
# While not necessary, it can be helpful to identify errors since the API will return
|
62
51
|
# an error if the request is invalid but does not tell you where the problem is.
|
63
52
|
#
|
64
|
-
|
53
|
+
rest_description = DiscoveryV1.discovery_service.get_rest_api('sheets', 'v4')
|
54
|
+
rest_description.validate_object(schema_name: 'batch_update_spreadsheet_request', object: requests)
|
65
55
|
|
66
56
|
spreadsheet_id = '18FAcgotK7nDfLTOTQuGCIjKwxkJMAguhn1OVzpFFgWY'
|
67
57
|
SheetsV4.sheets_service.batch_update_spreadsheet(spreadsheet_id, requests)
|
data/lib/sheets_v4/color.rb
CHANGED
@@ -141,13 +141,13 @@ module SheetsV4
|
|
141
141
|
malibu_tint: { red: 0.96862745098, green: 0.68235294118, blue: 0.80000000000 },
|
142
142
|
sea_foam_tint: { red: 0.74901960784, green: 0.92549019608, blue: 0.92156862745 },
|
143
143
|
|
144
|
-
#
|
144
|
+
# Health colors
|
145
145
|
#
|
146
146
|
health_green: { red: 0.00000000000, green: 0.69019607843, blue: 0.31372549020 },
|
147
147
|
health_yellow: { red: 1.00000000000, green: 0.65490196078, blue: 0.00000000000 },
|
148
148
|
health_red: { red: 1.00000000000, green: 0.00000000000, blue: 0.00000000000 },
|
149
149
|
|
150
|
-
#
|
150
|
+
# Fuji design color palette
|
151
151
|
#
|
152
152
|
fuji_color_watermelon: { red: 1.00000000000, green: 0.32156862745, blue: 0.34117647059 },
|
153
153
|
fuji_color_solo_cup: { red: 0.92156862745, green: 0.05882352941, blue: 0.16078431373 },
|
@@ -1,4 +1,3 @@
|
|
1
|
-
# Copyright (c) 2022 Yahoo
|
2
1
|
# frozen_string_literal: true
|
3
2
|
|
4
3
|
require 'google/apis/sheets_v4'
|
@@ -19,6 +18,86 @@ module SheetsV4
|
|
19
18
|
#
|
20
19
|
# @return [Google::Apis::SheetsV4::SheetsService]
|
21
20
|
attr_reader :sheets_service
|
21
|
+
|
22
|
+
# Return the matching sheet object or nil
|
23
|
+
#
|
24
|
+
# If `id_or_title` is an Integer, it is assumed to be the sheet ID. Otherwise,
|
25
|
+
# it is assumed to be the sheet title.
|
26
|
+
#
|
27
|
+
# @example Get the sheet whose title is 'Sheet1'
|
28
|
+
# sheet = spreadsheet.sheet('Sheet1')
|
29
|
+
#
|
30
|
+
# @example Get the sheet whose title is '2023-03-15'
|
31
|
+
# date = Date.new(2023, 3, 15)
|
32
|
+
# sheet = spreadsheet.sheet(date)
|
33
|
+
#
|
34
|
+
# @example Get the sheet whose ID is 123456
|
35
|
+
# sheet = spreadsheet.sheet(123456)
|
36
|
+
#
|
37
|
+
# @param id_or_title [Integer, #to_s] the ID or title of the sheet to return
|
38
|
+
#
|
39
|
+
# @return [Google::Apis::SheetsV4::Sheet, nil]
|
40
|
+
#
|
41
|
+
def sheet(id_or_title)
|
42
|
+
if id_or_title.is_a?(Integer)
|
43
|
+
sheets.find { |sheet| sheet.properties.sheet_id == id_or_title }
|
44
|
+
else
|
45
|
+
title = id_or_title.to_s
|
46
|
+
sheets.find { |sheet| sheet.properties.title == title }
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
# Return the ID of the matching sheet
|
51
|
+
#
|
52
|
+
# @example
|
53
|
+
# id = spreadsheet.sheet_id('Sheet1')
|
54
|
+
#
|
55
|
+
# @param id_or_title [Integer, #to_s] the ID or title of the sheet
|
56
|
+
#
|
57
|
+
# @return [Integer]
|
58
|
+
#
|
59
|
+
def sheet_id(id_or_title)
|
60
|
+
sheet(id_or_title)&.properties&.sheet_id
|
61
|
+
end
|
62
|
+
|
63
|
+
# Iterate over sheets in a spreadsheet
|
64
|
+
#
|
65
|
+
# If `ids_or_titles` is not given or is nil, all sheets are enumerated. Otherwise,
|
66
|
+
# only the sheets whose IDs or titles are in `sheets` are enumerated.
|
67
|
+
#
|
68
|
+
# @example Enumerate all sheets
|
69
|
+
# spreadsheet.each_sheet { |sheet| puts sheet.properties.title }
|
70
|
+
#
|
71
|
+
# @example Enumerate sheets whose IDs are 123456 and 789012
|
72
|
+
# sheets = [123456, 789012]
|
73
|
+
# spreadsheet.each_sheet(sheets).with_index do |sheet, index|
|
74
|
+
# puts "#{index}: #{sheet.properties.title} (#{sheet.properties.sheet_id})"
|
75
|
+
# end
|
76
|
+
#
|
77
|
+
# @param ids_or_titles [Array<Integer, #to_s>] an array of sheet IDs and/or titles
|
78
|
+
#
|
79
|
+
# An Integer in this array is match to the sheet ID. Anything else is matched
|
80
|
+
# to the sheet title after calling `#to_s` on it.
|
81
|
+
#
|
82
|
+
# @return [void]
|
83
|
+
#
|
84
|
+
# @yield each matching sheet
|
85
|
+
# @yieldparam [Google::Apis::SheetsV4::Sheet] the matching sheet
|
86
|
+
#
|
87
|
+
# @raise [RuntimeError] if one of the sheets does not exist
|
88
|
+
# @raise [RuntimeError] if a block is not given
|
89
|
+
#
|
90
|
+
def each_sheet(ids_or_titles = sheets.map { |sheet| sheet.properties.sheet_id }, &block)
|
91
|
+
return enum_for(:each_sheet, ids_or_titles) unless block
|
92
|
+
|
93
|
+
matching_sheets = ids_or_titles.map do |id_or_title|
|
94
|
+
sheet(id_or_title) || raise("Could not find sheet '#{id_or_title}'")
|
95
|
+
end
|
96
|
+
|
97
|
+
matching_sheets.each { |sheet| block[sheet] }
|
98
|
+
|
99
|
+
self
|
100
|
+
end
|
22
101
|
end
|
23
102
|
end
|
24
103
|
end
|
data/lib/sheets_v4/version.rb
CHANGED
data/lib/sheets_v4.rb
CHANGED
@@ -4,7 +4,6 @@ require_relative 'sheets_v4/version'
|
|
4
4
|
require_relative 'sheets_v4/color'
|
5
5
|
require_relative 'sheets_v4/convert_dates_and_times'
|
6
6
|
require_relative 'sheets_v4/create_credential'
|
7
|
-
require_relative 'sheets_v4/api_object_validation'
|
8
7
|
|
9
8
|
require 'active_support'
|
10
9
|
require 'active_support/values/time_zone'
|
@@ -57,41 +56,6 @@ module SheetsV4
|
|
57
56
|
end
|
58
57
|
end
|
59
58
|
|
60
|
-
# @!group Validation
|
61
|
-
|
62
|
-
# Validate the object using the named JSON schema
|
63
|
-
#
|
64
|
-
# The JSON schemas are loaded from the Google Disocvery API. The schemas names are
|
65
|
-
# returned by `SheetsV4.api_object_schema_names`.
|
66
|
-
#
|
67
|
-
# @example
|
68
|
-
# schema_name = 'batch_update_spreadsheet_request'
|
69
|
-
# object = { 'requests' => [] }
|
70
|
-
# SheetsV4.validate_api_object(schema_name:, object:)
|
71
|
-
#
|
72
|
-
# @param schema_name [String] the name of the schema to validate against
|
73
|
-
# @param object [Object] the object to validate
|
74
|
-
# @param logger [Logger] the logger to use for logging error, info, and debug message
|
75
|
-
#
|
76
|
-
# @raise [RuntimeError] if the object does not conform to the schema
|
77
|
-
#
|
78
|
-
# @return [void]
|
79
|
-
#
|
80
|
-
def validate_api_object(schema_name:, object:, logger: Logger.new(nil))
|
81
|
-
SheetsV4::ApiObjectValidation::ValidateApiObject.new(logger:).call(schema_name:, object:)
|
82
|
-
end
|
83
|
-
|
84
|
-
# List the names of the schemas available to use in the Google Sheets API
|
85
|
-
#
|
86
|
-
# @example List the name of the schemas available
|
87
|
-
# SheetsV4.api_object_schema_names #=> ["add_banding_request", "add_banding_response", ...]
|
88
|
-
#
|
89
|
-
# @return [Array<String>] the names of the schemas available
|
90
|
-
#
|
91
|
-
def api_object_schema_names(logger: Logger.new(nil))
|
92
|
-
SheetsV4::ApiObjectValidation::LoadSchemas.new(logger:).call.keys.sort
|
93
|
-
end
|
94
|
-
|
95
59
|
# @!group Colors
|
96
60
|
|
97
61
|
# Given the name of the color, return a Google Sheets API color object
|