workbook 0.5 → 0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2 -3
  3. data/lib/workbook/book.rb +13 -12
  4. data/lib/workbook/column.rb +13 -6
  5. data/lib/workbook/format.rb +16 -1
  6. data/lib/workbook/modules/cell.rb +3 -2
  7. data/lib/workbook/modules/diff_sort.rb +2 -2
  8. data/lib/workbook/modules/type_parser.rb +3 -2
  9. data/lib/workbook/readers/xls_reader.rb +4 -4
  10. data/lib/workbook/readers/xls_shared.rb +39 -0
  11. data/lib/workbook/readers/xlsx_reader.rb +159 -21
  12. data/lib/workbook/row.rb +2 -2
  13. data/lib/workbook/sheet.rb +9 -2
  14. data/lib/workbook/table.rb +20 -8
  15. data/lib/workbook/template.rb +5 -1
  16. data/lib/workbook/types/date.rb +1 -1
  17. data/lib/workbook/version.rb +1 -1
  18. data/lib/workbook/writers/xlsx_writer.rb +7 -7
  19. data/test/artifacts/txt_in_xls.xls +0 -0
  20. data/test/test_book.rb +7 -6
  21. data/test/test_format.rb +10 -0
  22. data/test/test_functional.rb +3 -3
  23. data/test/test_modules_cell.rb +19 -11
  24. data/test/test_modules_table_diff_sort.rb +0 -2
  25. data/test/test_modules_type_parser.rb +7 -5
  26. data/test/test_readers_csv_reader.rb +4 -4
  27. data/test/test_readers_ods_reader.rb +9 -9
  28. data/test/test_readers_txt_reader.rb +5 -5
  29. data/test/test_readers_xls_reader.rb +11 -15
  30. data/test/test_readers_xls_shared.rb +10 -0
  31. data/test/test_readers_xlsx_reader.rb +12 -9
  32. data/test/test_row.rb +6 -6
  33. data/test/test_table.rb +13 -11
  34. data/test/test_template.rb +10 -1
  35. data/test/test_types_date.rb +3 -0
  36. data/test/test_writers_html_writer.rb +1 -1
  37. data/test/test_writers_xls_writer.rb +9 -10
  38. data/test/test_writers_xlsx_writer.rb +2 -2
  39. data/workbook.gemspec +0 -1
  40. metadata +4 -18
  41. data/rbeautify.rb +0 -234
data/lib/workbook/row.rb CHANGED
@@ -12,7 +12,7 @@ module Workbook
12
12
  #
13
13
  # @param [Workbook::Row, Array<Workbook::Cell>, Array] cells list of cells to initialize the row with, default is empty
14
14
  # @param [Workbook::Table] table a row normally belongs to a table, reference it here
15
- # @param [Hash] options Supprted options: parse_cells_on_batch_creation (parse cell values during row-initalization, default: false), cell_parse_options (default {}, see Workbook::Modules::TypeParser)
15
+ # @param [Hash] options Supported options: parse_cells_on_batch_creation (parse cell values during row-initalization, default: false), cell_parse_options (default {}, see Workbook::Modules::TypeParser)
16
16
  def initialize cells=[], table=nil, options={}
17
17
  options=options ? {:parse_cells_on_batch_creation=>false,:cell_parse_options=>{},:clone_cells=>false}.merge(options) : {}
18
18
  cells = [] if cells==nil
@@ -38,7 +38,7 @@ module Workbook
38
38
  #
39
39
  # @return [Workbook::Table] the table this row belongs to
40
40
  def table
41
- @table
41
+ @table if defined?(@table)
42
42
  end
43
43
 
44
44
  # Set reference to the table this row belongs to without adding the row to the table
@@ -1,8 +1,7 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
  module Workbook
3
+ # A Sheet is a container of tables
3
4
  class Sheet < Array
4
- # A Sheet is a container of tables
5
- attr_accessor :name
6
5
 
7
6
  # Initialize a new sheet
8
7
  #
@@ -41,6 +40,14 @@ module Workbook
41
40
  @name ||= "Sheet #{book.index(self)+1}"
42
41
  end
43
42
 
