caxlsx 2.0.2 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (181) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +5 -0
  3. data/README.md +41 -33
  4. data/Rakefile +9 -11
  5. data/examples/auto_filter.rb +10 -1
  6. data/examples/conditional_formatting/example_conditional_formatting.rb +18 -3
  7. data/examples/data_validation.rb +57 -40
  8. data/examples/example.rb +115 -7
  9. data/examples/merge_cells.rb +17 -0
  10. data/examples/no_grid_with_borders.rb +18 -0
  11. data/examples/pivot_test.rb +63 -0
  12. data/examples/split.rb +16 -0
  13. data/lib/axlsx.rb +34 -15
  14. data/lib/axlsx/content_type/abstract_content_type.rb +1 -1
  15. data/lib/axlsx/content_type/content_type.rb +1 -1
  16. data/lib/axlsx/doc_props/app.rb +1 -1
  17. data/lib/axlsx/doc_props/core.rb +5 -5
  18. data/lib/axlsx/drawing/area_chart.rb +99 -0
  19. data/lib/axlsx/drawing/area_series.rb +110 -0
  20. data/lib/axlsx/drawing/axes.rb +1 -1
  21. data/lib/axlsx/drawing/axis.rb +12 -9
  22. data/lib/axlsx/drawing/bar_3D_chart.rb +13 -13
  23. data/lib/axlsx/drawing/bar_chart.rb +143 -0
  24. data/lib/axlsx/drawing/bar_series.rb +9 -9
  25. data/lib/axlsx/drawing/bubble_chart.rb +59 -0
  26. data/lib/axlsx/drawing/bubble_series.rb +63 -0
  27. data/lib/axlsx/drawing/cat_axis.rb +5 -5
  28. data/lib/axlsx/drawing/chart.rb +52 -8
  29. data/lib/axlsx/drawing/d_lbls.rb +3 -3
  30. data/lib/axlsx/drawing/drawing.rb +6 -1
  31. data/lib/axlsx/drawing/graphic_frame.rb +3 -3
  32. data/lib/axlsx/drawing/hyperlink.rb +1 -3
  33. data/lib/axlsx/drawing/line_3D_chart.rb +2 -2
  34. data/lib/axlsx/drawing/line_chart.rb +10 -10
  35. data/lib/axlsx/drawing/line_series.rb +32 -3
  36. data/lib/axlsx/drawing/marker.rb +1 -1
  37. data/lib/axlsx/drawing/num_data.rb +4 -4
  38. data/lib/axlsx/drawing/num_data_source.rb +6 -6
  39. data/lib/axlsx/drawing/num_val.rb +3 -1
  40. data/lib/axlsx/drawing/one_cell_anchor.rb +3 -2
  41. data/lib/axlsx/drawing/pic.rb +25 -19
  42. data/lib/axlsx/drawing/picture_locking.rb +1 -3
  43. data/lib/axlsx/drawing/pie_3D_chart.rb +5 -6
  44. data/lib/axlsx/drawing/pie_series.rb +6 -6
  45. data/lib/axlsx/drawing/scaling.rb +6 -6
  46. data/lib/axlsx/drawing/scatter_chart.rb +10 -10
  47. data/lib/axlsx/drawing/scatter_series.rb +40 -7
  48. data/lib/axlsx/drawing/ser_axis.rb +2 -2
  49. data/lib/axlsx/drawing/series.rb +3 -3
  50. data/lib/axlsx/drawing/series_title.rb +2 -2
  51. data/lib/axlsx/drawing/str_data.rb +3 -3
  52. data/lib/axlsx/drawing/str_val.rb +3 -1
  53. data/lib/axlsx/drawing/title.rb +22 -4
  54. data/lib/axlsx/drawing/two_cell_anchor.rb +6 -1
  55. data/lib/axlsx/drawing/val_axis.rb +1 -1
  56. data/lib/axlsx/drawing/view_3D.rb +2 -2
  57. data/lib/axlsx/drawing/vml_drawing.rb +1 -1
  58. data/lib/axlsx/package.rb +33 -31
  59. data/lib/axlsx/rels/relationship.rb +1 -1
  60. data/lib/axlsx/rels/relationships.rb +7 -4
  61. data/lib/axlsx/stylesheet/border_pr.rb +2 -2
  62. data/lib/axlsx/stylesheet/cell_alignment.rb +1 -3
  63. data/lib/axlsx/stylesheet/cell_protection.rb +1 -3
  64. data/lib/axlsx/stylesheet/cell_style.rb +1 -3
  65. data/lib/axlsx/stylesheet/color.rb +1 -3
  66. data/lib/axlsx/stylesheet/font.rb +1 -1
  67. data/lib/axlsx/stylesheet/gradient_stop.rb +1 -1
  68. data/lib/axlsx/stylesheet/num_fmt.rb +10 -3
  69. data/lib/axlsx/stylesheet/pattern_fill.rb +1 -1
  70. data/lib/axlsx/stylesheet/styles.rb +7 -7
  71. data/lib/axlsx/stylesheet/table_style_element.rb +1 -3
  72. data/lib/axlsx/util/accessors.rb +6 -6
  73. data/lib/axlsx/util/constants.rb +107 -99
  74. data/lib/axlsx/util/mime_type_utils.rb +11 -0
  75. data/lib/axlsx/util/options_parser.rb +2 -1
  76. data/lib/axlsx/util/parser.rb +4 -4
  77. data/lib/axlsx/util/serialized_attributes.rb +16 -6
  78. data/lib/axlsx/util/simple_typed_list.rb +28 -52
  79. data/lib/axlsx/util/storage.rb +4 -4
  80. data/lib/axlsx/util/validators.rb +29 -17
  81. data/lib/axlsx/version.rb +1 -1
  82. data/lib/axlsx/workbook/defined_name.rb +11 -12
  83. data/lib/axlsx/workbook/defined_names.rb +2 -2
  84. data/lib/axlsx/workbook/shared_strings_table.rb +5 -5
  85. data/lib/axlsx/workbook/workbook.rb +36 -11
  86. data/lib/axlsx/workbook/workbook_view.rb +80 -0
  87. data/lib/axlsx/workbook/workbook_views.rb +22 -0
  88. data/lib/axlsx/workbook/worksheet/auto_filter/auto_filter.rb +2 -2
  89. data/lib/axlsx/workbook/worksheet/auto_filter/filters.rb +1 -3
  90. data/lib/axlsx/workbook/worksheet/break.rb +1 -3
  91. data/lib/axlsx/workbook/worksheet/cell.rb +136 -74
  92. data/lib/axlsx/workbook/worksheet/cell_serializer.rb +63 -43
  93. data/lib/axlsx/workbook/worksheet/cfvo.rb +1 -3
  94. data/lib/axlsx/workbook/worksheet/cfvos.rb +4 -1
  95. data/lib/axlsx/workbook/worksheet/col.rb +7 -10
  96. data/lib/axlsx/workbook/worksheet/col_breaks.rb +2 -2
  97. data/lib/axlsx/workbook/worksheet/cols.rb +5 -2
  98. data/lib/axlsx/workbook/worksheet/comment.rb +5 -6
  99. data/lib/axlsx/workbook/worksheet/comments.rb +9 -12
  100. data/lib/axlsx/workbook/worksheet/conditional_formatting.rb +1 -1
  101. data/lib/axlsx/workbook/worksheet/conditional_formatting_rule.rb +1 -1
  102. data/lib/axlsx/workbook/worksheet/data_bar.rb +4 -6
  103. data/lib/axlsx/workbook/worksheet/data_validation.rb +6 -4
  104. data/lib/axlsx/workbook/worksheet/dimension.rb +2 -2
  105. data/lib/axlsx/workbook/worksheet/header_footer.rb +6 -8
  106. data/lib/axlsx/workbook/worksheet/icon_set.rb +3 -5
  107. data/lib/axlsx/workbook/worksheet/merged_cells.rb +4 -2
  108. data/lib/axlsx/workbook/worksheet/outline_pr.rb +33 -0
  109. data/lib/axlsx/workbook/worksheet/page_margins.rb +1 -3
  110. data/lib/axlsx/workbook/worksheet/page_set_up_pr.rb +1 -1
  111. data/lib/axlsx/workbook/worksheet/page_setup.rb +21 -23
  112. data/lib/axlsx/workbook/worksheet/pane.rb +1 -3
  113. data/lib/axlsx/workbook/worksheet/pivot_table.rb +44 -28
  114. data/lib/axlsx/workbook/worksheet/pivot_table_cache_definition.rb +4 -4
  115. data/lib/axlsx/workbook/worksheet/print_options.rb +1 -3
  116. data/lib/axlsx/workbook/worksheet/protected_range.rb +1 -3
  117. data/lib/axlsx/workbook/worksheet/protected_ranges.rb +5 -2
  118. data/lib/axlsx/workbook/worksheet/rich_text.rb +55 -0
  119. data/lib/axlsx/workbook/worksheet/rich_text_run.rb +250 -0
  120. data/lib/axlsx/workbook/worksheet/row.rb +40 -51
  121. data/lib/axlsx/workbook/worksheet/row_breaks.rb +2 -2
  122. data/lib/axlsx/workbook/worksheet/selection.rb +1 -3
  123. data/lib/axlsx/workbook/worksheet/sheet_data.rb +3 -1
  124. data/lib/axlsx/workbook/worksheet/sheet_pr.rb +21 -3
  125. data/lib/axlsx/workbook/worksheet/sheet_protection.rb +1 -3
  126. data/lib/axlsx/workbook/worksheet/table.rb +6 -6
  127. data/lib/axlsx/workbook/worksheet/table_style_info.rb +1 -3
  128. data/lib/axlsx/workbook/worksheet/tables.rb +4 -1
  129. data/lib/axlsx/workbook/worksheet/worksheet.rb +64 -78
  130. data/lib/axlsx/workbook/worksheet/worksheet_drawing.rb +10 -10
  131. data/lib/axlsx/workbook/worksheet/worksheet_hyperlinks.rb +3 -3
  132. data/test/drawing/tc_area_chart.rb +39 -0
  133. data/test/drawing/tc_area_series.rb +71 -0
  134. data/test/drawing/tc_axis.rb +27 -0
  135. data/test/drawing/tc_bar_chart.rb +71 -0
  136. data/test/drawing/tc_bubble_chart.rb +44 -0
  137. data/test/drawing/tc_bubble_series.rb +21 -0
  138. data/test/drawing/tc_chart.rb +23 -10
  139. data/test/drawing/tc_data_source.rb +6 -0
  140. data/test/drawing/tc_drawing.rb +2 -2
  141. data/test/drawing/tc_line_chart.rb +5 -5
  142. data/test/drawing/tc_line_series.rb +47 -6
  143. data/test/drawing/tc_pic.rb +11 -15
  144. data/test/drawing/tc_scatter_series.rb +36 -1
  145. data/test/drawing/tc_str_val.rb +9 -0
  146. data/test/drawing/tc_title.rb +5 -0
  147. data/test/stylesheet/tc_styles.rb +2 -2
  148. data/test/tc_axlsx.rb +31 -0
  149. data/test/tc_helper.rb +2 -0
  150. data/test/tc_package.rb +19 -1
  151. data/test/util/tc_mime_type_utils.rb +13 -0
  152. data/test/util/tc_simple_typed_list.rb +2 -3
  153. data/test/util/tc_validators.rb +34 -10
  154. data/test/workbook/tc_defined_name.rb +12 -4
  155. data/test/workbook/tc_shared_strings_table.rb +16 -1
  156. data/test/workbook/tc_workbook.rb +38 -3
  157. data/test/workbook/tc_workbook_view.rb +50 -0
  158. data/test/workbook/worksheet/auto_filter/tc_filters.rb +1 -1
  159. data/test/workbook/worksheet/tc_break.rb +1 -1
  160. data/test/workbook/worksheet/tc_cell.rb +76 -8
  161. data/test/workbook/worksheet/tc_col.rb +2 -2
  162. data/test/workbook/worksheet/tc_conditional_formatting.rb +2 -2
  163. data/test/workbook/worksheet/tc_data_bar.rb +1 -1
  164. data/test/workbook/worksheet/tc_data_validation.rb +11 -11
  165. data/test/workbook/worksheet/tc_header_footer.rb +2 -2
  166. data/test/workbook/worksheet/tc_icon_set.rb +1 -1
  167. data/test/workbook/worksheet/tc_outline_pr.rb +19 -0
  168. data/test/workbook/worksheet/tc_page_setup.rb +3 -3
  169. data/test/workbook/worksheet/tc_pivot_table.rb +21 -6
  170. data/test/workbook/worksheet/tc_print_options.rb +1 -1
  171. data/test/workbook/worksheet/tc_rich_text.rb +44 -0
  172. data/test/workbook/worksheet/tc_rich_text_run.rb +172 -0
  173. data/test/workbook/worksheet/tc_row.rb +7 -2
  174. data/test/workbook/worksheet/tc_sheet_calc_pr.rb +1 -1
  175. data/test/workbook/worksheet/tc_sheet_format_pr.rb +4 -4
  176. data/test/workbook/worksheet/tc_sheet_pr.rb +26 -4
  177. data/test/workbook/worksheet/tc_sheet_protection.rb +5 -5
  178. data/test/workbook/worksheet/tc_sheet_view.rb +4 -4
  179. data/test/workbook/worksheet/tc_table.rb +2 -3
  180. data/test/workbook/worksheet/tc_worksheet.rb +99 -45
  181. metadata +89 -16
