axlsx 1.1.5 → 1.1.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. data/README.md +26 -7
  2. data/Rakefile +2 -1
  3. data/examples/chart_colors.xlsx +0 -0
  4. data/examples/data_validation.rb +50 -0
  5. data/examples/example.xlsx +0 -0
  6. data/examples/example_streamed.xlsx +0 -0
  7. data/examples/examples_saved.xlsx +0 -0
  8. data/examples/fish.xlsx +0 -0
  9. data/examples/no-use_autowidth.xlsx +0 -0
  10. data/examples/pareto.rb +28 -0
  11. data/examples/pareto.xlsx +0 -0
  12. data/examples/pie_chart.rb +16 -0
  13. data/examples/pie_chart.xlsx +0 -0
  14. data/examples/pie_chart_saved.xlsx +0 -0
  15. data/examples/shared_strings_example.xlsx +0 -0
  16. data/examples/sheet_protection.rb +10 -0
  17. data/examples/sheet_protection.xlsx +0 -0
  18. data/examples/two_cell_anchor_image.rb +11 -0
  19. data/examples/two_cell_anchor_image.xlsx +0 -0
  20. data/examples/~$pie_chart_saved.xlsx +0 -0
  21. data/lib/axlsx.rb +7 -0
  22. data/lib/axlsx/content_type/default.rb +15 -9
  23. data/lib/axlsx/content_type/override.rb +10 -6
  24. data/lib/axlsx/doc_props/app.rb +152 -99
  25. data/lib/axlsx/drawing/axis.rb +30 -23
  26. data/lib/axlsx/drawing/bar_series.rb +1 -1
  27. data/lib/axlsx/drawing/drawing.rb +7 -2
  28. data/lib/axlsx/drawing/pic.rb +44 -4
  29. data/lib/axlsx/drawing/two_cell_anchor.rb +6 -1
  30. data/lib/axlsx/drawing/vml_shape.rb +2 -5
  31. data/lib/axlsx/rels/relationships.rb +1 -1
  32. data/lib/axlsx/stylesheet/table_style.rb +3 -3
  33. data/lib/axlsx/util/simple_typed_list.rb +0 -13
  34. data/lib/axlsx/util/validators.rb +21 -0
  35. data/lib/axlsx/version.rb +1 -1
  36. data/lib/axlsx/workbook/workbook.rb +2 -0
  37. data/lib/axlsx/workbook/worksheet/cell.rb +3 -4
  38. data/lib/axlsx/workbook/worksheet/comment.rb +3 -9
  39. data/lib/axlsx/workbook/worksheet/data_validation.rb +245 -0
  40. data/lib/axlsx/workbook/worksheet/page_setup.rb +17 -3
  41. data/lib/axlsx/workbook/worksheet/row.rb +34 -18
  42. data/lib/axlsx/workbook/worksheet/sheet_protection.rb +224 -0
  43. data/lib/axlsx/workbook/worksheet/table.rb +2 -2
  44. data/lib/axlsx/workbook/worksheet/worksheet.rb +57 -22
  45. data/test/doc_props/tc_app.rb +31 -1
  46. data/test/drawing/tc_axis.rb +12 -2
  47. data/test/drawing/tc_chart.rb +21 -3
  48. data/test/drawing/tc_drawing.rb +6 -1
  49. data/test/drawing/tc_hyperlink.rb +0 -5
  50. data/test/drawing/tc_pic.rb +22 -2
  51. data/test/drawing/tc_scatter_chart.rb +6 -1
  52. data/test/drawing/tc_two_cell_anchor.rb +1 -2
  53. data/test/stylesheet/tc_styles.rb +3 -4
  54. data/test/stylesheet/tc_table_style.rb +8 -0
  55. data/test/stylesheet/tc_table_style_element.rb +10 -1
  56. data/test/tc_package.rb +43 -15
  57. data/test/util/tc_simple_typed_list.rb +13 -0
  58. data/test/util/tc_validators.rb +7 -7
  59. data/test/workbook/worksheet/table/tc_table.rb +3 -3
  60. data/test/workbook/worksheet/tc_cell.rb +15 -6
  61. data/test/workbook/worksheet/tc_col.rb +9 -0
  62. data/test/workbook/worksheet/tc_comment.rb +8 -7
  63. data/test/workbook/worksheet/tc_comments.rb +8 -1
  64. data/test/workbook/worksheet/tc_conditional_formatting.rb +44 -0
  65. data/test/workbook/worksheet/tc_data_bar.rb +1 -1
  66. data/test/workbook/worksheet/tc_data_validation.rb +265 -0
  67. data/test/workbook/worksheet/tc_page_setup.rb +22 -4
  68. data/test/workbook/worksheet/tc_row.rb +14 -2
  69. data/test/workbook/worksheet/tc_sheet_protection.rb +117 -0
  70. data/test/workbook/worksheet/tc_worksheet.rb +29 -4
  71. metadata +31 -10