43
+ # Set the name of this sheet
44
+ #
45
+ # @param [String] the new name of the sheet
46
+ # @return [String] the given name
47
+ def name= name
48
+ @name = name
49
+ end
50
+
44
51
  # Set the first table of this sheet with a table or array of cells/values
45
52
  # @param [Workbook::Table, Array<Array>] table The new first table of this sheet
46
53
  # @param [Hash] options are forwarded to Workbook::Table.new
@@ -13,9 +13,7 @@ module Workbook
13
13
  include Workbook::Writers::JsonTableWriter
14
14
  include Workbook::Writers::HtmlTableWriter
15
15
 
16
- attr_accessor :sheet
17
16
  attr_accessor :name
18
- attr_accessor :columns
19
17
 
20
18
  def initialize row_cel_values=[], sheet=nil, options={}
21
19
  row_cel_values = [] if row_cel_values == nil
@@ -137,12 +135,16 @@ module Workbook
137
135
  #
138
136
  # @return [Workbook::Sheet] The sheet this table belongs to
139
137
  def sheet
140
- if @sheet
141
- return @sheet
142
- else
143
- self.sheet = Workbook::Sheet.new(self)
144
- return @sheet
145
- end
138
+ return @sheet if defined?(@sheet) and !@sheet.nil?
139
+ self.sheet= Workbook::Sheet.new(self)
140
+ end
141
+
142
+ # Returns the sheet this table belongs to, creates a new sheet if none exists
143
+ #
144
+ # @param [Workbook::Sheet] sheet this table belongs to
145
+ # @return [Workbook::Sheet] The sheet this table belongs to
146
+ def sheet= sheet
147
+ @sheet = sheet
146
148
  end
147
149
 
148
150
  # Removes all lines from this table
@@ -257,11 +259,21 @@ module Workbook
257
259
  [width,height]
258
260
  end
259
261
 
262
+ # Returns an array of Column-classes describing the columns of this table
263
+ # @return [Array<Column>] columns
260
264
  def columns
261
265
  @columns ||= header.collect do |header_cell|
262
266
  Column.new(self)
263
267
  end
264
268
  end
265
269
 
270
+ # Returns an array of Column-classes describing the columns of this table
271
+ # @param [Array<Column>] columns
272
+ # @return [Array<Column>] columns
273
+ def columns= columns
274
+ columns.each{|c| c.table=self}
275
+ @columns = columns
276
+ end
277
+
266
278
  end
267
279
  end
@@ -21,7 +21,11 @@ module Workbook
21
21
  # @param [Workbook::Format] format (of a cell) to add to the template
22
22
  def add_format format
23
23
  if format.is_a? Workbook::Format
24
- @formats[format.name]=format
24
+ if format.name
25
+ @formats[format.name]=format
26
+ else
27
+ @formats[@formats.keys.count]=format
28
+ end
25
29
  else
26
30
  raise ArgumentError, "format should be a Workboot::Format"
27
31
  end
@@ -12,7 +12,7 @@ module Workbook
12
12
  end
13
13
 
14
14
  def value= a
15
- throw "#value= is no longer available"
15
+ puts "#value= is deprecated"
16
16
  end
17
17
  end
18
18
  end
@@ -1,4 +1,4 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
  module Workbook
3
- VERSION = '0.5'
3
+ VERSION = '0.6'
4
4
  end
@@ -25,10 +25,10 @@ module Workbook
25
25
  xlsx_row.add_cell(c.value) unless xlsx_row_a[ci]
26
26
  xlsx_cell = xlsx_row_a[ci]
27
27
  xlsx_cell.value = c.value
28
- if c.format?
29
- format_to_xlsx_format(c.format) unless c.format.raws[Fixnum]
30
- xlsx_cell.style = c.format.raws[Fixnum]
31
- end
28
+ # if c.format?
29
+ # format_to_xlsx_format(c.format) unless c.format.raws[Fixnum]
30
+ # xlsx_cell.style = c.format.raws[Fixnum]
31
+ # end
32
32
  end
33
33
  xlsx_sheet.send(:update_column_info, xlsx_row.cells, [])
34
34
  end
@@ -76,7 +76,7 @@ module Workbook
76
76
  else
77
77
  t = Axlsx::Package.new