@@ -1,14 +1,14 @@
1
1
  require 'tc_helper'
2
2
 
3
3
  class TestDefinedNames < Test::Unit::TestCase
4
- def setup
4
+ def setup
5
5
  @dn = Axlsx::DefinedName.new('Sheet1!A1:A1')
6
6
  end
7
7
 
8
8
  def test_initialize
9
9
  assert_equal('Sheet1!A1:A1', @dn.formula)
10
10
  end
11
-
11
+
12
12
  def test_string_attributes
13
13
  %w(short_cut_key status_bar help description custom_menu comment).each do |attr|
14
14
  assert_raise(ArgumentError, 'only strings allowed in string attributes') { @dn.send("#{attr}=", 1) }
@@ -29,13 +29,21 @@ class TestDefinedNames < Test::Unit::TestCase
29
29
  assert_nothing_raised { @dn.local_sheet_id = 1 }
30
30
  end
31
31
 
32
+ def test_do_not_camelcase_value_for_name
33
+ @dn.name = '_xlnm._FilterDatabase'
34
+ doc = Nokogiri::XML(@dn.to_xml_string)
35
+ assert_equal(doc.xpath("//definedName[@name='_xlnm._FilterDatabase']").size, 1)
36
+ assert_equal('Sheet1!A1:A1', doc.xpath('//definedName').text)
37
+ end
38
+
32
39
  def test_to_xml_string
