workbook 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -74,9 +74,19 @@ module Workbook
74
74
 
75
75
 
76
76
  def load_xls file_obj
77
- sp = Spreadsheet.open(file_obj, mode='rb')
78
- template.add_raw sp
79
- parse_xls sp
77
+ begin
78
+ sp = Spreadsheet.open(file_obj, mode='rb')
79
+ template.add_raw sp
80
+ parse_xls sp
81
+ rescue Ole::Storage::FormatError => e
82
+ begin
83
+ # Assuming it is a tab separated txt inside .xls
84
+ open(file_obj.path, 'txt')
85
+ rescue
86
+ raise e
87
+ end
88
+ end
89
+
80
90
  end
81
91
 
82
92
 
@@ -1,4 +1,4 @@
1
- require 'CSV'
1
+ require 'csv'
2
2
 
3
3
  module Workbook
4
4
  module Writers
data/readme.markdown CHANGED
@@ -74,7 +74,7 @@ To some extent, sort_by works, it doesn't, however, adhere to the header setting
74
74
 
75
75
  ### Comparing tables
76
76
 
77
- Simply call
77
+ Simply call on a Workbook::Table
78
78
 
79
79
  t1.diff t2
80
80
 
@@ -86,7 +86,7 @@ Currently writing is limited to the following formats. Templating support is sti
86
86
 
87
87
  b.to_xls # returns a spreadsheet workbook
88
88
  b.write_to_xls filename # writes to filename
89
- t.to_csv # returns a csv-string
89
+ t.to_csv # returns a csv-string (called on tables)
90
90
 
91
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
92
 
@@ -0,0 +1,4 @@
1
+ a;b;c;d
2
+ 22-2-2012;27-12-2014;4,23;42000,22323
3
+ c;222;;27-12-14 5:21
4
+ 22-1-2012 11:00;42000;goh, idd;ls
Binary file
File without changes
Binary file
@@ -101,20 +101,5 @@ module Modules
101
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
102
  diff_result.write_to_xls({:rewrite_header=>true})
103
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
104
  end
120
105
  end
@@ -33,5 +33,21 @@ module Readers
33
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
34
  assert_equal(Date.new(2012,6,12),w.sheet.table[5][:c].value)
35
35
  end
36
+ def test_excel_standardized_open
37
+ w = Workbook::Book.new
38
+ w.open("test/artifacts/excel_different_types.csv")
39
+ # reads
40
+ # a,b,c,d
41
+ # 2012-02-22,2014-12-27,2012-11-23,2012-11-12T04:20:00+00:00
42
+ # c,222.0,,0027-12-14T05:21:00+00:00
43
+ # 2012-01-22T11:00:00+00:00,42000.0,"goh, idd",ls
44
+
45
+ assert_equal([:a,:b,:c, :d],w.sheet.table.header.to_symbols)
46
+ assert_equal(Date.new(2012,2,22),w.sheet.table[1][:a].value)
47
+ assert_equal("c",w.sheet.table[2][:a].value)
48
+ assert_equal(DateTime.new(2012,1,22,11),w.sheet.table[3][:a].value)
49
+ assert_equal(42000,w.sheet.table[3][:b].value)
50
+ assert_equal(nil,w.sheet.table[2][:c].value)
51
+ end
36
52
  end
37
53
  end
@@ -7,42 +7,22 @@ module Readers
7
7
  # w.open("test/artifacts/xls_with_txt_extension.txt")
8
8
  # puts w.sheet.table
9
9
  # end
10
- def test_complex_excel_txt_open
10
+
11
+ def test_excel_standardized_open
11
12
  w = Workbook::Book.new
12
- w.open("test/artifacts/sharepoint_download_excel.txt")
13
+ w.open("test/artifacts/excel_different_types.txt")
13
14
  # 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)
15
+ # a,b,c,d
16
+ # 2012-02-22,2014-12-27,2012-11-23,2012-11-12T04:20:00+00:00
17
+ # c,222.0,,0027-12-14T05:21:00+00:00
18
+ # 2012-01-22T11:00:00+00:00,42000.0,"goh, idd",ls
19
+
20
+ assert_equal([:a,:b,:c, :d],w.sheet.table.header.to_symbols)
21
+ assert_equal(Date.new(2012,2,22),w.sheet.table[1][:a].value)
22
+ assert_equal("c",w.sheet.table[2][:a].value)
23
+ assert_equal(DateTime.new(2012,1,22,11),w.sheet.table[3][:a].value)
24
+ assert_equal(42000,w.sheet.table[3][:b].value)
25
+ assert_equal(nil,w.sheet.table[2][:c].value)
46
26
  end
47
27
 
48
28
  end
@@ -4,12 +4,12 @@ module Readers
4
4
  def test_open
5
5
  w = Workbook::Book.new
6
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])
7
+ assert_equal([:a, :b, :c, :d, :e],w.sheet.table.header.to_symbols)
8
+ assert_equal(90588,w.sheet.table[2][:b].value)
9
+ assert_equal("#CCFFCC",w.sheet.table[3][:c].format[:background_color])
10
+ assert_equal(8.13671875,w.sheet.table.first[:b].format[:width])
11
+ assert_equal(3.85546875,w.sheet.table.first[:a].format[:width])
12
+ assert_equal(25.14453125,w.sheet.table.first[:c].format[:width])
13
13
 
14
14
 
15
15
  end
@@ -22,5 +22,40 @@ module Readers
22
22
  assert_equal("sadfasdfsd", w.sheet.table[4][2].value)
23
23
  assert_equal(1.2, w.sheet.table[3][1].value)
