sheets_v4 0.5.0 → 0.6.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 +8 -0
- data/README.md +195 -24
- data/lib/sheets_v4/version.rb +1 -1
- data/lib/sheets_v4.rb +20 -5
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 60f10846ff0c3b62cb29c487499eb717897a2b28916606de7d3199fe0424435f
|
4
|
+
data.tar.gz: a6bb345b4379f94dbaa82b266943c144321e3bcf3404db34c1788e766813a766
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6914986ccca02b55b260b2a39e6777ce5e2cbca03f6fd00053d8ed96a960456831ee008bd175448da343eeaf0bcfc8c67309e489035b9792ec4ce51aba29d917
|
7
|
+
data.tar.gz: f76c250d9b9bec531ca6185ef3b2b351df6e0a12eccb1d6f862cf6a63d4b81305798630d992d3ec22fb44a94cbd488f3736e85da6ff72302eb7b7cf87017e176
|
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.6.0 (2023-10-03)
|
8
|
+
|
9
|
+
[Full Changelog](https://github.com/main-branch/sheets_v4/compare/v0.5.0..v0.6.0)
|
10
|
+
|
11
|
+
Changes since v0.5.0:
|
12
|
+
|
13
|
+
* 2eab61d Update documentation explaining how to construct and validate a request (#17)
|
14
|
+
|
7
15
|
## v0.5.0 (2023-10-01)
|
8
16
|
|
9
17
|
[Full Changelog](https://github.com/main-branch/sheets_v4/compare/v0.4.0..v0.5.0)
|
data/README.md
CHANGED
@@ -9,29 +9,51 @@
|
|
9
9
|
|
10
10
|
Unofficial helpers for the Google Sheets V4 API
|
11
11
|
|
12
|
-
* [Important Links for Programming Google Sheets](#important-links-for-programming-google-sheets)
|
13
|
-
* [General API Documentation](#general-api-documentation)
|
14
|
-
* [Ruby Implementation of the Sheets API](#ruby-implementation-of-the-sheets-api)
|
15
|
-
* [Other Links](#other-links)
|
16
12
|
* [Installation](#installation)
|
13
|
+
* [Important links for programming Google Sheets](#important-links-for-programming-google-sheets)
|
14
|
+
* [General API documentation](#general-api-documentation)
|
15
|
+
* [Ruby implementation of the Sheets API](#ruby-implementation-of-the-sheets-api)
|
16
|
+
* [Other Links](#other-links)
|
17
|
+
* [Getting Started](#getting-started)
|
18
|
+
* [Creating a Google Cloud project](#creating-a-google-cloud-project)
|
19
|
+
* [Enable the APIs you want to use](#enable-the-apis-you-want-to-use)
|
20
|
+
* [Create a Google API credentials](#create-a-google-api-credentials)
|
17
21
|
* [Usage](#usage)
|
18
22
|
* [Obtaining an authenticated SheetsService](#obtaining-an-authenticated-sheetsservice)
|
23
|
+
* [Building a request](#building-a-request)
|
24
|
+
* [Method 1: constructing requests using `Google::Apis::SheetsV4::*` objects](#method-1-constructing-requests-using-googleapissheetsv4-objects)
|
25
|
+
* [Method 2: constructing requests using hashes](#method-2-constructing-requests-using-hashes)
|
26
|
+
* [Which method should be used?](#which-method-should-be-used)
|
27
|
+
* [Validating requests](#validating-requests)
|
19
28
|
* [Colors](#colors)
|
20
29
|
* [Development](#development)
|
21
|
-
* [Creating a Google API Service Account](#creating-a-google-api-service-account)
|
22
30
|
* [Contributing](#contributing)
|
23
31
|
* [License](#license)
|
24
32
|
|
25
|
-
##
|
33
|
+
## Installation
|
26
34
|
|
27
|
-
|
35
|
+
Install the gem and add to the application's Gemfile by executing:
|
36
|
+
|
37
|
+
```shell
|
38
|
+
bundle add sheets_v4
|
39
|
+
```
|
40
|
+
|
41
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
42
|
+
|
43
|
+
```shell
|
44
|
+
gem install sheets_v4
|
45
|
+
```
|
46
|
+
|
47
|
+
## Important links for programming Google Sheets
|
48
|
+
|
49
|
+
### General API documentation
|
28
50
|
|
29
51
|
* [Google Sheets API Overview](https://developers.google.com/sheets/api)
|
30
52
|
* [Google Sheets API Reference](https://developers.google.com/sheets/api/reference/rest)
|
31
53
|
* [Batch Update Requests](https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/request)
|
32
54
|
* [Discovery Document for the Sheets API](https://sheets.googleapis.com/$discovery/rest?version=v4)
|
33
55
|
|
34
|
-
### Ruby
|
56
|
+
### Ruby implementation of the Sheets API
|
35
57
|
|
36
58
|
* [SheetsService Class](https://github.com/googleapis/google-api-ruby-client/blob/main/generated/google-apis-sheets_v4/lib/google/apis/sheets_v4/service.rb)
|
37
59
|
* [All Other Sheets Classes](https://github.com/googleapis/google-api-ruby-client/blob/main/generated/google-apis-sheets_v4/lib/google/apis/sheets_v4/classes.rb)
|
@@ -40,19 +62,25 @@ Unofficial helpers for the Google Sheets V4 API
|
|
40
62
|
|
41
63
|
* [Apps Script for Sheets](https://developers.google.com/apps-script/guides/sheets)
|
42
64
|
|
43
|
-
##
|
65
|
+
## Getting Started
|
44
66
|
|
45
|
-
|
67
|
+
In order to use this gem, you will need to obtain a Google API sheets service
|
68
|
+
credential following the instructions below.
|
46
69
|
|
47
|
-
|
48
|
-
bundle add sheets_v4
|
49
|
-
```
|
70
|
+
### Creating a Google Cloud project
|
50
71
|
|
51
|
-
|
72
|
+
Create a Google Cloud project using [these directions](https://developers.google.com/workspace/guides/create-project).
|
52
73
|
|
53
|
-
|
54
|
-
|
55
|
-
|
74
|
+
### Enable the APIs you want to use
|
75
|
+
|
76
|
+
Enable the Sheets API for this project using [these directions](https://developers.google.com/workspace/guides/enable-apis).
|
77
|
+
|
78
|
+
### Create a Google API credentials
|
79
|
+
|
80
|
+
Create a service account and download credentials using [these directions](https://developers.google.com/workspace/guides/create-credentials#service-account).
|
81
|
+
|
82
|
+
You can store the download credential files anywhere on your system. The recommended
|
83
|
+
location is `~/.google-api-credential.json`.
|
56
84
|
|
57
85
|
## Usage
|
58
86
|
|
@@ -86,19 +114,165 @@ If the credential is stored elsewhere, pass the credential_source to `SheetsV4.s
|
|
86
114
|
manually. `credential_source` can be a String:
|
87
115
|
|
88
116
|
```Ruby
|
89
|
-
sheets_service = SheetsV4.sheets_service(
|
117
|
+
sheets_service = SheetsV4.sheets_service(credential_source: File.read('credential.json'))
|
90
118
|
```
|
91
119
|
|
92
120
|
an IO object:
|
93
121
|
|
94
122
|
```Ruby
|
95
123
|
sheets_service = File.open('credential.json') do |credential_source|
|
96
|
-
SheetsV4.sheets_service(
|
124
|
+
SheetsV4.sheets_service(credential_source:)
|
97
125
|
end
|
98
126
|
```
|
99
127
|
|
100
128
|
or an already constructed `Google::Auth::*`` object.
|
101
129
|
|
130
|
+
### Building a request
|
131
|
+
|
132
|
+
To use the Sheets API, you need to construct JSON formatted requests.
|
133
|
+
|
134
|
+
These requests can be constructed using two different methods:
|
135
|
+
1. constructing requests using `Google::Apis::SheetsV4::*` objects or
|
136
|
+
2. constructing requests using hashes
|
137
|
+
|
138
|
+
The following two sections show how each method can be used to construct
|
139
|
+
a request to update a row of values in a sheet.
|
140
|
+
|
141
|
+
For these two examples, values in the `values` array will be written to the
|
142
|
+
sheet whose ID is 0. The values will be written one per row starting at cell A1.
|
143
|
+
|
144
|
+
```Ruby
|
145
|
+
values = %w[one two three four] # 'one' goes in A1, 'two' goes in A2, etc.
|
146
|
+
```
|
147
|
+
|
148
|
+
The method `SheetsService#batch_update_spreadsheet` will be used to write the values. This
|
149
|
+
method takes a `batch_update_spreadsheet_request` object with a `update_cells` request
|
150
|
+
that defines the update to perform.
|
151
|
+
|
152
|
+
#### Method 1: constructing requests using `Google::Apis::SheetsV4::*` objects
|
153
|
+
|
154
|
+
When using this method, keep the Ruby source file containing the SheetsService class
|
155
|
+
([google/apis/sheets_v4/service.rb](https://github.com/googleapis/google-api-ruby-client/blob/main/generated/google-apis-sheets_v4/lib/google/apis/sheets_v4/service.rb))
|
156
|
+
and the Ruby source file containing the defitions of the request & data classes
|
157
|
+
([lib/google/apis/sheets_v4/classes.rb](https://github.com/googleapis/google-api-ruby-client/blob/main/generated/google-apis-sheets_v4/lib/google/apis/sheets_v4/classes.rb))
|
158
|
+
open for easy searching. These files will give you all the information you need
|
159
|
+
to construct valid requests.
|
160
|
+
|
161
|
+
Here is the example constructing requests using `Google::Apis::SheetsV4::*` objects
|
162
|
+
|
163
|
+
```Ruby
|
164
|
+
def values = %w[one two three four]
|
165
|
+
|
166
|
+
def row_data(value)
|
167
|
+
Google::Apis::SheetsV4::RowData.new(
|
168
|
+
values: [
|
169
|
+
Google::Apis::SheetsV4::CellData.new(
|
170
|
+
user_entered_value:
|
171
|
+
Google::Apis::SheetsV4::ExtendedValue.new(string_value: value.to_s)
|
172
|
+
)
|
173
|
+
]
|
174
|
+
)
|
175
|
+
end
|
176
|
+
|
177
|
+
def rows
|
178
|
+
values.map { |value| row_data(value) }
|
179
|
+
end
|
180
|
+
|
181
|
+
def write_values_request
|
182
|
+
fields = 'user_entered_value'
|
183
|
+
start = Google::Apis::SheetsV4::GridCoordinate.new(
|
184
|
+
sheet_id: 0, row_index: 0, column_index: 0
|
185
|
+
)
|
186
|
+
Google::Apis::SheetsV4::Request.new(
|
187
|
+
update_cells: Google::Apis::SheetsV4::UpdateCellsRequest.new(rows:, fields:, start:)
|
188
|
+
)
|
189
|
+
end
|
190
|
+
|
191
|
+
requests = Google::Apis::SheetsV4::BatchUpdateSpreadsheetRequest.new(requests: [write_values_request])
|
192
|
+
|
193
|
+
spreadsheet_id = '18FAcgotK7nDfLTOTQuGCIjKwxkJMAguhn1OVzpFFgWY'
|
194
|
+
SheetsV4.sheets_service.batch_update_spreadsheet(spreadsheet_id, requests)
|
195
|
+
```
|
196
|
+
|
197
|
+
#### Method 2: constructing requests using hashes
|
198
|
+
|
199
|
+
When constructing requests using this method, keep the [Google Sheets Rest API Reference](https://developers.google.com/sheets/api/reference/rest)
|
200
|
+
documentation open. In particular, [the Batch Update Requests page](https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/request#cutpasterequest)
|
201
|
+
is particularly useful for building spreadsheet batch update requests.
|
202
|
+
|
203
|
+
One caveat to keep in mind is that the Rest API documents object properties using
|
204
|
+
Camel case BUT the Ruby API requires snake case.
|
205
|
+
|
206
|
+
For instance, the Rest API documents the properties for a grid coordinate to be
|
207
|
+
"sheetId", "rowIndex", and "columnIndex". However, in the Ruby API, you should
|
208
|
+
construct this object using snake case:
|
209
|
+
|
210
|
+
```Ruby
|
211
|
+
grid_coordinate = { sheet_id: 0, row_index: 0, column_index: 0 }
|
212
|
+
```
|
213
|
+
|
214
|
+
Here is the example constructing requests using hashes:
|
215
|
+
|
216
|
+
```Ruby
|
217
|
+
def values = %w[one two three four]
|
218
|
+
|
219
|
+
def rows
|
220
|
+
values.map do |value|
|
221
|
+
{ values: [{ user_entered_value: { string_value: value } }] }
|
222
|
+
end
|
223
|
+
end
|
224
|
+
|
225
|
+
def write_values_request
|
226
|
+
fields = 'user_entered_value'
|
227
|
+
start = { sheet_id: 0, row_index: 0, column_index: 0 }
|
228
|
+
{ update_cells: { rows:, fields:, start: } }
|
229
|
+
end
|
230
|
+
|
231
|
+
requests = { requests: [write_values_request] }
|
232
|
+
|
233
|
+
spreadsheet_id = '18FAcgotK7nDfLTOTQuGCIjKwxkJMAguhn1OVzpFFgWY'
|
234
|
+
response = SheetsV4.sheets_service.batch_update_spreadsheet(spreadsheet_id, requests)
|
235
|
+
```
|
236
|
+
|
237
|
+
#### Which method should be used?
|
238
|
+
|
239
|
+
Either method will do the same job. I prefer "Method 2: constructing requests using
|
240
|
+
hashes" because my code is more concise and easy to read.
|
241
|
+
|
242
|
+
While either method can produce a malformed request, "Method 2" is more likely to
|
243
|
+
result in malformed requests. Unfortunately, when given a malformed request, the
|
244
|
+
Google Sheets API will do one of following depending on the nature of the problem:
|
245
|
+
|
246
|
+
1. Raise a `Google::Apis::ClientError` with some additional information
|
247
|
+
2. Raise a `Google::Apis::ClientError` with no additional information (this the most
|
248
|
+
common result)
|
249
|
+
3. Not return an error with some of the batch requests not having the expected outcome
|
250
|
+
|
251
|
+
Luckily, this library provides a way to validate that requests are valid and
|
252
|
+
identifies precisely where the request objects do not conform to the API description.
|
253
|
+
That is the subject of the next section [Validating requests](#validating-requests).
|
254
|
+
|
255
|
+
### Validating requests
|
256
|
+
|
257
|
+
The [`SheetsV4.validate_api_object`](https://rubydoc.info/gems/sheets_v4/SheetsV4#validate_api_object-class_method)
|
258
|
+
method can be used to validate request objects prior to using them in the Google
|
259
|
+
Sheets API.
|
260
|
+
|
261
|
+
This method takes a `schema_name` and an `object` to validate. Schema names can be
|
262
|
+
listed using [`SheetsV4.api_object_schema_names`](https://rubydoc.info/gems/sheets_v4/SheetsV4#api_object_schema_names-class_method).
|
263
|
+
|
264
|
+
This method will either return `true` if `object` conforms to the schema OR it
|
265
|
+
will raise a RuntimeError noting where the object structure did not conform to
|
266
|
+
the schema.
|
267
|
+
|
268
|
+
In the previous examples (see [Building a request](#building-a-request)), the
|
269
|
+
following line can be inserted after the `requests = ...` line to validate the
|
270
|
+
request:
|
271
|
+
|
272
|
+
```Ruby
|
273
|
+
SheetsV4.validate_api_object(schema: 'batch_update_spreadsheet_request', object: requests)
|
274
|
+
```
|
275
|
+
|
102
276
|
### Colors
|
103
277
|
|
104
278
|
Color objects (with appropriate :red, :green, :blue values) can be retrieved by name
|
@@ -114,8 +288,8 @@ SheetsV4::Color.black #=> {:red=>0.0, :green=>0.0, :blue=>0.0}
|
|
114
288
|
|
115
289
|
## Development
|
116
290
|
|
117
|
-
After checking out the repo, run `bin/setup` to install dependencies
|
118
|
-
`rake
|
291
|
+
After checking out the repo, run `bin/setup` to install dependencies and then, run
|
292
|
+
`rake` to run the tests, static analysis, etc. You can also run `bin/console` for an interactive
|
119
293
|
prompt that will allow you to experiment.
|
120
294
|
|
121
295
|
To install this gem onto your local machine, run `bundle exec rake install`. To
|
@@ -124,9 +298,6 @@ release a new version, update the version number in `version.rb`, and then run
|
|
124
298
|
commits and the created tag, and push the `.gem` file to
|
125
299
|
[rubygems.org](https://rubygems.org).
|
126
300
|
|
127
|
-
## Creating a Google API Service Account
|
128
|
-
|
129
|
-
|
130
301
|
## Contributing
|
131
302
|
|
132
303
|
Bug reports and pull requests are welcome on [the main-branch/sheets_v4 GitHub project](https://github.com/main-branch/sheets_v4).
|
data/lib/sheets_v4/version.rb
CHANGED
data/lib/sheets_v4.rb
CHANGED
@@ -19,12 +19,16 @@ module SheetsV4
|
|
19
19
|
#
|
20
20
|
# Simplifies creating and configuring a the credential.
|
21
21
|
#
|
22
|
-
# @example using the
|
22
|
+
# @example using the credential in `~/.google-api-credential`
|
23
23
|
# SheetsV4.sheets_service
|
24
24
|
#
|
25
25
|
# @example using a credential passed in as a string
|
26
|
-
# credential_source = File.read(File.
|
27
|
-
# SheetsV4.sheets_service(credential_source:
|
26
|
+
# credential_source = File.read(File.expand_path('~/.google-api-credential.json'))
|
27
|
+
# SheetsV4.sheets_service(credential_source:)
|
28
|
+
#
|
29
|
+
# @example using a credential passed in as an IO
|
30
|
+
# credential_source = File.open(File.expand_path('~/.google-api-credential.json'))
|
31
|
+
# SheetsV4.sheets_service(credential_source:)
|
28
32
|
#
|
29
33
|
# @param credential_source [nil, String, IO, Google::Auth::*] may
|
30
34
|
# be either an already constructed credential, the credential read into a String or
|
@@ -50,10 +54,10 @@ module SheetsV4
|
|
50
54
|
# Validate the object using the named JSON schema
|
51
55
|
#
|
52
56
|
# The JSON schemas are loaded from the Google Disocvery API. The schemas names are
|
53
|
-
# returned by `SheetsV4.
|
57
|
+
# returned by `SheetsV4.api_object_schema_names`.
|
54
58
|
#
|
55
59
|
# @example
|
56
|
-
# schema_name = '
|
60
|
+
# schema_name = 'batch_update_spreadsheet_request'
|
57
61
|
# object = { 'requests' => [] }
|
58
62
|
# SheetsV4.validate_api_object(schema_name:, object:)
|
59
63
|
#
|
@@ -69,6 +73,17 @@ module SheetsV4
|
|
69
73
|
SheetsV4::ValidateApiObjects::Validate.new(logger:).call(schema_name:, object:)
|
70
74
|
end
|
71
75
|
|
76
|
+
# List the names of the schemas available to use in the Google Sheets API
|
77
|
+
#
|
78
|
+
# @example List the name of the schemas available
|
79
|
+
# SheetsV4.api_object_schema_names #=> ["add_banding_request", "add_banding_response", ...]
|
80
|
+
#
|
81
|
+
# @return [Array<String>] the names of the schemas available
|
82
|
+
#
|
83
|
+
def self.api_object_schema_names(logger: Logger.new(nil))
|
84
|
+
SheetsV4::ValidateApiObjects::LoadSchemas.new(logger:).call.keys.sort
|
85
|
+
end
|
86
|
+
|
72
87
|
# Given the name of the color, return a Google Sheets API color object
|
73
88
|
#
|
74
89
|
# Available color names are listed using `SheetsV4.color_names`.
|
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.6.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-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler-audit
|