axlsx 1.0.15 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (184) hide show
  1. data/.yardopts +1 -0
  2. data/CHANGELOG.md +58 -2
  3. data/README.md +425 -207
  4. data/Rakefile +6 -2
  5. data/examples/axlsx.xlsx +0 -0
  6. data/examples/example.csv +1000 -0
  7. data/examples/example.rb +266 -132
  8. data/examples/example.xlsx +0 -0
  9. data/examples/example_streamed.xlsx +0 -0
  10. data/examples/no-use_autowidth.xlsx +0 -0
  11. data/examples/real_example.rb +63 -0
  12. data/examples/sample.png +0 -0
  13. data/examples/shared_strings_example.xlsx +0 -0
  14. data/lib/axlsx/content_type/content_type.rb +13 -12
  15. data/lib/axlsx/content_type/default.rb +10 -6
  16. data/lib/axlsx/content_type/override.rb +13 -8
  17. data/lib/axlsx/doc_props/app.rb +38 -41
  18. data/lib/axlsx/doc_props/core.rb +13 -17
  19. data/lib/axlsx/drawing/axis.rb +39 -19
  20. data/lib/axlsx/drawing/bar_3D_chart.rb +34 -32
  21. data/lib/axlsx/drawing/bar_series.rb +14 -14
  22. data/lib/axlsx/drawing/cat_axis.rb +16 -14
  23. data/lib/axlsx/drawing/cat_axis_data.rb +17 -18
  24. data/lib/axlsx/drawing/chart.rb +40 -41
  25. data/lib/axlsx/drawing/drawing.rb +27 -12
  26. data/lib/axlsx/drawing/graphic_frame.rb +22 -21
  27. data/lib/axlsx/drawing/hlink_click.rb~ +0 -0
  28. data/lib/axlsx/drawing/hyperlink.rb +96 -0
  29. data/lib/axlsx/drawing/hyperlink.rb~ +64 -0
  30. data/lib/axlsx/drawing/line_3D_chart.rb +31 -28
  31. data/lib/axlsx/drawing/line_series.rb +12 -11
  32. data/lib/axlsx/drawing/marker.rb +11 -8
  33. data/lib/axlsx/drawing/named_axis_data.rb +36 -0
  34. data/lib/axlsx/drawing/one_cell_anchor.rb +18 -16
  35. data/lib/axlsx/drawing/pic.rb +47 -33
  36. data/lib/axlsx/drawing/picture_locking.rb +22 -18
  37. data/lib/axlsx/drawing/pie_3D_chart.rb +11 -8
  38. data/lib/axlsx/drawing/pie_series.rb +16 -12
  39. data/lib/axlsx/drawing/scaling.rb +11 -10
  40. data/lib/axlsx/drawing/scatter_chart.rb +69 -0
  41. data/lib/axlsx/drawing/scatter_series.rb +39 -0
  42. data/lib/axlsx/drawing/ser_axis.rb +11 -10
  43. data/lib/axlsx/drawing/series.rb +16 -15
  44. data/lib/axlsx/drawing/series_title.rb +15 -14
  45. data/lib/axlsx/drawing/title.rb +27 -26
  46. data/lib/axlsx/drawing/two_cell_anchor.rb +19 -20
  47. data/lib/axlsx/drawing/val_axis.rb +9 -7
  48. data/lib/axlsx/drawing/val_axis_data.rb +18 -17
  49. data/lib/axlsx/drawing/view_3D.rb +23 -20
  50. data/lib/axlsx/package.rb +117 -52
  51. data/lib/axlsx/rels/relationship.rb +27 -8
  52. data/lib/axlsx/rels/relationships.rb +9 -2
  53. data/lib/axlsx/stylesheet/border.rb +28 -23
  54. data/lib/axlsx/stylesheet/border_pr.rb +17 -15
  55. data/lib/axlsx/stylesheet/cell_alignment.rb +24 -21
  56. data/lib/axlsx/stylesheet/cell_protection.rb +11 -7
  57. data/lib/axlsx/stylesheet/cell_style.rb +9 -5
  58. data/lib/axlsx/stylesheet/color.rb +20 -14
  59. data/lib/axlsx/stylesheet/fill.rb +8 -5
  60. data/lib/axlsx/stylesheet/font.rb +22 -14
  61. data/lib/axlsx/stylesheet/gradient_fill.rb +20 -17
  62. data/lib/axlsx/stylesheet/gradient_stop.rb +10 -6
  63. data/lib/axlsx/stylesheet/num_fmt.rb +13 -6
  64. data/lib/axlsx/stylesheet/pattern_fill.rb +26 -10
  65. data/lib/axlsx/stylesheet/styles.rb +55 -41
  66. data/lib/axlsx/stylesheet/table_style.rb +10 -4
  67. data/lib/axlsx/stylesheet/table_style_element.rb +11 -7
  68. data/lib/axlsx/stylesheet/table_styles.rb +12 -8
  69. data/lib/axlsx/stylesheet/xf.rb +30 -25
  70. data/lib/axlsx/util/cbf.rb +69 -33
  71. data/lib/axlsx/util/constants.rb +39 -20
  72. data/lib/axlsx/util/doc/_index.html +84 -0
  73. data/lib/axlsx/util/doc/class_list.html +47 -0
  74. data/lib/axlsx/util/doc/css/common.css +1 -0
  75. data/lib/axlsx/util/doc/css/full_list.css +55 -0
  76. data/lib/axlsx/util/doc/css/style.css +322 -0
  77. data/lib/axlsx/util/doc/file_list.html +46 -0
  78. data/lib/axlsx/util/doc/frames.html +13 -0
  79. data/lib/axlsx/util/doc/index.html +84 -0
  80. data/lib/axlsx/util/doc/js/app.js +205 -0
  81. data/lib/axlsx/util/doc/js/full_list.js +173 -0
  82. data/lib/axlsx/util/doc/js/jquery.js +16 -0
  83. data/lib/axlsx/util/doc/method_list.html +46 -0
  84. data/lib/axlsx/util/doc/top-level-namespace.html +95 -0
  85. data/lib/axlsx/util/font_tables.rb~ +0 -0
  86. data/lib/axlsx/util/ms_off_crypto.rb +10 -10
  87. data/lib/axlsx/util/parser.rb +1 -0
  88. data/lib/axlsx/util/simple_typed_list.rb +20 -10
  89. data/lib/axlsx/util/storage.rb +1 -1
  90. data/lib/axlsx/util/validators.rb +23 -7
  91. data/lib/axlsx/version.rb +5 -2
  92. data/lib/axlsx/workbook/shared_strings_table.rb +68 -0
  93. data/lib/axlsx/workbook/shared_strings_table.rb~ +69 -0
  94. data/lib/axlsx/workbook/workbook.rb +82 -26
  95. data/lib/axlsx/workbook/worksheet/cell.rb +166 -130
  96. data/lib/axlsx/workbook/worksheet/col.rb +114 -0
  97. data/lib/axlsx/workbook/worksheet/col.rb~ +0 -0
  98. data/lib/axlsx/workbook/worksheet/date_time_converter.rb +29 -0
  99. data/lib/axlsx/workbook/worksheet/page_margins.rb +97 -0
  100. data/lib/axlsx/workbook/worksheet/row.rb +53 -21
  101. data/lib/axlsx/workbook/worksheet/shared_strings_table.rb~ +0 -0
  102. data/lib/axlsx/workbook/worksheet/table.rb +96 -0
  103. data/lib/axlsx/workbook/worksheet/table.rb~ +97 -0
  104. data/lib/axlsx/workbook/worksheet/worksheet.rb +275 -124
  105. data/lib/axlsx.rb +41 -14
  106. data/lib/schema/dc.xsd +5 -5
  107. data/lib/schema/dcmitype.xsd +5 -3
  108. data/lib/schema/dcterms.xsd +15 -15
  109. data/lib/schema/opc-coreProperties.xsd +6 -2
  110. data/lib/schema/xml.xsd +7 -8
  111. data/test/benchmark.rb +73 -0
  112. data/test/content_type/tc_content_type.rb +31 -31
  113. data/test/content_type/tc_default.rb +9 -22
  114. data/test/content_type/tc_override.rb +8 -21
  115. data/test/doc_props/tc_app.rb +2 -8
  116. data/test/doc_props/tc_core.rb +6 -7
  117. data/test/drawing/tc_axis.rb +7 -3
  118. data/test/drawing/tc_bar_3D_chart.rb +6 -7
  119. data/test/drawing/tc_bar_series.rb +4 -5
  120. data/test/drawing/tc_cat_axis.rb +2 -3
  121. data/test/drawing/tc_cat_axis_data.rb +2 -3
  122. data/test/drawing/tc_chart.rb +13 -14
  123. data/test/drawing/tc_drawing.rb +7 -8
  124. data/test/drawing/tc_graphic_frame.rb +3 -4
  125. data/test/drawing/tc_hyperlink.rb +69 -0
  126. data/test/drawing/tc_line_3d_chart.rb +5 -6
  127. data/test/drawing/tc_line_series.rb +3 -4
  128. data/test/drawing/tc_marker.rb +3 -4
  129. data/test/drawing/tc_one_cell_anchor.rb +6 -7
  130. data/test/drawing/tc_pic.rb +13 -8
  131. data/test/drawing/tc_picture_locking.rb +2 -3
  132. data/test/drawing/tc_pie_3D_chart.rb +5 -6
  133. data/test/drawing/tc_pie_series.rb +4 -5
  134. data/test/drawing/tc_scaling.rb +3 -4
  135. data/test/drawing/tc_scatter_chart.rb +43 -0
  136. data/test/drawing/tc_scatter_series.rb +20 -0
  137. data/test/drawing/tc_ser_axis.rb +2 -3
  138. data/test/drawing/tc_series.rb +4 -5
  139. data/test/drawing/tc_series_title.rb +4 -5
  140. data/test/drawing/tc_title.rb +4 -5
  141. data/test/drawing/tc_two_cell_anchor.rb +4 -5
  142. data/test/drawing/tc_val_axis.rb +2 -3
  143. data/test/drawing/tc_val_axis_data.rb +2 -3
  144. data/test/drawing/tc_view_3D.rb +6 -7
  145. data/test/profile.rb +34 -0
  146. data/test/rels/tc_relationship.rb +10 -6
  147. data/test/rels/tc_relationships.rb +4 -5
  148. data/test/stylesheet/tc_border.rb +3 -4
  149. data/test/stylesheet/tc_border_pr.rb +3 -4
  150. data/test/stylesheet/tc_cell_alignment.rb +10 -6
  151. data/test/stylesheet/tc_cell_protection.rb +2 -3
  152. data/test/stylesheet/tc_cell_style.rb +2 -3
  153. data/test/stylesheet/tc_color.rb +2 -3
  154. data/test/stylesheet/tc_fill.rb +1 -2
  155. data/test/stylesheet/tc_font.rb +12 -5
  156. data/test/stylesheet/tc_gradient_fill.rb +1 -2
  157. data/test/stylesheet/tc_gradient_stop.rb +1 -2
  158. data/test/stylesheet/tc_num_fmt.rb +1 -2
  159. data/test/stylesheet/tc_pattern_fill.rb +3 -4
  160. data/test/stylesheet/tc_styles.rb +31 -6
  161. data/test/stylesheet/tc_table_style.rb +2 -3
  162. data/test/stylesheet/tc_table_style_element.rb +2 -3
  163. data/test/stylesheet/tc_table_styles.rb +3 -4
  164. data/test/stylesheet/tc_xf.rb +16 -17
  165. data/test/tc_axlsx.rb +39 -0
  166. data/test/tc_helper.rb +3 -0
  167. data/test/tc_package.rb +43 -9
  168. data/test/util/tc_simple_typed_list.rb +8 -9
  169. data/test/util/tc_validators.rb +7 -8
  170. data/test/workbook/tc_shared_strings_table.rb +38 -0
  171. data/test/workbook/tc_workbook.rb +32 -6
  172. data/test/workbook/worksheet/table/tc_table.rb +71 -0
  173. data/test/workbook/worksheet/table/tc_table.rb~ +72 -0
  174. data/test/workbook/worksheet/tc_cell.rb +80 -13
  175. data/test/workbook/worksheet/tc_col.rb +59 -0
  176. data/test/workbook/worksheet/tc_col.rb~ +10 -0
  177. data/test/workbook/worksheet/tc_date_time_converter.rb +132 -0
  178. data/test/workbook/worksheet/tc_page_margins.rb +97 -0
  179. data/test/workbook/worksheet/tc_row.rb +54 -4
  180. data/test/workbook/worksheet/tc_worksheet.rb +205 -31
  181. metadata +78 -84
  182. data/examples/example.rb~ +0 -112
  183. data/test/drawing/tc_line_series.tc~ +0 -34
  184. data/test/drawing/tc_picture_locking.rb~ +0 -77
