googlesheets 0.2.3 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d8f5d17077c83d17f62a0c18e129e1a1b88cfe1059606b22ca012b349b6ec682
4
- data.tar.gz: 34029b089e78a8e32cd1572971c248e5ff05d4e1e9d772754df26bfd5d6a3461
3
+ metadata.gz: 0aefe530c8bfd81c39a9117a0ae9fd698070e2886961c74e2c80a0ddb21aefeb
4
+ data.tar.gz: e623d3db0855c721127f56a463b79b4d2eb68d51e88d4ba8bce49da8c440a459
5
5
  SHA512:
6
- metadata.gz: bd7ff047f1181c713132bb09b6ac4b876c681b8fc6c89d657e2d2cf0a63aa2dc3bef9f973f8f92419f89035eb290d9b2f8a47ef020ad29326532eec2a41de57c
7
- data.tar.gz: 1bb3e0cc4fec63b073698ff2a33638d70e04a008a92a9d067d0f2c6de7d6b57a8e89caac3d9a83951cea41ace67f59eed0e1f557b83714ddf8e1e8f1ab863f82
6
+ metadata.gz: 130fed8b81b90818069b02343ebf919f8aaa0ca90a33a0278552287e65e2c410cf583c969498fe6e4cb73fd884eb9e1ec69a43b4b263badba17bd47d0d11a7d3
7
+ data.tar.gz: 0cc8e0f830615ca7708800900e0bfbc8307f7032f21740a961bc1047d6fd9aa10f43b5bbdf9e5684984b90b2a3ca5f84df825f61afb7ba02e3a92b1e130d5529
data/googlesheets.gemspec CHANGED
@@ -2,12 +2,14 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "googlesheets"
5
- s.version = "0.2.3"
5
+ s.version = `grep -m 1 '^\s*VERSION' lib/googlesheets.rb | head -1 | cut -f 2 -d '"'`
6
6
  s.author = "Steve Shreeve"
7
7
  s.email = "steve.shreeve@gmail.com"
8
- s.summary = "Ruby gem for Google Sheets"
8
+ s.summary = "Ruby library for Google Sheets"
9
9
  s.description = "This gem allows easy access to Google Sheets API V4."
10
10
  s.homepage = "https://github.com/shreeve/googlesheets"
11
11
  s.license = "MIT"
12
12
  s.files = `git ls-files`.split("\n") - %w[.gitignore]
13
+ s.executables = `cd bin && git ls-files .`.split("\n")
14
+ s.add_runtime_dependency "google-api-client", "~> 0.53.0"
13
15
  end
data/lib/googlesheets.rb CHANGED
@@ -2,6 +2,13 @@ require "google/apis/sheets_v4"
2
2
  require "googleauth"
3
3
  require "googleauth/stores/file_token_store"
4
4
 
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
+
5
12
  module Enumerable
6
13
  def first_result
7
14
  block_given? ? each {|item| result = (yield item) and return result} && nil : find {|item| item}
@@ -11,13 +18,17 @@ end
11
18
  # https://googleapis.dev/ruby/google-api-client/latest/Google/Apis/SheetsV4/Request.html
12
19
 
13
20
  class GoogleSheets
21
+ VERSION = "0.5.1"
22
+
14
23
  attr_accessor :api
15
24
 
16
25
  def initialize(ssid, **opts)