@@ -34,17 +34,17 @@ class TestTable < Test::Unit::TestCase
34
34
  end
35
35
 
36
36
  def test_pn
37
- table = @ws.add_table("A1:D5")
37
+ @ws.add_table("A1:D5")
38
38
  assert_equal(@ws.tables.first.pn, "tables/table1.xml")
39
39
  end
40
40
 
41
41
  def test_rId
42
- table = @ws.add_table("A1:D5")
42
+ @ws.add_table("A1:D5")
43
43
  assert_equal(@ws.tables.first.rId, "rId1")
44
44
  end
45
45
 
46
46
  def test_index
47
- table = @ws.add_table("A1:D5")
47
+ @ws.add_table("A1:D5")
48
48
  assert_equal(@ws.tables.first.index, @ws.workbook.tables.index(@ws.tables.first))
49
49
  end
50
50
 
@@ -33,7 +33,7 @@ class TestCell < Test::Unit::TestCase
33
33
  assert_equal(@c.index, @row.cells.index(@c))
34
34
  end
35
35
 
36
- def test_index
36
+ def test_pos
37
37
  assert_equal(@c.pos, [@c.index, @c.row.index])
38
38
  end
39
39
 
@@ -250,20 +250,29 @@ class TestCell < Test::Unit::TestCase
250
250
  c_xml = Nokogiri::XML(@c.to_xml_string(1,1))
251
251
  assert_equal(c_xml.xpath("/c[@s=1]").size, 1)
252
252
  end
253
-
253
+
254
+ def test_to_xml_string_with_run
255
+ @c.b = true
256
+ @c.type = :string
257
+ @c.value = "a"
258
+ @c.font_name = 'arial'
259
+ @c.color = 'FF0000'
260
+ c_xml = Nokogiri::XML(@c.to_xml_string(1,1))
261
+ assert(c_xml.xpath("//b"))
262
+ end
254
263
  def test_to_xml_string_formula
255
264
  p = Axlsx::Package.new
256
- ws = p.workbook.add_worksheet do |ws|
257
- ws.add_row ["=IF(2+2=4,4,5)"]
265
+ ws = p.workbook.add_worksheet do |sheet|
266
+ sheet.add_row ["=IF(2+2=4,4,5)"]
258
267
  end
259
268
  doc = Nokogiri::XML(ws.to_xml_string)
260
- assert("//f[text()=['IF(2+2=4,4,5)']")
269
+ assert(doc.xpath("//f[@text()='IF(2+2=4,4,5)']"))
261
270
 
262
271
  end
263
272
 
264
273
  def test_to_xml
265
274
  # TODO This could use some much more stringent testing related to the xml content generated!
266
- row = @ws.add_row [Time.now, Date.today, true, 1, 1.0, "text", "=sum(A1:A2)"]
275
+ @ws.add_row [Time.now, Date.today, true, 1, 1.0, "text", "=sum(A1:A2)"]
267
276
  schema = Nokogiri::XML::Schema(File.open(Axlsx::SML_XSD))
268
277
  doc = Nokogiri::XML(@ws.to_xml_string)
269
278
  errors = []
@@ -6,6 +6,13 @@ class TestCol < Test::Unit::TestCase
6
6
  @col = Axlsx::Col.new 1, 1
7
7
  end
8
8
 
9
+ def test_initialize
10
+ options = { :width => 12, :collapsed => true, :hidden => true, :outlineLevel => 1, :phonetic => true, :style => 1}
11
+
12
+ col = Axlsx::Col.new 0, 0, options
13
+ options.each{ |key, value| assert_equal(col.send(key.to_sym), value) }
14
+ end
15
+
9
16
  def test_min_max_required
