workbook 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. data/.gitignore +4 -0
  2. data/Gemfile +4 -0
  3. data/Gemfile.lock +22 -0
  4. data/Rakefile +12 -0
  5. data/lib/workbook/book.rb +122 -0
  6. data/lib/workbook/cell.rb +143 -0
  7. data/lib/workbook/format.rb +35 -0
  8. data/lib/workbook/modules/raw_objects_storage.rb +31 -0
  9. data/lib/workbook/modules/table_diff_sort.rb +140 -0
  10. data/lib/workbook/modules/type_parser.rb +97 -0
  11. data/lib/workbook/readers/csv_reader.rb +31 -0
  12. data/lib/workbook/readers/txt_reader.rb +17 -0
  13. data/lib/workbook/readers/xls_reader.rb +161 -0
  14. data/lib/workbook/row.rb +101 -0
  15. data/lib/workbook/sheet.rb +22 -0
  16. data/lib/workbook/table.rb +67 -0
  17. data/lib/workbook/template.rb +52 -0
  18. data/lib/workbook/writers/csv_table_writer.rb +23 -0
  19. data/lib/workbook/writers/xls_writer.rb +172 -0
  20. data/lib/workbook.rb +14 -0
  21. data/readme.markdown +99 -0
  22. data/test/artifacts/book_with_tabs_and_colours.xls +0 -0
  23. data/test/artifacts/complex_types.xls +0 -0
  24. data/test/artifacts/medewerkers.xls +0 -0
  25. data/test/artifacts/simple_csv.csv +4 -0
  26. data/test/artifacts/simple_excel_csv.csv +6 -0
  27. data/test/artifacts/simple_sheet.xls +0 -0
  28. data/test/artifacts/xls_with_txt_extension.txt +0 -0
  29. data/test/helper.rb +3 -0
  30. data/test/test_book.rb +56 -0
  31. data/test/test_cell.rb +82 -0
  32. data/test/test_format.rb +59 -0
  33. data/test/test_functional.rb +30 -0
  34. data/test/test_modules_table_diff_sort.rb +120 -0
  35. data/test/test_modules_type_parser.rb +53 -0
  36. data/test/test_readers_csv_reader.rb +37 -0
  37. data/test/test_readers_txt_reader.rb +49 -0
  38. data/test/test_readers_xls_reader.rb +26 -0
  39. data/test/test_row.rb +114 -0
  40. data/test/test_sheet.rb +26 -0
  41. data/test/test_table.rb +43 -0
  42. data/test/test_template.rb +24 -0
  43. data/test/test_writers_xls_writer.rb +37 -0
  44. data/workbook.gemspec +26 -0
  45. metadata +165 -0