33
40
  assert_raise(ArgumentError, 'name is required for serialization') { @dn.to_xml_string }
34
41
  @dn.name = '_xlnm.Print_Titles'
35
42
  @dn.hidden = true
36
43
  doc = Nokogiri::XML(@dn.to_xml_string)
37
- assert(doc.xpath("//definedName[@name='_xlnm.Print_Titles']"))
38
- assert(doc.xpath("//definedName[@hidden='true']"))
44
+ assert_equal(doc.xpath("//definedName[@name='_xlnm.Print_Titles']").size, 1)
45
+ assert_equal(doc.xpath("//definedName[@hidden='1']").size, 1)
39
46
  assert_equal('Sheet1!A1:A1', doc.xpath('//definedName').text)
40
47
  end
48
+
41
49
  end
@@ -4,10 +4,12 @@ class TestSharedStringsTable < Test::Unit::TestCase
4
4
 
5
5
  def setup
6
6
  @p = Axlsx::Package.new :use_shared_strings=>true
7
+
7
8
  ws = @p.workbook.add_worksheet
8
9
  ws.add_row ['a', 1, 'b']
9
10
  ws.add_row ['b', 1, 'c']
10
11
  ws.add_row ['c', 1, 'd']
12
+ ws.rows.last.add_cell('b', :type => :text)
11
13
  end
12
14
 
13
15
  def test_workbook_has_shared_strings
@@ -16,7 +18,7 @@ class TestSharedStringsTable < Test::Unit::TestCase
16
18
 
17
19
  def test_count
18
20
  sst = @p.workbook.shared_strings
19
- assert_equal(sst.count, 6)
21
+ assert_equal(sst.count, 7)
20
22
  end
21
23
 
22
24
  def test_unique_count
@@ -41,4 +43,17 @@ class TestSharedStringsTable < Test::Unit::TestCase
41
43
  assert_equal(errors.size, 0, "sharedStirngs.xml Invalid" + errors.map{ |e| e.message }.to_s)