10
17
  assert_raise(ArgumentError, 'min and max must be specified when creating a new column') { Axlsx::Col.new }
11
18
  assert_raise(ArgumentError, 'min and max must be specified when creating a new column') { Axlsx::Col.new nil, nil }
@@ -53,6 +60,8 @@ class TestCol < Test::Unit::TestCase
53
60
 
54
61
  def test_style
55
62
  assert_equal(@col.style, nil)
63
+ @col.style = 1
64
+ assert_equal(@col.style, 1)
56
65
  #TODO check that the style specified is actually in the styles xfs collection
57
66
  end
58
67
 
@@ -34,14 +34,15 @@ class TestComment < Test::Unit::TestCase
34
34
  end
35
35
 
36
36
  def test_vml_shape
37
+ pos = Axlsx::name_to_indices(@c1.ref)
37
38
  assert(@c1.vml_shape.is_a?(Axlsx::VmlShape))
38
- assert(@c1.vml_shape.column == Axlsx::name_to_indices(@c1.ref)[0])
39
- assert(@c1.vml_shape.row == Axlsx::name_to_indices(@c1.ref)[1])
40
- assert(@c1.vml_shape.row == Axlsx::name_to_indices(@c1.ref)[1])
41
- assert(@c1.vml_shape.left_column == Axlsx::name_to_indices(@c1.ref)[1]+1)
42
- assert(@c1.vml_shape.top_row == Axlsx::name_to_indices(@c1.ref)[1])
43
- assert(@c1.vml_shape.right_column == Axlsx::name_to_indices(@c1.ref)[0]+4)
44
- assert(@c1.vml_shape.bottom_row == Axlsx::name_to_indices(@c1.ref)[1]+4)
39
+ assert(@c1.vml_shape.column == pos[0])
40
+ assert(@c1.vml_shape.row == pos[1])
41
+ assert(@c1.vml_shape.row == pos[1])
42
+ assert_equal(pos[0], @c1.vml_shape.left_column)
43
+ assert(@c1.vml_shape.top_row == pos[1])
44
+ assert_equal(pos[0] + 2 , @c1.vml_shape.right_column)
45
+ assert(@c1.vml_shape.bottom_row == pos[1]+4)
45
46
  end
46
47
 
47
48
  def to_xml_string
@@ -37,9 +37,16 @@ class TestComments < Test::Unit::TestCase
37
37
  def test_index
38
38
  assert_equal(@ws.index, @ws.comments.index)
39
39
  end
40
+
40
41
  def test_to_xml_string
41
42
  doc = Nokogiri::XML(@ws.comments.to_xml_string)
42
- # puts doc.xpath("comments").to_xml
43
+ schema = Nokogiri::XML::Schema(File.open(Axlsx::SML_XSD))
44
+ errors = []
45
+ schema.validate(doc).each do |error|
46
+ errors << error
47
+ end
48
+ assert_equal(0, errors.length)
49
+
43
50
  # TODO figure out why these xpath expressions dont work!
44
51
  # assert(doc.xpath("//comments"))
45
52
  # assert_equal(doc.xpath("//xmlns:author").size, @ws.comments.authors.size)
@@ -15,6 +15,50 @@ class TestConditionalFormatting < Test::Unit::TestCase
15
15
  assert_equal("AA1:AB100", optioned.sqref)
16
16
  assert_equal([1, 2], optioned.rules)
17
17
  end
