google_sheets 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +12 -5
- data/lib/google_sheets/sheet.rb +33 -1
- 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: 5b4000bd3f32057853f95287ca9056849a102150d00b391559db283d8d37f2e2
|
4
|
+
data.tar.gz: 3810f53c188bef420dcf480d2cc05c66554fdf5f9e01fec5186127c1193a759d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 79c4d49b52421cbd15ad255049231f9496fb78d5754fb18df193822507bf98ebfd28bb873afeb2830f12adbc36f11f50ad5cb5868c82014ca67a0344724b6182
|
7
|
+
data.tar.gz: b6f4346231bb72a86f111689a0d04c9e2e3c2811058fbdbe0011ca12fd609ea74cd045e93999635f7553cc076ff59ec99b9a0b4d375cf9225683a6fc76166e94
|
data/README.md
CHANGED
@@ -1,10 +1,13 @@
|
|
1
|
+
[![Yard Docs](http://img.shields.io/badge/yard-docs-blue.svg)](http://www.rubydoc.info/github/shmay/google_sheets/)
|
2
|
+
|
1
3
|
[google-drive-ruby](https://github.com/gimite/google-drive-ruby), a great gem, doesn't support Google's v4 Drive API. As a result, I seem to encounter rate limiting errors fairly quickly.
|
2
4
|
|
3
5
|
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
6
|
|
5
|
-
* [Installing](#
|
7
|
+
* [Installing](#installing)
|
6
8
|
* [Getting started](#getting-started)
|
7
9
|
* [GitHub](http://github.com/shmay/google_sheets)
|
10
|
+
* [API docs](https://www.rubydoc.info/github/shmay/google_sheets/master)
|
8
11
|
|
9
12
|
<h3 id='installing'>Installing</h3>
|
10
13
|
|
@@ -85,10 +88,7 @@ spreadsheet.sheets.map &:title
|
|
85
88
|
|
86
89
|
# Sheet#to_json converts the csv to a json array
|
87
90
|
# it uses the top row as the keys
|
88
|
-
|
89
|
-
# sometimes gsheets values come in as ASCII
|
90
|
-
|
91
|
-
sheet1.to_json
|
91
|
+
sheet1_json = sheet1.to_json
|
92
92
|
# =>
|
93
93
|
# [
|
94
94
|
# {
|
@@ -103,6 +103,13 @@ sheet1.to_json
|
|
103
103
|
# }
|
104
104
|
# ]
|
105
105
|
|
106
|
+
sheet1_json[0][:first] = 'bobby'
|
107
|
+
|
108
|
+
sheet1.set_values_from_json sheet1_json
|
109
|
+
|
110
|
+
expect(sheet1.values[1][0]).to eq('bobby')
|
111
|
+
|
112
|
+
sheet1.save!
|
106
113
|
```
|
107
114
|
|
108
115
|
Or just look at [the spec](spec/test_all_the_things_spec.rb) to see it in action.
|
data/lib/google_sheets/sheet.rb
CHANGED
@@ -58,6 +58,38 @@ module GoogleSheets
|
|
58
58
|
hashify_data(values[1..-1], top_row)
|
59
59
|
end
|
60
60
|
|
61
|
+
# Converts an array of hashes back to csv format.
|
62
|
+
# So the opposite of to_json
|
63
|
+
#
|
64
|
+
# EG:
|
65
|
+
# ```
|
66
|
+
# sheet.set_values_from_json([{name: 'john', age: '20'}])
|
67
|
+
# sheet.values # => [['name', 'age'], ['john', '20']]
|
68
|
+
# ```
|
69
|
+
#
|
70
|
+
# @return Array(Array)
|
71
|
+
def set_values_from_json json
|
72
|
+
top_row = json.map(&:keys).flatten.uniq
|
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
|
81
|
+
end
|
82
|
+
|
83
|
+
# Save the current `values` to the spreadsheet
|
84
|
+
def save!
|
85
|
+
value_range_object = {
|
86
|
+
majorDimension: 'ROWS',
|
87
|
+
values: values
|
88
|
+
}
|
89
|
+
|
90
|
+
@service.update_spreadsheet_value(@spreadsheet.key, @title, value_range_object, value_input_option: 'RAW')
|
91
|
+
end
|
92
|
+
|
61
93
|
private
|
62
94
|
|
63
95
|
def hashify_data csv, top_row
|
@@ -65,7 +97,7 @@ module GoogleSheets
|
|
65
97
|
hash = {}
|
66
98
|
|
67
99
|
top_row.each_with_index do |attr, index|
|
68
|
-
hash[attr.to_sym] = utf8ify(arr[index])
|
100
|
+
hash[attr.to_sym] = arr[index] # utf8ify(arr[index])
|
69
101
|
end
|
70
102
|
|
71
103
|
hash
|