42
44
  end
43
45
 
46
+ def test_remove_control_characters_in_xml_serialization
47
+ nasties = "hello\x10\x00\x1C\x1Eworld"
48
+ @p.workbook.worksheets[0].add_row [nasties]
49
+
50
+ # test that the nasty string was added to the shared strings
51
+ assert @p.workbook.shared_strings.unique_cells.has_key?(nasties)
52
+
53
+ # test that none of the control characters are in the XML output for shared strings
54
+ assert_no_match(/#{Axlsx::CONTROL_CHARS}/, @p.workbook.shared_strings.to_xml_string)
55
+
56
+ # assert that the shared string was normalized to remove the control characters
57
+ assert_not_nil @p.workbook.shared_strings.to_xml_string.index("helloworld")
58
+ end
44
59
  end
@@ -33,12 +33,18 @@ class TestWorkbook < Test::Unit::TestCase
33
33
  assert_equal(@wb.use_autowidth, false)
34
34
  end
35
35
 
36
+ def test_is_reversed
37
+ assert_equal(@wb.is_reversed, nil)
38
+ assert_raise(ArgumentError) {@wb.is_reversed = 0.1}
39
+ assert_nothing_raised {@wb.is_reversed = true }
40
+ assert_equal(@wb.use_autowidth, true)
41
+ end
36
42
 
37
43
  def test_sheet_by_name_retrieval
38
44
  @wb.add_worksheet(:name=>'foo')
39
45
  @wb.add_worksheet(:name=>'bar')
40
46
  assert_equal('foo', @wb.sheet_by_name('foo').name)
41
-
47
+
42
48
  end
43
49
  def test_date1904
44
50
  assert_equal(Axlsx::Workbook.date1904, @wb.date1904)
@@ -53,6 +59,11 @@ class TestWorkbook < Test::Unit::TestCase
53
59
  assert_equal(1, @wb.defined_names.size)
54
60
  end
55
61
 
62
+ def test_add_view
63
+ @wb.add_view visibility: :hidden, window_width: 800
64
+ assert_equal(1, @wb.views.size)
65
+ end
66
+
56
67
  def test_shared_strings
57
68
  assert_equal(@wb.use_shared_strings, nil)
58
69
  assert_raise(ArgumentError) {@wb.use_shared_strings = 'bpb'}
@@ -66,7 +77,7 @@ class TestWorkbook < Test::Unit::TestCase
66
77
  assert_equal(@wb.worksheets.first, ws, "the worksheet returned is the worksheet added")
67
78
  assert_equal(ws.name, "bob", "name option gets passed to worksheet")
68
79
  end
69
-
80
+
70
81
  def test_insert_worksheet
71
82
  @wb.add_worksheet(:name => 'A')
72
83
  @wb.add_worksheet(:name => 'B')
@@ -93,7 +104,16 @@ class TestWorkbook < Test::Unit::TestCase
93
104
  end
94
105
  assert(errors.empty?, "error free validation")
95
106
  end
96
- def test_range_requires__valid_sheet
107
+
108
+ def test_to_xml_reversed
109
+ @wb.is_reversed = true
110
+ @wb.add_worksheet(:name => 'first')
111
+ second = @wb.add_worksheet(:name => 'second')
112
+ doc = Nokogiri::XML(@wb.to_xml_string)
113
+ assert_equal second.name, doc.xpath('//xmlns:workbook/xmlns:sheets/*[1]/@name').to_s
114
+ end
115
+
116
+ def test_range_requires_valid_sheet
97
117
  ws = @wb.add_worksheet :name=>'fish'
98
118
  ws.add_row [1,2,3]
99
119
  ws.add_row [4,5,6]
@@ -116,10 +136,25 @@ class TestWorkbook < Test::Unit::TestCase
116
136
  assert_equal(doc.xpath('//xmlns:workbook/xmlns:definedNames/xmlns:definedName').inner_text, @wb.worksheets[0].auto_filter.defined_name)
117
137
  end
118
138
 
139
+ def test_to_xml_string_book_views
140
+ @wb.add_worksheet do |sheet|
141
+ sheet.add_row [1, "two"]
142
+ end
143
+ @wb.add_view active_tab: 0, first_sheet: 0
144
+ doc = Nokogiri::XML(@wb.to_xml_string)
145
+ assert_equal(1, doc.xpath('//xmlns:workbook/xmlns:bookViews/xmlns:workbookView[@activeTab=0]').size)
146
+ end
147
+
119
148
  def test_to_xml_uses_correct_rIds_for_pivotCache
120
149
  ws = @wb.add_worksheet
121
150
  pivot_table = ws.add_pivot_table('G5:G6', 'A1:D5')
122
151
  doc = Nokogiri::XML(@wb.to_xml_string)
123
152
  assert_equal pivot_table.cache_definition.rId, doc.xpath("//xmlns:pivotCache").first["r:id"]
124
153
  end
154
+
155
+ def test_worksheet_name_is_intact_after_serialized_into_xml
156
+ sheet = @wb.add_worksheet(:name => '_Example')
157
+ wb_xml = Nokogiri::XML(@wb.to_xml_string)
158
+ assert_equal sheet.name, wb_xml.xpath('//xmlns:workbook/xmlns:sheets/*[1]/@name').to_s
159
+ end
125
160
  end
@@ -0,0 +1,50 @@
1
+ require 'tc_helper'
2
+
3
+ class TestWorkbookView < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @options = { visibility: :hidden, minimized: true, show_horizontal_scroll: true, show_vertical_scroll: true,
7
+ show_sheet_tabs: true, tab_ratio: 750, first_sheet: 0, active_tab: 1, x_window: 500, y_window: 400,
8
+ window_width: 800, window_height: 600, auto_filter_date_grouping: true }
9
+ @book_view = Axlsx::WorkbookView.new @options
10
+ end
11
+
12
+ def test_options_assignation
13
+ @options.each do |key, value|
14
+ assert_equal(value, @book_view.send(key))
15
+ end
16
+ end
17
+
18
+ def test_boolean_attribute_validation
19
+ %w(minimized show_horizontal_scroll show_vertical_scroll show_sheet_tabs auto_filter_date_grouping).each do |attr|
20
+ assert_raise(ArgumentError, 'only booleanish allowed in boolean attributes') { @book_view.send("#{attr}=", "banana") }
21
+ assert_nothing_raised { @book_view.send("#{attr}=", false )}
22
+ end
23
+ end
24
+
25
+ def test_integer_attribute_validation
26
+ %w(tab_ratio first_sheet active_tab x_window y_window window_width window_height).each do |attr|
27
+ assert_raise(ArgumentError, 'only integer allowed in integer attributes') { @book_view.send("#{attr}=", "b") }
28
+ assert_nothing_raised { @book_view.send("#{attr}=", 7 )}
29
+ end
30
+ end
31
+
32
+ def test_visibility_attribute_validation
33
+ assert_raise(ArgumentError) { @book_view.visibility = :foobar }
34
+ assert_nothing_raised { @book_view.visibility = :hidden }
35
+ assert_nothing_raised { @book_view.visibility = :very_hidden }
36
+ assert_nothing_raised { @book_view.visibility = :visible }
37
+ end
38
+
39
+ def test_to_xml_string
40
+ xml = @book_view.to_xml_string
41
+ doc = Nokogiri::XML(xml)
42
+ @options.each do |key, value|
43
+ if value == true || value == false
44
+ value = value ? 1 : 0
45
+ end
46
+ path = "workbookView[@#{Axlsx.camel(key, false)}='#{value}']"
47
+ assert_equal(1, doc.xpath(path).size)
48
+ end
49
+ end
50
+ end
@@ -44,7 +44,7 @@ class TestFilters < Test::Unit::TestCase
44
44
 
45
45
  def test_to_xml_string
46
46
  doc = Nokogiri::XML(@filters.to_xml_string)
47
- assert_equal(1, doc.xpath('//filters[@blank="true"]').size)
47
+ assert_equal(1, doc.xpath('//filters[@blank=1]').size)
48
48
  end
49
49
  end
50
50
 
@@ -44,6 +44,6 @@ class TestBreak < Test::Unit::TestCase
44
44
 
45
45
  def test_to_xml_string
46
46
  doc = Nokogiri::XML(@break.to_xml_string)
47
- assert_equal(doc.xpath('//brk[@id="1"][@min="1"][@max="10"][@pt="false"][@man="true"]').size, 1)
47
+ assert_equal(doc.xpath('//brk[@id="1"][@min="1"][@max="10"][@pt=0][@man=1]').size, 1)
48
48
  end
49
49
  end
@@ -4,6 +4,7 @@ class TestCell < Test::Unit::TestCase
4
4
 
5
5
  def setup
6
6
  p = Axlsx::Package.new
7
+ p.use_shared_strings = true
7
8
  @ws = p.workbook.add_worksheet :name=>"hmmm"
8
9
  p.workbook.styles.add_style :sz=>20
9
10
  @row = @ws.add_row
@@ -34,7 +35,7 @@ class TestCell < Test::Unit::TestCase
34
35
  end
35
36
 
36
37
  def test_pos
37
- assert_equal(@c.pos, [@c.index, @c.row.index])
38
+ assert_equal(@c.pos, [@c.index, @c.row.index(@c)])
38
39
  end
39
40
 
40
41
  def test_r
@@ -50,6 +51,25 @@ class TestCell < Test::Unit::TestCase
50
51
  assert_equal(@cAA.r_abs,"$AA$2", "needs to accept multi-digit columns")
51
52
  end
52
53
 
54
+ def test_name
55
+ @c.name = 'foo'
56
+ assert_equal(1, @ws.workbook.defined_names.size)
57
+ assert_equal('foo', @ws.workbook.defined_names.last.name)
58
+ end
59
+
60
+ def test_autowidth
61
+ style = @c.row.worksheet.workbook.styles.add_style({:alignment => {:horizontal => :center, :vertical => :center, :wrap_text => true}} )
62
+ @c.style = style
63
+ assert_equal(@c.autowidth, 5.5)
64
+ end
65
+
66
+ def test_time
67
+ @c.type = :time
68
+ now = DateTime.now
69
+ @c.value = now
70
+ assert_equal(@c.value, now.to_time)
71
+ end
72
+
53
73
  def test_style
54
74
  assert_raise(ArgumentError, "must reject invalid style indexes") { @c.style=@c.row.worksheet.workbook.styles.cellXfs.size }
55
75
  assert_nothing_raised("must allow valid style index changes") {@c.style=1}
@@ -89,6 +109,7 @@ class TestCell < Test::Unit::TestCase
89
109
  assert_equal(@c.send(:cell_type_from_value, true), :boolean)
90
110
  assert_equal(@c.send(:cell_type_from_value, false), :boolean)
91
111
  assert_equal(@c.send(:cell_type_from_value, 1.0/10**6), :float)
112
+ assert_equal(@c.send(:cell_type_from_value, Axlsx::RichText.new), :richtext)
92
113
  assert_equal(:iso_8601, @c.send(:cell_type_from_value, '2008-08-30T01:45:36.123+09:00'))
93
114
  end
94
115
 
@@ -101,6 +122,8 @@ class TestCell < Test::Unit::TestCase
101
122
  assert_equal(@c.send(:cast_value, "1.0"), 1.0)
102
123
  @c.type = :string
103
124
  assert_equal(@c.send(:cast_value, nil), nil)
125
+ @c.type = :richtext
126
+ assert_equal(@c.send(:cast_value, nil), nil)
104
127
  @c.type = :float
105
128
  assert_equal(@c.send(:cast_value, nil), nil)
106
129
  @c.type = :boolean
@@ -110,6 +133,19 @@ class TestCell < Test::Unit::TestCase
110
133
  assert_equal("2012-10-10T12:24", @c.send(:cast_value, "2012-10-10T12:24"))
111
134
  end
112
135
 
136
+ def test_cast_time_subclass
137
+ subtime = Class.new(Time) do
138
+ def to_time
139
+ raise "#to_time of Time subclass should not be called"
140
+ end
141
+ end
142
+
143
+ time = subtime.now
144
+
145
+ @c.type = :time
146
+ assert_equal(time, @c.send(:cast_value, time))
147
+ end
148
+
113
149
  def test_color
114
150
  assert_raise(ArgumentError) { @c.color = -1.1 }
115
151
  assert_nothing_raised { @c.color = "FF00FF00" }
@@ -217,6 +253,13 @@ class TestCell < Test::Unit::TestCase
217
253
  assert_equal(@c.row.worksheet.send(:merged_cells).last, "A1:C1")
218
254
  end
219
255
 
256
+ def test_reverse_merge_with_cell
257
+ @c.row.add_cell 2
258
+ @c.row.add_cell 3
259
+ @row.cells.last.merge @c
260
+ assert_equal(@c.row.worksheet.send(:merged_cells).last, "A1:C1")
261
+ end
262
+
220
263
  def test_ssti
221
264
  assert_raise(ArgumentError, "ssti must be an unsigned integer!") { @c.send(:ssti=, -1) }
222
265
  @c.send :ssti=, 1
@@ -255,7 +298,7 @@ class TestCell < Test::Unit::TestCase
255
298
  c_xml = Nokogiri::XML(@c.to_xml_string(1,1))
256
299
  assert_equal(c_xml.xpath("/c[@s=1]").size, 1)
257
300
  end
258
-
301
+
259
302
  def test_to_xml_string_with_run
260
303
  # Actually quite a number of similar run styles
261
304
  # but the processing should be the same
@@ -265,7 +308,7 @@ class TestCell < Test::Unit::TestCase
265
308
  @c.font_name = 'arial'
266
309
  @c.color = 'FF0000'
267
310
  c_xml = Nokogiri::XML(@c.to_xml_string(1,1))
268
- assert(c_xml.xpath("//b"))
311
+ assert(c_xml.xpath("//b").any?)
269
312
  end
270
313
 
271
314
  def test_to_xml_string_formula
@@ -274,8 +317,35 @@ class TestCell < Test::Unit::TestCase
274
317
  sheet.add_row ["=IF(2+2=4,4,5)"]
275
318
  end
276
319
  doc = Nokogiri::XML(ws.to_xml_string)
277
- assert(doc.xpath("//f[@text()='IF(2+2=4,4,5)']"))
320
+ doc.remove_namespaces!
321
+ assert(doc.xpath("//f[text()='IF(2+2=4,4,5)']").any?)
322
+ end
278
323
 
324
+ def test_to_xml_string_array_formula
325
+ p = Axlsx::Package.new
326
+ ws = p.workbook.add_worksheet do |sheet|
327
+ sheet.add_row ["{=SUM(C2:C11*D2:D11)}"]
328
+ end
329
+ doc = Nokogiri::XML(ws.to_xml_string)
330
+ doc.remove_namespaces!
331
+ assert(doc.xpath("//f[text()='SUM(C2:C11*D2:D11)']").any?)
332
+ assert(doc.xpath("//f[@t='array']").any?)
333
+ assert(doc.xpath("//f[@ref='A1']").any?)
334
+ end
335
+
336
+ def test_to_xml_string_text_formula
337
+ p = Axlsx::Package.new
338
+ ws = p.workbook.add_worksheet do |sheet|
339
+ sheet.add_row ["=1+1", "-1+1"], type: :text
340
+ end
341
+ doc = Nokogiri::XML(ws.to_xml_string)
342
+ doc.remove_namespaces!
343
+
344
+ assert(doc.xpath("//f[text()='1+1']").empty?)
345
+ assert(doc.xpath("//t[text()='=1+1']").any?)
346
+
347
+ assert(doc.xpath("//f[text()='1+1']").empty?)
348
+ assert(doc.xpath("//t[text()='-1+1']").any?)
279
349
  end
280
350
 
281
351
  def test_font_size_with_custom_style_and_no_sz
@@ -294,17 +364,17 @@ class TestCell < Test::Unit::TestCase
294
364
  sz = @c.send(:font_size)
295
365
  assert_equal(sz, 52)
296
366
  end
297
-
298
367
 
299
368
  def test_cell_with_sz
300
369
  @c.sz = 25
301
370
  assert_equal(25, @c.send(:font_size))
302
371
  end
372
+
303
373
  def test_to_xml
304
374
  # TODO This could use some much more stringent testing related to the xml content generated!
305
375
  @ws.add_row [Time.now, Date.today, true, 1, 1.0, "text", "=sum(A1:A2)", "2013-01-13T13:31:25.123"]
306
376
  @ws.rows.last.cells[5].u = true
307
-
377
+
308
378
  schema = Nokogiri::XML::Schema(File.open(Axlsx::SML_XSD))
309
379
  doc = Nokogiri::XML(@ws.to_xml_string)
310
380
  errors = []
@@ -313,7 +383,5 @@ class TestCell < Test::Unit::TestCase
313
383
  puts error.message
314
384
  end
315
385
  assert(errors.empty?, "error free validation")
316
-
317
386
  end
318
-
319
387
  end
@@ -61,11 +61,11 @@ class TestCol < Test::Unit::TestCase
61
61
  def test_to_xml_string
62
62
  @col.width = 100
63
63
  doc = Nokogiri::XML(@col.to_xml_string)
64
- assert_equal(1, doc.xpath("//col [@bestFit='#{@col.best_fit}']").size)
64
+ assert_equal(1, doc.xpath("//col [@bestFit='#{@col.best_fit ? 1 : 0}']").size)
65
65
  assert_equal(1, doc.xpath("//col [@max=#{@col.max}]").size)
66
66
  assert_equal(1, doc.xpath("//col [@min=#{@col.min}]").size)
67
67
  assert_equal(1, doc.xpath("//col [@width=#{@col.width}]").size)
68
- assert_equal(1, doc.xpath("//col [@customWidth='#{@col.custom_width}']").size)
68
+ assert_equal(1, doc.xpath("//col [@customWidth='#{@col.custom_width ? 1 : 0}']").size)
69
69
  end
70
70
 
71
71
  def test_style
@@ -113,8 +113,8 @@ class TestConditionalFormatting < Test::Unit::TestCase
113
113
  :percent => false, :rank => 0, :stdDev => 1, :stopIfTrue => true, :timePeriod => :today,
114
114
  :formula => "0.0"})