78
78
  template.add_raw t
79
- template.set_default_formats!
79
+ # template.set_default_formats!
80
80
  return t
81
81
  end
82
82
  end
@@ -90,7 +90,7 @@ module Workbook
90
90
  end
91
91
 
92
92
  def make_sure_f_is_a_workbook_format f
93
- f.is_a?(Workbook::Format) ? f : Workbook::Format.new(f)
93
+ f.is_a?(Workbook::Format) ? f : Workbook::Format.new({}, f)
94
94
  end
95
95
 
96
96
  def format_to_xlsx_format f
@@ -104,7 +104,7 @@ module Workbook
104
104
  xlsfmt[:bg_color] = f[:background_color] if f[:background_color]
105
105
  xlsfmt[:format_code] = strftime_to_ms_format(f[:number_format]) if f[:number_format]
106
106
  xlsfmt[:font_name] = f[:font_family].split.first if f[:font_family]
107
- xlsfmt[:family] = parse_font_family(f) if f[:font_family]
107
+ # xlsfmt[:family] = parse_font_family(f) if f[:font_family]
108
108
 
109
109
  f.add_raw init_xlsx_spreadsheet_template.workbook.styles.add_style(xlsfmt)
110
110
  f.add_raw xlsfmt
Binary file
data/test/test_book.rb CHANGED
@@ -5,19 +5,19 @@ class TestWorkbook < Minitest::Test
5
5
  def test_sheets
6
6
  w = Workbook::Book.new nil
7
7
  w.push
8
- assert_equal(2, w.count)
8
+ assert_equal(1, w.count)
9
9
  end
10
10
 
11
11
  def test_push
12
12
  w = Workbook::Book.new nil
13
- assert_equal(w.count,1)
14
- assert_equal([[[]]],w)
13
+ assert_equal(0, w.count)
14
+ assert_equal([],w)
15
15
  w = Workbook::Book.new
16
16
  w.push
17
17
  assert_equal(w.first.class,Workbook::Sheet)
18
18
  w.push
19
19
  assert_equal(w,w.last.book)
20
- assert_equal(w.count,3)
20
+ assert_equal(2, w.count)
21
21
  s = Workbook::Sheet.new
22
22
  w.push s
23
23
  assert_equal(w,w.last.book)
@@ -34,10 +34,11 @@ class TestWorkbook < Minitest::Test
34
34
  refute_equal(w.sheet, s)
35
35
  w = Workbook::Book.new s
36
36
  assert_equal(w.sheet, s)
37
+ w = Workbook::Book.new
38
+ s = w.sheet
39
+ assert_equal([[]], s)
37
40
  end
38
41
 
39
-
40
-
41
42
  def test_template
42
43
  b = Workbook::Book.new
43
44
  raw = "asdf"
data/test/test_format.rb CHANGED
@@ -19,6 +19,16 @@ class TestFormat < Minitest::Test
19
19
 
20
20
  end
21
21
 
22
+ def test_derived_type
23
+ f = Workbook::Format.new {}
24
+ assert_nil(f.derived_type)
25
+ f = Workbook::Format.new numberformat: "%m/%e/%y h:%m"
26
+ assert_equal(:time,f.derived_type)
27
+ f = Workbook::Format.new numberformat: "%m/%e/%y"
28
+ assert_equal(:date,f.derived_type)
29
+
30
+ end
31
+
22
32
  def test_available_raws
23
33
  deet = Time.now
24
34
  f = Workbook::Format.new {}
@@ -22,9 +22,9 @@ class TestFunctional < Minitest::Test
22
22
  end
23
23
 
24
24
  def test_chapter_Sorting
25
- b = Workbook::Book.new
26
- s = b.sheet[0] = Workbook::Sheet.new([['a','b'],[1,2],[3,4],[5,6]])
27
- t = s.table
25
+ # b = Workbook::Book.new
26
+ # s = b.sheet[0] = Workbook::Sheet.new([['a','b'],[1,2],[3,4],[5,6]])
27
+ # t = s.table
28
28
  #t.sort_by {|r| r[:b]}
29
29
  #p t.inspect
30
30
  end
@@ -6,7 +6,7 @@ class TestModulesCell < Minitest::Test
6
6
 
