google_sheets 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +9 -9
- data/lib/google_sheets/session.rb +14 -6
- data/lib/google_sheets/sheet.rb +18 -1
- data/lib/google_sheets/spreadsheet.rb +10 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4272935e8e6148c9003b8b9cce980c17b4fad1f618a8783d414e74599c3a9a81
|
4
|
+
data.tar.gz: c9bb34301243f5e883226002395ab0b827d3b8ea0cdf3a37ad7a70afde78c263
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c268f6821bfa7a75059853b691b89ca3103720f1dfb8bbb23c3dc7c90fcff9f8f598a356ad0099d5a642194a848c7e8306a1069b28991def6efc2f4e9934e937
|
7
|
+
data.tar.gz: 7efda40322d3a6af693862c97959363b994d0ddf066fb610c18ae7638f644d261815f0d9bfe90ec3b98d177b915efebef1a5571bb75f7bd0afa27a47a8f27f9c
|
data/README.md
CHANGED
@@ -2,11 +2,11 @@
|
|
2
2
|
|
3
3
|
Since I only ever used that gem for creating/reading spreadsheets, I created this simple gem for just that, but using the v4 API.
|
4
4
|
|
5
|
-
* [Installing](#
|
5
|
+
* [Installing](#Installing)
|
6
6
|
* [Getting started](#getting-started)
|
7
7
|
* [GitHub](http://github.com/shmay/google_sheets)
|
8
8
|
|
9
|
-
|
9
|
+
<h3 id='installing'>Installing</h3>
|
10
10
|
|
11
11
|
Add this line to your application's Gemfile & `bundle install`:
|
12
12
|
|
@@ -17,12 +17,12 @@ gem 'google_sheets'
|
|
17
17
|
Or install it yourself:
|
18
18
|
|
19
19
|
```
|
20
|
-
$ gem install
|
20
|
+
$ gem install google_sheets
|
21
21
|
```
|
22
22
|
|
23
|
-
|
23
|
+
<h3 id='authorization'>Authorization</h3>
|
24
24
|
|
25
|
-
The authorization process is taken from Google's [own tutorial](https://developers.google.com/sheets/api/quickstart/ruby#step_3_set_up_the_sample).
|
25
|
+
The authorization process is taken from Google's [own tutorial](https://developers.google.com/sheets/api/quickstart/ruby#step_3_set_up_the_sample). Take a look at [session.rb](lib/google_sheets/session.rb) - it closely resembles the authorization code in that tutorial.
|
26
26
|
|
27
27
|
You'll need to create a project and enable the GSheets API, as detailed [in step 1 of that tutorial](https://developers.google.com/sheets/api/quickstart/ruby#step_1_turn_on_the_api_name).
|
28
28
|
|
@@ -52,7 +52,7 @@ session = GoogleSheets::Session.start_session(
|
|
52
52
|
)
|
53
53
|
```
|
54
54
|
|
55
|
-
|
55
|
+
<h3 id='getting-started'>Getting Started</h3>
|
56
56
|
|
57
57
|
Once you're authorized, you can read, create, and delete sheets within a spreadsheet.
|
58
58
|
|
@@ -72,9 +72,7 @@ sheet1 = spreadsheet.sheets[0]
|
|
72
72
|
sheet1.values
|
73
73
|
# => [['first, 'last', 'age'], ['bob', 'jones', '92'], ['steve', 'johnson', '22']]
|
74
74
|
|
75
|
-
|
76
|
-
|
77
|
-
sheet2 = spreadsheet.add_sheet('what', values: values)
|
75
|
+
sheet2 = spreadsheet.add_sheet('what', values: [[1,2],[3,4]])
|
78
76
|
|
79
77
|
spreadsheet.sheets.map &:title
|
80
78
|
# => ['Sheet1', 'yoyo1', 'what']
|
@@ -89,6 +87,7 @@ spreadsheet.sheets.map &:title
|
|
89
87
|
# it uses the top row as the keys
|
90
88
|
# fyi, this will also convert the values to UTF-8
|
91
89
|
# sometimes gsheets values come in as ASCII
|
90
|
+
|
92
91
|
sheet1.to_json
|
93
92
|
# =>
|
94
93
|
# [
|
@@ -103,6 +102,7 @@ sheet1.to_json
|
|
103
102
|
# age: '22'
|
104
103
|
# }
|
105
104
|
# ]
|
105
|
+
|
106
106
|
```
|
107
107
|
|
108
108
|
Or just look at [the spec](spec/test_all_the_things_spec.rb) to see it in action.
|
@@ -10,28 +10,30 @@ require 'google_sheets/spreadsheet'
|
|
10
10
|
|
11
11
|
module GoogleSheets
|
12
12
|
class Session
|
13
|
-
OOB_URI = 'urn:ietf:wg:oauth:2.0:oob'
|
14
|
-
|
15
|
-
# CLIENT_SECRETS_FILENAME = 'client_secret.json'.freeze
|
16
|
-
CREDENTIALS_FILENAME = 'token.yaml'.freeze
|
13
|
+
OOB_URI = 'urn:ietf:wg:oauth:2.0:oob'
|
14
|
+
CREDENTIALS_FILENAME = 'token.yaml'
|
17
15
|
SCOPE = Google::Apis::SheetsV4::AUTH_SPREADSHEETS
|
18
16
|
|
17
|
+
# Accepts a [Google::Apis::SheetsV4::SheetsService](https://www.rubydoc.info/github/google/google-api-ruby-client/Google/Apis/SheetsV4/SheetsService)
|
18
|
+
# through which we can read/write gsheets
|
19
19
|
def initialize service
|
20
20
|
@service = service
|
21
21
|
end
|
22
22
|
|
23
|
+
# instantiates and returns a Spreadsheet object from a key
|
24
|
+
# @return [GoogleSheets::Spreadsheet]
|
23
25
|
def spreadsheet_from_key key
|
24
26
|
Spreadsheet.new @service, key
|
25
27
|
end
|
26
28
|
|
27
|
-
|
29
|
+
# This code is heavily borrowed from https://developers.google.com/sheets/api/quickstart/ruby#step_3_set_up_the_sample
|
28
30
|
#
|
29
31
|
##
|
30
32
|
# Ensure valid credentials, either by restoring from the saved credentials
|
31
33
|
# files or intitiating an OAuth2 authorization. If authorization is required,
|
32
34
|
# the user's default browser will be launched to approve the request.
|
33
35
|
#
|
34
|
-
# @return [Google::Auth::UserRefreshCredentials] OAuth2 credentials
|
36
|
+
# @return [Google::Auth::UserRefreshCredentials](https://www.rubydoc.info/gems/googleauth/0.5.0/Google/Auth/UserRefreshCredentials) OAuth2 credentials
|
35
37
|
def self.authorize client_id, client_secret, token_path
|
36
38
|
credentials_hash = {
|
37
39
|
"installed" =>
|
@@ -63,6 +65,12 @@ module GoogleSheets
|
|
63
65
|
credentials
|
64
66
|
end
|
65
67
|
|
68
|
+
private_class_method :authorize
|
69
|
+
|
70
|
+
##
|
71
|
+
# Everything starts from this method - authorizes the user and creates a Session object, through which we can read spreadsheets.
|
72
|
+
#
|
73
|
+
# @return [GoogleSheets::Session]
|
66
74
|
def self.start_session client_id:, client_secret:, token_path: '.'
|
67
75
|
# Initialize the API
|
68
76
|
service = Google::Apis::SheetsV4::SheetsService.new
|
data/lib/google_sheets/sheet.rb
CHANGED
@@ -1,6 +1,13 @@
|
|
1
|
+
# frozen_string_literal
|
2
|
+
|
1
3
|
module GoogleSheets
|
2
4
|
class Sheet
|
3
|
-
|
5
|
+
# [Google::Apis::SheetsV4::SheetProperties](https://www.rubydoc.info/github/google/google-api-ruby-client/Google/Apis/SheetsV4/SheetProperties) in hash form
|
6
|
+
# @return [Hash]
|
7
|
+
attr_reader :properties
|
8
|
+
# title of the sheet
|
9
|
+
# @return [String]
|
10
|
+
attr_reader :title
|
4
11
|
attr_writer :values
|
5
12
|
|
6
13
|
def initialize service, sheet, spreadsheet
|
@@ -11,14 +18,20 @@ module GoogleSheets
|
|
11
18
|
@title = @properties[:title]
|
12
19
|
end
|
13
20
|
|
21
|
+
# The internal ID of the sheet. From Google.
|
22
|
+
# @return [Integer]
|
14
23
|
def id
|
15
24
|
@properties[:sheet_id]
|
16
25
|
end
|
17
26
|
|
27
|
+
# Returns an Array of string values, EG: [['one', 'two'], ['three', 'four']]
|
28
|
+
# @return [Array(String)]
|
18
29
|
def values
|
19
30
|
@values ||= @service.get_spreadsheet_values(@spreadsheet.key, @title).values
|
20
31
|
end
|
21
32
|
|
33
|
+
# Deletes a sheet from a spreadsheet
|
34
|
+
# @return [Sheet]
|
22
35
|
def delete!
|
23
36
|
delete_sheet_request = Google::Apis::SheetsV4::DeleteSheetRequest.new
|
24
37
|
delete_sheet_request.sheet_id = self.id
|
@@ -36,6 +49,10 @@ module GoogleSheets
|
|
36
49
|
self
|
37
50
|
end
|
38
51
|
|
52
|
+
# Converts the spreadsheet to an array of hashes, using the top row as the keys
|
53
|
+
#
|
54
|
+
# EG `[['name', 'age'], ['john', '20']] => [{name: 'john', age: '20'}]`
|
55
|
+
# @return [Array(Hash)]
|
39
56
|
def to_json
|
40
57
|
top_row = values[0].map &:to_sym
|
41
58
|
hashify_data(values[1..-1], top_row)
|
@@ -1,5 +1,9 @@
|
|
1
|
+
# frozen_string_literal
|
2
|
+
|
1
3
|
module GoogleSheets
|
2
4
|
class Spreadsheet
|
5
|
+
# the spreadsheet key
|
6
|
+
# @return [String]
|
3
7
|
attr_reader :key
|
4
8
|
|
5
9
|
def initialize service, key
|
@@ -8,13 +12,16 @@ module GoogleSheets
|
|
8
12
|
load_spreadsheet
|
9
13
|
end
|
10
14
|
|
15
|
+
# loads the spreadsheet from google sheets
|
11
16
|
def load_spreadsheet
|
12
17
|
@spreadsheet = @service.get_spreadsheet(@key)
|
13
18
|
@properties = @spreadsheet.properties.to_h
|
14
19
|
end
|
15
20
|
|
21
|
+
# reloads the spreadsheet, effectively uncaching everything
|
16
22
|
alias refresh! load_spreadsheet
|
17
23
|
|
24
|
+
# @return [Array(Sheet)]
|
18
25
|
def sheets
|
19
26
|
@sheets ||= @spreadsheet.sheets.map do |sheet|
|
20
27
|
Sheet.new(@service, sheet, self)
|
@@ -22,6 +29,7 @@ module GoogleSheets
|
|
22
29
|
end
|
23
30
|
|
24
31
|
# HT this SO answer: https://stackoverflow.com/a/49886382/548170
|
32
|
+
# @return [Sheet]
|
25
33
|
def add_sheet title, values: []
|
26
34
|
add_sheet_request = Google::Apis::SheetsV4::AddSheetRequest.new
|
27
35
|
add_sheet_request.properties = Google::Apis::SheetsV4::SheetProperties.new
|
@@ -48,6 +56,8 @@ module GoogleSheets
|
|
48
56
|
sheet
|
49
57
|
end
|
50
58
|
|
59
|
+
private
|
60
|
+
|
51
61
|
def append_to_sheet title, values
|
52
62
|
# The A1 notation of a range to search for a logical table of data.
|
53
63
|
# Values will be appended after the last row of the table.
|