115
115
  doc = Nokogiri::XML.parse(cf.to_xml_string)
116
- assert_equal(1, doc.xpath(".//conditionalFormatting//cfRule[@type='cellIs'][@aboveAverage='false'][@bottom='false'][@dxfId=0][@equalAverage='false'][@priority=2][@operator='lessThan'][@text=''][@percent='false'][@rank=0][@stdDev=1][@stopIfTrue='true'][@timePeriod='today']").size)
117
- assert doc.xpath(".//conditionalFormatting//cfRule[@type='cellIs'][@aboveAverage='false'][@bottom='false'][@dxfId=0][@equalAverage='false'][@priority=2][@operator='lessThan'][@text=''][@percent='false'][@rank=0][@stdDev=1][@stopIfTrue='true'][@timePeriod='today']//formula=0.0")
116
+ assert_equal(1, doc.xpath(".//conditionalFormatting//cfRule[@type='cellIs'][@aboveAverage=0][@bottom=0][@dxfId=0][@equalAverage=0][@priority=2][@operator='lessThan'][@text=''][@percent=0][@rank=0][@stdDev=1][@stopIfTrue=1][@timePeriod='today']").size)
117
+ assert doc.xpath(".//conditionalFormatting//cfRule[@type='cellIs'][@aboveAverage=0][@bottom=0][@dxfId=0][@equalAverage=0][@priority=2][@operator='lessThan'][@text=''][@percent=0][@rank=0][@stdDev=1][@stopIfTrue=1][@timePeriod='today']//formula=0.0")
118
118
  end