7
7
  def test_init
8
8
  w = Workbook::Cell.new nil
9
- assert_equal(nil,w.value)
9
+ assert_nil(w.value)
10
10
  w = Workbook::Cell.new "asdf"
11
11
  assert_equal("asdf",w.value)
12
12
 
@@ -20,7 +20,7 @@ class TestModulesCell < Minitest::Test
20
20
 
21
21
  def test_value
22
22
  w = Workbook::Cell.new nil
23
- assert_equal(nil,w.value)
23
+ assert_nil(w.value)
24
24
  w.value = "asdf"
25
25
  assert_equal("asdf",w.value)
26
26
  w.value = Date.new
@@ -93,29 +93,29 @@ class TestModulesCell < Minitest::Test
93
93
 
94
94
  def test_nil
95
95
  c = Workbook::Cell.new nil
96
- assert_equal(true,c.nil?)
96
+ assert_nil(c)
97
97
  end
98
98
 
99
99
  def test_colspan_rowspan
100
100
  c = Workbook::Cell.new
101
101
  c.colspan = 1
102
102
  c.rowspan = 1
103
- assert_equal(nil,c.colspan)
104
- assert_equal(nil,c.rowspan)
103
+ assert_nil(c.colspan)
104
+ assert_nil(c.rowspan)
105
105
  c.colspan = nil
106
106
  c.rowspan = ""
107
- assert_equal(nil,c.colspan)
108
- assert_equal(nil,c.rowspan)
107
+ assert_nil(c.colspan)
108
+ assert_nil(c.rowspan)
109
109
  c.colspan = 3
110
110
  c.rowspan = "4"
111
111
  assert_equal(3,c.colspan)
112
112
  c.rowspan = 0
113
- assert_equal(nil,c.rowspan)
113
+ assert_nil(c.rowspan)
114
114
  assert_equal(3,c.colspan)
115
115
  c.colspan = 0
116
116
  c.rowspan = 3
117
117
  assert_equal(3,c.rowspan)
118
- assert_equal(nil,c.colspan)
118
+ assert_nil(c.colspan)
119
119
  end
120
120
 
121
121
  def test_cell_type
@@ -125,10 +125,18 @@ class TestModulesCell < Minitest::Test
125
125
  end
126
126
  end
127
127
 
128
- def test_index_and_key
128
+ def test_index
129
129
  t = Workbook::Table.new [[:a,:b,:c],[1,2,3],[4,5,6]]
130
130
  assert_equal(2, t[2][2].index)
131
- assert_equal(:c, t[2][2].key)
131
+ end
132
132
 
133
+ def test_key
134
+ t = Workbook::Table.new [[:a,:b,:c],[1,2,3],[4,5,6]]
135
+ assert_equal(:c, t[2][2].key)
136
+ t = Workbook::Table.new [[:d,nil,nil],[:a,:b,:c],[1,2,3],[4,5,6]]
137
+ assert_nil(t[2][2].key)
138
+ assert_equal(:d, t[2][0].key)
139
+ t.header = t[1]
140
+ assert_equal(:a, t[2][0].key)
133
141
  end
134
142
  end
@@ -31,8 +31,6 @@ module Modules
31
31
  tself = ba.sheet.table
32
32
  tother = bb.sheet.table
33
33
 
34
- placeholder_row = tother.placeholder_row
35
-
36
34
  ba = Workbook::Book.new [['a','b','c','d'],[1,2,3,4],[4,2,3,4],[3,2,3,4]]
37
35
  bb = Workbook::Book.new [['a','b','c','d'],[1,2,3,4],[3,2,3,4],[5,2,3,4]]
38
36
  tself = ba.sheet.table
@@ -3,7 +3,8 @@ require File.join(File.dirname(__FILE__), 'helper')
3
3
  module Modules
4
4
  class TestTypeParser < Minitest::Test
5
5
  def examples