17
- @ssid = ssid =~ /^https?:/ ? ssid.split('/')[5] : ssid
26
+ @ssid = ssid =~ /^https?:/ ? ssid.split("/")[5] : ssid # spreadsheet id
27
+ @wsid = ssid =~ /(?<=#gid=)(\d+)(?:!([a-z\d:]+))?$/i ? $1.to_i : "#1" # worksheet id
28
+ @rect = $2 ? $2.sub(/(?<=\D):/, "1:") : "A:ZZ"
18
29
 
19
- @json = opts[:credentials] || 'credentials.json'
20
- @yaml = opts[:token ] || 'token.yaml'
30
+ @json = opts[:credentials] || "credentials.json"
31
+ @yaml = opts[:token ] || "token.yaml"
21
32
 
22
33
  if opts[:debug] == true
23
34
  $stdout.sync = true
@@ -35,8 +46,8 @@ class GoogleSheets
35
46
  idno = Google::Auth::ClientId.from_file(@json)
36
47
  repo = Google::Auth::Stores::FileTokenStore.new(file: @yaml)
37
48
  auth = Google::Auth::UserAuthorizer.new(idno, Google::Apis::SheetsV4::AUTH_SPREADSHEETS, repo)
38
- oobs = 'urn:ietf:wg:oauth:2.0:oob'
39
- user = 'default'
49
+ oobs = "urn:ietf:wg:oauth:2.0:oob"
50
+ user = "default"
40
51
  info = auth.get_credentials(user) || begin
41
52
  href = auth.get_authorization_url(base_url: oobs)
42
53
  puts "Open the following URL and paste the code here:\n" + href
@@ -86,8 +97,8 @@ class GoogleSheets
86
97
  end
87
98
 
88
99
  def range(area)
89
- sh, rc = area.split('!', 2); rc, sh = sh, nil if sh.nil?
90
- as, ae = rc.split(':', 2); ae ||= as
100
+ sh, rc = area.split("!", 2); rc, sh = sh, nil if sh.nil?
101
+ as, ae = rc.split(":", 2); ae ||= as
91
102
  cs, rs = as.split(/(?=\d)/, 2); cs = biject(cs) - 1; rs = rs.to_i - 1
92
103
  ce, re = ae.split(/(?=\d)/, 2); ce = biject(ce) - 1; re = re.to_i - 1
93
104
  {
@@ -126,16 +137,29 @@ class GoogleSheets
126
137
  end
127
138
  end
128
139
 
129
- def sheet_name(obj)
130
- case obj
140
+ def sheet_name(obj=nil)
141
+ case obj ||= @wsid
131
142
  when /^#(\d+)$/ then sheets[$1.to_i - 1].title
132
- when Integer then sheets.first_result {|item| item.title if item.sheet_id == obj}
143
+ when "", 0, nil then sheets.first.title
144
+ when Integer then sheets.first_result {|item| item.title if item.sheet_id == obj }
133
145
  else obj
134
146
  end
135
147
  end
136
148
 
137
- def resolve_sheet(area)
138
- area.sub(/^(#\d+)(?=!)/) {|num| sheet_name(num)}
149
+ def resolve_area(area)
150
+ if area.blank?
151
+ wsid = sheet_name
152
+ elsif area =~ /!/
153
+ wsid = sheet_name($`)
154
+ rect = $'
155
+ elsif area =~ /:/ or area =~ /^[a-z]{1,3}\d+$/i
156
+ wsid = sheet_name
157
+ rect = area
158
+ else
159
+ wsid = sheet_name(area)
160
+ end
161
+ rect = rect ? rect.sub(/(?<=\D):/, "1:") : @rect
162
+ "#{wsid}!#{rect}"
139
163
  end
140
164
 
141
165
  def sheet_rename(pick, name=nil)
@@ -148,7 +172,7 @@ class GoogleSheets
148
172
  sheet_id: shid,
149
173
  title: name,
150
174
  },
151
- fields: 'title',
175
+ fields: "title",
152
176
  })
153
177
  resp = api.batch_update_spreadsheet(@ssid, { requests: reqs }, {})
154
178
  true
@@ -161,14 +185,14 @@ class GoogleSheets
161
185
  sheet_id: sheet_id(pick),
162
186
  tab_color: hex2rgb(color),
163
187
  },
164
- fields: 'tab_color',
188
+ fields: "tab_color",
165
189
  })
166
190
  resp = api.batch_update_spreadsheet(@ssid, { requests: reqs }, {})
167
191
  true
168
192
  end
169
193
 
170
194
  def sheet_filter(area, want=nil)
171
- area = resolve_sheet(area)
195
+ area = resolve_area(area)
172
196
  range = range(area)
173
197
  criteria = filter_criteria(want) if want
174
198
  reqs = []
@@ -179,7 +203,7 @@ class GoogleSheets
179
203
  end
180
204
 
181
205
  def sheet_format(area, pattern)
182
- area = resolve_sheet(area)
206
+ area = resolve_area(area)
183
207
  reqs = []
184
208
  reqs.push(repeat_cell: {
185
209
  range: range(area),
@@ -191,26 +215,24 @@ class GoogleSheets
191
215
  },
192
216
  },
193
217
  },
194
- fields: 'user_entered_format.number_format',
218
+ fields: "user_entered_format.number_format",
195
219
  })
196
220
  resp = api.batch_update_spreadsheet(@ssid, { requests: reqs }, {})
197
221
  true
198
222
  end
199
223
 
200
224
  def sheet_clear(area)
201
- area = resolve_sheet(area)
202
- area.sub!(/^(#\d+)(?=!)/) {|num| sheet_name(num)}
225
+ area = resolve_area(area)
203
226
  api.clear_values(@ssid, area)
204
227
  end
205
228
 
206
- def sheet_read(area)
207
- area = resolve_sheet(area)
208
- area.sub!(/^(#\d+)(?=!)/) {|num| sheet_name(num)}
229
+ def sheet_read(area=nil)
230
+ area = resolve_area(area)
209
231
  api.get_spreadsheet_values(@ssid, area).values
210
232
  end
211
233
 
212
234
  def sheet_save(area, rows, log=false)
213
- area = resolve_sheet(area)
235
+ area = resolve_area(area)
214
236
  gasv = Google::Apis::SheetsV4::ValueRange.new(range: area, values: rows)
215
237
  done = api.update_spreadsheet_value(@ssid, area, gasv, value_input_option: "USER_ENTERED")
216
238
  puts "#{done.updated_cells} cells updated." if log
metadata CHANGED
@@ -1,22 +1,35 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: googlesheets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.5.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: 2021-09-18 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2023-03-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: google-api-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.53.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.53.0
13
27
  description: This gem allows easy access to Google Sheets API V4.
14
28
  email: steve.shreeve@gmail.com
15
29
  executables: []
16
30
  extensions: []
17
31
  extra_rdoc_files: []
18
32
  files:
19
- - ".ruby-version"
20
33
  - Gemfile
21
34
  - LICENSE
22
35
  - README.md
@@ -41,8 +54,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
41
54
  - !ruby/object:Gem::Version
42
55
  version: '0'
43
56
  requirements: []
44
- rubygems_version: 3.2.16
57
+ rubygems_version: 3.4.8
45
58
  signing_key:
46
59
  specification_version: 4
47
- summary: Ruby gem for Google Sheets
60
+ summary: Ruby library for Google Sheets
48
61
  test_files: []
data/.ruby-version DELETED
@@ -1 +0,0 @@
1
- 2.6.5