data/test/tc_package.rb CHANGED
@@ -1,8 +1,7 @@
1
- require 'test/unit'
2
- require 'axlsx.rb'
1
+ require 'tc_helper.rb'
3
2
 
4
3
  class TestPackage < Test::Unit::TestCase
5
- def setup
4
+ def setup
6
5
  @package = Axlsx::Package.new
7
6
  ws = @package.workbook.add_worksheet
8
7
  chart = ws.add_chart Axlsx::Pie3DChart
@@ -21,6 +20,13 @@ class TestPackage < Test::Unit::TestCase
21
20
  assert_raise(NoMethodError) {@package.app = nil }
22
21
  end
23
22
 
23
+ def test_use_shared_strings
24
+ assert_equal(@package.use_shared_strings, nil)
25
+ assert_raise(ArgumentError) {@package.use_shared_strings 9}
26
+ assert_nothing_raised {@package.use_shared_strings = true}
27
+ assert_equal(@package.use_shared_strings, @package.workbook.use_shared_strings)
28
+ end
29
+
24
30
  def test_default_objects_are_created
25
31
  assert(@package.instance_values["app"].is_a?(Axlsx::App), 'App object not created')
26
32
  assert(@package.instance_values["core"].is_a?(Axlsx::Core), 'Core object not created')