6
- {"2312"=>2312,
6
+ {
7
+ "2312"=>2312,
7
8
  "12-12-2012"=>Date.new(2012,12,12),
8
9
  "12-12-2012 12:24"=>DateTime.new(2012,12,12,12,24),
9
10
  "2012-12-12 12:24"=>DateTime.new(2012,12,12,12,24),
@@ -14,10 +15,10 @@ module Modules
14
15
  "12/23/1980"=>Date.new(1980,12,23), #TODO: should probably depend on locale, see: http://bugs.ruby-lang.org/issues/634#note-10
15
16
  "jA"=>"jA",
16
17
  "n"=>"n",
18
+ ""=>nil,
19
+ " "=> nil,
17
20
  "12 bomen"=>"12 bomen",
18
21
  "12 bomenasdfasdfsadf"=>"12 bomenasdfasdfsadf",
19
- ""=>nil,
20
- " "=>nil,
21
22
  "mailto:sadf@asdf.as"=>"sadf@asdf.as",
22
23
  "012-3456789"=>"012-3456789",
23
24
  "TRUE"=>true
@@ -26,7 +27,7 @@ module Modules
26
27
 
27
28
  def test_parse
28
29
  examples.each do |k,v|
29
- assert_equal(v,Workbook::Cell.new(k).parse({:detect_date=>true}))
30
+ assert(v == Workbook::Cell.new(k).parse({:detect_date => true}))
30
31
  end
31
32
  end
32
33
 
@@ -51,11 +52,12 @@ module Modules
51
52
  assert_equal("xls_cell",r[0].value)
52
53
  r[1] = Workbook::Cell.new ""
53
54
  r[1].parse!
54
- assert_equal(nil,r[1].value)
55
+ assert_nil(r[1].value)
55
56
  end
56
57
 
57
58
  def test_once_failing_files
58
59
  w = Workbook::Book.open(File.join(File.dirname(__FILE__), 'artifacts/failing_import1.xls')) # TRUE wasn't parsed properly
60
+ assert_equal(Workbook::Book, w.class)
59
61
  end
60
62
  end
61
63
  end
@@ -4,7 +4,7 @@ module Readers
4
4
  class TestCsvWriter < Minitest::Test
5
5
  def test_open
6
6
  w = Workbook::Book.new
7
- w.open File.join(File.dirname(__FILE__), 'artifacts/simple_csv.csv')
7
+ w.import File.join(File.dirname(__FILE__), 'artifacts/simple_csv.csv')
8
8
  # reads
9
9
  # a,b,c,d
10
10
  # 1,2,3,4
@@ -18,7 +18,7 @@ module Readers
18
18
  end
19
19
  def test_excel_csv_open
20
20
  w = Workbook::Book.new
21
- w.open File.join(File.dirname(__FILE__), 'artifacts/simple_excel_csv.csv')
21
+ w.import File.join(File.dirname(__FILE__), 'artifacts/simple_excel_csv.csv')
22
22
  # reads
23
23
  # a;b;c
24
24
  # 1-1-2001;23;1
@@ -37,7 +37,7 @@ module Readers
37
37
  end
38
38
  def test_excel_standardized_open
39
39
  w = Workbook::Book.new
40
- w.open File.join(File.dirname(__FILE__), 'artifacts/excel_different_types.csv')
40
+ w.import File.join(File.dirname(__FILE__), 'artifacts/excel_different_types.csv')
41
41
  # reads
42
42
  # a,b,c,d
43
43
  # 2012-02-22,2014-12-27,2012-11-23,2012-11-12T04:20:00+00:00
@@ -49,7 +49,7 @@ module Readers
49
49
  assert_equal("c",w.sheet.table[2][:a].value)
50
50
  assert_equal(DateTime.new(2012,1,22,11),w.sheet.table[3][:a].value)
51
51
  assert_equal(42000,w.sheet.table[3][:b].value)
52
- assert_equal(nil,w.sheet.table[2][:c].value)
52
+ assert_nil(w.sheet.table[2][:c].value)
53
53
  end
54
54
  def test_class_read_string
55
55
  s = File.read File.join(File.dirname(__FILE__), 'artifacts/simple_csv.csv')
@@ -5,7 +5,7 @@ module Readers
5
5
  def test_ods_open
6
6
 
7
7
  w = Workbook::Book.new
8
- w.open File.join(File.dirname(__FILE__), 'artifacts/book_with_tabs_and_colours.ods')
8
+ w.import File.join(File.dirname(__FILE__), 'artifacts/book_with_tabs_and_colours.ods')
9
9
 
10
10
  assert_equal([:a, :b, :c, :d, :e],w.sheet.table.header.to_symbols)
11
11
  assert_equal(90588,w.sheet.table[2][:b].value)
@@ -13,7 +13,7 @@ module Readers
13
13
 
14
14
  def test_styling
15
15
  w = Workbook::Book.new
16
- w.open File.join(File.dirname(__FILE__), 'artifacts/book_with_tabs_and_colours.ods')
16
+ w.import File.join(File.dirname(__FILE__), 'artifacts/book_with_tabs_and_colours.ods')
17
17
  assert_equal("#ffff99",w.sheet.table[3][:c].format[:background_color])
18
18
  assert_equal(true,w.sheet.table[0][:e].format.all_names.include?("Heading1"))
19
19
  # TODO: column styles
@@ -24,7 +24,7 @@ module Readers
24
24
 
25
25
  def test_complex_types
26
26
  w = Workbook::Book.new
27
- w.open File.join(File.dirname(__FILE__), 'artifacts/complex_types.ods')
27
+ w.import File.join(File.dirname(__FILE__), 'artifacts/complex_types.ods')
28
28
  assert_equal(Date.new(2011,11,15), w.sheet.table[2][3].value)
29
29
  assert_equal("http://murb.nl", w.sheet.table[3][2].value)
30
30
  assert_equal("Sadfasdfsd > 2", w.sheet.table[4][2].value)
@@ -33,24 +33,24 @@ module Readers
33
33
 
34
34
  def test_excel_standardized_open
35
35
  w = Workbook::Book.new
36
- w.open File.join(File.dirname(__FILE__), 'artifacts/excel_different_types.ods')
36
+ w.import File.join(File.dirname(__FILE__), 'artifacts/excel_different_types.ods')
37
37
  assert_equal([:a,:b,:c, :d],w.sheet.table.header.to_symbols[0..3])
38
38
  assert_equal(Date.new(2012,2,22),w.sheet.table[1][:a].value)
39
39
  assert_equal("c",w.sheet.table[2][:a].value)
40
40
  assert_equal(DateTime.new(2012,1,22,11),w.sheet.table[3][:a].value)
41
41
  assert_equal("42000",w.sheet.table[3][:b].value.to_s)
42
- assert_equal(nil,w.sheet.table[2][:c].value)
42
+ assert_nil(w.sheet.table[2][:c].value)
43
43
  end
44
44
 
45
45
  def test_sheet_with_combined_cells
46
46
  w = Workbook::Book.new
47
- w.open File.join(File.dirname(__FILE__), 'artifacts/sheet_with_combined_cells.ods')
47
+ w.import File.join(File.dirname(__FILE__), 'artifacts/sheet_with_combined_cells.ods')
48
48
  t = w.sheet.table
49
49
  assert_equal("14 90589",t[1][:a].value)
50
50
  assert_equal(Workbook::NilValue,t[1][:b].value.class)
51
51
  assert_equal(:covered,t[1][:b].value.reason)
52
52
  assert_equal(2,t[1][:a].colspan)
53
- assert_equal(nil,t[1][:c].colspan)
53
+ assert_nil(t[1][:c].colspan)
54
54
  assert_equal(2,t["D3"].rowspan)
55
55
  assert_equal(2,t["D5"].rowspan)
56
56
  assert_equal(2,t["D5"].colspan)
@@ -58,11 +58,11 @@ module Readers
58
58
 
59
59
  def test_book_with_colspans
60
60
  w = Workbook::Book.new
61
- w.open File.join(File.dirname(__FILE__), 'artifacts/book_with_colspans.ods')
61
+ w.import File.join(File.dirname(__FILE__), 'artifacts/book_with_colspans.ods')
62
62
  t = w.sheet.table
63
63
  assert_equal(2,t["B1"].colspan)
64
64
  assert_equal(2,t["D1"].colspan)
65
- assert_equal(nil,t["D3"].value)
65
+ assert_nil(t["D3"].value)
66
66
  assert_equal("g",t["A19"].value)
67
67
  assert_equal(0.03,t["D17"].value)
68
68
  end