workbook 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +22 -0
- data/Rakefile +12 -0
- data/lib/workbook/book.rb +122 -0
- data/lib/workbook/cell.rb +143 -0
- data/lib/workbook/format.rb +35 -0
- data/lib/workbook/modules/raw_objects_storage.rb +31 -0
- data/lib/workbook/modules/table_diff_sort.rb +140 -0
- data/lib/workbook/modules/type_parser.rb +97 -0
- data/lib/workbook/readers/csv_reader.rb +31 -0
- data/lib/workbook/readers/txt_reader.rb +17 -0
- data/lib/workbook/readers/xls_reader.rb +161 -0
- data/lib/workbook/row.rb +101 -0
- data/lib/workbook/sheet.rb +22 -0
- data/lib/workbook/table.rb +67 -0
- data/lib/workbook/template.rb +52 -0
- data/lib/workbook/writers/csv_table_writer.rb +23 -0
- data/lib/workbook/writers/xls_writer.rb +172 -0
- data/lib/workbook.rb +14 -0
- data/readme.markdown +99 -0
- data/test/artifacts/book_with_tabs_and_colours.xls +0 -0
- data/test/artifacts/complex_types.xls +0 -0
- data/test/artifacts/medewerkers.xls +0 -0
- data/test/artifacts/simple_csv.csv +4 -0
- data/test/artifacts/simple_excel_csv.csv +6 -0
- data/test/artifacts/simple_sheet.xls +0 -0
- data/test/artifacts/xls_with_txt_extension.txt +0 -0
- data/test/helper.rb +3 -0
- data/test/test_book.rb +56 -0
- data/test/test_cell.rb +82 -0
- data/test/test_format.rb +59 -0
- data/test/test_functional.rb +30 -0
- data/test/test_modules_table_diff_sort.rb +120 -0
- data/test/test_modules_type_parser.rb +53 -0
- data/test/test_readers_csv_reader.rb +37 -0
- data/test/test_readers_txt_reader.rb +49 -0
- data/test/test_readers_xls_reader.rb +26 -0
- data/test/test_row.rb +114 -0
- data/test/test_sheet.rb +26 -0
- data/test/test_table.rb +43 -0
- data/test/test_template.rb +24 -0
- data/test/test_writers_xls_writer.rb +37 -0
- data/workbook.gemspec +26 -0
- metadata +165 -0
data/test/test_row.rb
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'helper')
|
2
|
+
|
3
|
+
class TestRow < Test::Unit::TestCase
|
4
|
+
|
5
|
+
|
6
|
+
def test_init
|
7
|
+
t = Workbook::Table.new
|
8
|
+
r = Workbook::Row.new([1,2,3],t)
|
9
|
+
c1 = Workbook::Cell.new(1)
|
10
|
+
c2 = Workbook::Cell.new(2)
|
11
|
+
c3 = Workbook::Cell.new(3)
|
12
|
+
assert_equal([c1,c2,c3].collect{|c| c.value},r.collect{|c| c.value})
|
13
|
+
|
14
|
+
#t = Workbook::Table.new
|
15
|
+
c1 = Workbook::Cell.new(1)
|
16
|
+
c2 = Workbook::Cell.new(2)
|
17
|
+
c3 = Workbook::Cell.new(3)
|
18
|
+
|
19
|
+
r = Workbook::Row.new([c1,c2,c3])
|
20
|
+
|
21
|
+
assert_equal([c1,c2,c3],r)
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_table=
|
26
|
+
r = Workbook::Row.new
|
27
|
+
assert_raise(ArgumentError, 'table should be a Workbook::Table (you passed a String)') { r.table = "asdf" }
|
28
|
+
r.table = nil
|
29
|
+
assert_equal(r.table, nil)
|
30
|
+
r = Workbook::Row.new
|
31
|
+
|
32
|
+
t = Workbook::Table.new
|
33
|
+
r.table = t
|
34
|
+
assert_equal(r.table, t)
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_header?
|
38
|
+
t = Workbook::Table.new
|
39
|
+
r1 = Workbook::Row.new
|
40
|
+
r1.table = t
|
41
|
+
assert_equal(true, r1.header?)
|
42
|
+
r2 = Workbook::Row.new
|
43
|
+
r2.table = t
|
44
|
+
assert_equal(false, r2.header?)
|
45
|
+
r2 = Workbook::Row.new
|
46
|
+
r2.table = t
|
47
|
+
assert_equal(false, r2.header?)
|
48
|
+
assert_equal(true, t.first.header?)
|
49
|
+
assert_equal(r1, t.first)
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_to_symbols
|
53
|
+
r1 = Workbook::Row.new ["test", "asdf-asd", "asdf - asdf", "asdf2"]
|
54
|
+
assert_equal([:test, :asdfasd, :asdf_asdf, :asdf2], r1.to_symbols)
|
55
|
+
r1 = Workbook::Row.new ["inït", "è-éë"]
|
56
|
+
assert_equal([:init, :eee], r1.to_symbols)
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_to_hash
|
61
|
+
r1 = Workbook::Row.new ["test", "asdf-asd", "asdf - asdf", "asdf2"]
|
62
|
+
assert_raise(NoMethodError, 'undefined method `header\' for nil:NilClass') { r1.to_hash }
|
63
|
+
|
64
|
+
t = Workbook::Table.new
|
65
|
+
r1 = Workbook::Row.new ["test", "asdf-asd"]
|
66
|
+
r1.table = t
|
67
|
+
expected = {:test=>Workbook::Cell.new("test"), :asdfasd=>Workbook::Cell.new("asdf-asd")}
|
68
|
+
assert_equal(expected, r1.to_hash)
|
69
|
+
date = DateTime.now
|
70
|
+
r2 = Workbook::Row.new [2, date]
|
71
|
+
r2.table = t
|
72
|
+
expected = {:test=>Workbook::Cell.new(2), :asdfasd=>Workbook::Cell.new(date)}
|
73
|
+
assert_equal(expected, r2.to_hash)
|
74
|
+
assert_equal(date, r2[:asdfasd].value)
|
75
|
+
assert_equal(date, r2[1].value)
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_compare
|
79
|
+
r1 = Workbook::Row.new ["test", "asdf-asd"]
|
80
|
+
r2 = Workbook::Row.new [nil, "asdf-asd"]
|
81
|
+
assert_equal(-1,r1<=>r2)
|
82
|
+
r1 = Workbook::Row.new [1, "asdf-asd"]
|
83
|
+
r2 = Workbook::Row.new ["test", "asdf-asd"]
|
84
|
+
assert_equal(-1,r1<=>r2)
|
85
|
+
r1 = Workbook::Row.new [nil, "asdf-asd"]
|
86
|
+
r2 = Workbook::Row.new [Time.now, "asdf-asd"]
|
87
|
+
assert_equal(1,r1<=>r2)
|
88
|
+
r1 = Workbook::Row.new [2, 3]
|
89
|
+
r2 = Workbook::Row.new [2, nil]
|
90
|
+
assert_equal(-1,r1<=>r2)
|
91
|
+
r1 = Workbook::Row.new [3, 0]
|
92
|
+
r2 = Workbook::Row.new [2, 100000]
|
93
|
+
assert_equal(1,r1<=>r2)
|
94
|
+
r1 = Workbook::Row.new [-10, 3]
|
95
|
+
r2 = Workbook::Row.new [nil, 5]
|
96
|
+
assert_equal(-1,r1<=>r2)
|
97
|
+
|
98
|
+
end
|
99
|
+
|
100
|
+
def test_find_cells_by_background_color
|
101
|
+
r = Workbook::Row.new ["test", "asdf-asd"]
|
102
|
+
assert_equal([],r.find_cells_by_background_color)
|
103
|
+
f = Workbook::Format.new
|
104
|
+
f[:background_color]='#ff00ff'
|
105
|
+
r.first.format = f
|
106
|
+
assert_equal([:test],r.find_cells_by_background_color)
|
107
|
+
assert_equal([],r.find_cells_by_background_color('#ff0000'))
|
108
|
+
end
|
109
|
+
|
110
|
+
def test_to_s
|
111
|
+
r1 = Workbook::Row.new ["test", "asdf-asd"]
|
112
|
+
assert_equal("test,asdf-asd\n",r1.to_csv)
|
113
|
+
end
|
114
|
+
end
|
data/test/test_sheet.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'helper')
|
2
|
+
|
3
|
+
class TestWorkbook < Test::Unit::TestCase
|
4
|
+
def test_init
|
5
|
+
w = Workbook::Sheet.new nil
|
6
|
+
assert_equal([[]],w)
|
7
|
+
assert_equal(w.count,1)
|
8
|
+
w = Workbook::Sheet.new
|
9
|
+
assert_equal([Workbook::Table.new],w)
|
10
|
+
assert_equal(w.count,1)
|
11
|
+
t = Workbook::Table.new []
|
12
|
+
w = Workbook::Sheet.new t
|
13
|
+
assert_equal([t],w)
|
14
|
+
assert_equal(w.count,1)
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_table
|
18
|
+
w = Workbook::Sheet.new nil
|
19
|
+
assert_equal([],w.table)
|
20
|
+
t = Workbook::Table.new []
|
21
|
+
w = Workbook::Sheet.new t
|
22
|
+
assert_equal(w.table,t)
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
data/test/test_table.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'helper')
|
2
|
+
|
3
|
+
class TestTable< Test::Unit::TestCase
|
4
|
+
def test_initialize
|
5
|
+
t = Workbook::Table.new
|
6
|
+
assert_equal(t,[])
|
7
|
+
c = Workbook::Cell.new("celllll")
|
8
|
+
t = Workbook::Table.new [[c]]
|
9
|
+
|
10
|
+
assert_equal([[c]],t)
|
11
|
+
end
|
12
|
+
def test_header
|
13
|
+
t = Workbook::Table.new
|
14
|
+
assert_equal(t.header,nil)
|
15
|
+
t = Workbook::Table.new [[1]]
|
16
|
+
assert_equal(t.header,[1])
|
17
|
+
assert_equal(t.header.class,Workbook::Row)
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_new_row
|
21
|
+
t = Workbook::Table.new
|
22
|
+
assert_equal(t.count, 0)
|
23
|
+
|
24
|
+
r = t.new_row [1,2,3,4]
|
25
|
+
assert_equal(r, [1,2,3,4])
|
26
|
+
assert_equal(r.class, Workbook::Row)
|
27
|
+
assert_equal(1,t.count)
|
28
|
+
|
29
|
+
r = t.new_row
|
30
|
+
assert_equal(r.empty?, true)
|
31
|
+
assert_equal(r.class, Workbook::Row)
|
32
|
+
|
33
|
+
assert_equal(2, t.count)
|
34
|
+
assert_equal(r, t.last)
|
35
|
+
|
36
|
+
r << 2
|
37
|
+
|
38
|
+
assert_equal(t.last.empty?, false)
|
39
|
+
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'helper')
|
2
|
+
|
3
|
+
class TestTemplate < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_initalize
|
6
|
+
t = Workbook::Template.new
|
7
|
+
assert_equal(true,(t.methods.include?(:add_raw) or t.methods.include?("add_raw")))
|
8
|
+
assert_equal(true,(t.methods.include?(:has_raw_for?) or t.methods.include?("has_raw_for?")))
|
9
|
+
assert_equal(true,(t.methods.include?(:raws) or t.methods.include?("raws")))
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_add_raw_and_has_raw_for
|
13
|
+
t = Workbook::Template.new
|
14
|
+
t.add_raw "asdfsadf"
|
15
|
+
assert_equal(false,t.has_raw_for?(Integer))
|
16
|
+
assert_equal(true,t.has_raw_for?(String))
|
17
|
+
end
|
18
|
+
def test_raws
|
19
|
+
t = Workbook::Template.new
|
20
|
+
t.add_raw "asdfsadf"
|
21
|
+
assert_equal({String=>"asdfsadf"}, t.raws)
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'helper')
|
2
|
+
|
3
|
+
module Writers
|
4
|
+
class TestXlsWriter < Test::Unit::TestCase
|
5
|
+
def test_to_xls
|
6
|
+
b = Workbook::Book.new [['a','b','c'],[1,2,3],[3,2,3]]
|
7
|
+
raw = Spreadsheet.open('test/artifacts/simple_sheet.xls')
|
8
|
+
t = Workbook::Template.new
|
9
|
+
t.add_raw raw
|
10
|
+
b.template = t
|
11
|
+
assert_equal(true, b.to_xls.is_a?(Spreadsheet::Workbook))
|
12
|
+
|
13
|
+
assert_equal('untitled document.xls', b.write_to_xls)
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_roundtrip
|
17
|
+
b = Workbook::Book.open('test/artifacts/simple_sheet.xls')
|
18
|
+
assert_equal(3.85546875,b.sheet.table.first[:vestiging_id].format[:width])
|
19
|
+
filename = b.write_to_xls
|
20
|
+
b = Workbook::Book.open filename
|
21
|
+
assert_equal(3.85546875,b.sheet.table.first[:vestiging_id].format[:width])
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_init_spreadsheet_template
|
26
|
+
b = Workbook::Book.new
|
27
|
+
b.init_spreadsheet_template
|
28
|
+
assert_equal(Spreadsheet::Workbook,b.xls_template.class)
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_xls_sheet
|
32
|
+
b = Workbook::Book.new
|
33
|
+
b.init_spreadsheet_template
|
34
|
+
assert_equal(Spreadsheet::Worksheet,b.xls_sheet(100).class)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/workbook.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "workbook"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = 'workbook'
|
7
|
+
s.rubyforge_project = 'workbook'
|
8
|
+
s.version = '0.1.1'
|
9
|
+
s.date = '2012-09-11'
|
10
|
+
s.summary = "Workbook is a datastructure to contain books of tables (an anlogy used in e.g. Excel)"
|
11
|
+
s.description = "Workbook contains workbooks, as in a table, contains rows, contains cells, reads/writes excels and csv's and tab separated, and offers basic diffing and sorting capabilities."
|
12
|
+
s.authors = ["Maarten Brouwers"]
|
13
|
+
s.add_dependency('spreadsheet', '>= 0.6.8')
|
14
|
+
s.add_dependency('fastercsv')
|
15
|
+
s.add_dependency("rchardet", "~> 1.3")
|
16
|
+
s.platform = Gem::Platform::RUBY
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
s.email = ['gem@murb.nl']
|
22
|
+
s.homepage =
|
23
|
+
'http://murb.nl/blog?tags=workbook'
|
24
|
+
end
|
25
|
+
|
26
|
+
|
metadata
ADDED
@@ -0,0 +1,165 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: workbook
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 1
|
9
|
+
version: 0.1.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Maarten Brouwers
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2012-09-11 00:00:00 +02:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: spreadsheet
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
- 6
|
30
|
+
- 8
|
31
|
+
version: 0.6.8
|
32
|
+
type: :runtime
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: fastercsv
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 0
|
43
|
+
version: "0"
|
44
|
+
type: :runtime
|
45
|
+
version_requirements: *id002
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rchardet
|
48
|
+
prerelease: false
|
49
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
segments:
|
54
|
+
- 1
|
55
|
+
- 3
|
56
|
+
version: "1.3"
|
57
|
+
type: :runtime
|
58
|
+
version_requirements: *id003
|
59
|
+
description: Workbook contains workbooks, as in a table, contains rows, contains cells, reads/writes excels and csv's and tab separated, and offers basic diffing and sorting capabilities.
|
60
|
+
email:
|
61
|
+
- gem@murb.nl
|
62
|
+
executables: []
|
63
|
+
|
64
|
+
extensions: []
|
65
|
+
|
66
|
+
extra_rdoc_files: []
|
67
|
+
|
68
|
+
files:
|
69
|
+
- .gitignore
|
70
|
+
- Gemfile
|
71
|
+
- Gemfile.lock
|
72
|
+
- Rakefile
|
73
|
+
- lib/workbook.rb
|
74
|
+
- lib/workbook/book.rb
|
75
|
+
- lib/workbook/cell.rb
|
76
|
+
- lib/workbook/format.rb
|
77
|
+
- lib/workbook/modules/raw_objects_storage.rb
|
78
|
+
- lib/workbook/modules/table_diff_sort.rb
|
79
|
+
- lib/workbook/modules/type_parser.rb
|
80
|
+
- lib/workbook/readers/csv_reader.rb
|
81
|
+
- lib/workbook/readers/txt_reader.rb
|
82
|
+
- lib/workbook/readers/xls_reader.rb
|
83
|
+
- lib/workbook/row.rb
|
84
|
+
- lib/workbook/sheet.rb
|
85
|
+
- lib/workbook/table.rb
|
86
|
+
- lib/workbook/template.rb
|
87
|
+
- lib/workbook/writers/csv_table_writer.rb
|
88
|
+
- lib/workbook/writers/xls_writer.rb
|
89
|
+
- readme.markdown
|
90
|
+
- test/artifacts/book_with_tabs_and_colours.xls
|
91
|
+
- test/artifacts/complex_types.xls
|
92
|
+
- test/artifacts/medewerkers.xls
|
93
|
+
- test/artifacts/simple_csv.csv
|
94
|
+
- test/artifacts/simple_excel_csv.csv
|
95
|
+
- test/artifacts/simple_sheet.xls
|
96
|
+
- test/artifacts/xls_with_txt_extension.txt
|
97
|
+
- test/helper.rb
|
98
|
+
- test/test_book.rb
|
99
|
+
- test/test_cell.rb
|
100
|
+
- test/test_format.rb
|
101
|
+
- test/test_functional.rb
|
102
|
+
- test/test_modules_table_diff_sort.rb
|
103
|
+
- test/test_modules_type_parser.rb
|
104
|
+
- test/test_readers_csv_reader.rb
|
105
|
+
- test/test_readers_txt_reader.rb
|
106
|
+
- test/test_readers_xls_reader.rb
|
107
|
+
- test/test_row.rb
|
108
|
+
- test/test_sheet.rb
|
109
|
+
- test/test_table.rb
|
110
|
+
- test/test_template.rb
|
111
|
+
- test/test_writers_xls_writer.rb
|
112
|
+
- workbook.gemspec
|
113
|
+
has_rdoc: true
|
114
|
+
homepage: http://murb.nl/blog?tags=workbook
|
115
|
+
licenses: []
|
116
|
+
|
117
|
+
post_install_message:
|
118
|
+
rdoc_options: []
|
119
|
+
|
120
|
+
require_paths:
|
121
|
+
- lib
|
122
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
123
|
+
requirements:
|
124
|
+
- - ">="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
segments:
|
127
|
+
- 0
|
128
|
+
version: "0"
|
129
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
130
|
+
requirements:
|
131
|
+
- - ">="
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
segments:
|
134
|
+
- 0
|
135
|
+
version: "0"
|
136
|
+
requirements: []
|
137
|
+
|
138
|
+
rubyforge_project: workbook
|
139
|
+
rubygems_version: 1.3.6
|
140
|
+
signing_key:
|
141
|
+
specification_version: 3
|
142
|
+
summary: Workbook is a datastructure to contain books of tables (an anlogy used in e.g. Excel)
|
143
|
+
test_files:
|
144
|
+
- test/artifacts/book_with_tabs_and_colours.xls
|
145
|
+
- test/artifacts/complex_types.xls
|
146
|
+
- test/artifacts/medewerkers.xls
|
147
|
+
- test/artifacts/simple_csv.csv
|
148
|
+
- test/artifacts/simple_excel_csv.csv
|
149
|
+
- test/artifacts/simple_sheet.xls
|
150
|
+
- test/artifacts/xls_with_txt_extension.txt
|
151
|
+
- test/helper.rb
|
152
|
+
- test/test_book.rb
|
153
|
+
- test/test_cell.rb
|
154
|
+
- test/test_format.rb
|
155
|
+
- test/test_functional.rb
|
156
|
+
- test/test_modules_table_diff_sort.rb
|
157
|
+
- test/test_modules_type_parser.rb
|
158
|
+
- test/test_readers_csv_reader.rb
|
159
|
+
- test/test_readers_txt_reader.rb
|
160
|
+
- test/test_readers_xls_reader.rb
|
161
|
+
- test/test_row.rb
|
162
|
+
- test/test_sheet.rb
|
163
|
+
- test/test_table.rb
|
164
|
+
- test/test_template.rb
|
165
|
+
- test/test_writers_xls_writer.rb
|