@@ -32,16 +38,16 @@ class TestPackage < Test::Unit::TestCase
32
38
  fname = 'axlsx_test_serialization.xlsx'
33
39
  assert_nothing_raised do
34
40
  begin
35
- z= @package.serialize(@fname)
41
+ z= @package.serialize(@fname)
36
42
  zf = Zip::ZipFile.open(@fname)
37
43
  @package.send(:parts).each{ |part| zf.get_entry(part[:entry]) }
38
- File.delete(@fname)
44
+ File.delete(@fname)
39
45
  rescue Errno::EACCES
40
46
  puts "WARNING:: test_serialization requires write access."
41
47
  end
42
- end
48
+ end
43
49
  end
44
-
50
+
45
51
  def test_validation
46
52
  assert_equal(@package.validate.size, 0, @package.validate)
47
53
  #how to test for failure? the internal validations on the models are so strict I cant break anthing.....
@@ -51,7 +57,7 @@ class TestPackage < Test::Unit::TestCase
51
57
  p = @package.send(:parts)
52
58
  p.each do |part|
53
59
  #all parts must have :doc, :entry, :schema
54
- assert(part.keys.size == 3 && part.keys.reject{ |k| [:doc, :entry, :schema].include? k}.empty?)
60
+ assert(part.keys.size == 3 && part.keys.reject{ |k| [:doc, :entry, :schema].include? k}.empty?)
55
61
  end
56
62
  #all parts have an entry
57
63
  assert_equal(p.select{ |part| part[:entry] =~ /_rels\/\.rels/ }.size, 1, "rels missing")
@@ -71,8 +77,36 @@ class TestPackage < Test::Unit::TestCase
71
77
  assert_equal(p.size, 12)
72
78
 
73
79
  end
74
-
80
+
81
+ def test_shared_strings_requires_part
82
+ @package.use_shared_strings = true
83
+ p = @package.send(:parts)
84
+ assert_equal(p.select{ |part| part[:entry] =~/xl\/sharedStrings.xml/}.size, 1, "shared strings table missing")
85
+ end
86
+
75
87
  def test_workbook_is_a_workbook
76
88
  assert @package.workbook.is_a? Axlsx::Workbook
77
89
  end
