google_sheets 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +21 -2
- data/lib/google_sheets/sheet.rb +28 -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: a0513b5f7ab0fcb68e08c1d80130fc6462c2905553ade3289c77c326c2f5fcc1
|
4
|
+
data.tar.gz: e50f4db63195185d320d6106d5a82fcaf77585f161284099d8d93b8ac2ab53c2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 160336d9e34ebe9e3c4c00c5425cf7a170305d451d3d942d0593f984a481449354542aba2d4243f2e9fd6d0f499f60f1a27fc4858d27c4390923302981b28d90
|
7
|
+
data.tar.gz: 60eb1a779b362f57ab79d2424485118e502d39331339e90d1ed222a4edd34c45a6f9a3f38d3cbc6f54749b3bcd12423b008cfc76b075efdbd307af8045fd5370
|
data/README.md
CHANGED
@@ -70,7 +70,7 @@ spreadsheet.sheets.map &:title
|
|
70
70
|
sheet1 = spreadsheet.sheets[0]
|
71
71
|
|
72
72
|
sheet1.values
|
73
|
-
# => [['
|
73
|
+
# => [['first, 'last', 'age'], ['bob', 'jones', '92'], ['steve', 'johnson', '22']]
|
74
74
|
|
75
75
|
values = [[1,2],[3,4]]
|
76
76
|
|
@@ -80,10 +80,29 @@ spreadsheet.sheets.map &:title
|
|
80
80
|
# => ['Sheet1', 'yoyo1', 'what']
|
81
81
|
|
82
82
|
# this will delete the sheet!!!
|
83
|
-
|
83
|
+
sheet2.delete!
|
84
84
|
|
85
85
|
spreadsheet.sheets.map &:title
|
86
86
|
# => ['Sheet1', 'yoyo1']
|
87
|
+
|
88
|
+
# Sheet#to_json converts the csv to a json array
|
89
|
+
# it uses the top row as the keys
|
90
|
+
# fyi, this will also convert the values to UTF-8
|
91
|
+
# sometimes gsheets values come in as ASCII
|
92
|
+
sheet1.to_json
|
93
|
+
# =>
|
94
|
+
# [
|
95
|
+
# {
|
96
|
+
# first: 'bob',
|
97
|
+
# last: 'jones',
|
98
|
+
# age: '92'
|
99
|
+
# },
|
100
|
+
# {
|
101
|
+
# first: 'steve',
|
102
|
+
# last: 'johnson',
|
103
|
+
# age: '22'
|
104
|
+
# }
|
105
|
+
# ]
|
87
106
|
```
|
88
107
|
|
89
108
|
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
@@ -35,5 +35,33 @@ module GoogleSheets
|
|
35
35
|
|
36
36
|
self
|
37
37
|
end
|
38
|
+
|
39
|
+
def to_json
|
40
|
+
top_row = values[0].map &:to_sym
|
41
|
+
hashify_data(values[1..-1], top_row)
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def hashify_data csv, top_row
|
47
|
+
csv.map do |arr|
|
48
|
+
hash = {}
|
49
|
+
|
50
|
+
top_row.each_with_index do |attr, index|
|
51
|
+
hash[attr.to_sym] = utf8ify(arr[index])
|
52
|
+
end
|
53
|
+
|
54
|
+
hash
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def utf8ify val
|
59
|
+
if val.class == String && val.encoding.to_s != 'UTF-8'
|
60
|
+
val = val.dup.force_encoding('utf-8')
|
61
|
+
end
|
62
|
+
|
63
|
+
val
|
64
|
+
end
|
65
|
+
|
38
66
|
end
|
39
67
|
end
|