data/readme.markdown ADDED
@@ -0,0 +1,99 @@
1
+ # Workbook
2
+
3
+ Goal of this gem is to make working with workbooks (spreadsheets) as programmer friendly as possible. Not reinventing a totally new DSL or all kinds of new methodnames, but just borrowing from known concepts such as hashes and arrays (much like (Faster)CSV does)). Workbook is a gem that mimicks a typical spreadsheet, a bundle of sheets, bundled in a *workbook*. A sheet may contain one or more tables (which might the multi table sheets of Apple Numbers or Excel ranges). Basically:
4
+
5
+ * Book
6
+ * Sheet (one or more)
7
+ * Table (one or more)
8
+
9
+ Subsequently a table consists of:
10
+
11
+ * Table
12
+ * Row (one or more)
13
+ * Cell ( wich has may have a (shared) Format )
14
+
15
+ Book, Sheet, Table and Row inherit from the base Array class, and hence walks and quacks as such. The row is extended with hashlike lookups (`row[:id]`) and writers (`row[:id]=`). Values are converted to ruby native types, and optional parsers can be added to improve recognition.
16
+
17
+ In addition to offering you this plain structure it allows for importing and writing .xls and .csv files (more to come), and includes the utility to easily create an overview of the differences between two tables and read out basic cell-styling properties as css.
18
+
19
+ ## The Basics
20
+
21
+ Simply initialize a simple spreadsheet using:
22
+
23
+ b = Workbook::Book.new
24
+
25
+ or
26
+
27
+ b = Workbook::Book.open filename
28
+
29
+ Calling
30
+
31
+ s = b.sheet
32
+ t = s.table
33
+
34
+ will give you an the first Sheet and Table (if one doesn't exist it is created on the fly).
35
+
36
+ You can initialize with simple 2-d array like this:
37
+
38
+ b = Workbook::Book.new [['a','b'],[1,2],[3,4],[5,6]]
39
+ t = s.sheet.table
40
+
41
+ Subsequently you lookup values in the table like this:
42
+
43
+ t[1][:b]
44
+ # returns <Workbook::Cel @value=2>
45
+
46
+
47
+ <!-- Feature *to implement*:
48
+
49
+ t['A2']
50
+ # returns <Workbook::Cel @value=1>
51
+
52
+ Feature *to implement*, get a single column:
53
+
54
+ t[:b]
55
+ # returns [<Workbook::Cel @value=2>,<Workbook::Cel @value=4>,<Workbook::Cel @value=6>]
56
+
57
+ On my wishlist: In the future I hope to return the cell value directly, without the intermediate Workbook::Cel class in between.
58
+
59
+ -->
60
+
61
+ ## Utilities
62
+
63
+ ### Sorting
64
+
65
+ Sorting leaves the header alone, if it exists, and doesn't complain about comparing strings with dates with floats (Ever found OpenOffice Calc or Excel complainging about its inability to compare integers and strings? We're talking spreadsheet here). When classes are different the following (default) order is used: Numbers, Strings, Dates and Times, Booleans and Nils (empty values).
66
+
67
+ t.sort
68
+
69
+ *To implement*:
70
+
71
+ To some extent, sort_by works, it doesn't, however, adhere to the header settings...
72
+
73
+ t.sort_by {|r| r[:b]}
74
+
75
+ ### Comparing tables
76
+
77
+ Simply call
78
+
79
+ t1.diff t2
80
+
81
+ And a new book with a new sheet/table will be returned containing the differences between the two tables.
82
+
83
+ ## Writing
84
+
85
+ Currently writing is limited to the following formats. Templating support is still limited.
86
+
87
+ b.to_xls # returns a spreadsheet workbook
88
+ b.write_to_xls filename # writes to filename
89
+ t.to_csv # returns a csv-string
90
+
91
+ In case you want to display the table in HTML, some conversion is offered to convert text/background properties to css-entities. Internally the hash storing style elements tries to map to CSS where possible.
92
+
93
+ ## Alternatives
94
+
95
+ The [ruby toolbox lists plenty of alternatives](https://www.ruby-toolbox.com/search?utf8=%E2%9C%93&q=spreadsheet), that just didn't suit my needs.
96
+
97
+ ## License
98
+
99
+ MIT... (c) murb / Maarten Brouwers, 2011-2012
Binary file
Binary file
@@ -0,0 +1,4 @@
1
+ a,b,c,d
2
+ 1,2,3,4
3
+ 5,3,2,1
4
+ "asdf",123,12,2001-02-02
@@ -0,0 +1,6 @@
1
+ a;b;c
2
+ 1-1-2001;23;1
3
+ asdf;23;asd
4
+ 23;asdf;sadf
5
+ 12;23;12-02-2011 12:23
6
+ 12 asdf;"6/12 ovk getekend terugontv.+>acq ter tekening. 13/12 ovk getekend terugontv.+>Fred ter tekening.";6/12
Binary file
data/test/helper.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require File.join(File.dirname(__FILE__), '../lib/workbook')
data/test/test_book.rb ADDED
@@ -0,0 +1,56 @@
1
+ require File.join(File.dirname(__FILE__), 'helper')
2
+
3
+ class TestWorkbook < Test::Unit::TestCase
4
+ def test_sheets
5
+ w = Workbook::Book.new nil
6
+ w.push
7
+ assert_equal(2, w.count)
8
+ end
9
+
10
+ def test_push
11
+ w = Workbook::Book.new nil
12
+ assert_equal([[[]]],w)
13
+ w = Workbook::Book.new
14
+ assert_equal(w.count,1)
15
+
16
+ w.push
17
+ assert_equal(w.first.class,Workbook::Sheet)
18
+ w.push
19
+ assert_equal(w.count,3)
20
+ s = Workbook::Sheet.new
21
+ w.push s
22
+ assert_equal(w.last,s)
23
+ w = Workbook::Book.new
24
+ assert_equal(w.sheet.table.class,Workbook::Table)
25
+ end
26
+
27
+ def test_sheet
28
+ w = Workbook::Book.new nil
29
+ s = Workbook::Sheet.new [Workbook::Row.new(Workbook::Table.new)]
30
+ assert_equal(w.sheet.class,Workbook::Sheet)
31
+ assert_not_equal(w.sheet, s)
32
+ w = Workbook::Book.new s
33
+ assert_equal(w.sheet, s)
34
+ end
35
+
36
+ def test_template
37
+ b = Workbook::Book.new
38
+ raw = "asdf"
39
+ assert_raise(ArgumentError) { b.template = raw }
40
+ raw = Workbook::Template.new
41
+ b.template = raw
42
+
43
+ assert_equal(raw,b.template)
44
+ end
45
+
46
+ def test_parent_child
47
+ b = Workbook::Book.new [[1,2,3],[1,2,3]]
48
+ assert_equal(Workbook::Sheet, b.first.class)
49
+ assert_equal(b,b.first.book)
50
+ assert_equal(Workbook::Table, b.first.table.class)
51
+ assert_equal(b,b.first.table.sheet.book)
52
+ assert_equal(Workbook::Row, b.first.table.header.class)
53
+ assert_equal(b,b.first.table.header.table.sheet.book)
54
+
55
+ end
56
+ end
data/test/test_cell.rb ADDED
@@ -0,0 +1,82 @@
1
+ require File.join(File.dirname(__FILE__), 'helper')
2
+
3
+ class TestCell < Test::Unit::TestCase
4
+
5
+
6
+ def test_init
7
+ w = Workbook::Cell.new nil
8
+ assert_equal(nil,w.value)
9
+ w = Workbook::Cell.new "asdf"
10
+ assert_equal("asdf",w.value)
11
+
12
+ assert_raise(ArgumentError) { w = Workbook::Cell.new :asdf }
13
+
14
+ t = Time.now
15
+ w = Workbook::Cell.new t
16
+ assert_equal(t,w.value)
17
+
18
+ end
19
+
20
+ def test_value
21
+ w = Workbook::Cell.new nil
22
+ assert_equal(nil,w.value)
23
+ w.value = "asdf"
24
+ assert_equal("asdf",w.value)
25
+ w.value = Date.new
26
+ assert_equal(Date.new,w.value)
27
+ end
28
+
29
+ def test_comp
30
+ a = Workbook::Cell.new 1
31
+ b = Workbook::Cell.new 2
32
+ assert_equal(-1, a<=>b)
33
+ a = Workbook::Cell.new "c"
34
+ b = Workbook::Cell.new "bsdf"
35
+ assert_equal(1, a<=>b)
36
+ a = Workbook::Cell.new "c"
37
+ b = Workbook::Cell.new "c"
38
+ assert_equal(0, a<=>b)
39
+ a = Workbook::Cell.new true
40
+ b = Workbook::Cell.new false
41
+ assert_equal(-1, a<=>b)
42
+ a = Workbook::Cell.new "true"
43
+ b = Workbook::Cell.new "false"
44
+ assert_equal(1, a<=>b)
45
+ a = Workbook::Cell.new 1
46
+ b = Workbook::Cell.new "a"
47
+ assert_equal(-1, a<=>b)
48
+ a = Workbook::Cell.new nil
49
+ b = Workbook::Cell.new "a"
50
+ assert_equal(1, a<=>b)
51
+ a = Workbook::Cell.new nil
52
+ b = Workbook::Cell.new nil
53
+ assert_equal(0, a<=>b)
54
+ end
55
+
56
+ def test_cloning_as_expected?
57
+ a = Workbook::Cell.new 1
58
+ a.format = Workbook::Format.new({:value=>1})
59
+ b = a.clone
60
+ b.value = 2
61
+ b.format[:value]=2
62
+ assert_equal(1,a.value)
63
+ assert_equal(2,b.value)
64
+ assert_equal(2,a.format[:value])
65
+ assert_equal(2,b.format[:value])
66
+ end
67
+
68
+ def test_to_sym
69
+ c = Workbook::Cell.new "A - B"
70
+ assert_equal(:a_b, c.to_sym)
71
+ c = Workbook::Cell.new "A-B"
72
+ assert_equal(:ab, c.to_sym)
73
+ c = Workbook::Cell.new "A - c (B123)"
74
+ assert_equal(:a_c_b123, c.to_sym)
75
+
76
+ end
77
+
78
+ def test_nil
79
+ c = Workbook::Cell.new nil
80
+ assert_equal(true,c.nil?)
81
+ end
82
+ end
@@ -0,0 +1,59 @@
1
+ require File.join(File.dirname(__FILE__), 'helper')
2
+
3
+ class TestFormat < Test::Unit::TestCase
4
+
5
+
6
+ def test_initialize
7
+ f = Workbook::Format.new {}
8
+ assert_equal({},f)
9
+ f = Workbook::Format.new({:background=>:red})
10
+ assert_equal({:background=>:red},f)
11
+ f = Workbook::Format.new({:background=>:red})
12
+ deet = Time.now
13
+ assert_equal(false,f.has_raw_for?(Time))
14
+
15
+ f.add_raw deet
16
+ assert_equal(deet,f.raws[Time])
17
+ assert_equal(true,f.has_raw_for?(Time))
18
+
19
+ end
20
+
21
+ def test_merge
22
+ a = Workbook::Format.new({:background=>:red})
23
+ b = Workbook::Format.new({:background=>:yellow, :color=>:green})
24
+ result = b.clone.merge(a)
25
+ assert_equal({:background=>:red, :color=>:green},result)
26
+ assert_equal(true,result.is_a?(Workbook::Format))
27
+ end
28
+
29
+ def test_remove_raw_on_merge
30
+ a = Workbook::Format.new({:background=>:red})
31
+ b = Workbook::Format.new({:background=>:yellow, :color=>:green})
32
+ b.add_raw Date.new
33
+ result = b.clone.merge(a)
34
+ assert_equal({},result.raws)
35
+ end
36
+
37
+ def test_has_background_color?
38
+ a = Workbook::Format.new
39
+ assert_equal(false,a.has_background_color?)
40
+ a = Workbook::Format.new({:background_color=>'#ff0000'})
41
+ assert_equal(true,a.has_background_color?)
42
+ assert_equal(false,a.has_background_color?('#00ff00'))
43
+ a = Workbook::Format.new({:background_color=>'#ffffff'})
44
+ assert_equal(false,a.has_background_color?)
45
+ a = Workbook::Format.new({:background_color=>'#FFFFFf'})
46
+ assert_equal(false,a.has_background_color?)
47
+
48
+
49
+ end
50
+
51
+ def test_to_css
52
+ a = Workbook::Format.new({:background_color=>'#ffffff'})
53
+ assert_equal("background: #ffffff",a.to_css)
54
+ a = Workbook::Format.new({:background_color=>'#fffdff', :color=>:red})
55
+ assert_equal("background: #fffdff; color: red",a.to_css)
56
+
57
+ end
58
+
59
+ end
@@ -0,0 +1,30 @@
1
+ require File.join(File.dirname(__FILE__), 'helper')
2
+
3
+ class TestFunctional < Test::Unit::TestCase
4
+ def test_chapter_Initializing
5
+ b = Workbook::Book.new
6
+ assert_equal(Workbook::Book, b.class)
7
+ s = b.sheet
8
+ assert_equal(Workbook::Sheet, s.class)
9
+ t = s.table
10
+ assert_equal(Workbook::Table, t.class)
11
+ s = b.sheet[0] = Workbook::Sheet.new([['a','b'],[1,2],[3,4],[5,6]])
12
+ assert_equal(Workbook::Sheet, s.class)
13
+
14
+ t = s.table
15
+ assert_equal(Workbook::Table, t.class)
16
+ assert_equal(Workbook::Row, t.first.class)
17
+ assert_equal(Workbook::Cell, t.first.first.class)
18
+ assert_equal(true, t.header.header?)
19
+ assert_equal(false, t.last.header?)
20
+ assert_equal(2,t[1][:b].value)
21
+ end
22
+
23
+ def test_chapter_Sorting
24
+ b = Workbook::Book.new
25
+ s = b.sheet[0] = Workbook::Sheet.new([['a','b'],[1,2],[3,4],[5,6]])
26
+ t = s.table
27
+ #t.sort_by {|r| r[:b]}
28
+ #p t.inspect
29
+ end
30
+ end
@@ -0,0 +1,120 @@
1
+ require File.join(File.dirname(__FILE__), 'helper')
2
+ module Modules
3
+ class TestTableDiffSort < Test::Unit::TestCase
4
+ def test_sort
5
+ time = Time.now
6
+ b = Workbook::Book.new [[1,2,3],[2,2,3],[true,false,true],["asdf","sdf","as"],[time,2,3],[2,2,2],[22,2,3],[1,2,233]]
7
+ t = b.sheet.table
8
+ assert_equal(
9
+ [ [Workbook::Cell.new(1),Workbook::Cell.new(2),Workbook::Cell.new(3)],
10
+ [Workbook::Cell.new(1),Workbook::Cell.new(2),Workbook::Cell.new(233)],
11
+ [Workbook::Cell.new(2),Workbook::Cell.new(2),Workbook::Cell.new(2)],
12
+ [Workbook::Cell.new(2),Workbook::Cell.new(2),Workbook::Cell.new(3)],
13
+ [Workbook::Cell.new(22),Workbook::Cell.new(2),Workbook::Cell.new(3)],
14
+ [Workbook::Cell.new("asdf"),Workbook::Cell.new("sdf"),Workbook::Cell.new("as")],
15
+ [Workbook::Cell.new(time),Workbook::Cell.new(2),Workbook::Cell.new(3)],
16
+ [Workbook::Cell.new(true),Workbook::Cell.new(false),Workbook::Cell.new(true)]
17
+ ],
18
+ t.sort)
19
+
20
+ ba = Workbook::Book.new [['a','b','c','d'],[1,2,3,4],[4,2,3,4],[3,2,3,4]]
21
+ sba = ba.sheet.table.sort
22
+ assert_not_equal(Workbook::Table.new([['a','b','c','d'],[1,2,3,4],[4,2,3,4],[3,2,3,4]]),sba)
23
+ assert_equal(Workbook::Table.new([['a','b','c','d'],[1,2,3,4],[3,2,3,4],[4,2,3,4]]),sba)
24
+
25
+ end
26
+
27
+ def test_align
28
+ ba = Workbook::Book.new [['a','b','c','d'],[1,2,3,4],[4,2,3,4],[3,2,3,4]]
29
+ bb = Workbook::Book.new [['a','b','c','d'],[1,2,3,4],[3,2,3,4],[5,2,3,4]]
30
+ tself = ba.sheet.table
31
+ tother = bb.sheet.table
32
+
33
+ placeholder_row = tother.placeholder_row
34
+
35
+ ba = Workbook::Book.new [['a','b','c','d'],[1,2,3,4],[4,2,3,4],[3,2,3,4]]
36
+ bb = Workbook::Book.new [['a','b','c','d'],[1,2,3,4],[3,2,3,4],[5,2,3,4]]
37
+ tself = ba.sheet.table
38
+ tother = bb.sheet.table
39
+ align_result = tself.align tother
40
+ assert_equal("a,b,c,d\n1,2,3,4\n3,2,3,4\n\n5,2,3,4\n",align_result[:other].to_csv)
41
+ assert_equal("a,b,c,d\n1,2,3,4\n3,2,3,4\n4,2,3,4\n\n",align_result[:self].to_csv)
42
+
43
+ ba = Workbook::Book.new [['a','b','c','d'],[1,2,3,4],[3,2,3,4],[5,2,3,4]]
44
+ bb = Workbook::Book.new [['a','b','c','d'],[1,2,3,4],[2,2,3,4],[5,2,3,4]]
45
+ tself = ba.sheet.table
46
+ tother = bb.sheet.table
47
+ align_result = tself.align tother
48
+ assert_equal("a,b,c,d\n1,2,3,4\n\n3,2,3,4\n5,2,3,4\n",align_result[:self].to_csv)
49
+ assert_equal("a,b,c,d\n1,2,3,4\n2,2,3,4\n\n5,2,3,4\n",align_result[:other].to_csv)
50
+ align_result = tother.align tself
51
+ assert_equal("a,b,c,d\n1,2,3,4\n\n3,2,3,4\n5,2,3,4\n",align_result[:other].to_csv)
52
+ assert_equal("a,b,c,d\n1,2,3,4\n2,2,3,4\n\n5,2,3,4\n",align_result[:self].to_csv)
53
+
54
+ tself = Workbook::Book.new([['a','b','c','d'],[1,2,3,4],[1,3,3,4],[3,2,3,4],[5,2,3,4]]).sheet.table
55
+ tother = Workbook::Book.new([['a','b','c','d'],[1,2,3,4],[2,2,3,4],[5,2,3,4]]).sheet.table
56
+ align_result = tself.align tother
57
+ assert_equal("a,b,c,d\n1,2,3,4\n1,3,3,4\n\n3,2,3,4\n5,2,3,4\n",align_result[:self].to_csv)
58
+ assert_equal("a,b,c,d\n1,2,3,4\n\n2,2,3,4\n\n5,2,3,4\n",align_result[:other].to_csv)
59
+ tself = Workbook::Book.new([['a','b','c','d'],[1,2,3,4],[3,2,3,4],[5,2,3,4]]).sheet.table
60
+ tother = Workbook::Book.new([['a','b','c','d'],[1,2,3,4],[1,2,3,4],[1,2,3,4],[2,2,3,4],[5,2,3,4]]).sheet.table
61
+ align_result = tself.align tother
62
+ assert_equal("a,b,c,d\n1,2,3,4\n\n\n\n3,2,3,4\n5,2,3,4\n",align_result[:self].to_csv)
63
+ assert_equal("a,b,c,d\n1,2,3,4\n1,2,3,4\n1,2,3,4\n2,2,3,4\n\n5,2,3,4\n",align_result[:other].to_csv)
64
+ tself = Workbook::Book.new([['a','b','c','d'],[9,2,3,4],[3,2,3,4],[5,2,3,4]]).sheet.table
65
+ tother = Workbook::Book.new([['a','b','c','d'],[1,2,3,4],[1,2,3,4],[1,2,3,4],[2,2,3,4],[5,2,3,4]]).sheet.table
66
+ align_result = tself.align tother
67
+ assert_equal("a,b,c,d\n\n\n\n\n3,2,3,4\n5,2,3,4\n9,2,3,4\n",align_result[:self].to_csv)
68
+ assert_equal("a,b,c,d\n1,2,3,4\n1,2,3,4\n1,2,3,4\n2,2,3,4\n\n5,2,3,4\n\n",align_result[:other].to_csv)
69
+ tself = Workbook::Book.new([['a','b','c','d'],[49,2,3,4],[10,2,3,4],[45,2,3,4]]).sheet.table
70
+ tother = Workbook::Book.new([['a','b','c','d'],[1,2,3,4],[1,2,3,4],[1,2,3,4],[2,2,3,4],[5,2,3,4]]).sheet.table
71
+ align_result = tself.align tother
72
+ assert_equal("a,b,c,d\n\n\n\n\n\n10,2,3,4\n45,2,3,4\n49,2,3,4\n",align_result[:self].to_csv)
73
+ assert_equal("a,b,c,d\n1,2,3,4\n1,2,3,4\n1,2,3,4\n2,2,3,4\n5,2,3,4\n\n\n\n",align_result[:other].to_csv)
74
+
75
+ end
76
+
77
+ # def test_sort_by
78
+ # b = Workbook::Book.new [['a','b','c','d'],[1,2,3,4],[4,2,3,3],[3,2,3,2]]
79
+ # y b.sheet.table.sort_by{|r| r[:d]}
80
+ # end
81
+
82
+ def test_diff
83
+ ba = Workbook::Book.new [['a','b','c','d'],[1,2,3,4],[4,2,3,4],[3,2,3,4],[3,3,3,4]]
84
+ bb = Workbook::Book.new [['a','b','c','d'],[1,2,3,4],[3,2,3,4],[5,2,3,4],[3,2,3,4]]
85
+ # Start:
86
+ # ba = [['a','b','c','d'],[1,2,3,4],[4,2,3,4],[3,2,3,4],[3,3,3,4]]
87
+ # bb = [['a','b','c','d'],[1,2,3,4],[3,2,3,4],[5,2,3,4],[3,2,3,4]]
88
+ # As it starts out with sorting, it is basically a comparison between
89
+ # ba = [['a','b','c','d'],[1,2,3,4],[3,2,3,4],[3,3,3,4],[4,2,3,4],[]]
90
+ # bb = [['a','b','c','d'],[1,2,3,4],[3,2,3,4],[3,2,3,4],[],[5,2,3,4]]
91
+ # then it aligns:
92
+ # ba = [['a','b','c','d'],[1,2,3,4],[3,2,3,4],[3,3,3,4],[4,2,3,4],[]]
93
+ # bb = [['a','b','c','d'],[1,2,3,4],[3,2,3,4],[3,2,3,4],[],[5,2,3,4]]
94
+ # hence,
95
+ expected = [['a','b','c','d'],[1,2,3,4],[3,2,3,4],[3,'3 (was: 2)',3,4],[4,2,3,4],['(was: 5)','(was: 2)','(was: 3)','(was: 4)']]
96
+
97
+ tself = ba.sheet.table
98
+ tother = bb.sheet.table
99
+ diff_result = tself.diff tother
100
+ assert_equal('a',diff_result.sheet.table.header[0].value)
101
+ assert_equal("a,b,c,d\n1,2,3,4\n3,2,3,4\n3,3 (was: 2),3,4\n4,2,3,4\n(was: 5),(was: 2),(was: 3),(was: 4)\n",diff_result.sheet.table.to_csv)
102
+ diff_result.write_to_xls({:rewrite_header=>true})
103
+ end
104
+
105
+
106
+
107
+ def test_diff_xls
108
+ (1..8).each do |index|
109
+ prev = "test/artifacts/compare#{index}_prev.xls"
110
+ curr = "test/artifacts/compare#{index}_current.xls"
111
+
112
+ wprev=Workbook::Book.open prev
113
+ wcurr=Workbook::Book.open curr
114
+
115
+ diff = wcurr.sheet.table.diff wprev.sheet.table
116
+ diff.write_to_xls({:filename=>"compare#{index}.xls"})
117
+ end
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,53 @@
1
+ require File.join(File.dirname(__FILE__), 'helper')
2
+ module Modules
3
+ class TestTypeParser < Test::Unit::TestCase
4
+ def examples
5
+ {"2312"=>2312,
6
+ "12-12-2012"=>Date.new(2012,12,12),
7
+ "12-12-2012 12:24"=>DateTime.new(2012,12,12,12,24),
8
+ "2012-12-12 12:24"=>DateTime.new(2012,12,12,12,24),
9
+ "2011-05-19T15_37_49 - 52349.xml"=>"2011-05-19T15_37_49 - 52349.xml",
10
+ "20-2-2012 20:52"=>DateTime.new(2012,2,20,20,52),
11
+ "1-11-2011"=>Date.new(2011,11,1),
12
+ "jA"=>"jA",
13
+ "n"=>"n",
14
+ "12 bomen"=>"12 bomen",
15
+ "12 bomenasdfasdfsadf"=>"12 bomenasdfasdfsadf",
16
+ ""=>nil,
17
+ " "=>nil,
18
+ "mailto:sadf@asdf.as"=>"sadf@asdf.as"
19
+ }
20
+ end
21
+
22
+ def test_parse
23
+ examples.each do |k,v|
24
+ assert_equal(v,Workbook::Cell.new(k).parse({:detect_date=>true}))
25
+ end
26
+ end
27
+
28
+ def test_custom_parse
29
+ customparsers = [proc{|v| "#{v}2" }]
30
+ examples.each do |k,v|
31
+ c = Workbook::Cell.new(k)
32
+ c.string_parsers = customparsers
33
+ assert_not_equal(v,c.parse)
34
+ end
35
+ c = Workbook::Cell.new("233")
36
+ c.string_parsers = customparsers
37
+ assert_equal("2332",c.parse)
38
+ c.value = "v"
39
+ assert_equal("v2",c.parse)
40
+ end
41
+
42
+ def test_parse!
43
+ r= Workbook::Row.new
44
+ r[0] = Workbook::Cell.new "xls_cell"
45
+ r[0].parse!
46
+ assert_equal("xls_cell",r[0].value)
47
+ r[1] = Workbook::Cell.new ""
48
+ r[1].parse!
49
+ assert_equal(nil,r[1].value)
50
+
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,37 @@
1
+ require File.join(File.dirname(__FILE__), 'helper')
2
+ module Readers
3
+ class TestCsvWriter < Test::Unit::TestCase
4
+ def test_open
5
+ w = Workbook::Book.new
6
+ w.open 'test/artifacts/simple_csv.csv'
7
+ # reads
8
+ # a,b,c,d
9
+ # 1,2,3,4
10
+ # 5,3,2,1
11
+ # "asdf",123,12,2001-02-02
12
+ #
13
+ assert_equal([:a,:b,:c,:d],w.sheet.table.header.to_symbols)
14
+ assert_equal(3,w.sheet.table[2][:b].value)
15
+ assert_equal("asdf",w.sheet.table[3][:a].value)
16
+ assert_equal(Date.new(2001,2,2),w.sheet.table[3][:d].value)
17
+ end
18
+ def test_excel_csv_open
19
+ w = Workbook::Book.new
20
+ w.open("test/artifacts/simple_excel_csv.csv")
21
+ # reads
22
+ # a;b;c
23
+ # 1-1-2001;23;1
24
+ # asdf;23;asd
25
+ # 23;asdf;sadf
26
+ # 12;23;12-02-2011 12:23
27
+ #y w.sheet.table
28
+ assert_equal([:a,:b,:c],w.sheet.table.header.to_symbols)
29
+ assert_equal(23,w.sheet.table[2][:b].value)
30
+ assert_equal("sadf",w.sheet.table[3][:c].value)
31
+ assert_equal(Date.new(2001,1,1),w.sheet.table[1][:a].value)
32
+ assert_equal(DateTime.new(2011,2,12,12,23),w.sheet.table[4][:c].value)
33
+ assert_equal("6/12 ovk getekend terugontv.+>acq ter tekening. 13/12 ovk getekend terugontv.+>Fred ter tekening.", w.sheet.table[5][:b].value)
34
+ assert_equal(Date.new(2012,6,12),w.sheet.table[5][:c].value)
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,49 @@
1
+ require File.join(File.dirname(__FILE__), 'helper')
2
+ module Readers
3
+ class TestTxtReader < Test::Unit::TestCase
4
+ # Should one day throw an error..
5
+ # def test_failure_excel_as_txt_open
6
+ # w = Workbook::Book.new
7
+ # w.open("test/artifacts/xls_with_txt_extension.txt")
8
+ # puts w.sheet.table
9
+ # end
10
+ def test_complex_excel_txt_open
11
+ w = Workbook::Book.new
12
+ w.open("test/artifacts/sharepoint_download_excel.txt")
13
+ # reads
14
+ # a;b;c
15
+ # 1-1-2001;23;1
16
+ # asdf;23;asd
17
+ # 23;asdf;sadf
18
+ # 12;23;12-02-2011 12:23
19
+ #y w.sheet.table
20
+ assert_equal([:id,
21
+ :naam,
22
+ :locatie_naam,
23
+ :locatie_rayon,
24
+ :datum_gesprek,
25
+ :algehele_indruk,
26
+ :gemaakt_door,
27
+ :gemaakt,
28
+ :titel,
29
+ :toelichting,
30
+ :gewenste_migratie_maand,
31
+ :plek_ing_balie,
32
+ :plek_tnt_balie,
33
+ :alternatieve_locaties,
34
+ :datum_introductie_gesprek,
35
+ :geen_aanbod_voor_isp,
36
+ :oplevering_geslaagd_op,
37
+ :nazorgpunten,
38
+ :locatie],w.sheet.table.header.to_symbols)
39
+ assert_equal(Date.new(2011,11,01),w.sheet.table[29][:gewenste_migratie_maand].value)
40
+ assert_equal("Belt , G (Gerrit)",w.sheet.table[1][:gemaakt_door].value)
41
+ assert_equal(false,w.sheet.table[1][:geen_aanbod_voor_isp].value)
42
+ assert_equal(DateTime.new(2011,7,25,11,02),w.sheet.table[4][:gemaakt].value)
43
+ assert_equal("Roggeveen , PC (Peter)",w.sheet.table[4][:gemaakt_door].value)
44
+ assert_equal("Groeneveld, R (René)",w.sheet.table[400][:gemaakt_door].value)
45
+ assert_equal(24208,w.sheet.table[7][:locatie].value)
46
+ end
47
+
48
+ end
49
+ end
@@ -0,0 +1,26 @@
1
+ require File.join(File.dirname(__FILE__), 'helper')
2
+ module Readers
3
+ class TestXlsWriter < Test::Unit::TestCase
4
+ def test_open
5
+ w = Workbook::Book.new
6
+ w.open 'test/artifacts/book_with_tabs_and_colours.xls'
7
+ assert_equal([:vestiging_id,:pirnr_ing,:ing_kantoornaam,:installatiedag_ispapparatuur,:mo_openingsdatum_ing],w.sheet.table.header.to_symbols)
8
+ assert_equal(90588,w.sheet.table[2][:pirnr_ing].value)
9
+ assert_equal("#CCFFCC",w.sheet.table[3][:ing_kantoornaam].format[:background_color])
10
+ assert_equal(8.13671875,w.sheet.table.first[:pirnr_ing].format[:width])
11
+ assert_equal(3.85546875,w.sheet.table.first[:vestiging_id].format[:width])
12
+ assert_equal(25.14453125,w.sheet.table.first[:ing_kantoornaam].format[:width])
13
+
14
+
15
+ end
16
+
17
+ def test_complex_types
18
+ w = Workbook::Book.new
19
+ w.open 'test/artifacts/complex_types.xls'
20
+ assert_equal(Date.new(2011,11,15), w.sheet.table[2][3].value)
21
+ assert_equal("http://murb.nl", w.sheet.table[3][2].value)
22
+ assert_equal("sadfasdfsd", w.sheet.table[4][2].value)
23
+ assert_equal(1.2, w.sheet.table[3][1].value)
24
+ end
25
+ end
26
+ end