google_sheets 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +89 -0
- data/lib/google_sheets/session.rb +76 -0
- data/lib/google_sheets/sheet.rb +39 -0
- data/lib/google_sheets/spreadsheet.rb +61 -0
- data/lib/google_sheets.rb +4 -0
- metadata +82 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 598703000a2529218f78b0bb7f0c049aec30d79ce9f3e309ebeab51a5c6d3451
|
4
|
+
data.tar.gz: 0fed844fbb50b5cb4275f647d4b8704ddec1ba90e7a69194498b0180ad98da22
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c439fe3506db60ae0cfd37f26822a7525ed120b55a237be5eff4aea121d9d2f083d7bbfe897444a92ad5735d0550bdde3096c9a5d36754eb42d311749f922bc2
|
7
|
+
data.tar.gz: 69a0e9ef52693cf4dfd6011a1404288f16db25d285a60a1ad53c57b6c3cfe3f51f5b4542bc4f343387006c199dfa219e8ebd3c324efea295c20e2e08143330ab
|
data/README.md
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
[google-drive-ruby](https://github.com/gimite/google-drive-ruby), a great gem, doesn't support Google's v4 Drive API. As a result, I seem to encounter rate limiting errors fairly quickly.
|
2
|
+
|
3
|
+
Since I only ever used that gem for creating/reading spreadsheets, I created this simple gem for just that, but using the v4 API.
|
4
|
+
|
5
|
+
* [Installing](#installing)
|
6
|
+
* [Getting started](#getting-started)
|
7
|
+
* [GitHub](http://github.com/shmay/google_sheets)
|
8
|
+
|
9
|
+
## <a name="installing">Installing</a>
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile & `bundle install`:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'google_sheets'
|
15
|
+
```
|
16
|
+
|
17
|
+
Or install it yourself:
|
18
|
+
|
19
|
+
```
|
20
|
+
$ gem install google_drive
|
21
|
+
```
|
22
|
+
|
23
|
+
### Authorization
|
24
|
+
|
25
|
+
The authorization process is taken from Google's [own tutorial](https://developers.google.com/sheets/api/quickstart/ruby#step_3_set_up_the_sample).
|
26
|
+
|
27
|
+
You'll need to create a project and enable the GSheets API, as detailed [in step 1 of that tutorial](https://developers.google.com/sheets/api/quickstart/ruby#step_1_turn_on_the_api_name).
|
28
|
+
|
29
|
+
You'll download a `client_secret.json` that will contain a `client_id` and `client_secret`
|
30
|
+
|
31
|
+
I recommend using Rails 5.2's encrypted credentials to store the id & secret. So the final will result will look something like:
|
32
|
+
|
33
|
+
``` ruby
|
34
|
+
client_id = Rails.application.credentials[:client_id]
|
35
|
+
client_secret = Rails.application.credentials[:client_secret]
|
36
|
+
|
37
|
+
session = GoogleSheets::Session.start_session(
|
38
|
+
client_id: client_id,
|
39
|
+
client_secret: client_secret
|
40
|
+
)
|
41
|
+
```
|
42
|
+
|
43
|
+
Or store them in an environment variable, EG: `ENV['client_id']`
|
44
|
+
|
45
|
+
This will prompt you to authorize the app in the browser. Once completed, you'll notice a `token.yaml` in your cwd. If you'd like the file to be placed elsewhere, there's a `token_path` parameter that you can pass into `start_session`, EG:
|
46
|
+
|
47
|
+
``` ruby
|
48
|
+
session = GoogleSheets::Session.start_session(
|
49
|
+
client_id: client_id,
|
50
|
+
client_secret: client_secret,
|
51
|
+
token_path: './tmp'
|
52
|
+
)
|
53
|
+
```
|
54
|
+
|
55
|
+
### Getting Started
|
56
|
+
|
57
|
+
Once you're authorized, you can read, create, and delete sheets within a spreadsheet.
|
58
|
+
|
59
|
+
``` ruby
|
60
|
+
session = GoogleSheets::Session.start_session(
|
61
|
+
client_id: ENV['test_client_id'],
|
62
|
+
client_secret: ENV['test_client_secret']
|
63
|
+
)
|
64
|
+
|
65
|
+
spreadsheet = session.spreadsheet_from_key '[your spreadsheet key]'
|
66
|
+
|
67
|
+
spreadsheet.sheets.map &:title
|
68
|
+
# => ['Sheet1', 'yoyo1']
|
69
|
+
|
70
|
+
sheet1 = spreadsheet.sheets[0]
|
71
|
+
|
72
|
+
sheet1.values
|
73
|
+
# => [['one','two'], ['three', 'four']
|
74
|
+
|
75
|
+
values = [[1,2],[3,4]]
|
76
|
+
|
77
|
+
sheet2 = spreadsheet.add_sheet('what', values: values)
|
78
|
+
|
79
|
+
spreadsheet.sheets.map &:title
|
80
|
+
# => ['Sheet1', 'yoyo1', 'what']
|
81
|
+
|
82
|
+
# this will delete the sheet!!!
|
83
|
+
sheet.delete!
|
84
|
+
|
85
|
+
spreadsheet.sheets.map &:title
|
86
|
+
# => ['Sheet1', 'yoyo1']
|
87
|
+
```
|
88
|
+
|
89
|
+
Or just look at [the spec](spec/test_all_the_things_spec.rb) to see it in action.
|
@@ -0,0 +1,76 @@
|
|
1
|
+
# frozen_string_literal
|
2
|
+
|
3
|
+
require 'google/apis/sheets_v4'
|
4
|
+
require 'googleauth'
|
5
|
+
require 'googleauth/stores/file_token_store'
|
6
|
+
require 'fileutils'
|
7
|
+
|
8
|
+
require 'google_sheets/sheet'
|
9
|
+
require 'google_sheets/spreadsheet'
|
10
|
+
|
11
|
+
module GoogleSheets
|
12
|
+
class Session
|
13
|
+
OOB_URI = 'urn:ietf:wg:oauth:2.0:oob'.freeze
|
14
|
+
# APPLICATION_NAME = 'Google Sheets API Ruby Quickstart'.freeze
|
15
|
+
# CLIENT_SECRETS_FILENAME = 'client_secret.json'.freeze
|
16
|
+
CREDENTIALS_FILENAME = 'token.yaml'.freeze
|
17
|
+
SCOPE = Google::Apis::SheetsV4::AUTH_SPREADSHEETS
|
18
|
+
|
19
|
+
def initialize service
|
20
|
+
@service = service
|
21
|
+
end
|
22
|
+
|
23
|
+
def spreadsheet_from_key key
|
24
|
+
Spreadsheet.new @service, key
|
25
|
+
end
|
26
|
+
|
27
|
+
### AUTH CODE TAKEN FROM: https://developers.google.com/sheets/api/quickstart/ruby#step_3_set_up_the_sample
|
28
|
+
#
|
29
|
+
##
|
30
|
+
# Ensure valid credentials, either by restoring from the saved credentials
|
31
|
+
# files or intitiating an OAuth2 authorization. If authorization is required,
|
32
|
+
# the user's default browser will be launched to approve the request.
|
33
|
+
#
|
34
|
+
# @return [Google::Auth::UserRefreshCredentials] OAuth2 credentials
|
35
|
+
def self.authorize client_id, client_secret, token_path
|
36
|
+
credentials_hash = {
|
37
|
+
"installed" =>
|
38
|
+
{
|
39
|
+
"client_id" => client_id,
|
40
|
+
"client_secret" => client_secret
|
41
|
+
}
|
42
|
+
}
|
43
|
+
|
44
|
+
client_id = Google::Auth::ClientId.from_hash(credentials_hash)
|
45
|
+
|
46
|
+
# client_id = Google::Auth::ClientId.from_file(CLIENT_SECRETS_PATH)
|
47
|
+
token_store = Google::Auth::Stores::FileTokenStore.new(file: token_path + '/' + CREDENTIALS_FILENAME)
|
48
|
+
authorizer = Google::Auth::UserAuthorizer.new(client_id, SCOPE, token_store)
|
49
|
+
user_id = 'default'
|
50
|
+
credentials = authorizer.get_credentials(user_id)
|
51
|
+
|
52
|
+
if credentials.nil?
|
53
|
+
url = authorizer.get_authorization_url(base_url: OOB_URI)
|
54
|
+
puts "Open the following URL in the browser and enter the resulting code after authorization:\n #{url} \n"
|
55
|
+
|
56
|
+
code = STDIN.gets
|
57
|
+
|
58
|
+
credentials = authorizer.get_and_store_credentials_from_code(
|
59
|
+
user_id: user_id, code: code, base_url: OOB_URI
|
60
|
+
)
|
61
|
+
end
|
62
|
+
|
63
|
+
credentials
|
64
|
+
end
|
65
|
+
|
66
|
+
def self.start_session client_id:, client_secret:, token_path: '.'
|
67
|
+
# Initialize the API
|
68
|
+
service = Google::Apis::SheetsV4::SheetsService.new
|
69
|
+
# service.client_options.application_name = APPLICATION_NAME
|
70
|
+
service.authorization = authorize(client_id, client_secret, token_path)
|
71
|
+
|
72
|
+
Session.new(service)
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module GoogleSheets
|
2
|
+
class Sheet
|
3
|
+
attr_reader :properties, :title
|
4
|
+
attr_writer :values
|
5
|
+
|
6
|
+
def initialize service, sheet, spreadsheet
|
7
|
+
@service = service
|
8
|
+
@spreadsheet = spreadsheet
|
9
|
+
@sheet = sheet
|
10
|
+
@properties = sheet.properties.to_h
|
11
|
+
@title = @properties[:title]
|
12
|
+
end
|
13
|
+
|
14
|
+
def id
|
15
|
+
@properties[:sheet_id]
|
16
|
+
end
|
17
|
+
|
18
|
+
def values
|
19
|
+
@values ||= @service.get_spreadsheet_values(@spreadsheet.key, @title).values
|
20
|
+
end
|
21
|
+
|
22
|
+
def delete!
|
23
|
+
delete_sheet_request = Google::Apis::SheetsV4::DeleteSheetRequest.new
|
24
|
+
delete_sheet_request.sheet_id = self.id
|
25
|
+
|
26
|
+
batch_update_spreadsheet_request = Google::Apis::SheetsV4::BatchUpdateSpreadsheetRequest.new
|
27
|
+
batch_update_spreadsheet_request.requests = Google::Apis::SheetsV4::Request.new
|
28
|
+
|
29
|
+
batch_update_spreadsheet_request_object = [ delete_sheet: delete_sheet_request ]
|
30
|
+
batch_update_spreadsheet_request.requests = batch_update_spreadsheet_request_object
|
31
|
+
|
32
|
+
response = @service.batch_update_spreadsheet(@spreadsheet.key, batch_update_spreadsheet_request)
|
33
|
+
|
34
|
+
@spreadsheet.sheets.delete(self)
|
35
|
+
|
36
|
+
self
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
module GoogleSheets
|
2
|
+
class Spreadsheet
|
3
|
+
attr_reader :key
|
4
|
+
|
5
|
+
def initialize service, key
|
6
|
+
@key = key
|
7
|
+
@service = service
|
8
|
+
load_spreadsheet
|
9
|
+
end
|
10
|
+
|
11
|
+
def load_spreadsheet
|
12
|
+
@spreadsheet = @service.get_spreadsheet(@key)
|
13
|
+
@properties = @spreadsheet.properties.to_h
|
14
|
+
end
|
15
|
+
|
16
|
+
alias refresh! load_spreadsheet
|
17
|
+
|
18
|
+
def sheets
|
19
|
+
@sheets ||= @spreadsheet.sheets.map do |sheet|
|
20
|
+
Sheet.new(@service, sheet, self)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# HT this SO answer: https://stackoverflow.com/a/49886382/548170
|
25
|
+
def add_sheet title, values: []
|
26
|
+
add_sheet_request = Google::Apis::SheetsV4::AddSheetRequest.new
|
27
|
+
add_sheet_request.properties = Google::Apis::SheetsV4::SheetProperties.new
|
28
|
+
add_sheet_request.properties.title = title
|
29
|
+
|
30
|
+
batch_update_spreadsheet_request = Google::Apis::SheetsV4::BatchUpdateSpreadsheetRequest.new
|
31
|
+
batch_update_spreadsheet_request.requests = Google::Apis::SheetsV4::Request.new
|
32
|
+
|
33
|
+
batch_update_spreadsheet_request_object = [ add_sheet: add_sheet_request ]
|
34
|
+
batch_update_spreadsheet_request.requests = batch_update_spreadsheet_request_object
|
35
|
+
|
36
|
+
response = @service.batch_update_spreadsheet(@key, batch_update_spreadsheet_request)
|
37
|
+
|
38
|
+
resp = append_to_sheet(title, values) if values&.any?
|
39
|
+
|
40
|
+
add_sheet = response.replies[0].add_sheet
|
41
|
+
|
42
|
+
sheet = Sheet.new(@service, add_sheet, self)
|
43
|
+
|
44
|
+
sheet.values = values if values&.any?
|
45
|
+
|
46
|
+
self.sheets << sheet
|
47
|
+
|
48
|
+
sheet
|
49
|
+
end
|
50
|
+
|
51
|
+
def append_to_sheet title, values
|
52
|
+
# The A1 notation of a range to search for a logical table of data.
|
53
|
+
# Values will be appended after the last row of the table.
|
54
|
+
range = title
|
55
|
+
|
56
|
+
request_body = Google::Apis::SheetsV4::ValueRange.new(values: values)
|
57
|
+
|
58
|
+
response = @service.append_spreadsheet_value(@key, range, request_body, value_input_option: 'RAW')
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: google_sheets
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kyle Murphy
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-07-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: google-api-client
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.11.0
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 0.22.0
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 0.11.0
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 0.22.0
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: rspec
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
description: CRUD Google Sheets in Ruby
|
48
|
+
email: kmurph73@gmail.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- README.md
|
54
|
+
- lib/google_sheets.rb
|
55
|
+
- lib/google_sheets/session.rb
|
56
|
+
- lib/google_sheets/sheet.rb
|
57
|
+
- lib/google_sheets/spreadsheet.rb
|
58
|
+
homepage: http://rubygems.org/gems/google_sheets
|
59
|
+
licenses:
|
60
|
+
- MIT
|
61
|
+
metadata: {}
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options: []
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
requirements: []
|
77
|
+
rubyforge_project:
|
78
|
+
rubygems_version: 2.7.6
|
79
|
+
signing_key:
|
80
|
+
specification_version: 4
|
81
|
+
summary: Basic Google Sheets interaction, using the v4 api.
|
82
|
+
test_files: []
|