90
+
91
+ def test_base_content_types
92
+ ct = @package.send(:base_content_types)
93
+ assert(ct.select { |c| c.ContentType == Axlsx::RELS_CT }.size == 1, "rels content type missing")
94
+ assert(ct.select { |c| c.ContentType == Axlsx::XML_CT }.size == 1, "xml content type missing")
95
+ assert(ct.select { |c| c.ContentType == Axlsx::APP_CT }.size == 1, "app content type missing")
96
+ assert(ct.select { |c| c.ContentType == Axlsx::CORE_CT }.size == 1, "core content type missing")
97
+ assert(ct.select { |c| c.ContentType == Axlsx::STYLES_CT }.size == 1, "styles content type missing")
98
+ assert(ct.select { |c| c.ContentType == Axlsx::WORKBOOK_CT }.size == 1, "workbook content type missing")
99
+ assert(ct.size == 6)
100
+ end
101
+
102
+ def test_content_type_added_with_shared_strings
103
+ @package.use_shared_strings = true
104
+ ct = @package.send(:content_types)
105
+ assert(ct.select { |ct| ct.ContentType == Axlsx::SHARED_STRINGS_CT }.size == 1)
106
+ end
107
+
108
+ def test_name_to_indices
109
+ assert(Axlsx::name_to_indices('A1') == [0,0])
110
+ assert(Axlsx::name_to_indices('A100') == [0,99], 'needs to axcept rows that contain 0')
111
+ end
78
112
  end
@@ -1,11 +1,10 @@
1
- require 'test/unit'
2
- require 'axlsx.rb'
1
+ require 'tc_helper.rb'
3
2
  class TestSimpleTypedList < Test::Unit::TestCase
4
- def setup
3
+ def setup
5
4
  @list = Axlsx::SimpleTypedList.new Fixnum
6
5
  end
7
6
 
8
- def teardown
7
+ def teardown
9
8
  end
10
9
 
11
10
  def test_type_is_a_class_or_array_of_class
@@ -15,18 +14,18 @@ class TestSimpleTypedList < Test::Unit::TestCase
15
14
  assert_raise(ArgumentError) { Axlsx::SimpleTypedList.new "1" }
16
15
  assert_raise(ArgumentError) { Axlsx::SimpleTypedList.new [Integer, "Class"] }
17
16
  end
18
-
17
+
19
18
  def test_indexed_based_assignment
20
19
  #should not allow nil assignment
21
20
  assert_raise(ArgumentError) { @list[0] = nil }
22
21
  assert_raise(ArgumentError) { @list[0] = "1" }
23
22
  assert_nothing_raised { @list[0] = 1 }
24
23
  end
25
-
24
+
26
25
  def test_concat_assignment
27
26
  assert_raise(ArgumentError) { @list << nil }
28
27
  assert_raise(ArgumentError) { @list << "1" }
29
- assert_nothing_raised { @list << 1 }
28
+ assert_nothing_raised { @list << 1 }
30
29
  end
31
30
 
32
31
  def test_concat_should_return_index
@@ -59,8 +58,8 @@ class TestSimpleTypedList < Test::Unit::TestCase
59
58
  assert_nothing_raised { @list.delete_at 3 }
60
59
  @list.unlock
61
60
  #ignore garbage
62
- assert_nothing_raised { @list.delete 0 }
63
- assert_nothing_raised { @list.delete 9 }
61
+ assert_nothing_raised { @list.delete 0 }
62
+ assert_nothing_raised { @list.delete 9 }
64
63
  end
65
64
 
66
65
  end
@@ -1,30 +1,29 @@
1
- require 'test/unit'
2
- require 'axlsx.rb'
1
+ require 'tc_helper.rb'
3
2
  class TestValidators < Test::Unit::TestCase
4
- def setup
3
+ def setup
5
4
  end
6
5
  def teardown
7
6
  end
8
-
7
+
9
8
  def test_validators
10
9
  #unsigned_int
11
10
  assert_nothing_raised { Axlsx.validate_unsigned_int 1 }
12
11
  assert_nothing_raised { Axlsx.validate_unsigned_int +1 }
13
12
  assert_raise(ArgumentError) { Axlsx.validate_unsigned_int -1 }
14
13
  assert_raise(ArgumentError) { Axlsx.validate_unsigned_int '1' }
15
-
14
+
16
15
  #int
17
16
  assert_nothing_raised { Axlsx.validate_int 1 }
18
17
  assert_nothing_raised { Axlsx.validate_int -1 }
19
18
  assert_raise(ArgumentError) { Axlsx.validate_int 'a' }
20
19
  assert_raise(ArgumentError) { Axlsx.validate_int Array }
21
-
20
+
22
21
  #boolean (as 0 or 1, :true, :false, true, false, or "true," "false")
23
22
  [0,1,:true, :false, true, false, "true", "false"].each do |v|
24
23
  assert_nothing_raised { Axlsx.validate_boolean 0 }
25
24
  end
26
25
  assert_raise(ArgumentError) { Axlsx.validate_boolean 2 }
27
-
26
+
28
27
  #string
29
28
  assert_nothing_raised { Axlsx.validate_string "1" }
30
29
  assert_raise(ArgumentError) { Axlsx.validate_string 2 }
@@ -64,7 +63,7 @@ class TestValidators < Test::Unit::TestCase
64
63
  assert_raise(ArgumentError) { Axlsx.validate_content_type nil }
65
64
  assert_raise(ArgumentError) { Axlsx.validate_content_type "http://some.url" }
66
65
  assert_raise(ArgumentError) { Axlsx.validate_content_type false }
67
-
66
+
68
67
  #relationshipType
69
68
  assert_nothing_raised { Axlsx.validate_relationship_type Axlsx::WORKBOOK_R }
70
69
  assert_raise(ArgumentError) { Axlsx.validate_relationship_type nil }
