drive_v3 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.markdownlint.yml +25 -0
- data/.rspec +3 -0
- data/.rubocop.yml +37 -0
- data/.yardopts +6 -0
- data/CHANGELOG.md +24 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/LICENSE.txt +21 -0
- data/README.md +436 -0
- data/Rakefile +101 -0
- data/examples/README.md +300 -0
- data/examples/drive_service_batch +72 -0
- data/examples/file_create +66 -0
- data/examples/file_create_folder +46 -0
- data/examples/file_create_spreadsheet +70 -0
- data/examples/file_delete +24 -0
- data/examples/file_download_content +27 -0
- data/examples/file_export_spreadsheet +48 -0
- data/examples/file_get +37 -0
- data/examples/file_recover_from_trash +24 -0
- data/examples/file_search +86 -0
- data/examples/file_send_to_trash +24 -0
- data/examples/file_upload_content +25 -0
- data/examples/permission_create +165 -0
- data/examples/permission_delete +22 -0
- data/examples/permission_list +49 -0
- data/examples/permission_update +134 -0
- data/lib/drive_v3/create_credential.rb +93 -0
- data/lib/drive_v3/version.rb +6 -0
- data/lib/drive_v3.rb +52 -0
- metadata +307 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 882a97d3563d0c5565e00ae57c230fbef5fcd94b233dfe4281cfbaaa81ed4bbe
|
4
|
+
data.tar.gz: 215a903e2c6f89652ae2da0e8a3ad55ad0294a170982206e9525f1c572fc8ab0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 42a1dca40e3e0fcd85c679ffd90b906bd0ef0cc3e384b0aa5121999b5122dc174fd0c379bb3d2aafcabca14b5984246dde28a4cf53023e9de79ba423232f9916
|
7
|
+
data.tar.gz: 54a99a6bf6b9751914def5bece84593e1423045867a40e126d8c9bc749a79adcb2288d8f7f42a3d3ed46a90f0dfa96910047e255409a81551a452b4bfefaf005
|
data/.markdownlint.yml
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
default: true
|
2
|
+
|
3
|
+
# Unordered list indentation
|
4
|
+
MD007: { indent: 2 }
|
5
|
+
|
6
|
+
# Line length
|
7
|
+
MD013: { line_length: 90, tables: false, code_blocks: false }
|
8
|
+
|
9
|
+
# Heading duplication is allowed for non-sibling headings
|
10
|
+
MD024: { siblings_only: true }
|
11
|
+
|
12
|
+
# Do not allow the specified trailig punctuation in a header
|
13
|
+
MD026: { punctuation: '.,;:' }
|
14
|
+
|
15
|
+
# Order list items must have a prefix that increases in numerical order
|
16
|
+
MD029: { style: 'ordered' }
|
17
|
+
|
18
|
+
# Lists do not need to be surrounded by blank lines
|
19
|
+
MD032: false
|
20
|
+
|
21
|
+
# Allow raw HTML in Markdown
|
22
|
+
MD033: false
|
23
|
+
|
24
|
+
# Allow emphasis to be used instead of a heading
|
25
|
+
MD036: false
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
AllCops:
|
2
|
+
NewCops: enable
|
3
|
+
# Output extra information for each offense to make it easier to diagnose:
|
4
|
+
DisplayCopNames: true
|
5
|
+
DisplayStyleGuide: true
|
6
|
+
ExtraDetails: true
|
7
|
+
SuggestExtensions: false
|
8
|
+
# RuboCop enforces rules depending on the oldest version of Ruby which
|
9
|
+
# your project supports:
|
10
|
+
TargetRubyVersion: 3.1
|
11
|
+
|
12
|
+
Gemspec/DevelopmentDependencies:
|
13
|
+
EnforcedStyle: gemspec
|
14
|
+
|
15
|
+
# The default max line length is 80 characters
|
16
|
+
Layout/LineLength:
|
17
|
+
Max: 120
|
18
|
+
|
19
|
+
# The DSL for RSpec and the gemspec file make it very hard to limit block length:
|
20
|
+
Metrics/BlockLength:
|
21
|
+
Exclude:
|
22
|
+
- "spec/spec_helper.rb"
|
23
|
+
- "spec/**/*_spec.rb"
|
24
|
+
- "*.gemspec"
|
25
|
+
|
26
|
+
Metrics/ModuleLength:
|
27
|
+
CountAsOne: ['hash']
|
28
|
+
|
29
|
+
# When writing minitest tests, it is very hard to limit test class length:
|
30
|
+
Metrics/ClassLength:
|
31
|
+
CountAsOne: ['hash']
|
32
|
+
Exclude:
|
33
|
+
- "test/**/*_test.rb"
|
34
|
+
|
35
|
+
Style/AsciiComments:
|
36
|
+
Enabled: false
|
37
|
+
|
data/.yardopts
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# Change Log
|
2
|
+
|
3
|
+
Changes for each release are listed in this file.
|
4
|
+
|
5
|
+
This project adheres to [Semantic Versioning](https://semver.org/) for its releases.
|
6
|
+
|
7
|
+
## v0.2.0 (2023-12-04)
|
8
|
+
|
9
|
+
[Full Changelog](https://github.com/main-branch/drive_v3/compare/v0.1.0..v0.2.0)
|
10
|
+
|
11
|
+
Changes since v0.1.0:
|
12
|
+
|
13
|
+
* e23a653 Add parameters to support searching all drives (#4)
|
14
|
+
|
15
|
+
## v0.1.0 (2023-12-02)
|
16
|
+
|
17
|
+
[Full Changelog](https://github.com/main-branch/drive_v3/compare/cadfd09..v0.1.0)
|
18
|
+
|
19
|
+
Changes:
|
20
|
+
|
21
|
+
* 75e1077 Fix links in the READMEs (#2)
|
22
|
+
* 671e412 Add examples for managing files and permissions (#1)
|
23
|
+
* d0bab77 Update examples documentation
|
24
|
+
* cadfd09 Initial commit
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at jcouball@yahoo.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2023 James Couball
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,436 @@
|
|
1
|
+
# DriveV3
|
2
|
+
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/drive_v3.svg)](https://badge.fury.io/rb/drive_v3)
|
4
|
+
[![Documentation](https://img.shields.io/badge/Documentation-Latest-green)](https://rubydoc.info/gems/drive_v3/)
|
5
|
+
[![Change Log](https://img.shields.io/badge/CHANGELOG-Latest-green)](https://rubydoc.info/gems/drive_v3/file/CHANGELOG.md)
|
6
|
+
[![Build Status](https://github.com/main-branch/drive_v3/workflows/CI%20Build/badge.svg?branch=main)](https://github.com/main-branch/drive_v3/actions?query=workflow%3ACI%20Build)
|
7
|
+
[![Maintainability](https://api.codeclimate.com/v1/badges/aeebc016487c5cad881e/maintainability)](https://codeclimate.com/github/main-branch/drive_v3/maintainability)
|
8
|
+
[![Test Coverage](https://api.codeclimate.com/v1/badges/aeebc016487c5cad881e/test_coverage)](https://codeclimate.com/github/main-branch/drive_v3/test_coverage)
|
9
|
+
|
10
|
+
Unofficial helpers and extensions for the Google Drive V3 API
|
11
|
+
|
12
|
+
* [Installation](#installation)
|
13
|
+
* [Examples](#examples)
|
14
|
+
* [Important links for programming Google Drive](#important-links-for-programming-google-drive)
|
15
|
+
* [DriveV3 documenation](#drivev3-documenation)
|
16
|
+
* [General API documentation](#general-api-documentation)
|
17
|
+
* [Ruby implementation of the Drive API](#ruby-implementation-of-the-drive-api)
|
18
|
+
* [Other links](#other-links)
|
19
|
+
* [Getting started](#getting-started)
|
20
|
+
* [Create a Google Cloud project](#create-a-google-cloud-project)
|
21
|
+
* [Enable the APIs you want to use](#enable-the-apis-you-want-to-use)
|
22
|
+
* [Download a Google API credential file](#download-a-google-api-credential-file)
|
23
|
+
* [Usage](#usage)
|
24
|
+
* [Obtaining an authenticated DriveService](#obtaining-an-authenticated-driveservice)
|
25
|
+
* [Building a request](#building-a-request)
|
26
|
+
* [Method 1: constructing requests using `Google::Apis::SheetsV4::*` objects](#method-1-constructing-requests-using-googleapissheetsv4-objects)
|
27
|
+
* [Method 2: constructing requests using hashes](#method-2-constructing-requests-using-hashes)
|
28
|
+
* [Which method should be used?](#which-method-should-be-used)
|
29
|
+
* [Validating requests](#validating-requests)
|
30
|
+
* [Google Extensions](#google-extensions)
|
31
|
+
* [SheetsService Extensions](#sheetsservice-extensions)
|
32
|
+
* [Spreadsheet Extensions](#spreadsheet-extensions)
|
33
|
+
* [Sheet Extensions](#sheet-extensions)
|
34
|
+
* [Working with dates and times](#working-with-dates-and-times)
|
35
|
+
* [Working with colors](#working-with-colors)
|
36
|
+
* [Development](#development)
|
37
|
+
* [Contributing](#contributing)
|
38
|
+
* [License](#license)
|
39
|
+
|
40
|
+
## Installation
|
41
|
+
|
42
|
+
Install the gem and add to the application's Gemfile by executing:
|
43
|
+
|
44
|
+
```shell
|
45
|
+
bundle add drive_v3
|
46
|
+
```
|
47
|
+
|
48
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
49
|
+
|
50
|
+
```shell
|
51
|
+
gem install drive_v3
|
52
|
+
```
|
53
|
+
|
54
|
+
## Examples
|
55
|
+
|
56
|
+
Many examples can be found in the `examples` directory of this project. The examples
|
57
|
+
have [a README of their own](https://github.com/main-branch/drive_v3/blob/main/examples/README.md)
|
58
|
+
which describes each example.
|
59
|
+
|
60
|
+
Prior to running an example, clone this project and run `bundle install` in the
|
61
|
+
project's root working directory.
|
62
|
+
|
63
|
+
Run an example using `bundle exec`. For example, to run the `set_background_color1`
|
64
|
+
example:
|
65
|
+
|
66
|
+
```shell
|
67
|
+
bundle exec examples/set_background_color1
|
68
|
+
```
|
69
|
+
|
70
|
+
## Important links for programming Google Drive
|
71
|
+
|
72
|
+
### DriveV3 documenation
|
73
|
+
|
74
|
+
This Gem's YARD documentation is hosted on [rubydoc.info]https://rubydoc.info/gems/drive_v3/.
|
75
|
+
|
76
|
+
### General API documentation
|
77
|
+
|
78
|
+
* [Google Drive API Overview](https://developers.google.com/drive/api)
|
79
|
+
* [Google Drive API Reference](https://developers.google.com/drive/api/reference/rest)
|
80
|
+
* [Discovery Document for the Drive API](https://www.googleapis.com/discovery/v1/apis/drive/v3/rest)
|
81
|
+
|
82
|
+
### Ruby implementation of the Drive API
|
83
|
+
|
84
|
+
* [DriveService Class](https://github.com/googleapis/google-api-ruby-client/blob/main/generated/google-apis-drive_v3/lib/google/apis/drive_v3/service.rb)
|
85
|
+
* [All Other Drive Classes](https://github.cofilem/googleapis/google-api-ruby-client/blob/main/generated/google-apis-drive_v3/lib/google/apis/drive_v3/classes.rb)
|
86
|
+
|
87
|
+
### Other links
|
88
|
+
|
89
|
+
* None yet!
|
90
|
+
|
91
|
+
## Getting started
|
92
|
+
|
93
|
+
In order to use this gem, you will need to obtain a Google API service account
|
94
|
+
credential following the instructions below.
|
95
|
+
|
96
|
+
### Create a Google Cloud project
|
97
|
+
|
98
|
+
Create a Google Cloud project using [these directions](https://developers.google.com/workspace/guides/create-project).
|
99
|
+
|
100
|
+
### Enable the APIs you want to use
|
101
|
+
|
102
|
+
Enable the Drive API for this project using [these directions](https://developers.google.com/workspace/guides/enable-apis).
|
103
|
+
|
104
|
+
### Download a Google API credential file
|
105
|
+
|
106
|
+
Create a service account and download a credential file using [these directions](https://developers.google.com/workspace/guides/create-credentials#service-account).
|
107
|
+
|
108
|
+
You can store the download credential files anywhere on your system.
|
109
|
+
The recommended location is `~/.google-api-credential.json` since this is default
|
110
|
+
credential file that `DriveV3.drive_service` uses.
|
111
|
+
|
112
|
+
## Usage
|
113
|
+
|
114
|
+
[Detailed API documenation](https://rubydoc.info/gems/drive_v3/) is hosted on rubygems.org.
|
115
|
+
|
116
|
+
### Obtaining an authenticated DriveService
|
117
|
+
|
118
|
+
Typically, to construct an authenticated DriveService object where the credential
|
119
|
+
is read from a file, the following code is required:
|
120
|
+
|
121
|
+
```Ruby
|
122
|
+
require 'googleauth'
|
123
|
+
drive_service = Google::Apis::DriveV3::DriveService.new
|
124
|
+
credential = File.read(File.expand_path('~/google-api-credential.json')) do |credential_source|
|
125
|
+
scopes = Google::Apis::DriveV4::AUTH_DRIVE
|
126
|
+
options = { json_key_io: credential_source, scope: scopes }
|
127
|
+
Google::Auth::DefaultCredentials.make_creds(options).tap(&:fetch_access_token)
|
128
|
+
end
|
129
|
+
drive_service.authorization = credential
|
130
|
+
```
|
131
|
+
|
132
|
+
The `SheetsV4.sheets_service` method simplifies this a bit.
|
133
|
+
|
134
|
+
By default, the credential is read from `~/.google-api-credential.json`. In that case,
|
135
|
+
an authenticated SheetsService object can be obtained with one method call:
|
136
|
+
|
137
|
+
```Ruby
|
138
|
+
sheets_service = SheetsV4.sheets_service
|
139
|
+
```
|
140
|
+
|
141
|
+
If the credential is stored somewhere else, pass the `credential_source` to
|
142
|
+
`SheetsV4.sheets_service` manually. `credential_source` can be a String:
|
143
|
+
|
144
|
+
```Ruby
|
145
|
+
sheets_service = SheetsV4.sheets_service(credential_source: File.read('credential.json'))
|
146
|
+
```
|
147
|
+
|
148
|
+
an IO object:
|
149
|
+
|
150
|
+
```Ruby
|
151
|
+
sheets_service = File.open('credential.json') do |credential_source|
|
152
|
+
SheetsV4.sheets_service(credential_source:)
|
153
|
+
end
|
154
|
+
```
|
155
|
+
|
156
|
+
or an already constructed `Google::Auth::*` object.
|
157
|
+
|
158
|
+
### Building a request
|
159
|
+
|
160
|
+
To use the Sheets API, you need to construct JSON formatted requests.
|
161
|
+
|
162
|
+
These requests can be constructed using two different methods:
|
163
|
+
1. constructing requests using `Google::Apis::SheetsV4::*` objects or
|
164
|
+
2. constructing requests using hashes
|
165
|
+
|
166
|
+
The following two sections show how each method can be used to construct
|
167
|
+
a request to update a row of values in a sheet.
|
168
|
+
|
169
|
+
For these two examples, values in the `values` array will be written to the
|
170
|
+
sheet whose ID is 0. The values will be written one per row starting at cell A1.
|
171
|
+
|
172
|
+
```Ruby
|
173
|
+
values = %w[one two three four] # 'one' goes in A1, 'two' goes in A2, etc.
|
174
|
+
```
|
175
|
+
|
176
|
+
The method `SheetsService#batch_update_spreadsheet` will be used to write the values. This
|
177
|
+
method takes a `batch_update_spreadsheet_request` object with a `update_cells` request
|
178
|
+
that defines the update to perform.
|
179
|
+
|
180
|
+
#### Method 1: constructing requests using `Google::Apis::SheetsV4::*` objects
|
181
|
+
|
182
|
+
When using this method, keep the Ruby source file containing the SheetsService class
|
183
|
+
([google/apis/drive_v3/service.rb](https://github.com/googleapis/google-api-ruby-client/blob/main/generated/google-apis-drive_v3/lib/google/apis/drive_v3/service.rb))
|
184
|
+
and the Ruby source file containing the defitions of the request & data classes
|
185
|
+
([lib/google/apis/drive_v3/classes.rb](https://github.com/googleapis/google-api-ruby-client/blob/main/generated/google-apis-drive_v3/lib/google/apis/drive_v3/classes.rb))
|
186
|
+
open for easy searching. These files will give you all the information you need
|
187
|
+
to construct valid requests.
|
188
|
+
|
189
|
+
Here is the example constructing requests using `Google::Apis::SheetsV4::*` objects
|
190
|
+
|
191
|
+
```Ruby
|
192
|
+
def values = %w[one two three four]
|
193
|
+
|
194
|
+
def row_data(value)
|
195
|
+
Google::Apis::SheetsV4::RowData.new(
|
196
|
+
values: [
|
197
|
+
Google::Apis::SheetsV4::CellData.new(
|
198
|
+
user_entered_value:
|
199
|
+
Google::Apis::SheetsV4::ExtendedValue.new(string_value: value.to_s)
|
200
|
+
)
|
201
|
+
]
|
202
|
+
)
|
203
|
+
end
|
204
|
+
|
205
|
+
def rows
|
206
|
+
values.map { |value| row_data(value) }
|
207
|
+
end
|
208
|
+
|
209
|
+
def write_values_request
|
210
|
+
fields = 'user_entered_value'
|
211
|
+
start = Google::Apis::SheetsV4::GridCoordinate.new(
|
212
|
+
sheet_id: 0, row_index: 0, column_index: 0
|
213
|
+
)
|
214
|
+
Google::Apis::SheetsV4::Request.new(
|
215
|
+
update_cells: Google::Apis::SheetsV4::UpdateCellsRequest.new(rows:, fields:, start:)
|
216
|
+
)
|
217
|
+
end
|
218
|
+
|
219
|
+
requests = Google::Apis::SheetsV4::BatchUpdateSpreadsheetRequest.new(requests: [write_values_request])
|
220
|
+
|
221
|
+
spreadsheet_id = '18FAcgotK7nDfLTOTQuGCIjKwxkJMAguhn1OVzpFFgWY'
|
222
|
+
SheetsV4.sheets_service.batch_update_spreadsheet(spreadsheet_id, requests)
|
223
|
+
```
|
224
|
+
|
225
|
+
#### Method 2: constructing requests using hashes
|
226
|
+
|
227
|
+
When constructing requests using this method, keep the [Google Sheets Rest API Reference](https://developers.google.com/sheets/api/reference/rest)
|
228
|
+
documentation open. In particular, [the Batch Update Requests page](https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/request#cutpasterequest)
|
229
|
+
is particularly useful for building spreadsheet batch update requests.
|
230
|
+
|
231
|
+
One caveat to keep in mind is that the Rest API documents object properties using
|
232
|
+
Camel case BUT the Ruby API requires snake case.
|
233
|
+
|
234
|
+
For instance, the Rest API documents the properties for a grid coordinate to be
|
235
|
+
"sheetId", "rowIndex", and "columnIndex". However, in the Ruby API, you should
|
236
|
+
construct this object using snake case:
|
237
|
+
|
238
|
+
```Ruby
|
239
|
+
grid_coordinate = { sheet_id: 0, row_index: 0, column_index: 0 }
|
240
|
+
```
|
241
|
+
|
242
|
+
Here is the example constructing requests using hashes:
|
243
|
+
|
244
|
+
```Ruby
|
245
|
+
def values = %w[one two three four]
|
246
|
+
|
247
|
+
def rows
|
248
|
+
values.map do |value|
|
249
|
+
{ values: [{ user_entered_value: { string_value: value } }] }
|
250
|
+
end
|
251
|
+
end
|
252
|
+
|
253
|
+
def write_values_request
|
254
|
+
fields = 'user_entered_value'
|
255
|
+
start = { sheet_id: 0, row_index: 0, column_index: 0 }
|
256
|
+
{ update_cells: { rows:, fields:, start: } }
|
257
|
+
end
|
258
|
+
|
259
|
+
requests = { requests: [write_values_request] }
|
260
|
+
|
261
|
+
spreadsheet_id = '18FAcgotK7nDfLTOTQuGCIjKwxkJMAguhn1OVzpFFgWY'
|
262
|
+
response = SheetsV4.sheets_service.batch_update_spreadsheet(spreadsheet_id, requests)
|
263
|
+
```
|
264
|
+
|
265
|
+
#### Which method should be used?
|
266
|
+
|
267
|
+
Either method will do the same job. I prefer "Method 2: constructing requests using
|
268
|
+
hashes" because my code is more concise and easy to read.
|
269
|
+
|
270
|
+
While either method can produce a malformed request, "Method 2" is more likely to
|
271
|
+
result in malformed requests. Unfortunately, when given a malformed request, the
|
272
|
+
Google Sheets API will do one of following depending on the nature of the problem:
|
273
|
+
|
274
|
+
1. Raise a `Google::Apis::ClientError` with some additional information
|
275
|
+
2. Raise a `Google::Apis::ClientError` with no additional information (this the most
|
276
|
+
common result)
|
277
|
+
3. Not return an error with some of the batch requests not having the expected outcome
|
278
|
+
|
279
|
+
Luckily, this library provides a way to validate that requests are valid and
|
280
|
+
identifies precisely where the request objects do not conform to the API description.
|
281
|
+
That is the subject of the next section [Validating requests](#validating-requests).
|
282
|
+
|
283
|
+
### Validating requests
|
284
|
+
|
285
|
+
The [`SheetsV4.validate_api_object`](https://rubydoc.info/gems/drive_v3/SheetsV4#validate_api_object-class_method)
|
286
|
+
method can be used to validate request objects prior to using them in the Google
|
287
|
+
Sheets API.
|
288
|
+
|
289
|
+
This method takes a `schema_name` and an `object` to validate. Schema names can be
|
290
|
+
listed using [`SheetsV4.api_object_schema_names`](https://rubydoc.info/gems/drive_v3/SheetsV4#api_object_schema_names-class_method).
|
291
|
+
|
292
|
+
This method will either return `true` if `object` conforms to the schema OR it
|
293
|
+
will raise a RuntimeError noting where the object structure did not conform to
|
294
|
+
the schema.
|
295
|
+
|
296
|
+
In the previous examples (see [Building a request](#building-a-request)), the
|
297
|
+
following line can be inserted after the `requests = ...` line to validate the
|
298
|
+
request:
|
299
|
+
|
300
|
+
```Ruby
|
301
|
+
SheetsV4.validate_api_object(schema: 'batch_update_spreadsheet_request', object: requests)
|
302
|
+
```
|
303
|
+
|
304
|
+
### Google Extensions
|
305
|
+
|
306
|
+
The `SheetsV4::GoogleExtensions` module provides extensions to the `Google::Apis::SheetsV4`
|
307
|
+
classes to simplify use of the SheetsV4 API.
|
308
|
+
|
309
|
+
These extensions are not loaded by default and are not required to use other parts
|
310
|
+
of this Gem. To enable these extension, you must:
|
311
|
+
|
312
|
+
```Ruby
|
313
|
+
require 'drive_v3/google_extensions'
|
314
|
+
```
|
315
|
+
|
316
|
+
#### SheetsService Extensions
|
317
|
+
|
318
|
+
Functionality is added to `get_spreadsheet` to set the `sheets_service` attribute on
|
319
|
+
the returned spreadsheet and set the `sheets_service` and `spreadsheet` attributes
|
320
|
+
on the sheets contained in the spreadsheet.
|
321
|
+
|
322
|
+
This can simplify complex spreadsheet updates because you won't have to pass a
|
323
|
+
sheets_service, spreadsheet, and sheet objects separately.
|
324
|
+
|
325
|
+
#### Spreadsheet Extensions
|
326
|
+
|
327
|
+
The `sheets_service` attribute is added and is set by `SheetsService#get_spreadsheet`.
|
328
|
+
|
329
|
+
Convenience methods for getting sheets within the spreadsheet are added:
|
330
|
+
|
331
|
+
* `sheet(id_or_title)`: returns the sheet matching the id or title given
|
332
|
+
* `sheet_id(title)`: returns the ID for the sheet matching the title given
|
333
|
+
* `each_sheet(ids_or_titles)`: enumerates the sheets within a spreadsheet matching
|
334
|
+
the given IDs or titles.
|
335
|
+
|
336
|
+
#### Sheet Extensions
|
337
|
+
|
338
|
+
The `sheets_service` and `spreadsheet` attributes are added. Both are set when the
|
339
|
+
sheet's spreadsheet is loaded by `SheetsService#get_spreadsheet`.
|
340
|
+
|
341
|
+
### Working with dates and times
|
342
|
+
|
343
|
+
Google Sheets, similar to other spreadsheet programs, stores dates and date-time
|
344
|
+
values as numbers. This system makes it easier to perform calculations with
|
345
|
+
dates and times.
|
346
|
+
|
347
|
+
This gem provides two sets of equavalent conversion methods. The first set is defined
|
348
|
+
as class methods on the `SheetsV4` class.
|
349
|
+
|
350
|
+
* `SheetsV4.date_to_gs(date)` returns a numeric cell value
|
351
|
+
* `SheetsV4.gs_to_date(cell_value)` returns a Date object
|
352
|
+
* `SheetsV4.datetime_to_gs(datetime)` returns a numeric cell value
|
353
|
+
* `SheetsV4.gs_to_datetime(cell_value)` returns a DateTime object
|
354
|
+
|
355
|
+
In order to convert to and from spreadsheet values, the spreadsheet timezone must
|
356
|
+
be known. A spreadsheet's timezone is found in the Google Sheets spreadsheet object's
|
357
|
+
properties:
|
358
|
+
|
359
|
+
```Ruby
|
360
|
+
SheetsV4.default_spreadsheet_tz = spreadsheet.properties.time_zone
|
361
|
+
```
|
362
|
+
|
363
|
+
If a time zone is not set using `SheetsV4.default_spreadsheet_tz`, a RuntimeError
|
364
|
+
will be raised when any of the above methods are used.
|
365
|
+
|
366
|
+
Here is an example of how the timezone can change the values fetched from the
|
367
|
+
spreadsheet:
|
368
|
+
|
369
|
+
```Ruby
|
370
|
+
cell_value = 44333.191666666666
|
371
|
+
|
372
|
+
SheetsV4.default_spreadsheet_tz = 'America/New_York'
|
373
|
+
datetime = SheetsV4.gs_to_datetime(cell_value) #=> Mon, 17 May 2021 04:36:00 -0400
|
374
|
+
datetime.utc #=> 2021-05-17 08:36:00 UTC
|
375
|
+
|
376
|
+
SheetsV4.default_spreadsheet_tz = 'America/Los_Angeles'
|
377
|
+
datetime = SheetsV4.gs_to_datetime(cell_value) #=> Mon, 17 May 2021 04:36:00 -0700
|
378
|
+
datetime.utc #=> 2021-05-17 11:36:00 UTC
|
379
|
+
```
|
380
|
+
|
381
|
+
Valid time zone names are those listed in one of these two sources:
|
382
|
+
|
383
|
+
* `ActiveSupport::TimeZone.all.map { |tz| tz.tzinfo.name }`
|
384
|
+
* `ActiveSupport::TimeZone.all.map(&:name)`
|
385
|
+
|
386
|
+
The `SheetsV4` methods works well if the spreadsheet timezone is constant through
|
387
|
+
the run of the program. If this is not the case -- for instance when working with
|
388
|
+
multiple spreadsheets whose timezones may be different -- then use
|
389
|
+
`SheetsV4::ConvertDatesAndTimes`.
|
390
|
+
|
391
|
+
Each instance of `SheetsV4::ConvertDatesAndTimes` has it's own spreadsheet timezone
|
392
|
+
used in the conversions. Instance methods for this class are the same as the
|
393
|
+
date conversion methods on the SheetsV4 class.
|
394
|
+
|
395
|
+
Example:
|
396
|
+
|
397
|
+
```Ruby
|
398
|
+
cell_value = 44333.191666666666
|
399
|
+
converter = SheetsV4::ConvertDatesAndTimes.new('America/Los_Angeles')
|
400
|
+
datetime = SheetsV4.gs_to_datetime(cell_value) #=> Mon, 17 May 2021 04:36:00 -0700
|
401
|
+
datetime.utc #=> 2021-05-17 11:36:00 UTC
|
402
|
+
```
|
403
|
+
|
404
|
+
### Working with colors
|
405
|
+
|
406
|
+
Color objects (with appropriate :red, :green, :blue values) can be retrieved by name
|
407
|
+
using `SheetsV4.color(:black)` or `SheetsV4::Color.black` (these are equivalent).
|
408
|
+
|
409
|
+
Color names can be listed using `SheetsV4.color_names`.
|
410
|
+
|
411
|
+
The color object returned is a Hash as follows:
|
412
|
+
|
413
|
+
```Ruby
|
414
|
+
SheetsV4::Color.black #=> {:red=>0.0, :green=>0.0, :blue=>0.0}
|
415
|
+
```
|
416
|
+
|
417
|
+
## Development
|
418
|
+
|
419
|
+
After checking out the repo, run `bin/setup` to install dependencies and then, run
|
420
|
+
`rake` to run the tests, static analysis, etc. You can also run `bin/console` for an interactive
|
421
|
+
prompt that will allow you to experiment.
|
422
|
+
|
423
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To
|
424
|
+
release a new version, update the version number in `version.rb`, and then run
|
425
|
+
`bundle exec rake release`, which will create a git tag for the version, push git
|
426
|
+
commits and the created tag, and push the `.gem` file to
|
427
|
+
[rubygems.org](https://rubygems.org).
|
428
|
+
|
429
|
+
## Contributing
|
430
|
+
|
431
|
+
Bug reports and pull requests are welcome on [the main-branch/drive_v3 GitHub project](https://github.com/main-branch/drive_v3).
|
432
|
+
|
433
|
+
## License
|
434
|
+
|
435
|
+
The gem is available as open source under the terms of the
|
436
|
+
[MIT License](https://opensource.org/licenses/MIT).
|