googlesheets 0.5.1 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/googlesheets.rb +74 -78
- 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: e6a26194973635e4ea51c7926c8cd03f900855b5c1776f517b0bbec78ac2b375
|
4
|
+
data.tar.gz: 801e67399e5c075742fe11913e6c9d996eb32f6fca24d7617fd4dfc17c2984e5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fd5432ce50b9d6528b444db90beda6d4b561b3b430fc8cac3a07dd3b4b3d8417f1fbdd84ff58ec18b19d8b3c520644febace35b3610b32e6c9a661abe615ec53
|
7
|
+
data.tar.gz: c2019272c6b2a079a33d1d6a831f2692ae6da0a195a46728a04524df742053e2691b3587ce01f3c01ef334fb44fe4314814950525c3f2bcdd3697f4b1dade097
|
data/lib/googlesheets.rb
CHANGED
@@ -1,24 +1,18 @@
|
|
1
|
+
# ============================================================================
|
2
|
+
# googlesheets - Ruby gem for Google Sheets
|
3
|
+
#
|
4
|
+
# Author: Steve Shreeve (steve.shreeve@gmail.com)
|
5
|
+
# Date: March 16, 2023
|
6
|
+
# ============================================================================
|
7
|
+
|
8
|
+
# See https://googleapis.dev/ruby/google-api-client/latest/Google/Apis/SheetsV4/Request.html
|
9
|
+
|
1
10
|
require "google/apis/sheets_v4"
|
2
11
|
require "googleauth"
|
3
12
|
require "googleauth/stores/file_token_store"
|
4
13
|
|
5
|
-
class Object
|
6
|
-
def blank?
|
7
|
-
respond_to?(:empty?) or return !self
|
8
|
-
empty? or respond_to?(:strip) && strip.empty?
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
module Enumerable
|
13
|
-
def first_result
|
14
|
-
block_given? ? each {|item| result = (yield item) and return result} && nil : find {|item| item}
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
# https://googleapis.dev/ruby/google-api-client/latest/Google/Apis/SheetsV4/Request.html
|
19
|
-
|
20
14
|
class GoogleSheets
|
21
|
-
VERSION = "0.
|
15
|
+
VERSION = "0.6.1"
|
22
16
|
|
23
17
|
attr_accessor :api
|
24
18
|
|
@@ -55,12 +49,15 @@ class GoogleSheets
|
|
55
49
|
end
|
56
50
|
end
|
57
51
|
|
58
|
-
def
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
52
|
+
def biject(x) # a=1, z=26, aa=27, az=52, ba=53, aaa=703
|
53
|
+
case x
|
54
|
+
when String
|
55
|
+
x.each_char.inject(0) {|n,c| (n * 26) + (c.ord & 31) }
|
56
|
+
when Integer
|
57
|
+
s = []
|
58
|
+
s << (((x -= 1) % 26) + 65).chr && x /= 26 while x > 0
|
59
|
+
s.reverse.join
|
60
|
+
end
|
64
61
|
end
|
65
62
|
|
66
63
|
def hex2rgb(color=nil)
|
@@ -72,15 +69,12 @@ class GoogleSheets
|
|
72
69
|
{ red: r, green: g, blue: b }
|
73
70
|
end
|
74
71
|
|
75
|
-
def
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
s << (((x -= 1) % 26) + 65).chr && x /= 26 while x > 0
|
82
|
-
s.reverse.join
|
83
|
-
end
|
72
|
+
def rgb2hex(color=nil)
|
73
|
+
color or return
|
74
|
+
r = ((color.red || 0) * 255).to_i
|
75
|
+
g = ((color.green || 0) * 255).to_i
|
76
|
+
b = ((color.blue || 0) * 255).to_i
|
77
|
+
"#%02x%02x%02x" % [r, g, b]
|
84
78
|
end
|
85
79
|
|
86
80
|
def filter_criteria(hash)
|
@@ -110,6 +104,8 @@ class GoogleSheets
|
|
110
104
|
}.compact
|
111
105
|
end
|
112
106
|
|
107
|
+
# --------------------------------------------------------------------------
|
108
|
+
|
113
109
|
def sheets
|
114
110
|
@sheets ||= api.get_spreadsheet(@ssid).sheets.map {|item| item.properties }
|
115
111
|
end
|
@@ -119,33 +115,6 @@ class GoogleSheets
|
|
119
115
|
sheets
|
120
116
|
end
|
121
117
|
|
122
|
-
def sheet_list
|
123
|
-
sheets.map do |item|
|
124
|
-
{
|
125
|
-
id: item.sheet_id,
|
126
|
-
name: item.title,
|
127
|
-
color: rgb2hex(item.tab_color),
|
128
|
-
}.compact
|
129
|
-
end
|
130
|
-
end
|
131
|
-
|
132
|
-
def sheet_id(obj)
|
133
|
-
case obj
|
134
|
-
when /^#(\d+)$/ then sheets[$1.to_i - 1].sheet_id
|
135
|
-
when Integer then obj
|
136
|
-
else sheets.first_result {|item| item.sheet_id if item.title == obj}
|
137
|
-
end
|
138
|
-
end
|
139
|
-
|
140
|
-
def sheet_name(obj=nil)
|
141
|
-
case obj ||= @wsid
|
142
|
-
when /^#(\d+)$/ then sheets[$1.to_i - 1].title
|
143
|
-
when "", 0, nil then sheets.first.title
|
144
|
-
when Integer then sheets.first_result {|item| item.title if item.sheet_id == obj }
|
145
|
-
else obj
|
146
|
-
end
|
147
|
-
end
|
148
|
-
|
149
118
|
def resolve_area(area)
|
150
119
|
if area.blank?
|
151
120
|
wsid = sheet_name
|
@@ -162,27 +131,25 @@ class GoogleSheets
|
|
162
131
|
"#{wsid}!#{rect}"
|
163
132
|
end
|
164
133
|
|
165
|
-
def
|
166
|
-
|
167
|
-
|
134
|
+
def sheet_name(obj=nil)
|
135
|
+
case obj ||= @wsid
|
136
|
+
when /^#(\d+)(?:!?|$)/ then sheets[$1.to_i - 1].title
|
137
|
+
when "", 0, nil then sheets.first.title
|
138
|
+
when Integer then sheets.find {|item| item.sheet_id == obj}&.title
|
139
|
+
else obj
|
140
|
+
end
|
141
|
+
end
|
168
142
|
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
sheet_id: shid,
|
173
|
-
title: name,
|
174
|
-
},
|
175
|
-
fields: "title",
|
176
|
-
})
|
177
|
-
resp = api.batch_update_spreadsheet(@ssid, { requests: reqs }, {})
|
178
|
-
true
|
143
|
+
def sheet_clear(area)
|
144
|
+
area = resolve_area(area)
|
145
|
+
api.clear_values(@ssid, area)
|
179
146
|
end
|
180
147
|
|
181
|
-
def sheet_color(
|
148
|
+
def sheet_color(curr, color=nil) # NOTE: ignores alpha
|
182
149
|
reqs = []
|
183
150
|
reqs.push(update_sheet_properties: {
|
184
151
|
properties: {
|
185
|
-
sheet_id: sheet_id(
|
152
|
+
sheet_id: sheet_id(curr),
|
186
153
|
tab_color: hex2rgb(color),
|
187
154
|
},
|
188
155
|
fields: "tab_color",
|
@@ -191,10 +158,10 @@ class GoogleSheets
|
|
191
158
|
true
|
192
159
|
end
|
193
160
|
|
194
|
-
def sheet_filter(area,
|
161
|
+
def sheet_filter(area, filt=nil)
|
195
162
|
area = resolve_area(area)
|
196
163
|
range = range(area)
|
197
|
-
criteria = filter_criteria(
|
164
|
+
criteria = filter_criteria(filt) if filt
|
198
165
|
reqs = []
|
199
166
|
reqs.push(clear_basic_filter: { sheet_id: range[:sheet_id] })
|
200
167
|
reqs.push(set_basic_filter: { filter: { range: range, criteria: criteria}.compact })
|
@@ -221,9 +188,38 @@ class GoogleSheets
|
|
221
188
|
true
|
222
189
|
end
|
223
190
|
|
224
|
-
def
|
225
|
-
|
226
|
-
|
191
|
+
def sheet_id(obj)
|
192
|
+
case obj
|
193
|
+
when /^#(\d+)(?:!?|$)/ then sheets[$1.to_i - 1].sheet_id
|
194
|
+
when Integer then obj
|
195
|
+
else sheets.find {|item| item.title == obj}&.sheet_id
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
199
|
+
def sheet_list
|
200
|
+
sheets.map do |item|
|
201
|
+
{
|
202
|
+
id: item.sheet_id,
|
203
|
+
name: item.title,
|
204
|
+
color: rgb2hex(item.tab_color),
|
205
|
+
}.compact
|
206
|
+
end
|
207
|
+
end
|
208
|
+
|
209
|
+
def sheet_rename(curr, name=nil)
|
210
|
+
shid = sheet_id(curr)
|
211
|
+
name ||= yield(sheet_name(shid)) if block_given?
|
212
|
+
|
213
|
+
reqs = []
|
214
|
+
reqs.push(update_sheet_properties: {
|
215
|
+
properties: {
|
216
|
+
sheet_id: shid,
|
217
|
+
title: name,
|
218
|
+
},
|
219
|
+
fields: "title",
|
220
|
+
})
|
221
|
+
resp = api.batch_update_spreadsheet(@ssid, { requests: reqs }, {})
|
222
|
+
true
|
227
223
|
end
|
228
224
|
|
229
225
|
def sheet_read(area=nil)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: googlesheets
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Steve Shreeve
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-03-
|
11
|
+
date: 2023-03-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: google-api-client
|