@@ -0,0 +1,38 @@
1
+ require 'tc_helper.rb'
2
+
3
+ class TestSharedStringsTable < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @p = Axlsx::Package.new :use_shared_strings=>true
7
+ ws = @p.workbook.add_worksheet
8
+ ws.add_row ['a', 1, 'b']
9
+ ws.add_row ['b', 1, 'c']
10
+ ws.add_row ['c', 1, 'd']
11
+ end
12
+
13
+ def test_workbook_has_shared_strings
14
+ assert(@p.workbook.shared_strings.is_a?(Axlsx::SharedStringsTable), "shared string table was not created")
15
+ end
16
+
17
+ def test_count
18
+ sst = @p.workbook.shared_strings
19
+ assert_equal(sst.count, 6)
20
+ end
21
+
22
+ def test_unique_count
23
+ sst = @p.workbook.shared_strings
24
+ assert_equal(sst.unique_count, 4)
25
+ end
26
+
27
+ def test_valid_document
28
+ schema = Nokogiri::XML::Schema(File.open(Axlsx::SML_XSD))
29
+ doc = Nokogiri::XML(@p.workbook.shared_strings.to_xml_string)
30
+ errors = []
31
+ schema.validate(doc).each do |error|
32
+ puts error.message
33
+ errors << error
34
+ end
35
+ assert_equal(errors.size, 0, "sharedStirngs.xml Invalid" + errors.map{ |e| e.message }.to_s)
36
+ end
37
+
38
+ end
@@ -1,15 +1,21 @@
1
- require 'test/unit'
2
- require 'axlsx.rb'
1
+ require 'tc_helper.rb'
3
2
 
4
3
  class TestWorkbook < Test::Unit::TestCase
5
- def setup
4
+ def setup
6
5
  p = Axlsx::Package.new
7
- @wb = p.workbook
6
+ @wb = p.workbook
8
7
  end
9
8
 
10
9
  def teardown
11
10
  end
12
11
 
12
+ def test_no_autowidth
13
+ assert_equal(@wb.use_autowidth, true)
14
+ assert_raise(ArgumentError) {@wb.use_autowidth = 0.1}
15
+ assert_nothing_raised {@wb.use_autowidth = false}
16
+ assert_equal(@wb.use_autowidth, false)
17
+ end
18
+
13
19
  def test_date1904
14
20
  assert_equal(Axlsx::Workbook.date1904, @wb.date1904)
15
21
  @wb.date1904 = :false
@@ -18,6 +24,14 @@ class TestWorkbook < Test::Unit::TestCase
18
24
  assert_equal(Axlsx::Workbook.date1904, @wb.date1904)
19
25
  end
20
26
 
27
+
28
+
29
+ def test_shared_strings
30
+ assert_equal(@wb.use_shared_strings, nil)
31
+ assert_raise(ArgumentError) {@wb.use_shared_strings = 'bpb'}
32
+ assert_nothing_raised {@wb.use_shared_strings = :true}
33
+ end
34
+
21
35
  def test_add_worksheet
22
36
  assert(@wb.worksheets.empty?, "worbook has no worksheets by default")
23
37
  ws = @wb.add_worksheet(:name=>"bob")
@@ -30,11 +44,13 @@ class TestWorkbook < Test::Unit::TestCase
30
44
  assert(@wb.relationships.size == 1)
31
45
  @wb.add_worksheet
32
46
  assert(@wb.relationships.size == 2)
47
+ @wb.use_shared_strings = true
48
+ assert(@wb.relationships.size == 3)
33
49
  end
34
50
 
35
51
  def test_to_xml
36
52
  schema = Nokogiri::XML::Schema(File.open(Axlsx::SML_XSD))
37
- doc = Nokogiri::XML(@wb.to_xml)
53
+ doc = Nokogiri::XML(@wb.to_xml_string)
38
54
  errors = []
39
55
  schema.validate(doc).each do |error|
40
56
  errors.push error
@@ -52,9 +68,19 @@ class TestWorkbook < Test::Unit::TestCase
52
68
 
53
69
  def test_to_xml_adds_worksheet_when_worksheets_is_empty
54
70
  assert(@wb.worksheets.empty?)
55
- @wb.to_xml
71
+ @wb.to_xml_string
56
72
  assert(@wb.worksheets.size == 1)
57
73
  end
58
74
 
59
75
 
76
+ def test_to_xml_string_defined_names
77
+ @wb.add_worksheet do |sheet|
78
+ sheet.add_row [1, "two"]
79
+ sheet.auto_filter = "A1:B1"
80
+ end
81
+ doc = Nokogiri::XML(@wb.to_xml_string)
82
+ assert_equal(doc.xpath('//xmlns:workbook/xmlns:definedNames/xmlns:definedName').inner_text, @wb.worksheets[0].abs_auto_filter)
83
+ end
84
+
85
+
60
86
  end
