googlesheets 0.5.1 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/googlesheets.rb +74 -78
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0aefe530c8bfd81c39a9117a0ae9fd698070e2886961c74e2c80a0ddb21aefeb
4
- data.tar.gz: e623d3db0855c721127f56a463b79b4d2eb68d51e88d4ba8bce49da8c440a459
3
+ metadata.gz: e6a26194973635e4ea51c7926c8cd03f900855b5c1776f517b0bbec78ac2b375
4
+ data.tar.gz: 801e67399e5c075742fe11913e6c9d996eb32f6fca24d7617fd4dfc17c2984e5
5
5
  SHA512:
6
- metadata.gz: 130fed8b81b90818069b02343ebf919f8aaa0ca90a33a0278552287e65e2c410cf583c969498fe6e4cb73fd884eb9e1ec69a43b4b263badba17bd47d0d11a7d3
7
- data.tar.gz: 0cc8e0f830615ca7708800900e0bfbc8307f7032f21740a961bc1047d6fd9aa10f43b5bbdf9e5684984b90b2a3ca5f84df825f61afb7ba02e3a92b1e130d5529
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.5.1"
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 rgb2hex(color=nil)
59
- color or return
60
- r = ((color.red || 0) * 255).to_i
61
- g = ((color.green || 0) * 255).to_i
62
- b = ((color.blue || 0) * 255).to_i
63
- "#%02x%02x%02x" % [r, g, b]
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 biject(x) # a=1, z=26, aa=27, az=52, ba=53, aaa=703
76
- case x
77
- when String
78
- x.each_char.inject(0) {|n,c| (n * 26) + (c.ord & 31) }
79
- when Integer
80
- s = []
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 sheet_rename(pick, name=nil)
166
- shid = sheet_id(pick)
167
- name ||= yield(sheet_name(shid)) if block_given?
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
- reqs = []
170
- reqs.push(update_sheet_properties: {
171
- properties: {
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(pick, color=nil) # NOTE: ignores alpha
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(pick),
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, want=nil)
161
+ def sheet_filter(area, filt=nil)
195
162
  area = resolve_area(area)
196
163
  range = range(area)
197
- criteria = filter_criteria(want) if want
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 sheet_clear(area)
225
- area = resolve_area(area)
226
- api.clear_values(@ssid, area)
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.5.1
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-15 00:00:00.000000000 Z
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