googlesheets 0.2.3 → 0.5.0

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: 484ead7c7ddf83bd062e24d8edb28fb0da072cbbe348f598974c7dcc6fc81687
4
+ data.tar.gz: 224d0e6b86d115a4b9aff60faba79a174686f65f551dff9322bb8ff22d0445ce
5
5
  SHA512:
6
- metadata.gz: bd7ff047f1181c713132bb09b6ac4b876c681b8fc6c89d657e2d2cf0a63aa2dc3bef9f973f8f92419f89035eb290d9b2f8a47ef020ad29326532eec2a41de57c
7
- data.tar.gz: 1bb3e0cc4fec63b073698ff2a33638d70e04a008a92a9d067d0f2c6de7d6b57a8e89caac3d9a83951cea41ace67f59eed0e1f557b83714ddf8e1e8f1ab863f82
6
+ metadata.gz: 38d00ab0cd8ac505238f604542e0f56f814d397cdf8d606c0a64cba8bf022afcd58173cf5af5e23cbf38590b18c49bc849001c231c23b509aee8b384a62b4580
7
+ data.tar.gz: a34dc14942dd9433331221804a95f940104a58b08c2830e1a6866c046db9b35c9ec220c91fd9c26aedc8b98b54a96274d007f045f0434c3db10fcf804543f670
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,16 @@ 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.0"
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+)$/] && $1.to_i # default worksheet id
18
28
 
19
- @json = opts[:credentials] || 'credentials.json'
20
- @yaml = opts[:token ] || 'token.yaml'
29
+ @json = opts[:credentials] || "credentials.json"
30
+ @yaml = opts[:token ] || "token.yaml"
21
31
 
22
32
  if opts[:debug] == true
23
33
  $stdout.sync = true
@@ -35,8 +45,8 @@ class GoogleSheets
35
45
  idno = Google::Auth::ClientId.from_file(@json)
36
46
  repo = Google::Auth::Stores::FileTokenStore.new(file: @yaml)
37
47
  auth = Google::Auth::UserAuthorizer.new(idno, Google::Apis::SheetsV4::AUTH_SPREADSHEETS, repo)
38
- oobs = 'urn:ietf:wg:oauth:2.0:oob'
39
- user = 'default'
48
+ oobs = "urn:ietf:wg:oauth:2.0:oob"
49
+ user = "default"
40
50
  info = auth.get_credentials(user) || begin
41
51
  href = auth.get_authorization_url(base_url: oobs)
42
52
  puts "Open the following URL and paste the code here:\n" + href
@@ -86,8 +96,8 @@ class GoogleSheets
86
96
  end
87
97
 
88
98
  def range(area)
89
- sh, rc = area.split('!', 2); rc, sh = sh, nil if sh.nil?
90
- as, ae = rc.split(':', 2); ae ||= as
99
+ sh, rc = area.split("!", 2); rc, sh = sh, nil if sh.nil?
100
+ as, ae = rc.split(":", 2); ae ||= as
91
101
  cs, rs = as.split(/(?=\d)/, 2); cs = biject(cs) - 1; rs = rs.to_i - 1
92
102
  ce, re = ae.split(/(?=\d)/, 2); ce = biject(ce) - 1; re = re.to_i - 1
93
103
  {
@@ -126,16 +136,28 @@ class GoogleSheets
126
136
  end
127
137
  end
128
138
 
129
- def sheet_name(obj)
130
- case obj
139
+ def sheet_name(obj=nil)
140
+ case obj ||= @wsid || "#1"
131
141
  when /^#(\d+)$/ then sheets[$1.to_i - 1].title
132
- when Integer then sheets.first_result {|item| item.title if item.sheet_id == obj}
142
+ when Integer then sheets.first_result {|item| item.title if item.sheet_id == obj }
143
+ when "", nil then sheets.first.title
133
144
  else obj
134
145
  end
135
146
  end
136
147
 
137
- def resolve_sheet(area)
138
- area.sub(/^(#\d+)(?=!)/) {|num| sheet_name(num)}
148
+ def resolve_area(area)
149
+ if area.blank?
150
+ wsid = sheet_name
151
+ elsif area =~ /!/
152
+ wsid = sheet_name($`)
153
+ rect = $'
154
+ elsif area =~ /:/ or area =~ /^[a-z]{1,3}\d+$/i
155
+ wsid = sheet_name
156
+ rect = area
157
+ else
158
+ wsid = sheet_name(area)
159
+ end
160
+ "#{wsid}!#{rect || 'A:ZZ'}"
139
161
  end
140
162
 
141
163
  def sheet_rename(pick, name=nil)
@@ -148,7 +170,7 @@ class GoogleSheets
148
170
  sheet_id: shid,
149
171
  title: name,
150
172
  },
151
- fields: 'title',
173
+ fields: "title",
152
174
  })
153
175
  resp = api.batch_update_spreadsheet(@ssid, { requests: reqs }, {})
154
176
  true
@@ -161,14 +183,14 @@ class GoogleSheets
161
183
  sheet_id: sheet_id(pick),
162
184
  tab_color: hex2rgb(color),
163
185
  },
164
- fields: 'tab_color',
186
+ fields: "tab_color",
165
187
  })
166
188
  resp = api.batch_update_spreadsheet(@ssid, { requests: reqs }, {})
167
189
  true
168
190
  end
169
191
 
170
192
  def sheet_filter(area, want=nil)
171
- area = resolve_sheet(area)
193
+ area = resolve_area(area)
172
194
  range = range(area)
173
195
  criteria = filter_criteria(want) if want
174
196
  reqs = []
@@ -179,7 +201,7 @@ class GoogleSheets
179
201
  end
180
202
 
181
203
  def sheet_format(area, pattern)
182
- area = resolve_sheet(area)
204
+ area = resolve_area(area)
183
205
  reqs = []
184
206
  reqs.push(repeat_cell: {
185
207
  range: range(area),
@@ -191,26 +213,24 @@ class GoogleSheets
191
213
  },
192
214
  },
193
215
  },
194
- fields: 'user_entered_format.number_format',
216
+ fields: "user_entered_format.number_format",
195
217
  })
196
218
  resp = api.batch_update_spreadsheet(@ssid, { requests: reqs }, {})
197
219
  true
198
220
  end
199
221
 
200
222
  def sheet_clear(area)
201
- area = resolve_sheet(area)
202
- area.sub!(/^(#\d+)(?=!)/) {|num| sheet_name(num)}
223
+ area = resolve_area(area)
203
224
  api.clear_values(@ssid, area)
204
225
  end
205
226
 
206
- def sheet_read(area)
207
- area = resolve_sheet(area)
208
- area.sub!(/^(#\d+)(?=!)/) {|num| sheet_name(num)}
227
+ def sheet_read(area=nil)
228
+ area = resolve_area(area)
209
229
  api.get_spreadsheet_values(@ssid, area).values
210
230
  end
211
231
 
212
232
  def sheet_save(area, rows, log=false)
213
- area = resolve_sheet(area)
233
+ area = resolve_area(area)
214
234
  gasv = Google::Apis::SheetsV4::ValueRange.new(range: area, values: rows)
215
235
  done = api.update_spreadsheet_value(@ssid, area, gasv, value_input_option: "USER_ENTERED")
216
236
  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.0
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