@@ -0,0 +1,71 @@
1
+ require 'tc_helper.rb'
2
+
3
+ class TestTable < Test::Unit::TestCase
4
+ def setup
5
+ p = Axlsx::Package.new
6
+ @ws = p.workbook.add_worksheet
7
+ 40.times do
8
+ @ws << ["aa","aa","aa","aa","aa","aa"]
9
+ end
10
+ end
11
+
12
+ def test_initialization
13
+ assert(@ws.workbook.tables.empty?)
14
+ assert(@ws.tables.empty?)
15
+
16
+ end
17
+
18
+ def test_add_table
19
+ name = "test"
20
+ table = @ws.add_table("A1:D5", :name => name)
21
+ assert(table.is_a?(Axlsx::Table), "must create a table")
22
+ assert_equal(@ws.workbook.tables.last, table, "must be added to workbook table collection")
23
+ assert_equal(@ws.tables.last, table, "must be added to worksheet table collection")
24
+ assert_equal(table.name, name, "options for name are applied")
25
+
26
+ end
27
+
28
+ def test_charts
29
+ assert(@ws.drawing.charts.empty?)
30
+ chart = @ws.add_chart(Axlsx::Pie3DChart, :title=>"bob", :start_at=>[0,0], :end_at=>[1,1])
31
+ assert_equal(@ws.drawing.charts.last, chart, "add chart is returned")
32
+ chart = @ws.add_chart(Axlsx::Pie3DChart, :title=>"nancy", :start_at=>[1,5], :end_at=>[5,10])
33
+ assert_equal(@ws.drawing.charts.last, chart, "add chart is returned")
34
+ end
35
+
36
+ def test_pn
37
+ table = @ws.add_table("A1:D5")
38
+ assert_equal(@ws.tables.first.pn, "tables/table1.xml")
39
+ end
40
+
41
+ def test_rId
42
+ table = @ws.add_table("A1:D5")
43
+ assert_equal(@ws.tables.first.rId, "rId1")
44
+ end
45
+
46
+ def test_index
47
+ table = @ws.add_table("A1:D5")
48
+ assert_equal(@ws.tables.first.index, @ws.workbook.tables.index(@ws.tables.first))
49
+ end
50
+
51
+ def test_relationships
52
+ assert(@ws.relationships.empty?)
53
+ table = @ws.add_table("A1:D5")
54
+ assert_equal(@ws.relationships.size, 1, "adding a table adds a relationship")
55
+ table = @ws.add_table("F1:J5")
56
+ assert_equal(@ws.relationships.size, 2, "adding a table adds a relationship")
57
+ end
58
+
59
+ def test_to_xml_string
60
+ table = @ws.add_table("A1:D5")
61
+ schema = Nokogiri::XML::Schema(File.open(Axlsx::SML_XSD))
62
+ doc = Nokogiri::XML(table.to_xml_string)
63
+ errors = []
64
+ schema.validate(doc).each do |error|
65
+ errors.push error
66
+ puts error.message
67
+ end
68
+ assert(errors.empty?, "error free validation")
69
+ end
70
+
71
+ end
@@ -0,0 +1,72 @@
1
+ require 'test/unit'
2
+ require 'tc_helper.rb'
3
+
4
+ class TestTable < Test::Unit::TestCase
5
+ def setup
6
+ p = Axlsx::Package.new
7
+ @ws = p.workbook.add_worksheet
8
+ 40.times do
9
+ @ws << ["aa","aa","aa","aa","aa","aa"]
10
+ end
11
+ end
12
+
13
+ def test_initialization
14
+ assert(@ws.workbook.tables.empty?)
15
+ assert(@ws.tables.empty?)
16
+
17
+ end
18
+
19
+ def test_add_table
20
+ name = "test"
21
+ table = @ws.add_table("A1:D5", :name => name)
22
+ assert(table.is_a?(Axlsx::Table), "must create a table")
23
+ assert_equal(@ws.workbook.tables.last, table, "must be added to workbook table collection")
24
+ assert_equal(@ws.tables.last, table, "must be added to worksheet table collection")
25
+ assert_equal(table.name, name, "options for name are applied")
26
+
27
+ end
28
+
29
+ def test_charts
30
+ assert(@ws.drawing.charts.empty?)
31
+ chart = @ws.add_chart(Axlsx::Pie3DChart, :title=>"bob", :start_at=>[0,0], :end_at=>[1,1])
32
+ assert_equal(@ws.drawing.charts.last, chart, "add chart is returned")
33
+ chart = @ws.add_chart(Axlsx::Pie3DChart, :title=>"nancy", :start_at=>[1,5], :end_at=>[5,10])
34
+ assert_equal(@ws.drawing.charts.last, chart, "add chart is returned")
35
+ end
36
+
37
+ def test_pn
38
+ table = @ws.add_table("A1:D5")
39
+ assert_equal(@ws.tables.first.pn, "tables/table1.xml")
40
+ end
41
+
42
+ def test_rId
43
+ table = @ws.add_table("A1:D5")
44
+ assert_equal(@ws.tables.first.rId, "rId1")
45
+ end
46
+
47
+ def test_index
48
+ table = @ws.add_table("A1:D5")
49
+ assert_equal(@ws.tables.first.index, @ws.workbook.tables.index(@ws.tables.first))
50
+ end
51
+
52
+ def test_relationships
53
+ assert(@ws.relationships.empty?)
54
+ table = @ws.add_table("A1:D5")
55
+ assert_equal(@ws.relationships.size, 1, "adding a table adds a relationship")
56
+ table = @ws.add_table("F1:J5")
57
+ assert_equal(@ws.relationships.size, 2, "adding a table adds a relationship")
58
+ end
59
+
60
+ def test_to_xml
61
+ table = @ws.add_table("A1:D5")
62
+ schema = Nokogiri::XML::Schema(File.open(Axlsx::SML_XSD))
63
+ doc = Nokogiri::XML(table.to_xml)
64
+ errors = []
65
+ schema.validate(doc).each do |error|
66
+ errors.push error
67
+ puts error.message
68
+ end
69
+ assert(errors.empty?, "error free validation")
70
+ end
71
+
72
+ end
@@ -1,5 +1,4 @@
1
- require 'test/unit'
2
- require 'axlsx.rb'
1
+ require 'tc_helper.rb'
3
2
 
4
3
  class TestCell < Test::Unit::TestCase
