google_spreadsheet_fetcher 1.2.0 → 1.3.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: 8931e371e571bd4765be2b63a647d8028d4ed763abddd82dacf9f6d70f30a987
4
- data.tar.gz: ce41e8fa0daf651ea25ffecb195a74dad258d4a9ad6b5abde05c80999de3609a
3
+ metadata.gz: 5a1d4d60f1365915f18a079381ea39c070746355c7e1311c4ec5e82ad2fe1787
4
+ data.tar.gz: 752b32a2a75a0861961a9e88ac1346d053f8d6afb8cb8c21356d947bcdcfeca5
5
5
  SHA512:
6
- metadata.gz: 325db541542d87960f7bd388be44f6d4977bb6e74df8804bfdeb152fd8b82f1079d1c0387bd8e1e878a629e2266aa3baad58264e5c4696056158b9d1f6e34377
7
- data.tar.gz: 9e297605b02343a4f688df0790b1c1e980689c74e092477967f385687dc36773f17ca1326a2521d0c9196a04c4c21dfc42e23b3127cc209cda88809eefae33ff
6
+ metadata.gz: b1868fe75af23fdda5750b7cf89e7ace9662d782b94de1d9a6eef6e28b92db459532ead2afb430a127dfdc814c5d451911dbd8a6cd806b58d59536920666301e
7
+ data.tar.gz: db9ae90340519957cabdea6bf25b686b243312d23ba541443e15cfb14a3c7576f0e9f5b1c185a6185194a7af11eeed64002ce4643b738775aa7972039de3733e
@@ -1,3 +1,4 @@
1
+ require "active_support/json"
1
2
  require "active_support/core_ext"
2
3
  require "google_spreadsheet_fetcher/version"
3
4
  require "google_spreadsheet_fetcher/config"
@@ -1,3 +1,5 @@
1
+ require 'active_support/configurable'
2
+
1
3
  module GoogleSpreadsheetFetcher
2
4
  class Config
3
5
  include ActiveSupport::Configurable
@@ -1,6 +1,6 @@
1
+ require 'google/apis/sheets_v4'
1
2
  require 'googleauth'
2
3
  require 'googleauth/stores/file_token_store'
3
- require "google/apis/sheets_v4"
4
4
  require 'shellwords'
5
5
 
6
6
  module GoogleSpreadsheetFetcher
@@ -18,27 +18,9 @@ module GoogleSpreadsheetFetcher
18
18
  @application_name = application_name
19
19
  end
20
20
 
21
- # @param [String] range(https://developers.google.com/sheets/api/guides/concepts#a1_notation)
22
- # @param [Integer] skip
23
- def fetch_all_rows(range, skip: 0, structured: false)
24
- rows = service.batch_get_spreadsheet_values(@spreadsheet_id, ranges: [range])&.value_ranges&.first&.values
25
- return [] if rows.blank?
26
-
27
- if structured
28
- headers = rows.delete_at(0)
29
- rows.slice!(0, skip)
30
- rows.map { |r| headers.zip(r).to_h }
31
- else
32
- rows.slice!(0, skip)
33
- rows
34
- end
35
- end
36
-
37
21
  def fetch_all_rows_by!(index: nil, sheet_id: nil, title: nil, skip: 0, structured: false)
38
22
  sheet = fetch_sheet_by!(index: index, sheet_id: sheet_id, title: title)
39
-
40
- range = "#{sheet.properties.title}!A:Z"
41
- fetch_all_rows(range, skip: skip, structured: structured)
23
+ fetch_all_rows(sheet, skip: skip, structured: structured)
42
24
  end
43
25
 
44
26
  def service
@@ -50,13 +32,43 @@ module GoogleSpreadsheetFetcher
50
32
 
51
33
  private
52
34
 
35
+ # @param [Google::Apis::SheetsV4::Sheet] sheet
36
+ # @param [Integer] skip
37
+ # @param [Boolean] structured
38
+ def fetch_all_rows(sheet, skip: 0, structured: false)
39
+ # https://developers.google.com/sheets/api/guides/concepts#a1_notation
40
+ range = "#{sheet.properties.title}!A:Z"
41
+ rows = service.get_spreadsheet_values(@spreadsheet_id, range)&.values
42
+ return [] if rows.blank?
43
+
44
+ headers = rows.first
45
+ count = headers.count
46
+
47
+ if structured
48
+ rows.delete_at(0)
49
+ rows.slice!(0, skip)
50
+ rows.map { |r| headers.zip(r).to_h }
51
+ else
52
+ rows.slice!(0, skip)
53
+ rows.map { |r| fill_array(r, count) }
54
+ # rows.map { |r| items = Array.new(count, ""); items[0..r.count - 1] = r; items }
55
+ # def fill_array(items, count, fill: "")
56
+ end
57
+ end
58
+
53
59
  def fetch_sheet_by!(index: nil, sheet_id: nil, title: nil)
54
60
  sheets = spreadsheet.sheets
55
61
  raise SheetNotFound if sheets.blank?
56
62
 
57
- return sheets[index] if index.present?
58
- return sheets.find { |s| s.properties.sheet_id == sheet_id } if sheet_id.present?
59
- return sheets.find { |s| s.properties.title == title } if title.present?
63
+ sheet = if index.present?
64
+ sheets[index]
65
+ elsif sheet_id.present?
66
+ sheets.find { |s| s.properties.sheet_id == sheet_id }
67
+ elsif title.present?
68
+ sheets.find { |s| s.properties.title == title }
69
+ end
70
+
71
+ return sheet if sheet.present?
60
72
 
61
73
  raise SheetNotFound
62
74
  end
@@ -80,5 +92,12 @@ module GoogleSpreadsheetFetcher
80
92
  def spreadsheet
81
93
  service.get_spreadsheet(@spreadsheet_id)
82
94
  end
95
+
96
+ def fill_array(items, count, fill: "")
97
+ max_count = [count, items.count].max
98
+ results = Array.new(max_count, fill)
99
+ results[0..items.count - 1] = items
100
+ results
101
+ end
83
102
  end
84
103
  end
@@ -1,3 +1,3 @@
1
1
  module GoogleSpreadsheetFetcher
2
- VERSION = "1.2.0"
2
+ VERSION = "1.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google_spreadsheet_fetcher
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Takahiro Ooishi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-10-03 00:00:00.000000000 Z
11
+ date: 2019-10-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: google-api-client