googlesheets 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.ruby-version +1 -0
- data/Gemfile +6 -0
- data/LICENSE +21 -0
- data/README.md +3 -0
- data/googlesheets.gemspec +13 -0
- data/lib/googlesheets.rb +181 -0
- metadata +48 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 8943510443e9c7f239448ca1f51f24aef69623a55943cccd9cca909ed898fc0a
|
4
|
+
data.tar.gz: 1af91f45355e465b6576c722a5fbc9a1cd2371dbd6706d76dd61a019c4e933e7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6d149d61718cf93b2a4e8c373a4cbe30b9f462591b1a603a26a5a455cc14ea03742f65125b9ca61ef5d83213530f958304c69eb501a2f1c00ab17912156b8a8a
|
7
|
+
data.tar.gz: a3d4744b72f90b29bbc7c9fa85c27f81b7f5bb0c4e317f454d48ab856d9cba83af8be5f533ea550f7f2c55f1a43bc8af7ec6f51c016c0e7dc953c0de58b92132
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.6
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2020 Steve Shreeve
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = "googlesheets"
|
5
|
+
s.version = "0.1.0"
|
6
|
+
s.author = "Steve Shreeve"
|
7
|
+
s.email = "steve.shreeve@gmail.com"
|
8
|
+
s.summary = "Ruby gem for Google Sheets"
|
9
|
+
s.description = "This gem allows easy access to Google Sheets API V4."
|
10
|
+
s.homepage = "https://github.com/shreeve/googlesheets"
|
11
|
+
s.license = "MIT"
|
12
|
+
s.files = `git ls-files`.split("\n") - %w[.gitignore]
|
13
|
+
end
|
data/lib/googlesheets.rb
ADDED
@@ -0,0 +1,181 @@
|
|
1
|
+
require "google/apis/sheets_v4"
|
2
|
+
require "googleauth"
|
3
|
+
require "googleauth/stores/file_token_store"
|
4
|
+
|
5
|
+
module Enumerable
|
6
|
+
def first_result
|
7
|
+
block_given? ? each {|item| result = (yield item) and return result} && nil : find {|item| item}
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class GoogleSheets
|
12
|
+
attr_accessor :api
|
13
|
+
|
14
|
+
def initialize(ssid, **opts)
|
15
|
+
@ssid = ssid
|
16
|
+
|
17
|
+
@json = opts[:credentials] || 'credentials.json'
|
18
|
+
@yaml = opts[:token ] || 'token.yaml'
|
19
|
+
|
20
|
+
if opts[:debug] == true
|
21
|
+
$stdout.sync = true
|
22
|
+
Google::Apis.logger = Logger.new(STDERR)
|
23
|
+
Google::Apis.logger.level = Logger::DEBUG
|
24
|
+
end
|
25
|
+
|
26
|
+
@api = Google::Apis::SheetsV4::SheetsService.new
|
27
|
+
@api.client_options.application_name = opts[:app] || "Ruby"
|
28
|
+
@api.client_options.application_version = opts[:ver] || "1.0.0"
|
29
|
+
@api.authorization = authorize
|
30
|
+
end
|
31
|
+
|
32
|
+
def authorize
|
33
|
+
idno = Google::Auth::ClientId.from_file(@json)
|
34
|
+
repo = Google::Auth::Stores::FileTokenStore.new(file: @yaml)
|
35
|
+
auth = Google::Auth::UserAuthorizer.new(idno, Google::Apis::SheetsV4::AUTH_SPREADSHEETS, repo)
|
36
|
+
oobs = 'urn:ietf:wg:oauth:2.0:oob'
|
37
|
+
user = 'default'
|
38
|
+
info = auth.get_credentials(user) || begin
|
39
|
+
href = auth.get_authorization_url(base_url: oobs)
|
40
|
+
puts "Open the following URL and paste the code here:\n" + href
|
41
|
+
info = auth.get_and_store_credentials_from_code(user_id: user, code: gets, base_url: oobs)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def rgb2hex(color=nil)
|
46
|
+
color or return
|
47
|
+
r = ((color.red || 0) * 255).to_i
|
48
|
+
g = ((color.green || 0) * 255).to_i
|
49
|
+
b = ((color.blue || 0) * 255).to_i
|
50
|
+
"#%02x%02x%02x" % [r, g, b]
|
51
|
+
end
|
52
|
+
|
53
|
+
def hex2rgb(color=nil)
|
54
|
+
color =~ /\A#?(?:(\h\h)(\h\h)(\h\h)|(\h)(\h)(\h))\z/ or return
|
55
|
+
r, g, b = $1 ? [$1, $2, $3] : [$4*2, $5*2, $6*2]
|
56
|
+
r = "%.2f" % (r.hex / 255.0)
|
57
|
+
g = "%.2f" % (g.hex / 255.0)
|
58
|
+
b = "%.2f" % (b.hex / 255.0)
|
59
|
+
{ red: r, green: g, blue: b }
|
60
|
+
end
|
61
|
+
|
62
|
+
def biject(x) # a=1, z=26, aa=27, az=52, ba=53, aaa=703
|
63
|
+
case x
|
64
|
+
when String
|
65
|
+
x.each_char.inject(0) {|n,c| (n * 26) + (c.ord & 31) }
|
66
|
+
when Integer
|
67
|
+
s = []
|
68
|
+
s << (((x -= 1) % 26) + 65).chr && x /= 26 while x > 0
|
69
|
+
s.reverse.join
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def range(area)
|
74
|
+
sh, rc = area.split('!', 2); rc, sh = sh, nil if sh.nil?
|
75
|
+
as, ae = rc.split(':', 2); ae ||= as
|
76
|
+
cs, rs = as.split(/(?=\d)/, 2); cs = biject(cs) - 1; rs = rs.to_i - 1
|
77
|
+
ce, re = ae.split(/(?=\d)/, 2); ce = biject(ce) - 1; re = re.to_i - 1
|
78
|
+
{
|
79
|
+
sheet_id: sh ? sheet_id(sh) : nil,
|
80
|
+
start_column_index: cs,
|
81
|
+
start_row_index: rs,
|
82
|
+
end_column_index: ce + 1,
|
83
|
+
end_row_index: re + 1,
|
84
|
+
}.compact
|
85
|
+
end
|
86
|
+
|
87
|
+
def sheets
|
88
|
+
@sheets ||= api.get_spreadsheet(@ssid).sheets.map {|item| item.properties }
|
89
|
+
end
|
90
|
+
|
91
|
+
def sheets!
|
92
|
+
@sheets = nil
|
93
|
+
sheets
|
94
|
+
end
|
95
|
+
|
96
|
+
def sheet_list
|
97
|
+
sheets.map do |item|
|
98
|
+
{
|
99
|
+
id: item.sheet_id,
|
100
|
+
name: item.title,
|
101
|
+
color: rgb2hex(item.tab_color),
|
102
|
+
}.compact
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def sheet_id(obj)
|
107
|
+
case obj
|
108
|
+
when /^#(\d+)$/ then sheets[$1.to_i - 1].sheet_id
|
109
|
+
when Integer then obj
|
110
|
+
else sheets.first_result {|item| item.sheet_id if item.title == obj}
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
def sheet_name(obj)
|
115
|
+
case obj
|
116
|
+
when /^#(\d+)$/ then sheets[$1.to_i - 1].title
|
117
|
+
when Integer then sheets.first_result {|item| item.title if item.sheet_id == obj}
|
118
|
+
else obj
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
def sheet_color(pick, color=nil) # NOTE: ignores alpha
|
123
|
+
reqs = []
|
124
|
+
reqs.push(update_sheet_properties: {
|
125
|
+
properties: {
|
126
|
+
sheet_id: sheet_id(pick),
|
127
|
+
tab_color: hex2rgb(color),
|
128
|
+
},
|
129
|
+
fields: 'tab_color',
|
130
|
+
})
|
131
|
+
resp = api.batch_update_spreadsheet(@ssid, { requests: reqs }, {})
|
132
|
+
true
|
133
|
+
end
|
134
|
+
|
135
|
+
def sheet_filter(area)
|
136
|
+
range = range(area)
|
137
|
+
reqs = []
|
138
|
+
reqs.push(clear_basic_filter: { sheet_id: range[:sheet_id] })
|
139
|
+
reqs.push(set_basic_filter: { filter: { range: range } })
|
140
|
+
resp = api.batch_update_spreadsheet(@ssid, { requests: reqs }, {})
|
141
|
+
true
|
142
|
+
end
|
143
|
+
|
144
|
+
def sheet_format(area, pattern)
|
145
|
+
reqs = []
|
146
|
+
reqs.push(repeat_cell: {
|
147
|
+
range: range(area),
|
148
|
+
cell: {
|
149
|
+
user_entered_format: {
|
150
|
+
number_format: {
|
151
|
+
type: "NUMBER",
|
152
|
+
pattern: pattern,
|
153
|
+
},
|
154
|
+
},
|
155
|
+
},
|
156
|
+
fields: 'user_entered_format.number_format',
|
157
|
+
})
|
158
|
+
resp = api.batch_update_spreadsheet(@ssid, { requests: reqs }, {})
|
159
|
+
true
|
160
|
+
end
|
161
|
+
|
162
|
+
def sheet_clear(area)
|
163
|
+
api.clear_values(@ssid, area)
|
164
|
+
end
|
165
|
+
|
166
|
+
def sheet_read(area)
|
167
|
+
api.get_spreadsheet_values(@ssid, area).values
|
168
|
+
end
|
169
|
+
|
170
|
+
def sheet_save(area, rows, log=false)
|
171
|
+
area.sub!(/^(#\d+)(?=!)/) {|num| sheet_name(num)}
|
172
|
+
gasv = Google::Apis::SheetsV4::ValueRange.new(range: area, values: rows)
|
173
|
+
done = api.update_spreadsheet_value(@ssid, area, gasv, value_input_option: "USER_ENTERED")
|
174
|
+
puts "#{done.updated_cells} cells updated." if log
|
175
|
+
done.updated_cells
|
176
|
+
end
|
177
|
+
|
178
|
+
def sheet_save!(area, rows)
|
179
|
+
sheet_save(area, rows, true)
|
180
|
+
end
|
181
|
+
end
|
metadata
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: googlesheets
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Steve Shreeve
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-03-10 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: This gem allows easy access to Google Sheets API V4.
|
14
|
+
email: steve.shreeve@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- ".ruby-version"
|
20
|
+
- Gemfile
|
21
|
+
- LICENSE
|
22
|
+
- README.md
|
23
|
+
- googlesheets.gemspec
|
24
|
+
- lib/googlesheets.rb
|
25
|
+
homepage: https://github.com/shreeve/googlesheets
|
26
|
+
licenses:
|
27
|
+
- MIT
|
28
|
+
metadata: {}
|
29
|
+
post_install_message:
|
30
|
+
rdoc_options: []
|
31
|
+
require_paths:
|
32
|
+
- lib
|
33
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
requirements: []
|
44
|
+
rubygems_version: 3.1.2
|
45
|
+
signing_key:
|
46
|
+
specification_version: 4
|
47
|
+
summary: Ruby gem for Google Sheets
|
48
|
+
test_files: []
|