5
4
 
@@ -9,8 +8,11 @@ class TestCell < Test::Unit::TestCase
9
8
  p.workbook.styles.add_style :sz=>20
10
9
  @row = @ws.add_row
11
10
  @c = @row.add_cell 1, :type=>:float, :style=>1
11
+ data = (0..26).map { |index| index }
12
+ @ws.add_row data
13
+ @cAA = @ws["AA2"]
12
14
  end
13
-
15
+
14
16
  def test_initialize
15
17
  assert_equal(@row.cells.last, @c, "the cell was added to the row")
16
18
  assert_equal(@c.type, :float, "type option is applied")
@@ -23,6 +25,10 @@ class TestCell < Test::Unit::TestCase
23
25
  assert_equal(Axlsx::STYLE_DATE, c.style)
24
26
  end
25
27
 
28
+ def test_row
29
+ assert_equal(@c.row, @row)
30
+ end
31
+
26
32
  def test_index
27
33
  assert_equal(@c.index, @row.cells.index(@c))
28
34
  end
@@ -35,45 +41,56 @@ class TestCell < Test::Unit::TestCase
35
41
  assert_equal(@c.r, "A1", "calculate cell reference")
36
42
  end
37
43
 
44
+ def test_wide_r
45
+ assert_equal(@cAA.r, "AA2", "calculate cell reference")
46
+ end
47
+
38
48
  def test_r_abs
39
49
  assert_equal(@c.r_abs,"$A$1", "calculate absolute cell reference")
50
+ assert_equal(@cAA.r_abs,"$AA$2", "needs to accept multi-digit columns")
40
51
  end
41
52
 
42
53
  def test_style
43
54
  assert_raise(ArgumentError, "must reject invalid style indexes") { @c.style=@c.row.worksheet.workbook.styles.cellXfs.size }
44
- assert_nothing_raised("must allow valid style index changes") {@c.style=1}
55
+ assert_nothing_raised("must allow valid style index changes") {@c.style=1}
45
56
  assert_equal(@c.style, 1)
46
57
  end
47
58
 
48
59
  def test_type
49
- assert_raise(ArgumentError, "type must be :string, :integer, :float, :time") { @c.type = :array }
60
+ assert_raise(ArgumentError, "type must be :string, :integer, :float, :date, :time, :boolean") { @c.type = :array }
50
61
  assert_nothing_raised("type can be changed") { @c.type = :string }
51
62
  assert_equal(@c.value, "1.0", "changing type casts the value")
52
-
63
+
53
64
  assert_equal(@row.add_cell(Time.now).type, :time, 'time should be time')
65
+ assert_equal(@row.add_cell(Date.today).type, :date, 'date should be date')
66
+ assert_equal(@row.add_cell(true).type, :boolean, 'boolean should be boolean')
54
67
  end
55
68
 
56
69
  def test_value
57
- assert_raise(ArgumentError, "type must be :string, :integer, :float, :time") { @c.type = :array }
70
+ assert_raise(ArgumentError, "type must be :string, :integer, :float, :date, :time, :boolean") { @c.type = :array }
58
71
  assert_nothing_raised("type can be changed") { @c.type = :string }
59
72
  assert_equal(@c.value, "1.0", "changing type casts the value")
60
73
  end
61
74
 
62
75
  def test_col_ref
63
- assert_equal(@c.send(:col_ref), "A")
76
+ #TODO move to axlsx spec
77
+ assert_equal(Axlsx.col_ref(0), "A")
64
78
  end
65
79
 
66
80
  def test_cell_type_from_value
67
81
  assert_equal(@c.send(:cell_type_from_value, 1.0), :float)
68
82
  assert_equal(@c.send(:cell_type_from_value, 1), :integer)
83
+ assert_equal(@c.send(:cell_type_from_value, Date.today), :date)
69
84
  assert_equal(@c.send(:cell_type_from_value, Time.now), :time)
70
85
  assert_equal(@c.send(:cell_type_from_value, []), :string)
71
86
  assert_equal(@c.send(:cell_type_from_value, "d"), :string)
72
87
  assert_equal(@c.send(:cell_type_from_value, nil), :string)
73
88
  assert_equal(@c.send(:cell_type_from_value, -1), :integer)
89
+ assert_equal(@c.send(:cell_type_from_value, true), :boolean)
90
+ assert_equal(@c.send(:cell_type_from_value, false), :boolean)
74
91
  end
75
92
 
76
- def test_cast_value
93
+ def test_cast_value
77
94
  @c.type = :string
78
95
  assert_equal(@c.send(:cast_value, 1.0), "1.0")
79
96
  @c.type = :integer
@@ -81,8 +98,12 @@ class TestCell < Test::Unit::TestCase
81
98
  @c.type = :float
82
99
  assert_equal(@c.send(:cast_value, "1.0"), 1.0)
83
100
  @c.type = :string
84
- assert_equal(@c.send(:cast_value, nil), "")
85
-
101
+ assert_equal(@c.send(:cast_value, nil), nil)
102
+ @c.type = :float
103
+ assert_equal(@c.send(:cast_value, nil), nil)
104
+ @c.type = :boolean
105
+ assert_equal(@c.send(:cast_value, true), 1)
106
+ assert_equal(@c.send(:cast_value, false), 0)
86
107
  end
87
108
 
88
109
  def test_color
@@ -180,7 +201,7 @@ class TestCell < Test::Unit::TestCase
180
201
  @c.row.add_cell 2
181
202
  @c.row.add_cell 3
182
203
  @c.merge "A2"