18
+ def test_add_as_rule
19
+
20
+ color_scale = Axlsx::ColorScale.new do |cs|
21
+ cs.colors.first.rgb = "FFDFDFDF"
22
+ cs.colors.last.rgb = "FF00FF00"
23
+ cs.value_objects.first.type = :percentile
24
+ cs.value_objects.first.val = 5
25
+ end
26
+
27
+ data_bar = Axlsx::DataBar.new :color => "FFFF0000"
28
+ icon_set = Axlsx::IconSet.new :iconSet => "5Rating"
29
+ cfr = Axlsx::ConditionalFormattingRule.new( { :type => :containsText, :text => "TRUE",
30
+ :dxfId => 0, :priority => 1,
31
+ :formula => 'NOT(ISERROR(SEARCH("FALSE",AB1)))',
32
+ :color_scale => color_scale,
33
+ :data_bar => data_bar,
34
+ :icon_set => icon_set})
35
+
36
+ assert(cfr.data_bar.is_a?(Axlsx::DataBar))
37
+ assert(cfr.icon_set.is_a?(Axlsx::IconSet))
38
+ assert(cfr.color_scale.is_a?(Axlsx::ColorScale))
39
+ cfs = @ws.add_conditional_formatting( "B2:B2", [cfr])
40
+ doc = Nokogiri::XML.parse(cfs.last.to_xml_string)
41
+ assert_equal(1, doc.xpath(".//conditionalFormatting[@sqref='B2:B2']//cfRule[@type='containsText'][@dxfId=0][@priority=1]").size)
42
+ assert doc.xpath(".//conditionalFormatting//cfRule[@type='containsText'][@dxfId=0][@priority=1]//formula='NOT(ISERROR(SEARCH(\"FALSE\",AB1)))'")
43
+
44
+ cfs.last.rules.last.type = :colorScale
45
+ doc = Nokogiri::XML.parse(cfs.last.to_xml_string)
46
+ assert_equal(doc.xpath(".//conditionalFormatting//cfRule//colorScale//cfvo").size, 2)
47
+ assert_equal(doc.xpath(".//conditionalFormatting//cfRule//colorScale//color").size, 2)
48
+
49
+ cfs.last.rules.last.type = :dataBar
50
+ doc = Nokogiri::XML.parse(cfs.last.to_xml_string)
51
+ assert_equal(doc.xpath(".//conditionalFormatting//cfRule//dataBar").size, 1)
52
+ assert_equal(doc.xpath(".//conditionalFormatting//cfRule//dataBar//cfvo").size, 2)
53
+ assert_equal(doc.xpath(".//conditionalFormatting//cfRule//dataBar//color[@rgb='FFFF0000']").size, 1)
54
+
55
+ cfs.last.rules.last.type = :iconSet
56
+ doc = Nokogiri::XML.parse(cfs.last.to_xml_string)
57
+ assert_equal(doc.xpath(".//conditionalFormatting//cfRule//iconSet//cfvo").size, 3)
58
+ assert_equal(doc.xpath(".//conditionalFormatting//cfRule//iconSet[@iconSet='5Rating']").size, 1)
59
+
60
+ end
61
+
18
62
 
19
63
  def test_add_as_hash
20
64
 
@@ -23,7 +23,7 @@ class TestDataBar < Test::Unit::TestCase
23
23
  assert_equal(@data_bar.maxLength, 0)
24
24
  end
25
25
 
26
- def test_maxLength
26
+ def test_showValue
27
27
  assert_raise(ArgumentError) { @data_bar.showValue = :invalid_type }
28
28
  assert_nothing_raised { @data_bar.showValue = false}
29
29
  assert_equal(@data_bar.showValue, false)