119
119
 
120
120
  def test_to_xml
@@ -38,7 +38,7 @@ class TestDataBar < Test::Unit::TestCase
38
38
 
39
39
  def test_to_xml_string
40
40
  doc = Nokogiri::XML.parse(@data_bar.to_xml_string)
41
- assert_equal(doc.xpath(".//dataBar[@minLength=10][@maxLength=90][@showValue='true']").size, 1)
41
+ assert_equal(doc.xpath(".//dataBar[@minLength=10][@maxLength=90][@showValue=1]").size, 1)
42
42
  assert_equal(doc.xpath(".//dataBar//cfvo").size, 2)
43
43
  assert_equal(doc.xpath(".//dataBar//color").size, 1)
44
44
  end
@@ -160,11 +160,11 @@ class TestDataValidation < Test::Unit::TestCase
160
160
  #test attributes
161
161
  assert_equal(1, doc.xpath("//xmlns:worksheet/xmlns:dataValidations[@count='1']/xmlns:dataValidation[@sqref='A1']
162
162
  [@promptTitle='Be carful!'][@prompt='Only values between 5 and 10'][@operator='between'][@errorTitle='Wrong input']
163
- [@error='Only values between 5 and 10'][@showErrorMessage='true'][@allowBlank='true'][@showInputMessage='true'][@type='whole']
163
+ [@error='Only values between 5 and 10'][@showErrorMessage=1][@allowBlank=1][@showInputMessage=1][@type='whole']
164
164
  [@errorStyle='information']").size)
165
165
  assert doc.xpath("//xmlns:worksheet/xmlns:dataValidations[@count='1']/xmlns:dataValidation[@sqref='A1']
166
166
  [@promptTitle='Be carful!'][@prompt='Only values between 5 and 10'][@operator='between'][@errorTitle='Wrong input']
167
- [@error='Only values between 5 and 10'][@showErrorMessage='true'][@allowBlank='true'][@showInputMessage='true']
167
+ [@error='Only values between 5 and 10'][@showErrorMessage=1][@allowBlank=1][@showInputMessage=1]
168
168
  [@type='whole'][@errorStyle='information']")
169
169
 
170
170
  #test forumula1
@@ -189,11 +189,11 @@ class TestDataValidation < Test::Unit::TestCase
189
189
  #test attributes
190
190
  assert_equal(1, doc.xpath("//xmlns:worksheet/xmlns:dataValidations[@count='1']/xmlns:dataValidation[@sqref='A1']
191
191
  [@promptTitle='Be carful!'][@prompt='Only values from list'][@errorTitle='Wrong input'][@error='Only values from list']
192
- [@showErrorMessage='true'][@allowBlank='true'][@showInputMessage='true'][@showDropDown='true'][@type='list']
192
+ [@showErrorMessage=1][@allowBlank=1][@showInputMessage=1][@showDropDown=1][@type='list']
193
193
  [@errorStyle='stop']").size)
194
194
  assert doc.xpath("//xmlns:worksheet/xmlns:dataValidations[@count='1']/xmlns:dataValidation[@sqref='A1']
195
195
  [@promptTitle='Be carful!'][@prompt='Only values from list'][@errorTitle='Wrong input'][@error='Only values from list']
196
- [@showErrorMessage='true'][@allowBlank='true'][@showInputMessage='true'][@showDropDown='true'][@type='list'][@errorStyle='stop']")
196
+ [@showErrorMessage=1][@allowBlank=1][@showInputMessage=1][@showDropDown=1][@type='list'][@errorStyle='stop']")
197
197
 
198
198
  #test forumula1
199
199
  assert_equal(1, doc.xpath("//xmlns:worksheet/xmlns:dataValidations/xmlns:dataValidation/xmlns:formula1").size)
@@ -212,11 +212,11 @@ class TestDataValidation < Test::Unit::TestCase
212
212
 
213
213
  #test attributes
214
214
  assert_equal(1, doc.xpath("//xmlns:worksheet/xmlns:dataValidations[@count='1']/xmlns:dataValidation[@sqref='A1'][@promptTitle='Be carful!']
215
- [@prompt='Only values corresponding formula'][@errorTitle='Wrong input'][@error='Only values corresponding formula'][@showErrorMessage='true']
216
- [@allowBlank='true'][@showInputMessage='true'][@type='custom'][@errorStyle='stop']").size)
215
+ [@prompt='Only values corresponding formula'][@errorTitle='Wrong input'][@error='Only values corresponding formula'][@showErrorMessage=1]
216
+ [@allowBlank=1][@showInputMessage=1][@type='custom'][@errorStyle='stop']").size)
217
217
  assert doc.xpath("//xmlns:worksheet/xmlns:dataValidations[@count='1']/xmlns:dataValidation[@sqref='A1'][@promptTitle='Be carful!']
218
218
  [@prompt='Only values corresponding formula'][@errorTitle='Wrong input'][@error='Only values corresponding formula']
219
- [@showErrorMessage='true'][@allowBlank='true'][@showInputMessage='true'][@type='custom'][@errorStyle='stop']")
219
+ [@showErrorMessage=1][@allowBlank=1][@showInputMessage=1][@type='custom'][@errorStyle='stop']")
220
220
 
221
221
  #test forumula1
222
222
  assert_equal(1, doc.xpath("//xmlns:worksheet/xmlns:dataValidations/xmlns:dataValidation/xmlns:formula1").size)
@@ -240,21 +240,21 @@ class TestDataValidation < Test::Unit::TestCase
240
240
  #test attributes
241
241
  assert_equal(1, doc.xpath("//xmlns:worksheet/xmlns:dataValidations[@count='2']/xmlns:dataValidation[@sqref='A1']
242
242
  [@promptTitle='Be carful!'][@prompt='Only values between 5 and 10'][@operator='between'][@errorTitle='Wrong input']
243
- [@error='Only values between 5 and 10'][@showErrorMessage='true'][@allowBlank='true'][@showInputMessage='true'][@type='whole']
243
+ [@error='Only values between 5 and 10'][@showErrorMessage=1][@allowBlank=1][@showInputMessage=1][@type='whole']
244
244
  [@errorStyle='information']").size)
245
245
  assert doc.xpath("//xmlns:worksheet/xmlns:dataValidations[@count='2']/xmlns:dataValidation[@sqref='A1']
246
246
  [@promptTitle='Be carful!'][@prompt='Only values between 5 and 10'][@operator='between'][@errorTitle='Wrong input']
247
- [@error='Only values between 5 and 10'][@showErrorMessage='true'][@allowBlank='true'][@showInputMessage='true']
247
+ [@error='Only values between 5 and 10'][@showErrorMessage=1][@allowBlank=1][@showInputMessage=1]
248
248
  [@type='whole'][@errorStyle='information']")
249
249
 
250
250
  #test attributes
251
251
  assert_equal(1, doc.xpath("//xmlns:worksheet/xmlns:dataValidations[@count='2']/xmlns:dataValidation[@sqref='B1']
252
252
  [@promptTitle='Be carful!'][@prompt='Only values from list'][@errorTitle='Wrong input'][@error='Only values from list']
253
- [@showErrorMessage='true'][@allowBlank='true'][@showInputMessage='true'][@showDropDown='true'][@type='list']
253
+ [@showErrorMessage=1][@allowBlank=1][@showInputMessage=1][@showDropDown=1][@type='list']
254
254
  [@errorStyle='stop']").size)
255
255
  assert doc.xpath("//xmlns:worksheet/xmlns:dataValidations[@count='2']/xmlns:dataValidation[@sqref='B1']
256
256
  [@promptTitle='Be carful!'][@prompt='Only values from list'][@errorTitle='Wrong input'][@error='Only values from list']
257
- [@showErrorMessage='true'][@allowBlank='true'][@showInputMessage='true'][@showDropDown='true'][@type='list'][@errorStyle='stop']")
257
+ [@showErrorMessage=1][@allowBlank=1][@showInputMessage=1][@showDropDown=1][@type='list'][@errorStyle='stop']")
258
258
  end
259
259
 
260
260
  def test_empty_attributes