googlesheets 0.5.0 → 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 +78 -80
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 484ead7c7ddf83bd062e24d8edb28fb0da072cbbe348f598974c7dcc6fc81687
4
- data.tar.gz: 224d0e6b86d115a4b9aff60faba79a174686f65f551dff9322bb8ff22d0445ce
3
+ metadata.gz: e6a26194973635e4ea51c7926c8cd03f900855b5c1776f517b0bbec78ac2b375
4
+ data.tar.gz: 801e67399e5c075742fe11913e6c9d996eb32f6fca24d7617fd4dfc17c2984e5
5
5
  SHA512:
6
- metadata.gz: 38d00ab0cd8ac505238f604542e0f56f814d397cdf8d606c0a64cba8bf022afcd58173cf5af5e23cbf38590b18c49bc849001c231c23b509aee8b384a62b4580
7
- data.tar.gz: a34dc14942dd9433331221804a95f940104a58b08c2830e1a6866c046db9b35c9ec220c91fd9c26aedc8b98b54a96274d007f045f0434c3db10fcf804543f670
6
+ metadata.gz: fd5432ce50b9d6528b444db90beda6d4b561b3b430fc8cac3a07dd3b4b3d8417f1fbdd84ff58ec18b19d8b3c520644febace35b3610b32e6c9a661abe615ec53
7
+ data.tar.gz: c2019272c6b2a079a33d1d6a831f2692ae6da0a195a46728a04524df742053e2691b3587ce01f3c01ef334fb44fe4314814950525c3f2bcdd3697f4b1dade097
data/lib/googlesheets.rb CHANGED
@@ -1,30 +1,25 @@
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.0"
15
+ VERSION = "0.6.1"
22
16
 
23
17
  attr_accessor :api
24
18
 
25
19
  def initialize(ssid, **opts)
26
20
  @ssid = ssid =~ /^https?:/ ? ssid.split("/")[5] : ssid # spreadsheet id
27
- @wsid = ssid[/(?<=#gid=)(\d+)$/] && $1.to_i # default worksheet id
21
+ @wsid = ssid =~ /(?<=#gid=)(\d+)(?:!([a-z\d:]+))?$/i ? $1.to_i : "#1" # worksheet id
22
+ @rect = $2 ? $2.sub(/(?<=\D):/, "1:") : "A:ZZ"
28
23
 
29
24
  @json = opts[:credentials] || "credentials.json"
30
25
  @yaml = opts[:token ] || "token.yaml"
@@ -54,12 +49,15 @@ class GoogleSheets
54
49
  end
55
50
  end
56
51
 
57
- def rgb2hex(color=nil)
58
- color or return
59
- r = ((color.red || 0) * 255).to_i
60
- g = ((color.green || 0) * 255).to_i
61
- b = ((color.blue || 0) * 255).to_i
62
- "#%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
63
61
  end
64
62
 
65
63
  def hex2rgb(color=nil)
@@ -71,15 +69,12 @@ class GoogleSheets
71
69
  { red: r, green: g, blue: b }
72
70
  end
73
71
 
74
- def biject(x) # a=1, z=26, aa=27, az=52, ba=53, aaa=703
75
- case x
76
- when String
77
- x.each_char.inject(0) {|n,c| (n * 26) + (c.ord & 31) }
78
- when Integer
79
- s = []
80
- s << (((x -= 1) % 26) + 65).chr && x /= 26 while x > 0
81
- s.reverse.join
82
- 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]
83
78
  end
84
79
 
85
80
  def filter_criteria(hash)
@@ -109,6 +104,8 @@ class GoogleSheets
109
104
  }.compact
110
105
  end
111
106
 
107
+ # --------------------------------------------------------------------------
108
+
112
109
  def sheets
113
110
  @sheets ||= api.get_spreadsheet(@ssid).sheets.map {|item| item.properties }
114
111
  end
@@ -118,33 +115,6 @@ class GoogleSheets
118
115
  sheets
119
116
  end
120
117
 
121
- def sheet_list
122
- sheets.map do |item|
123
- {
124
- id: item.sheet_id,
125
- name: item.title,
126
- color: rgb2hex(item.tab_color),
127
- }.compact
128
- end
129
- end
130
-
131
- def sheet_id(obj)
132
- case obj
133
- when /^#(\d+)$/ then sheets[$1.to_i - 1].sheet_id
134
- when Integer then obj
135
- else sheets.first_result {|item| item.sheet_id if item.title == obj}
136
- end
137
- end
138
-
139
- def sheet_name(obj=nil)
140
- case obj ||= @wsid || "#1"
141
- when /^#(\d+)$/ then sheets[$1.to_i - 1].title
142
- when Integer then sheets.first_result {|item| item.title if item.sheet_id == obj }
143
- when "", nil then sheets.first.title
144
- else obj
145
- end
146
- end
147
-
148
118
  def resolve_area(area)
149
119
  if area.blank?
150
120
  wsid = sheet_name
@@ -157,30 +127,29 @@ class GoogleSheets
157
127
  else
158
128
  wsid = sheet_name(area)
159
129
  end
160
- "#{wsid}!#{rect || 'A:ZZ'}"
130
+ rect = rect ? rect.sub(/(?<=\D):/, "1:") : @rect
131
+ "#{wsid}!#{rect}"
161
132
  end
162
133
 
163
- def sheet_rename(pick, name=nil)
164
- shid = sheet_id(pick)
165
- 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
166
142
 
167
- reqs = []
168
- reqs.push(update_sheet_properties: {
169
- properties: {
170
- sheet_id: shid,
171
- title: name,
172
- },
173
- fields: "title",
174
- })
175
- resp = api.batch_update_spreadsheet(@ssid, { requests: reqs }, {})
176
- true
143
+ def sheet_clear(area)
144
+ area = resolve_area(area)
145
+ api.clear_values(@ssid, area)
177
146
  end
178
147
 
179
- def sheet_color(pick, color=nil) # NOTE: ignores alpha
148
+ def sheet_color(curr, color=nil) # NOTE: ignores alpha
180
149
  reqs = []
181
150
  reqs.push(update_sheet_properties: {
182
151
  properties: {
183
- sheet_id: sheet_id(pick),
152
+ sheet_id: sheet_id(curr),
184
153
  tab_color: hex2rgb(color),
185
154
  },
186
155
  fields: "tab_color",
@@ -189,10 +158,10 @@ class GoogleSheets
189
158
  true
190
159
  end
191
160
 
192
- def sheet_filter(area, want=nil)
161
+ def sheet_filter(area, filt=nil)
193
162
  area = resolve_area(area)
194
163
  range = range(area)
195
- criteria = filter_criteria(want) if want
164
+ criteria = filter_criteria(filt) if filt
196
165
  reqs = []
197
166
  reqs.push(clear_basic_filter: { sheet_id: range[:sheet_id] })
198
167
  reqs.push(set_basic_filter: { filter: { range: range, criteria: criteria}.compact })
@@ -219,9 +188,38 @@ class GoogleSheets
219
188
  true
220
189
  end
221
190
 
222
- def sheet_clear(area)
223
- area = resolve_area(area)
224
- 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
225
223
  end
226
224
 
227
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.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-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