183
- assert_equal(@c.row.worksheet.merged_cells.last, "A1:A2")
204
+ assert_equal(@c.row.worksheet.merged_cells.last, "A1:A2")
184
205
  end
185
206
 
186
207
  def test_merge_with_cell
@@ -188,7 +209,53 @@ class TestCell < Test::Unit::TestCase
188
209
  @c.row.add_cell 2
189
210
  @c.row.add_cell 3
190
211
  @c.merge @row.cells.last
191
- assert_equal(@c.row.worksheet.merged_cells.last, "A1:C1")
212
+ assert_equal(@c.row.worksheet.merged_cells.last, "A1:C1")
213
+ end
214
+
215
+ def test_ssti
216
+ assert_raise(ArgumentError, "ssti must be an unsigned integer!") { @c.send(:ssti=, -1) }
217
+ @c.send :ssti=, 1
218
+ assert_equal(@c.ssti, 1)
219
+ end
220
+
221
+ def test_plain_string
222
+ @c.type = :integer
223
+ assert_equal(@c.plain_string?, false)
224
+
225
+ @c.type = :string
226
+ @c.value = 'plain string'
227
+ assert_equal(@c.plain_string?, true)
228
+
229
+ @c.value = nil
230
+ assert_equal(@c.plain_string?, false)
231
+
232
+ @c.value = ''
233
+ assert_equal(@c.plain_string?, false)
234
+
235
+ @c.value = '=sum'
236
+ assert_equal(@c.plain_string?, false)
237
+
238
+ @c.value = 'plain string'
239
+ @c.font_name = 'Arial'
240
+ assert_equal(@c.plain_string?, false)
241
+ end
242
+
243
+ def test_to_xml_string
244
+ c_xml = Nokogiri::XML(@c.to_xml_string(1,1))
245
+ assert_equal(c_xml.xpath("/c[@s=1]").size, 1)
246
+ end
247
+ def test_to_xml
248
+ # TODO This could use some much more stringent testing related to the xml content generated!
249
+ row = @ws.add_row [Time.now, Date.today, true, 1, 1.0, "text", "=sum(A1:A2)"]
250
+ schema = Nokogiri::XML::Schema(File.open(Axlsx::SML_XSD))
251
+ doc = Nokogiri::XML(@ws.to_xml_string)
252
+ errors = []
253
+ schema.validate(doc).each do |error|
254
+ errors.push error
255
+ puts error.message
256
+ end
257
+ assert(errors.empty?, "error free validation")
258
+
192
259
  end
193
260
 
194
261
  end
@@ -0,0 +1,59 @@
1
+ require 'tc_helper.rb'
2
+
3
+ class TestCol < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @col = Axlsx::Col.new 1, 1
7
+ end
8
+
9
+ def test_min_max_required
10
+ assert_raise(ArgumentError, 'min and max must be specified when creating a new column') { Axlsx::Col.new }
11
+ assert_raise(ArgumentError, 'min and max must be specified when creating a new column') { Axlsx::Col.new nil, nil }
12
+ assert_nothing_raised { Axlsx::Col.new 1, 1 }
13
+ end
14
+
15
+ def test_bestFit
16
+ assert_equal(@col.bestFit, nil)
17
+ assert_raise(NoMethodError, 'bestFit is read only') { @col.bestFit = 'bob' }
18
+ @col.width = 1.999
19
+ assert_equal(@col.bestFit, true, 'bestFit should be true when width has been set')
20
+ end
21
+
22
+ def test_collapsed
23
+ assert_equal(@col.collapsed, nil)
24
+ assert_raise(ArgumentError, 'collapsed must be boolean(ish)') { @col.collapsed = 'bob' }
25
+ assert_nothing_raised('collapsed must be boolean(ish)') { @col.collapsed = true }
26
+ end
27
+
28
+ def test_customWidth
29
+ assert_equal(@col.customWidth, nil)
30
+ @col.width = 3
31
+ assert_raise(NoMethodError, 'customWidth is read only') { @col.customWidth = 3 }
32
+ assert_equal(@col.customWidth, true, 'customWidth is true when width is set')
33
+ end
34
+
35
+ def test_hidden
36
+ assert_equal(@col.hidden, nil)
37
+ assert_raise(ArgumentError, 'hidden must be boolean(ish)') { @col.hidden = 'bob' }
38
+ assert_nothing_raised(ArgumentError, 'hidden must be boolean(ish)') { @col.hidden = true }
39
+ end
40
+
41
+ def test_outlineLevel
42
+ assert_equal(@col.outlineLevel, nil)
43
+ assert_raise(ArgumentError, 'outline level cannot be negative') { @col.outlineLevel = -1 }
44
+ assert_raise(ArgumentError, 'outline level cannot be greater than 7') { @col.outlineLevel = 8 }
45
+ assert_nothing_raised('can set outlineLevel') { @col.outlineLevel = 1 }
46
+ end
47
+
48
+ def test_phonetic
49
+ assert_equal(@col.phonetic, nil)
50
+ assert_raise(ArgumentError, 'phonetic must be boolean(ish)') { @col.phonetic = 'bob' }
51
+ assert_nothing_raised(ArgumentError, 'phonetic must be boolean(ish)') { @col.phonetic = true }
52
+ end
53
+
54
+ def test_style
55
+ assert_equal(@col.style, nil)
56
+ #TODO check that the style specified is actually in the styles xfs collection
57
+ end
58
+
59
+ end
@@ -0,0 +1,10 @@
1
+ require 'tc_helper.rb'
2
+
3
+ class TestCol < Test::Unit::TestCase
4
+
5
+ def setup
6
+
7
+ end
8
+
9
+
10
+ end