google_sheets 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +7 -3
- data/lib/google_sheets/sheet.rb +16 -9
- 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: 8784a8e9d487e921e6e95a133cda24925ef38e946ba2e94dde9049c9db3020ac
|
4
|
+
data.tar.gz: 4b5f5d2a0c622610c942e5e59a36f4d53f571ec8e631bc531ca826e2ecfe9427
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: db65c288119d44c821aca69ecf8d27a6c64400f7c4d25578661a22b3a8c2cd7dc3e402cfa4de5f3e46a49becd4b5d4b1be93438961a09aca022f08eb5bc9caf0
|
7
|
+
data.tar.gz: ec592fbaba4b61fc98302cd5a904cdce435b7085d0db451aa2c877f43a0049d682d8c17abceb348e514e8b77915e81b54d650eebc8b4322bbb0daccd9134665d
|
data/README.md
CHANGED
@@ -57,7 +57,7 @@ session = GoogleSheets::Session.start_session(
|
|
57
57
|
|
58
58
|
<h3 id='getting-started'>Getting Started</h3>
|
59
59
|
|
60
|
-
Once you're authorized, you can
|
60
|
+
Once you're authorized, you can create, read, update and delete sheets within a spreadsheet.
|
61
61
|
|
62
62
|
``` ruby
|
63
63
|
session = GoogleSheets::Session.start_session(
|
@@ -105,10 +105,14 @@ sheet1_json = sheet1.to_json
|
|
105
105
|
|
106
106
|
sheet1_json[0][:first] = 'bobby'
|
107
107
|
|
108
|
-
|
108
|
+
# Sheet#set_values_from_json is the inverse of to_json
|
109
|
+
# accepts an array of hashes, turns it back to csv format
|
110
|
+
# sets that as the sheet's values
|
111
|
+
sheet1.set_values_from_json(sheet1_json)
|
109
112
|
|
110
|
-
|
113
|
+
sheet1.values[1][0] # => 'bobby'
|
111
114
|
|
115
|
+
# save the spreadsheet's values
|
112
116
|
sheet1.save!
|
113
117
|
```
|
114
118
|
|
data/lib/google_sheets/sheet.rb
CHANGED
@@ -69,15 +69,7 @@ module GoogleSheets
|
|
69
69
|
#
|
70
70
|
# @return Array(Array)
|
71
71
|
def set_values_from_json json
|
72
|
-
|
73
|
-
|
74
|
-
csv = json.map do |hash|
|
75
|
-
top_row.map {|c| hash[c] }
|
76
|
-
end
|
77
|
-
|
78
|
-
csv.unshift top_row.map &:to_s
|
79
|
-
|
80
|
-
self.values = csv
|
72
|
+
self.values = Sheet.json_to_csv(json)
|
81
73
|
end
|
82
74
|
|
83
75
|
# Save the current `values` to the spreadsheet
|
@@ -90,6 +82,21 @@ module GoogleSheets
|
|
90
82
|
@service.update_spreadsheet_value(@spreadsheet.key, @title, value_range_object, value_input_option: 'RAW')
|
91
83
|
end
|
92
84
|
|
85
|
+
# Helper method for converting an array of hashes to csv-style values
|
86
|
+
# @param an array of hashes to be converted to csv-style nested array format
|
87
|
+
# @return [Array(Array)] csv style nested array
|
88
|
+
def self.json_to_csv json
|
89
|
+
top_row = json.map(&:keys).flatten.uniq
|
90
|
+
|
91
|
+
csv = json.map do |hash|
|
92
|
+
top_row.map {|c| hash[c] }
|
93
|
+
end
|
94
|
+
|
95
|
+
csv.unshift top_row.map &:to_s
|
96
|
+
|
97
|
+
csv
|
98
|
+
end
|
99
|
+
|
93
100
|
private
|
94
101
|
|
95
102
|
def hashify_data csv, top_row
|