excel-esv 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 +15 -9
- data/lib/esv/version.rb +1 -1
- data/lib/esv.rb +6 -0
- data/spec/esv_spec.rb +21 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 497175de91508cfae61cef9c418d19368fe40db9
|
4
|
+
data.tar.gz: c9b36a3fb2bb4ed670eff42935cd0a49f169bc40
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5f50059616af9ab743c24c7e9baf3b13c32d1b6203c8c300a4bced7d16e389438889f446af0c1f71f7295afd55d05c609ee46527f7307ced5cc750521044e444
|
7
|
+
data.tar.gz: 30e21e0646c7cf9f34850656069bdae2bc27b6db433ff665e77ef3f19947b316a7cd0a2cda74f21b1df4ab57686e8ea972920b911830817a20d711328d1ad85a
|
data/README.md
CHANGED
@@ -11,7 +11,9 @@ CSVs can be difficult to open correctly, e.g. in Excel on Mac.
|
|
11
11
|
|
12
12
|
### Generate
|
13
13
|
|
14
|
-
```
|
14
|
+
``` ruby
|
15
|
+
require "esv"
|
16
|
+
|
15
17
|
data = ESV.generate do |esv|
|
16
18
|
esv << [ "Name", "Dogs", "Cats" ]
|
17
19
|
esv << [ "Victor", 1, 4 ]
|
@@ -22,24 +24,28 @@ File.write("/tmp/test.xls", data)
|
|
22
24
|
|
23
25
|
### Parse
|
24
26
|
|
25
|
-
```
|
26
|
-
|
27
|
+
``` ruby
|
28
|
+
require "esv"
|
29
|
+
|
30
|
+
data = File.read("/tmp/test.xls")
|
27
31
|
output = ESV.parse(data) # => [ [ "Name", "Dogs", … ], … ]
|
28
32
|
```
|
29
33
|
|
34
|
+
This assumes a file with a single worksheet and will raise otherwise.
|
35
|
+
|
30
36
|
### Generate in Ruby on Rails
|
31
37
|
|
32
38
|
In `config/initializers/mime_types.rb`:
|
33
39
|
|
34
|
-
```
|
40
|
+
``` ruby
|
35
41
|
Mime::Type.register ESV::MIME_TYPE, "xls"
|
36
42
|
```
|
37
43
|
|
38
44
|
As a model or whatever you prefer:
|
39
45
|
|
40
|
-
```
|
46
|
+
``` ruby
|
41
47
|
class MyExcelDocument
|
42
|
-
def generate(name)
|
48
|
+
def self.generate(name)
|
43
49
|
ESV.generate { |esv| esv << [ "Hello #{name}" ] }
|
44
50
|
end
|
45
51
|
end
|
@@ -47,12 +53,12 @@ end
|
|
47
53
|
|
48
54
|
Controller:
|
49
55
|
|
50
|
-
```
|
56
|
+
``` ruby
|
51
57
|
class MyController < ApplicationController
|
52
58
|
include ESV::RailsController # for send_excel
|
53
59
|
|
54
60
|
def show
|
55
|
-
data = MyExcelDocument.
|
61
|
+
data = MyExcelDocument.generate("Rails")
|
56
62
|
send_excel(data)
|
57
63
|
end
|
58
64
|
|
@@ -70,7 +76,7 @@ end
|
|
70
76
|
|
71
77
|
Add this line to your application's Gemfile:
|
72
78
|
|
73
|
-
```ruby
|
79
|
+
``` ruby
|
74
80
|
gem 'excel-esv'
|
75
81
|
```
|
76
82
|
|
data/lib/esv/version.rb
CHANGED
data/lib/esv.rb
CHANGED
@@ -13,6 +13,12 @@ module ESV
|
|
13
13
|
def self.parse(data)
|
14
14
|
fake_file = StringIO.new(data)
|
15
15
|
book = Spreadsheet.open(fake_file)
|
16
|
+
|
17
|
+
# We could support multiple worksheets, but let's not until we actually need it.
|
18
|
+
# Until then, we prefer raising to silently ignoring worksheets.
|
19
|
+
worksheet_count = book.worksheets.length
|
20
|
+
raise "Expected 1 worksheet, found #{worksheet_count}." if worksheet_count > 1
|
21
|
+
|
16
22
|
book.worksheet(0).to_a
|
17
23
|
end
|
18
24
|
end
|
data/spec/esv_spec.rb
CHANGED
@@ -15,3 +15,24 @@ describe ESV, ".generate and .parse" do
|
|
15
15
|
]
|
16
16
|
end
|
17
17
|
end
|
18
|
+
|
19
|
+
describe ESV, ".parse" do
|
20
|
+
it "raises if there's more than one worksheet" do
|
21
|
+
expect {
|
22
|
+
ESV.parse(excel_file_with_two_worksheets)
|
23
|
+
}.to raise_error(/Expected 1 worksheet, found 2/)
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def excel_file_with_two_worksheets
|
29
|
+
book = Spreadsheet::Workbook.new
|
30
|
+
book.create_worksheet
|
31
|
+
book.create_worksheet
|
32
|
+
|
33
|
+
data = ""
|
34
|
+
fake_file = StringIO.new(data)
|
35
|
+
book.write(fake_file)
|
36
|
+
data
|
37
|
+
end
|
38
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: excel-esv
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Henrik Nyh
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-07-
|
11
|
+
date: 2015-07-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: spreadsheet
|