googlesheets 0.5.1 → 0.6.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/bin/shat +32 -0
  3. data/lib/googlesheets.rb +75 -72
  4. metadata +5 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0aefe530c8bfd81c39a9117a0ae9fd698070e2886961c74e2c80a0ddb21aefeb
4
- data.tar.gz: e623d3db0855c721127f56a463b79b4d2eb68d51e88d4ba8bce49da8c440a459
3
+ metadata.gz: 337343bcb13d2a791f5aee8a239ce9f6fb176d9844da5d2d672efd98d21974d5
4
+ data.tar.gz: 68006ba26a382e5599a95822731e55261b7bb10b871c3536ecf4a2f0671d4755
5
5
  SHA512:
6
- metadata.gz: 130fed8b81b90818069b02343ebf919f8aaa0ca90a33a0278552287e65e2c410cf583c969498fe6e4cb73fd884eb9e1ec69a43b4b263badba17bd47d0d11a7d3
7
- data.tar.gz: 0cc8e0f830615ca7708800900e0bfbc8307f7032f21740a961bc1047d6fd9aa10f43b5bbdf9e5684984b90b2a3ca5f84df825f61afb7ba02e3a92b1e130d5529
6
+ metadata.gz: 250b28e880607001b70f71a1f417325cd73c301fe4cad19af54e390f17e08630dd9e7a44a2aaa537646d64c2bc8e0571aeadda02e3929561b88232f581700e04
7
+ data.tar.gz: 3e302a8611cd632225afac79f038ef000d3ea286e872de931640d43786b3de2acd7c325e9540a932be9b02dc6fc456f03b7dfb2e29092c0fa65703499990e9ff
data/bin/shat ADDED
@@ -0,0 +1,32 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ version="0.0.1"
4
+
5
+ STDOUT.sync = true
6
+
7
+ begin
8
+ require "censive"
9
+ rescue LoadError
10
+ abort "#{File.basename($0)} needs another gem, run 'gem install censive'"
11
+ end
12
+
13
+ require "googlesheets"
14
+ require "optparse"
15
+
16
+ trap("INT" ) { abort "\n" }
17
+
18
+ OptionParser.new.instance_eval do
19
+ @banner = "usage: #{program_name} [options] <url>"
20
+
21
+ on "-h", "--help" , "Show help and command usage" do Kernel.abort to_s; end
22
+ on "-v", "--version" , "Show version number" do Kernel.abort "#{program_name} #{version}"; end
23
+
24
+ self
25
+ end.parse!(into: opts={}) rescue abort($!.message)
26
+
27
+ link = ARGV.shift or abort("no url provided")
28
+
29
+ goog = GoogleSheets.new(link)
30
+ rows = goog.sheet_read
31
+
32
+ Censive.writer {|csv| rows.each {|row| csv << row }}
data/lib/googlesheets.rb CHANGED
@@ -1,3 +1,12 @@
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"
@@ -6,19 +15,11 @@ class Object
6
15
  def blank?
7
16
  respond_to?(:empty?) or return !self
8
17
  empty? or respond_to?(:strip) && strip.empty?
9
- end
18
+ end unless defined? blank?
10
19
  end
11
20
 
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
21
  class GoogleSheets
21
- VERSION = "0.5.1"
22
+ VERSION = "0.6.3"
22
23
 
23
24
  attr_accessor :api
24
25
 
@@ -55,12 +56,15 @@ class GoogleSheets
55
56
  end
56
57
  end
57
58
 
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]
59
+ def biject(x) # a=1, z=26, aa=27, az=52, ba=53, aaa=703
60
+ case x
61
+ when String
62
+ x.each_char.inject(0) {|n,c| (n * 26) + (c.ord & 31) }
63
+ when Integer
64
+ s = []
65
+ s << (((x -= 1) % 26) + 65).chr && x /= 26 while x > 0
66
+ s.reverse.join
67
+ end
64
68
  end
65
69
 
66
70
  def hex2rgb(color=nil)
@@ -72,15 +76,12 @@ class GoogleSheets
72
76
  { red: r, green: g, blue: b }
73
77
  end
74
78
 
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
79
+ def rgb2hex(color=nil)
80
+ color or return
81
+ r = ((color.red || 0) * 255).to_i
82
+ g = ((color.green || 0) * 255).to_i
83
+ b = ((color.blue || 0) * 255).to_i
84
+ "#%02x%02x%02x" % [r, g, b]
84
85
  end
85
86
 
86
87
  def filter_criteria(hash)