24
24
  end
25
+
26
+ def test_excel_standardized_open
27
+ w = Workbook::Book.new
28
+ w.open("test/artifacts/excel_different_types.xls")
29
+ # reads
30
+ # a,b,c,d
31
+ # 2012-02-22,2014-12-27,2012-11-23,2012-11-12T04:20:00+00:00
32
+ # c,222.0,,0027-12-14T05:21:00+00:00
33
+ # 2012-01-22T11:00:00+00:00,42000.0,"goh, idd",ls
34
+
35
+ assert_equal([:a,:b,:c, :d],w.sheet.table.header.to_symbols)
36
+ assert_equal(Date.new(2012,2,22),w.sheet.table[1][:a].value)
37
+ assert_equal("c",w.sheet.table[2][:a].value)
38
+ assert_equal(DateTime.new(2012,1,22,11),w.sheet.table[3][:a].value)
39
+ assert_equal(42000,w.sheet.table[3][:b].value)
40
+ assert_equal(nil,w.sheet.table[2][:c].value)
41
+ end
42
+
43
+ def test_txt_in_xls_open
44
+ w = Workbook::Book.new
45
+ w.open("test/artifacts/txt_in_xls.xls")
46
+ assert_equal([:naam,:nummer,:ilt,:corporate_key,:naam_medewerker, nil, nil, :telefoon, :openingsdatum],w.sheet.table.header.to_symbols)
47
+ assert_equal(["dddd",2222,"i9000","asd","Anita",nil,"Betera",DateTime.new(2012,1,12),Date.new(2011,10,5)],w.sheet.table[1].collect{|a| a.value})
48
+ end
49
+ def test_zip_in_xls_open
50
+ w = Workbook::Book.new
51
+ failed_properly = false
52
+ begin
53
+ w.open("test/artifacts/zip_in_xls.xls")
54
+ w.sheet.table.to_csv
55
+ rescue Ole::Storage::FormatError
56
+ failed_properly = true
57
+ end
58
+ assert_equal(true,failed_properly)
59
+ end
25
60
  end
26
61
  end
@@ -15,11 +15,10 @@ module Writers
15
15
 
16
16
  def test_roundtrip
17
17
  b = Workbook::Book.open('test/artifacts/simple_sheet.xls')
18
- assert_equal(3.85546875,b.sheet.table.first[:vestiging_id].format[:width])
18
+ assert_equal(3.85546875,b.sheet.table.first[:a].format[:width])
19
19
  filename = b.write_to_xls
20
20
  b = Workbook::Book.open filename
21
- assert_equal(3.85546875,b.sheet.table.first[:vestiging_id].format[:width])
22
-
21
+ assert_equal(3.85546875,b.sheet.table.first[:a].format[:width])
23
22
  end
24
23
 
25
24
  def test_init_spreadsheet_template
data/workbook.gemspec CHANGED
@@ -5,8 +5,8 @@ require "workbook"
5
5
  Gem::Specification.new do |s|
6
6
  s.name = 'workbook'
7
7
  s.rubyforge_project = 'workbook'
8
- s.version = '0.1.1'
9
- s.date = '2012-09-11'
8
+ s.version = '0.1.2'
9
+ s.date = '2012-11-12'
10
10
  s.summary = "Workbook is a datastructure to contain books of tables (an anlogy used in e.g. Excel)"
11
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
12
  s.authors = ["Maarten Brouwers"]
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 1
9
- version: 0.1.1
8
+ - 2
9
+ version: 0.1.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Maarten Brouwers
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2012-09-11 00:00:00 +02:00
17
+ date: 2012-11-12 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -89,11 +89,16 @@ files:
89
89
  - readme.markdown
90
90
  - test/artifacts/book_with_tabs_and_colours.xls
91
91
  - test/artifacts/complex_types.xls
92
- - test/artifacts/medewerkers.xls
92
+ - test/artifacts/excel_different_types.csv
93
+ - test/artifacts/excel_different_types.txt
94
+ - test/artifacts/excel_different_types.xls
95
+ - test/artifacts/excel_different_types.xlsx
93
96
  - test/artifacts/simple_csv.csv
94
97
  - test/artifacts/simple_excel_csv.csv
95
98
  - test/artifacts/simple_sheet.xls
99
+ - test/artifacts/txt_in_xls.xls
96
100
  - test/artifacts/xls_with_txt_extension.txt
101
+ - test/artifacts/zip_in_xls.xls
97
102
  - test/helper.rb
98
103
  - test/test_book.rb
99
104
  - test/test_cell.rb
@@ -143,11 +148,16 @@ summary: Workbook is a datastructure to contain books of tables (an anlogy used
143
148
  test_files:
144
149
  - test/artifacts/book_with_tabs_and_colours.xls
145
150
  - test/artifacts/complex_types.xls
146
- - test/artifacts/medewerkers.xls
151
+ - test/artifacts/excel_different_types.csv
152
+ - test/artifacts/excel_different_types.txt
153
+ - test/artifacts/excel_different_types.xls
154
+ - test/artifacts/excel_different_types.xlsx
147
155
  - test/artifacts/simple_csv.csv
148
156
  - test/artifacts/simple_excel_csv.csv
149
157
  - test/artifacts/simple_sheet.xls
158
+ - test/artifacts/txt_in_xls.xls
150
159
  - test/artifacts/xls_with_txt_extension.txt
160
+ - test/artifacts/zip_in_xls.xls
151
161
  - test/helper.rb
152
162
  - test/test_book.rb
153
163
  - test/test_cell.rb