@@ -0,0 +1,265 @@
1
+ # encoding: UTF-8
2
+ require 'tc_helper.rb'
3
+
4
+
5
+ class TestDataValidation < Test::Unit::TestCase
6
+ def setup
7
+ #inverse defaults
8
+ @boolean_options = { :allowBlank => false, :showDropDown => true, :showErrorMessage => false, :showInputMessage => true }
9
+ @nil_options = { :formula1 => 'foo', :formula2 => 'foo', :errorTitle => 'foo', :operator => :lessThan, :prompt => 'foo', :promptTitle => 'foo', :sqref => 'foo' }
10
+ @type_option = { :type => :whole }
11
+ @error_style_option = { :errorStyle => :warning }
12
+
13
+ @string_options = { :formula1 => 'foo', :formula2 => 'foo', :error => 'foo', :errorTitle => 'foo', :prompt => 'foo', :promptTitle => 'foo', :sqref => 'foo' }
14
+ @symbol_options = { :errorStyle => :warning, :operator => :lessThan, :type => :whole}
15
+
16
+ @options = @boolean_options.merge(@nil_options).merge(@type_option).merge(@error_style_option)
17
+
18
+ @dv = Axlsx::DataValidation.new(@options)
19
+ end
20
+
21
+ def test_initialize
22
+ dv = Axlsx::DataValidation.new
23
+
24
+ @boolean_options.each do |key, value|
25
+ assert_equal(!value, dv.send(key.to_sym), "initialized default #{key} should be #{!value}")
26
+ assert_equal(value, @dv.send(key.to_sym), "initialized options #{key} should be #{value}")
27
+ end
28
+
29
+ @nil_options.each do |key, value|
30
+ assert_equal(nil, dv.send(key.to_sym), "initialized default #{key} should be nil")
31
+ assert_equal(value, @dv.send(key.to_sym), "initialized options #{key} should be #{value}")
32
+ end
33
+
34
+ @type_option.each do |key, value|
35
+ assert_equal(:none, dv.send(key.to_sym), "initialized default #{key} should be :none")
36
+ assert_equal(value, @dv.send(key.to_sym), "initialized options #{key} should be #{value}")
37
+ end
38
+
39
+ @error_style_option.each do |key, value|
40
+ assert_equal(:stop, dv.send(key.to_sym), "initialized default #{key} should be :stop")
41
+ assert_equal(value, @dv.send(key.to_sym), "initialized options #{key} should be #{value}")
42
+ end
43
+ end
44
+
45
+ def test_boolean_attribute_validation
46
+ @boolean_options.each do |key, value|
47
+ assert_raise(ArgumentError, "#{key} must be boolean") { @dv.send("#{key}=".to_sym, 'A') }
48
+ assert_nothing_raised { @dv.send("#{key}=".to_sym, true) }
49
+ end
50
+ end
51
+
52
+ def test_string_attribute_validation
53
+ @string_options.each do |key, value|
54
+ assert_raise(ArgumentError, "#{key} must be string") { @dv.send("#{key}=".to_sym, :symbol) }
55
+ assert_nothing_raised { @dv.send("#{key}=".to_sym, "foo") }
56
+ end
57
+ end
58
+
59
+ def test_symbol_attribute_validation
60
+ @symbol_options.each do |key, value|
61
+ assert_raise(ArgumentError, "#{key} must be symbol") { @dv.send("#{key}=".to_sym, "foo") }
62
+ assert_nothing_raised { @dv.send("#{key}=".to_sym, value) }
63
+ end
64
+ end
65
+
66
+ def test_formula1
67
+ assert_raise(ArgumentError) { @dv.formula1 = 10 }
68
+ assert_nothing_raised { @dv.formula1 = "=SUM(A1:A1)" }
69
+ assert_equal(@dv.formula1, "=SUM(A1:A1)")
70
+ end
71
+
72
+ def test_formula2
73
+ assert_raise(ArgumentError) { @dv.formula2 = 10 }
74
+ assert_nothing_raised { @dv.formula2 = "=SUM(A1:A1)" }
75
+ assert_equal(@dv.formula2, "=SUM(A1:A1)")
76
+ end
77
+
78
+ def test_allowBlank
79
+ assert_raise(ArgumentError) { @dv.allowBlank = "foo´" }
80
+ assert_nothing_raised { @dv.allowBlank = false }
81
+ assert_equal(@dv.allowBlank, false)
82
+ end
83
+
84
+ def test_error
85
+ assert_raise(ArgumentError) { @dv.error = :symbol }
86
+ assert_nothing_raised { @dv.error = "This is a error message" }
87
+ assert_equal(@dv.error, "This is a error message")
88
+ end
89
+
90
+ def test_errorStyle
91
+ assert_raise(ArgumentError) { @dv.errorStyle = "foo" }
92
+ assert_nothing_raised { @dv.errorStyle = :information }
93
+ assert_equal(@dv.errorStyle, :information)
94
+ end
95
+
96
+ def test_errorTitle
97
+ assert_raise(ArgumentError) { @dv.errorTitle = :symbol }
98
+ assert_nothing_raised { @dv.errorTitle = "This is the error title" }
99
+ assert_equal(@dv.errorTitle, "This is the error title")
100
+ end
101
+
102
+ def test_operator
103
+ assert_raise(ArgumentError) { @dv.operator = "foo" }
104
+ assert_nothing_raised { @dv.operator = :greaterThan }
105
+ assert_equal(@dv.operator, :greaterThan)
106
+ end
107
+
108
+ def test_prompt
109
+ assert_raise(ArgumentError) { @dv.prompt = :symbol }
110
+ assert_nothing_raised { @dv.prompt = "This is a prompt message" }
111
+ assert_equal(@dv.prompt, "This is a prompt message")
112
+ end
113
+
114
+ def test_promptTitle
115
+ assert_raise(ArgumentError) { @dv.promptTitle = :symbol }
116
+ assert_nothing_raised { @dv.promptTitle = "This is the prompt title" }
117
+ assert_equal(@dv.promptTitle, "This is the prompt title")
118
+ end
119
+
120
+ def test_showDropDown
121
+ assert_raise(ArgumentError) { @dv.showDropDown = "foo´" }
122
+ assert_nothing_raised { @dv.showDropDown = false }
123
+ assert_equal(@dv.showDropDown, false)
124
+ end
125
+
126
+ def test_showErrorMessage
127
+ assert_raise(ArgumentError) { @dv.showErrorMessage = "foo´" }
128
+ assert_nothing_raised { @dv.showErrorMessage = false }
129
+ assert_equal(@dv.showErrorMessage, false)
130
+ end
131
+
132
+ def test_showInputMessage
133
+ assert_raise(ArgumentError) { @dv.showInputMessage = "foo´" }
134
+ assert_nothing_raised { @dv.showInputMessage = false }
135
+ assert_equal(@dv.showInputMessage, false)
136
+ end
137
+
138
+ def test_sqref
139
+ assert_raise(ArgumentError) { @dv.sqref = 10 }
140
+ assert_nothing_raised { @dv.sqref = "A1:A1" }
141
+ assert_equal(@dv.sqref, "A1:A1")
142
+ end
143
+
144
+ def test_type
145
+ assert_raise(ArgumentError) { @dv.type = "foo" }
146
+ assert_nothing_raised { @dv.type = :list }
147
+ assert_equal(@dv.type, :list)
148
+ end
149
+
150
+ def test_whole_decimal_data_time_textLength_to_xml
151
+ p = Axlsx::Package.new
152
+ @ws = p.workbook.add_worksheet :name=>"data_validation"
153
+ @ws.add_data_validation("A1", { :type => :whole, :operator => :between, :formula1 => '5', :formula2 => '10',
154
+ :showErrorMessage => true, :errorTitle => 'Wrong input', :error => 'Only values between 5 and 10',
155
+ :errorStyle => :information, :showInputMessage => true, :promptTitle => 'Be carful!',
156
+ :prompt => 'Only values between 5 and 10'})
157
+
158
+ doc = Nokogiri::XML.parse(@ws.to_xml_string)
159
+
160
+ #test attributes
161
+ assert_equal(1, doc.xpath("//xmlns:worksheet/xmlns:dataValidations[@count='1']/xmlns:dataValidation[@sqref='A1']
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']
164
+ [@errorStyle='information']").size)
165
+ assert doc.xpath("//xmlns:worksheet/xmlns:dataValidations[@count='1']/xmlns:dataValidation[@sqref='A1']
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']
168
+ [@type='whole'][@errorStyle='information']")
169
+
170
+ #test forumula1
171
+ assert_equal(1, doc.xpath("//xmlns:worksheet/xmlns:dataValidations/xmlns:dataValidation/xmlns:formula1").size)
172
+ assert doc.xpath("//xmlns:worksheet/xmlns:dataValidations/xmlns:dataValidation/xmlns:formula1='5'")
173
+
174
+ #test forumula2
175
+ assert_equal(1, doc.xpath("//xmlns:worksheet/xmlns:dataValidations/xmlns:dataValidation/xmlns:formula2").size)
176
+ assert doc.xpath("//xmlns:worksheet/xmlns:dataValidations/xmlns:dataValidation/xmlns:formula2='10'")
177
+ end
178
+
179
+ def test_list_to_xml
180
+ p = Axlsx::Package.new
181
+ @ws = p.workbook.add_worksheet :name=>"data_validation"
182
+ @ws.add_data_validation("A1", { :type => :list, :formula1 => 'A1:A5',
183
+ :showErrorMessage => true, :errorTitle => 'Wrong input', :error => 'Only values from list',
184
+ :errorStyle => :stop, :showInputMessage => true, :promptTitle => 'Be carful!',
185
+ :prompt => 'Only values from list', :showDropDown => true})
186
+
187
+ doc = Nokogiri::XML.parse(@ws.to_xml_string)
188
+
189
+ #test attributes
190
+ assert_equal(1, doc.xpath("//xmlns:worksheet/xmlns:dataValidations[@count='1']/xmlns:dataValidation[@sqref='A1']
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']
193
+ [@errorStyle='stop']").size)
194
+ assert doc.xpath("//xmlns:worksheet/xmlns:dataValidations[@count='1']/xmlns:dataValidation[@sqref='A1']
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']")
197
+
198
+ #test forumula1
199
+ assert_equal(1, doc.xpath("//xmlns:worksheet/xmlns:dataValidations/xmlns:dataValidation/xmlns:formula1").size)
200
+ assert doc.xpath("//xmlns:worksheet/xmlns:dataValidations/xmlns:dataValidation/xmlns:formula1='A1:A5'")
201
+ end
202
+
203
+ def test_custom_to_xml
204
+ p = Axlsx::Package.new
205
+ @ws = p.workbook.add_worksheet :name=>"data_validation"
206
+ @ws.add_data_validation("A1", { :type => :custom, :formula1 => '=5/2',
207
+ :showErrorMessage => true, :errorTitle => 'Wrong input', :error => 'Only values corresponding formula',
208
+ :errorStyle => :stop, :showInputMessage => true, :promptTitle => 'Be carful!',
209
+ :prompt => 'Only values corresponding formula'})
210
+
211
+ doc = Nokogiri::XML.parse(@ws.to_xml_string)
212
+
213
+ #test attributes
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)
217
+ assert doc.xpath("//xmlns:worksheet/xmlns:dataValidations[@count='1']/xmlns:dataValidation[@sqref='A1'][@promptTitle='Be carful!']
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']")
220
+
221
+ #test forumula1
222
+ assert_equal(1, doc.xpath("//xmlns:worksheet/xmlns:dataValidations/xmlns:dataValidation/xmlns:formula1").size)
223
+ assert doc.xpath("//xmlns:worksheet/xmlns:dataValidations/xmlns:dataValidation/xmlns:formula1='=5/2'")
224
+ end
225
+
226
+ def test_multiple_datavalidations_to_xml
227
+ p = Axlsx::Package.new
228
+ @ws = p.workbook.add_worksheet :name=>"data_validation"
229
+ @ws.add_data_validation("A1", { :type => :whole, :operator => :between, :formula1 => '5', :formula2 => '10',
230
+ :showErrorMessage => true, :errorTitle => 'Wrong input', :error => 'Only values between 5 and 10',
231
+ :errorStyle => :information, :showInputMessage => true, :promptTitle => 'Be carful!',
232
+ :prompt => 'Only values between 5 and 10'})
233
+ @ws.add_data_validation("B1", { :type => :list, :formula1 => 'A1:A5',
234
+ :showErrorMessage => true, :errorTitle => 'Wrong input', :error => 'Only values from list',
235
+ :errorStyle => :stop, :showInputMessage => true, :promptTitle => 'Be carful!',
236
+ :prompt => 'Only values from list', :showDropDown => true})
237
+
238
+ doc = Nokogiri::XML.parse(@ws.to_xml_string)
239
+
240
+ #test attributes
241
+ assert_equal(1, doc.xpath("//xmlns:worksheet/xmlns:dataValidations[@count='2']/xmlns:dataValidation[@sqref='A1']
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']
244
+ [@errorStyle='information']").size)
245
+ assert doc.xpath("//xmlns:worksheet/xmlns:dataValidations[@count='2']/xmlns:dataValidation[@sqref='A1']
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']
248
+ [@type='whole'][@errorStyle='information']")
249
+
250
+ #test attributes
251
+ assert_equal(1, doc.xpath("//xmlns:worksheet/xmlns:dataValidations[@count='2']/xmlns:dataValidation[@sqref='B1']
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']
254
+ [@errorStyle='stop']").size)
255
+ assert doc.xpath("//xmlns:worksheet/xmlns:dataValidations[@count='2']/xmlns:dataValidation[@sqref='B1']
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']")
258
+ end
259
+
260
+ def test_empty_attributes
261
+ v = Axlsx::DataValidation.new
262
+ assert_equal(nil, v.send(:get_valid_attributes))
263
+
264
+ end
265
+ end