@@ -110,6 +111,8 @@ class GoogleSheets
110
111
  }.compact
111
112
  end
112
113
 
114
+ # --------------------------------------------------------------------------
115
+
113
116
  def sheets
114
117
  @sheets ||= api.get_spreadsheet(@ssid).sheets.map {|item| item.properties }
115
118
  end
@@ -119,33 +122,6 @@ class GoogleSheets
119
122
  sheets
120
123
  end
121
124
 
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
125
  def resolve_area(area)
150
126
  if area.blank?
151
127
  wsid = sheet_name
@@ -162,27 +138,25 @@ class GoogleSheets
162
138
  "#{wsid}!#{rect}"
163
139
  end
164
140
 
165
- def sheet_rename(pick, name=nil)
166
- shid = sheet_id(pick)
167
- name ||= yield(sheet_name(shid)) if block_given?
141
+ def sheet_name(obj=nil)
142
+ case obj ||= @wsid
143
+ when /^#(\d+)(?:!?|$)/ then sheets[$1.to_i - 1].title
144
+ when "", 0, nil then sheets.first.title
145
+ when Integer then sheets.find {|item| item.sheet_id == obj}&.title
146
+ else obj
147
+ end
148
+ end
168
149
 
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
150
+ def sheet_clear(area)
151
+ area = resolve_area(area)
152
+ api.clear_values(@ssid, area)
179
153
  end
180
154
 
181
- def sheet_color(pick, color=nil) # NOTE: ignores alpha
155
+ def sheet_color(curr, color=nil) # NOTE: ignores alpha
182
156
  reqs = []
183
157
  reqs.push(update_sheet_properties: {
184
158
  properties: {
185
- sheet_id: sheet_id(pick),
159
+ sheet_id: sheet_id(curr),
186
160
  tab_color: hex2rgb(color),
187
161
  },
188
162
  fields: "tab_color",
@@ -191,10 +165,10 @@ class GoogleSheets
191
165
  true
192
166
  end
193
167
 
194
- def sheet_filter(area, want=nil)
168
+ def sheet_filter(area, filt=nil)
195
169
  area = resolve_area(area)
196
170
  range = range(area)
197
- criteria = filter_criteria(want) if want
171
+ criteria = filter_criteria(filt) if filt
198
172
  reqs = []
199
173
  reqs.push(clear_basic_filter: { sheet_id: range[:sheet_id] })
200
174
  reqs.push(set_basic_filter: { filter: { range: range, criteria: criteria}.compact })
@@ -221,9 +195,38 @@ class GoogleSheets
221
195
  true
222
196
  end
223
197
 
224
- def sheet_clear(area)
225
- area = resolve_area(area)
226
- api.clear_values(@ssid, area)
198
+ def sheet_id(obj)
199
+ case obj
200
+ when /^#(\d+)(?:!?|$)/ then sheets[$1.to_i - 1].sheet_id
201
+ when Integer then obj
202
+ else sheets.find {|item| item.title == obj}&.sheet_id
203
+ end
204
+ end
205
+
206
+ def sheet_list
207
+ sheets.map do |item|
208
+ {
209
+ id: item.sheet_id,
210
+ name: item.title,
211
+ color: rgb2hex(item.tab_color),
212
+ }.compact
213
+ end
214
+ end
215
+
216
+ def sheet_rename(curr, name=nil)
217
+ shid = sheet_id(curr)
218
+ name ||= yield(sheet_name(shid)) if block_given?
219
+
220
+ reqs = []
221
+ reqs.push(update_sheet_properties: {
222
+ properties: {
223
+ sheet_id: shid,
224
+ title: name,
225
+ },
226
+ fields: "title",
227
+ })
228
+ resp = api.batch_update_spreadsheet(@ssid, { requests: reqs }, {})
229
+ true
227
230
  end
228
231
 
229
232
  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.3
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
@@ -26,13 +26,15 @@ dependencies:
26
26
  version: 0.53.0
27
27
  description: This gem allows easy access to Google Sheets API V4.
28
28
  email: steve.shreeve@gmail.com
29
- executables: []
29
+ executables:
30
+ - shat
30
31
  extensions: []
31
32
  extra_rdoc_files: []
32
33
  files:
33
34
  - Gemfile
34
35
  - LICENSE
35
36
  - README.md
37
+ - bin/shat
36
38
  - googlesheets.gemspec
37
39
  - lib/googlesheets.rb
38
40
  homepage: https://github.com/shreeve/googlesheets