caxlsx 3.0.4 → 3.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (294) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +9 -9
  3. data/.yardopts_guide +18 -18
  4. data/CHANGELOG.md +354 -334
  5. data/LICENSE +21 -21
  6. data/README.md +168 -170
  7. data/Rakefile +29 -29
  8. data/examples/generate.rb +15 -15
  9. data/lib/axlsx/content_type/abstract_content_type.rb +32 -32
  10. data/lib/axlsx/content_type/content_type.rb +26 -26
  11. data/lib/axlsx/content_type/default.rb +25 -25
  12. data/lib/axlsx/content_type/override.rb +25 -25
  13. data/lib/axlsx/doc_props/app.rb +235 -235
  14. data/lib/axlsx/doc_props/core.rb +39 -39
  15. data/lib/axlsx/drawing/area_chart.rb +99 -99
  16. data/lib/axlsx/drawing/area_series.rb +110 -110
  17. data/lib/axlsx/drawing/ax_data_source.rb +26 -26
  18. data/lib/axlsx/drawing/axes.rb +61 -61
  19. data/lib/axlsx/drawing/axis.rb +190 -190
  20. data/lib/axlsx/drawing/bar_3D_chart.rb +148 -151
  21. data/lib/axlsx/drawing/bar_chart.rb +138 -143
  22. data/lib/axlsx/drawing/bar_series.rb +97 -80
  23. data/lib/axlsx/drawing/bubble_chart.rb +59 -59
  24. data/lib/axlsx/drawing/bubble_series.rb +63 -63
  25. data/lib/axlsx/drawing/cat_axis.rb +85 -85
  26. data/lib/axlsx/drawing/chart.rb +276 -276
  27. data/lib/axlsx/drawing/d_lbls.rb +90 -90
  28. data/lib/axlsx/drawing/drawing.rb +167 -167
  29. data/lib/axlsx/drawing/graphic_frame.rb +54 -54
  30. data/lib/axlsx/drawing/hyperlink.rb +100 -100
  31. data/lib/axlsx/drawing/line_3D_chart.rb +68 -68
  32. data/lib/axlsx/drawing/line_chart.rb +99 -99
  33. data/lib/axlsx/drawing/line_series.rb +110 -110
  34. data/lib/axlsx/drawing/marker.rb +84 -84
  35. data/lib/axlsx/drawing/num_data.rb +52 -52
  36. data/lib/axlsx/drawing/num_data_source.rb +62 -62
  37. data/lib/axlsx/drawing/num_val.rb +34 -34
  38. data/lib/axlsx/drawing/one_cell_anchor.rb +99 -99
  39. data/lib/axlsx/drawing/pic.rb +211 -211
  40. data/lib/axlsx/drawing/picture_locking.rb +42 -42
  41. data/lib/axlsx/drawing/pie_3D_chart.rb +47 -47
  42. data/lib/axlsx/drawing/pie_series.rb +74 -74
  43. data/lib/axlsx/drawing/scaling.rb +60 -60
  44. data/lib/axlsx/drawing/scatter_chart.rb +74 -74
  45. data/lib/axlsx/drawing/scatter_series.rb +129 -98
  46. data/lib/axlsx/drawing/ser_axis.rb +45 -45
  47. data/lib/axlsx/drawing/series.rb +69 -69
  48. data/lib/axlsx/drawing/series_title.rb +25 -25
  49. data/lib/axlsx/drawing/str_data.rb +42 -42
  50. data/lib/axlsx/drawing/str_val.rb +34 -34
  51. data/lib/axlsx/drawing/title.rb +97 -97
  52. data/lib/axlsx/drawing/two_cell_anchor.rb +97 -97
  53. data/lib/axlsx/drawing/val_axis.rb +37 -37
  54. data/lib/axlsx/drawing/view_3D.rb +115 -115
  55. data/lib/axlsx/drawing/vml_drawing.rb +42 -42
  56. data/lib/axlsx/drawing/vml_shape.rb +66 -66
  57. data/lib/axlsx/package.rb +388 -363
  58. data/lib/axlsx/rels/relationship.rb +130 -130
  59. data/lib/axlsx/rels/relationships.rb +32 -32
  60. data/lib/axlsx/stylesheet/border.rb +73 -71
  61. data/lib/axlsx/stylesheet/border_pr.rb +71 -71
  62. data/lib/axlsx/stylesheet/cell_alignment.rb +132 -132
  63. data/lib/axlsx/stylesheet/cell_protection.rb +41 -41
  64. data/lib/axlsx/stylesheet/cell_style.rb +72 -72
  65. data/lib/axlsx/stylesheet/color.rb +76 -76
  66. data/lib/axlsx/stylesheet/dxf.rb +79 -79
  67. data/lib/axlsx/stylesheet/fill.rb +35 -35
  68. data/lib/axlsx/stylesheet/font.rb +156 -156
  69. data/lib/axlsx/stylesheet/gradient_fill.rb +103 -103
  70. data/lib/axlsx/stylesheet/gradient_stop.rb +37 -37
  71. data/lib/axlsx/stylesheet/num_fmt.rb +86 -86
  72. data/lib/axlsx/stylesheet/pattern_fill.rb +73 -73
  73. data/lib/axlsx/stylesheet/styles.rb +494 -420
  74. data/lib/axlsx/stylesheet/table_style.rb +54 -54
  75. data/lib/axlsx/stylesheet/table_style_element.rb +77 -77
  76. data/lib/axlsx/stylesheet/table_styles.rb +46 -46
  77. data/lib/axlsx/stylesheet/xf.rb +147 -147
  78. data/lib/axlsx/util/accessors.rb +64 -64
  79. data/lib/axlsx/util/constants.rb +410 -401
  80. data/lib/axlsx/util/mime_type_utils.rb +11 -11
  81. data/lib/axlsx/util/options_parser.rb +16 -16
  82. data/lib/axlsx/util/serialized_attributes.rb +89 -89
  83. data/lib/axlsx/util/simple_typed_list.rb +179 -179
  84. data/lib/axlsx/util/storage.rb +146 -146
  85. data/lib/axlsx/util/validators.rb +312 -312
  86. data/lib/axlsx/util/zip_command.rb +73 -73
  87. data/lib/axlsx/version.rb +5 -5
  88. data/lib/axlsx/workbook/defined_name.rb +128 -128
  89. data/lib/axlsx/workbook/defined_names.rb +21 -21
  90. data/lib/axlsx/workbook/shared_strings_table.rb +77 -77
  91. data/lib/axlsx/workbook/workbook.rb +395 -370
  92. data/lib/axlsx/workbook/workbook_view.rb +80 -80
  93. data/lib/axlsx/workbook/workbook_views.rb +22 -22
  94. data/lib/axlsx/workbook/worksheet/auto_filter/auto_filter.rb +77 -77
  95. data/lib/axlsx/workbook/worksheet/auto_filter/filter_column.rb +94 -94
  96. data/lib/axlsx/workbook/worksheet/auto_filter/filters.rb +244 -244
  97. data/lib/axlsx/workbook/worksheet/break.rb +35 -35
  98. data/lib/axlsx/workbook/worksheet/cell.rb +506 -505
  99. data/lib/axlsx/workbook/worksheet/cell_serializer.rb +164 -164
  100. data/lib/axlsx/workbook/worksheet/cfvo.rb +60 -60
  101. data/lib/axlsx/workbook/worksheet/cfvos.rb +18 -18
  102. data/lib/axlsx/workbook/worksheet/col.rb +145 -145
  103. data/lib/axlsx/workbook/worksheet/col_breaks.rb +35 -35
  104. data/lib/axlsx/workbook/worksheet/color_scale.rb +110 -110
  105. data/lib/axlsx/workbook/worksheet/cols.rb +23 -23
  106. data/lib/axlsx/workbook/worksheet/comment.rb +91 -91
  107. data/lib/axlsx/workbook/worksheet/comments.rb +82 -82
  108. data/lib/axlsx/workbook/worksheet/conditional_formatting.rb +82 -82
  109. data/lib/axlsx/workbook/worksheet/conditional_formatting_rule.rb +220 -220
  110. data/lib/axlsx/workbook/worksheet/conditional_formattings.rb +25 -25
  111. data/lib/axlsx/workbook/worksheet/data_bar.rb +129 -129
  112. data/lib/axlsx/workbook/worksheet/data_validation.rb +246 -246
  113. data/lib/axlsx/workbook/worksheet/data_validations.rb +28 -28
  114. data/lib/axlsx/workbook/worksheet/date_time_converter.rb +30 -30
  115. data/lib/axlsx/workbook/worksheet/dimension.rb +64 -64
  116. data/lib/axlsx/workbook/worksheet/header_footer.rb +52 -52
  117. data/lib/axlsx/workbook/worksheet/icon_set.rb +81 -81
  118. data/lib/axlsx/workbook/worksheet/merged_cells.rb +37 -37
  119. data/lib/axlsx/workbook/worksheet/outline_pr.rb +33 -33
  120. data/lib/axlsx/workbook/worksheet/page_margins.rb +97 -97
  121. data/lib/axlsx/workbook/worksheet/page_set_up_pr.rb +44 -44
  122. data/lib/axlsx/workbook/worksheet/page_setup.rb +240 -240
  123. data/lib/axlsx/workbook/worksheet/pane.rb +139 -139
  124. data/lib/axlsx/workbook/worksheet/pivot_table.rb +296 -289
  125. data/lib/axlsx/workbook/worksheet/pivot_table_cache_definition.rb +66 -66
  126. data/lib/axlsx/workbook/worksheet/pivot_tables.rb +24 -24
  127. data/lib/axlsx/workbook/worksheet/print_options.rb +39 -39
  128. data/lib/axlsx/workbook/worksheet/protected_range.rb +47 -47
  129. data/lib/axlsx/workbook/worksheet/protected_ranges.rb +37 -37
  130. data/lib/axlsx/workbook/worksheet/rich_text.rb +55 -55
  131. data/lib/axlsx/workbook/worksheet/rich_text_run.rb +250 -250
  132. data/lib/axlsx/workbook/worksheet/row.rb +164 -162
  133. data/lib/axlsx/workbook/worksheet/row_breaks.rb +33 -33
  134. data/lib/axlsx/workbook/worksheet/selection.rb +101 -101
  135. data/lib/axlsx/workbook/worksheet/sheet_calc_pr.rb +29 -29
  136. data/lib/axlsx/workbook/worksheet/sheet_data.rb +27 -27
  137. data/lib/axlsx/workbook/worksheet/sheet_pr.rb +87 -87
  138. data/lib/axlsx/workbook/worksheet/sheet_protection.rb +118 -118
  139. data/lib/axlsx/workbook/worksheet/sheet_view.rb +213 -213
  140. data/lib/axlsx/workbook/worksheet/table.rb +102 -102
  141. data/lib/axlsx/workbook/worksheet/table_style_info.rb +49 -49
  142. data/lib/axlsx/workbook/worksheet/tables.rb +34 -34
  143. data/lib/axlsx/workbook/worksheet/worksheet.rb +786 -764
  144. data/lib/axlsx/workbook/worksheet/worksheet_comments.rb +58 -58
  145. data/lib/axlsx/workbook/worksheet/worksheet_drawing.rb +58 -58
  146. data/lib/axlsx/workbook/worksheet/worksheet_hyperlink.rb +74 -74
  147. data/lib/axlsx/workbook/worksheet/worksheet_hyperlinks.rb +38 -38
  148. data/lib/axlsx.rb +185 -170
  149. data/lib/caxlsx.rb +2 -2
  150. data/lib/schema/dc.xsd +118 -118
  151. data/lib/schema/dcmitype.xsd +51 -51
  152. data/lib/schema/dcterms.xsd +331 -331
  153. data/lib/schema/dml-chartDrawing.xsd +146 -146
  154. data/lib/schema/dml-compatibility.xsd +14 -14
  155. data/lib/schema/dml-lockedCanvas.xsd +11 -11
  156. data/lib/schema/dml-main.xsd +3048 -3048
  157. data/lib/schema/dml-picture.xsd +23 -23
  158. data/lib/schema/dml-spreadsheetDrawing.xsd +185 -185
  159. data/lib/schema/dml-wordprocessingDrawing.xsd +185 -185
  160. data/lib/schema/shared-additionalCharacteristics.xsd +28 -28
  161. data/lib/schema/shared-bibliography.xsd +144 -144
  162. data/lib/schema/shared-commonSimpleTypes.xsd +166 -166
  163. data/lib/schema/shared-customXmlDataProperties.xsd +25 -25
  164. data/lib/schema/shared-customXmlSchemaProperties.xsd +18 -18
  165. data/lib/schema/shared-documentPropertiesCustom.xsd +59 -59
  166. data/lib/schema/shared-documentPropertiesExtended.xsd +56 -56
  167. data/lib/schema/shared-documentPropertiesVariantTypes.xsd +195 -195
  168. data/lib/schema/shared-relationshipReference.xsd +25 -25
  169. data/lib/schema/vml-main.xsd +569 -569
  170. data/lib/schema/vml-officeDrawing.xsd +509 -509
  171. data/lib/schema/vml-presentationDrawing.xsd +12 -12
  172. data/lib/schema/vml-spreadsheetDrawing.xsd +108 -108
  173. data/lib/schema/vml-wordprocessingDrawing.xsd +96 -96
  174. data/lib/schema/xml.xsd +116 -116
  175. data/test/benchmark.rb +72 -72
  176. data/test/content_type/tc_content_type.rb +76 -76
  177. data/test/content_type/tc_default.rb +16 -16
  178. data/test/content_type/tc_override.rb +14 -14
  179. data/test/doc_props/tc_app.rb +43 -43
  180. data/test/doc_props/tc_core.rb +42 -42
  181. data/test/drawing/tc_area_chart.rb +39 -39
  182. data/test/drawing/tc_area_series.rb +71 -71
  183. data/test/drawing/tc_axes.rb +7 -7
  184. data/test/drawing/tc_axis.rb +112 -112
  185. data/test/drawing/tc_bar_3D_chart.rb +86 -71
  186. data/test/drawing/tc_bar_chart.rb +86 -71
  187. data/test/drawing/tc_bar_series.rb +46 -37
  188. data/test/drawing/tc_bubble_chart.rb +44 -44
  189. data/test/drawing/tc_bubble_series.rb +21 -21
  190. data/test/drawing/tc_cat_axis.rb +31 -31
  191. data/test/drawing/tc_cat_axis_data.rb +27 -27
  192. data/test/drawing/tc_chart.rb +123 -123
  193. data/test/drawing/tc_d_lbls.rb +57 -57
  194. data/test/drawing/tc_data_source.rb +23 -23
  195. data/test/drawing/tc_drawing.rb +80 -80
  196. data/test/drawing/tc_graphic_frame.rb +27 -27
  197. data/test/drawing/tc_hyperlink.rb +64 -64
  198. data/test/drawing/tc_line_3d_chart.rb +47 -47
  199. data/test/drawing/tc_line_chart.rb +39 -39
  200. data/test/drawing/tc_line_series.rb +71 -71
  201. data/test/drawing/tc_marker.rb +44 -44
  202. data/test/drawing/tc_named_axis_data.rb +27 -27
  203. data/test/drawing/tc_num_data.rb +31 -31
  204. data/test/drawing/tc_num_val.rb +29 -29
  205. data/test/drawing/tc_one_cell_anchor.rb +66 -66
  206. data/test/drawing/tc_pic.rb +103 -103
  207. data/test/drawing/tc_picture_locking.rb +72 -72
  208. data/test/drawing/tc_pie_3D_chart.rb +28 -28
  209. data/test/drawing/tc_pie_series.rb +33 -33
  210. data/test/drawing/tc_scaling.rb +36 -36
  211. data/test/drawing/tc_scatter_chart.rb +48 -48
  212. data/test/drawing/tc_scatter_series.rb +74 -56
  213. data/test/drawing/tc_ser_axis.rb +31 -31
  214. data/test/drawing/tc_series.rb +23 -23
  215. data/test/drawing/tc_series_title.rb +54 -54
  216. data/test/drawing/tc_str_data.rb +18 -18
  217. data/test/drawing/tc_str_val.rb +30 -30
  218. data/test/drawing/tc_title.rb +70 -70
  219. data/test/drawing/tc_two_cell_anchor.rb +36 -36
  220. data/test/drawing/tc_val_axis.rb +24 -24
  221. data/test/drawing/tc_view_3D.rb +54 -54
  222. data/test/drawing/tc_vml_drawing.rb +25 -25
  223. data/test/drawing/tc_vml_shape.rb +106 -106
  224. data/test/profile.rb +24 -24
  225. data/test/rels/tc_relationship.rb +52 -52
  226. data/test/rels/tc_relationships.rb +37 -37
  227. data/test/stylesheet/tc_border.rb +37 -37
  228. data/test/stylesheet/tc_border_pr.rb +32 -32
  229. data/test/stylesheet/tc_cell_alignment.rb +81 -81
  230. data/test/stylesheet/tc_cell_protection.rb +29 -29
  231. data/test/stylesheet/tc_cell_style.rb +57 -57
  232. data/test/stylesheet/tc_color.rb +43 -43
  233. data/test/stylesheet/tc_dxf.rb +81 -81
  234. data/test/stylesheet/tc_fill.rb +18 -18
  235. data/test/stylesheet/tc_font.rb +133 -133
  236. data/test/stylesheet/tc_gradient_fill.rb +72 -72
  237. data/test/stylesheet/tc_gradient_stop.rb +31 -31
  238. data/test/stylesheet/tc_num_fmt.rb +30 -30
  239. data/test/stylesheet/tc_pattern_fill.rb +43 -43
  240. data/test/stylesheet/tc_styles.rb +309 -261
  241. data/test/stylesheet/tc_table_style.rb +44 -44
  242. data/test/stylesheet/tc_table_style_element.rb +45 -45
  243. data/test/stylesheet/tc_table_styles.rb +29 -29
  244. data/test/stylesheet/tc_xf.rb +120 -120
  245. data/test/tc_axlsx.rb +109 -109
  246. data/test/tc_helper.rb +10 -12
  247. data/test/tc_package.rb +317 -264
  248. data/test/util/tc_mime_type_utils.rb +13 -13
  249. data/test/util/tc_serialized_attributes.rb +19 -19
  250. data/test/util/tc_simple_typed_list.rb +77 -77
  251. data/test/util/tc_validators.rb +210 -210
  252. data/test/workbook/tc_defined_name.rb +49 -49
  253. data/test/workbook/tc_shared_strings_table.rb +59 -59
  254. data/test/workbook/tc_workbook.rb +165 -160
  255. data/test/workbook/tc_workbook_view.rb +50 -50
  256. data/test/workbook/worksheet/auto_filter/tc_auto_filter.rb +38 -38
  257. data/test/workbook/worksheet/auto_filter/tc_filter_column.rb +76 -76
  258. data/test/workbook/worksheet/auto_filter/tc_filters.rb +50 -50
  259. data/test/workbook/worksheet/tc_break.rb +49 -49
  260. data/test/workbook/worksheet/tc_cell.rb +465 -453
  261. data/test/workbook/worksheet/tc_cfvo.rb +31 -31
  262. data/test/workbook/worksheet/tc_col.rb +93 -93
  263. data/test/workbook/worksheet/tc_color_scale.rb +58 -58
  264. data/test/workbook/worksheet/tc_comment.rb +72 -72
  265. data/test/workbook/worksheet/tc_comments.rb +57 -57
  266. data/test/workbook/worksheet/tc_conditional_formatting.rb +224 -224
  267. data/test/workbook/worksheet/tc_data_bar.rb +46 -46
  268. data/test/workbook/worksheet/tc_data_validation.rb +265 -265
  269. data/test/workbook/worksheet/tc_date_time_converter.rb +124 -124
  270. data/test/workbook/worksheet/tc_header_footer.rb +151 -151
  271. data/test/workbook/worksheet/tc_icon_set.rb +45 -45
  272. data/test/workbook/worksheet/tc_outline_pr.rb +19 -19
  273. data/test/workbook/worksheet/tc_page_margins.rb +97 -97
  274. data/test/workbook/worksheet/tc_page_set_up_pr.rb +15 -15
  275. data/test/workbook/worksheet/tc_page_setup.rb +143 -143
  276. data/test/workbook/worksheet/tc_pane.rb +54 -54
  277. data/test/workbook/worksheet/tc_pivot_table.rb +180 -135
  278. data/test/workbook/worksheet/tc_pivot_table_cache_definition.rb +62 -54
  279. data/test/workbook/worksheet/tc_print_options.rb +72 -72
  280. data/test/workbook/worksheet/tc_protected_range.rb +17 -17
  281. data/test/workbook/worksheet/tc_rich_text.rb +44 -44
  282. data/test/workbook/worksheet/tc_rich_text_run.rb +173 -173
  283. data/test/workbook/worksheet/tc_row.rb +160 -139
  284. data/test/workbook/worksheet/tc_selection.rb +55 -55
  285. data/test/workbook/worksheet/tc_sheet_calc_pr.rb +18 -18
  286. data/test/workbook/worksheet/tc_sheet_format_pr.rb +88 -88
  287. data/test/workbook/worksheet/tc_sheet_pr.rb +49 -49
  288. data/test/workbook/worksheet/tc_sheet_protection.rb +117 -117
  289. data/test/workbook/worksheet/tc_sheet_view.rb +214 -214
  290. data/test/workbook/worksheet/tc_table.rb +77 -67
  291. data/test/workbook/worksheet/tc_table_style_info.rb +53 -53
  292. data/test/workbook/worksheet/tc_worksheet.rb +632 -601
  293. data/test/workbook/worksheet/tc_worksheet_hyperlink.rb +55 -55
  294. metadata +11 -11
@@ -1,601 +1,632 @@
1
- require 'tc_helper.rb'
2
-
3
- class TestWorksheet < Test::Unit::TestCase
4
- def setup
5
- @p = Axlsx::Package.new
6
- @wb = @p.workbook
7
- @ws = @wb.add_worksheet
8
- end
9
-
10
-
11
- def test_pn
12
- assert_equal(@ws.pn, "worksheets/sheet1.xml")
13
- ws = @ws.workbook.add_worksheet
14
- assert_equal(ws.pn, "worksheets/sheet2.xml")
15
- end
16
-
17
- def test_name_is_html_encoded
18
- @ws.name = '<foo> & <bar>'
19
- assert_equal(@ws.name, '&lt;foo&gt; &amp; &lt;bar&gt;')
20
- end
21
-
22
- def test_name_exception_on_invalid_character
23
- assert_raises(ArgumentError) { @ws.name = 'foo:bar' }
24
- assert_raises(ArgumentError) { @ws.name = 'foo[bar' }
25
- assert_raises(ArgumentError) { @ws.name = 'foo]bar' }
26
- assert_raises(ArgumentError) { @ws.name = 'foo*bar' }
27
- assert_raises(ArgumentError) { @ws.name = 'foo/bar' }
28
- assert_raises(ArgumentError) { @ws.name = 'foo\bar' }
29
- assert_raises(ArgumentError) { @ws.name = 'foo?bar' }
30
- end
31
-
32
- def test_name_unique
33
- assert_raise(ArgumentError, "worksheet name must be unique") { n = @ws.name; @ws.workbook.add_worksheet(:name=> n) }
34
- end
35
-
36
- def test_name_unique_only_checks_other_worksheet_names
37
- assert_nothing_raised { @ws.name = @ws.name }
38
- assert_nothing_raised { Axlsx::Package.new.workbook.add_worksheet :name => 'Sheet1' }
39
- end
40
-
41
- def test_exception_if_name_too_long
42
- assert_nothing_raised { @ws.name = 'x' * 31 }
43
- assert_raises(ArgumentError) { @ws.name = 'x' * 32 }
44
- end
45
-
46
- def test_exception_if_name_too_long_because_of_multibyte_characters
47
- four_characters_for_excel = "\u{1F1EB 1F1F7}" # french flag emoji
48
- assert_raises(ArgumentError, "name too long!") do
49
- @ws.name = four_characters_for_excel + "x" * 28
50
- end
51
- assert_nothing_raised { @ws.name = "#{four_characters_for_excel}123456789012345678901234567" }
52
- assert_nothing_raised { @ws.name = "123456789012345678901234567890…" }
53
- assert_nothing_raised { @ws.name = "123456789012345678901234567890✔" }
54
- end
55
-
56
- def test_page_margins
57
- assert(@ws.page_margins.is_a? Axlsx::PageMargins)
58
- end
59
-
60
- def test_page_margins_yeild
61
- @ws.page_margins do |pm|
62
- assert(pm.is_a? Axlsx::PageMargins)
63
- assert(@ws.page_margins == pm)
64
- end
65
- end
66
-
67
- def test_page_setup
68
- assert(@ws.page_setup.is_a? Axlsx::PageSetup)
69
- end
70
-
71
- def test_page_setup_yield
72
- @ws.page_setup do |ps|
73
- assert(ps.is_a? Axlsx::PageSetup)
74
- assert(@ws.page_setup == ps)
75
- end
76
- end
77
-
78
- def test_print_options
79
- assert(@ws.print_options.is_a? Axlsx::PrintOptions)
80
- end
81
-
82
- def test_print_options_yield
83
- @ws.print_options do |po|
84
- assert(po.is_a? Axlsx::PrintOptions)
85
- assert(@ws.print_options == po)
86
- end
87
- end
88
-
89
- def test_header_footer
90
- assert(@ws.header_footer.is_a? Axlsx::HeaderFooter)
91
- end
92
-
93
- def test_header_footer_yield
94
- @ws.header_footer do |hf|
95
- assert(hf.is_a? Axlsx::HeaderFooter)
96
- assert(@ws.header_footer == hf)
97
- end
98
- end
99
-
100
- def test_state
101
- assert_equal(:visible, @ws.state)
102
- end
103
-
104
- def test_state_validation
105
- assert_raise(ArgumentError) { @ws.state = :dead }
106
- assert_nothing_raised { @ws.state = :very_hidden }
107
- end
108
-
109
- def test_no_autowidth
110
- @ws.workbook.use_autowidth = false
111
- @ws.add_row [1,2,3,4]
112
- assert_equal(@ws.column_info[0].width, nil)
113
- end
114
-
115
- def test_initialization_options
116
- page_margins = {:left => 2, :right => 2, :bottom => 2, :top => 2, :header => 2, :footer => 2}
117
- page_setup = {:fit_to_height => 1, :fit_to_width => 1, :orientation => :landscape, :paper_width => "210mm", :paper_height => "297mm", :scale => 80}
118
- print_options = {:grid_lines => true, :headings => true, :horizontal_centered => true, :vertical_centered => true}
119
- header_footer = {:different_first => false, :different_odd_even => false, :odd_header => 'Header'}
120
- optioned = @ws.workbook.add_worksheet(:name => 'bob', :page_margins => page_margins, :page_setup => page_setup, :print_options => print_options, :header_footer => header_footer)
121
- page_margins.keys.each do |key|
122
- assert_equal(page_margins[key], optioned.page_margins.send(key))
123
- end
124
- page_setup.keys.each do |key|
125
- assert_equal(page_setup[key], optioned.page_setup.send(key))
126
- end
127
- print_options.keys.each do |key|
128
- assert_equal(print_options[key], optioned.print_options.send(key))
129
- end
130
- header_footer.keys.each do |key|
131
- assert_equal(header_footer[key], optioned.header_footer.send(key))
132
- end
133
- assert_equal(optioned.name, 'bob')
134
-
135
- end
136
-
137
-
138
- # def test_use_gridlines
139
- # assert_raise(ArgumentError) { @ws.show_gridlines = -1.1 }
140
- # assert_nothing_raised { @ws.show_gridlines = false }
141
- # assert_equal(@ws.show_gridlines, false)
142
- # end
143
-
144
- # def test_selected
145
- # assert_raise(ArgumentError) { @ws.selected = -1.1 }
146
- # assert_nothing_raised { @ws.selected = true }
147
- # assert_equal(@ws.selected, true)
148
- # end
149
-
150
- def test_rels_pn
151
- assert_equal(@ws.rels_pn, "worksheets/_rels/sheet1.xml.rels")
152
- ws = @ws.workbook.add_worksheet
153
- assert_equal(ws.rels_pn, "worksheets/_rels/sheet2.xml.rels")
154
- end
155
-
156
- def test_rId
157
- assert_equal @ws.workbook.relationships.for(@ws).Id, @ws.rId
158
- end
159
-
160
- def test_index
161
- assert_equal(@ws.index, @ws.workbook.worksheets.index(@ws))
162
- end
163
-
164
- def test_dimension
165
- @ws.add_row [1, 2, 3]
166
- @ws.add_row [4, 5, 6]
167
- assert_equal @ws.dimension.sqref, "A1:C2"
168
- end
169
-
170
- def test_dimension_with_empty_row
171
- @ws.add_row
172
- assert_equal "A1:AA200", @ws.dimension.sqref
173
- end
174
-
175
- def test_referencing
176
- @ws.add_row [1, 2, 3]
177
- @ws.add_row [4, 5, 6]
178
- range = @ws["A1:C2"]
179
- first_row = @ws[0]
180
- last_row = @ws[1]
181
- assert_equal(@ws.rows[0],first_row)
182
- assert_equal(@ws.rows[1],last_row)
183
- assert_equal(range.size, 6)
184
- assert_equal(range.first, @ws.rows.first.cells.first)
185
- assert_equal(range.last, @ws.rows.last.cells.last)
186
- end
187
-
188
- def test_add_row
189
- assert(@ws.rows.empty?, "sheet has no rows by default")
190
- r = @ws.add_row([1,2,3])
191
- assert_equal(@ws.rows.size, 1, "add_row adds a row")
192
- assert_equal(@ws.rows.first, r, "the row returned is the row added")
193
- end
194
-
195
- def test_add_chart
196
- assert(@ws.workbook.charts.empty?, "the sheet's workbook should not have any charts by default")
197
- @ws.add_chart Axlsx::Pie3DChart
198
- assert_equal(@ws.workbook.charts.size, 1, "add_chart adds a chart to the workbook")
199
- end
200
-
201
- def test_add_page_break_with_string_cell_ref
202
- assert(@ws.row_breaks.empty?)
203
- assert(@ws.col_breaks.empty?)
204
- @ws.add_page_break("B1")
205
- assert_equal(1, @ws.row_breaks.size)
206
- assert_equal(1, @ws.col_breaks.size)
207
- end
208
-
209
- def test_add_page_break_with_cell
210
- @ws.add_row [1, 2, 3, 4]
211
- @ws.add_row [1, 2, 3, 4]
212
-
213
-
214
- assert(@ws.row_breaks.empty?)
215
- assert(@ws.col_breaks.empty?)
216
- @ws.add_page_break(@ws.rows.last.cells[1])
217
- assert_equal(1, @ws.row_breaks.size)
218
- assert_equal(1, @ws.col_breaks.size)
219
- end
220
-
221
-
222
- def test_drawing
223
- assert @ws.drawing == nil
224
- @ws.add_chart(Axlsx::Pie3DChart)
225
- assert @ws.drawing.is_a?(Axlsx::Drawing)
226
- end
227
-
228
- def test_add_pivot_table
229
- assert(@ws.workbook.pivot_tables.empty?, "the sheet's workbook should not have any pivot tables by default")
230
- @ws.add_pivot_table 'G5:G6', 'A1:D:10'
231
- assert_equal(@ws.workbook.pivot_tables.size, 1, "add_pivot_tables adds a pivot_table to the workbook")
232
- end
233
-
234
- def test_col_style
235
- @ws.add_row [1,2,3,4]
236
- @ws.add_row [1,2,3,4]
237
- @ws.add_row [1,2,3,4]
238
- @ws.add_row [1,2,3,4]
239
- @ws.col_style( (1..2), 1, :row_offset=>1)
240
- @ws.rows[(1..-1)].each do | r |
241
- assert_equal(r.cells[1].style, 1)
242
- assert_equal(r.cells[2].style, 1)
243
- end
244
- assert_equal(@ws.rows.first.cells[1].style, 0)
245
- assert_equal(@ws.rows.first.cells[0].style, 0)
246
- end
247
-
248
- def test_col_style_with_empty_column
249
- @ws.add_row [1,2,3,4]
250
- @ws.add_row [1]
251
- @ws.add_row [1,2,3,4]
252
- assert_nothing_raised {@ws.col_style(1, 1)}
253
- end
254
-
255
- def test_cols
256
- @ws.add_row [1,2,3,4]
257
- @ws.add_row [1,2,3,4]
258
- @ws.add_row [1,2,3]
259
- @ws.add_row [1,2,3,4]
260
- c = @ws.cols[1]
261
- assert_equal(c.size, 4)
262
- assert_equal(c[0].value, 2)
263
- end
264
-
265
- def test_cols_with_block
266
- @ws.add_row [1,2,3]
267
- @ws.add_row [1]
268
- cols = @ws.cols {|row, column| :foo }
269
- assert_equal(:foo, cols[1][1])
270
- end
271
-
272
- def test_row_style
273
- @ws.add_row [1,2,3,4]
274
- @ws.add_row [1,2,3,4]
275
- @ws.add_row [1,2,3,4]
276
- @ws.add_row [1,2,3,4]
277
- @ws.row_style 1, 1, :col_offset=>1
278
- @ws.rows[1].cells[(1..-1)].each do | c |
279
- assert_equal(c.style, 1)
280
- end
281
- assert_equal(@ws.rows[1].cells[0].style, 0)
282
- assert_equal(@ws.rows[2].cells[1].style, 0)
283
- @ws.row_style( 1..2, 1, :col_offset => 2)
284
- @ws.rows[(1..2)].each do |r|
285
- r.cells[(2..-1)].each do |c|
286
- assert_equal(c.style, 1)
287
- end
288
- end
289
- end
290
-
291
- # def test_to_xml_string_fit_to_page
292
- # @ws.page_setup.fit_to_width = 1
293
- # doc = Nokogiri::XML(@ws.to_xml_string)
294
- # assert_equal(doc.xpath('//xmlns:worksheet/xmlns:sheetPr/xmlns:pageSetUpPr[@fitToPage=1]').size, 1)
295
- # end
296
-
297
- def test_to_xml_string_dimensions
298
- @ws.add_row [1,2,3]
299
- doc = Nokogiri::XML(@ws.to_xml_string)
300
- assert_equal(doc.xpath('//xmlns:worksheet/xmlns:dimension[@ref="A1:C1"]').size, 1)
301
- end
302
-
303
- # def test_fit_to_page_assignation_does_nothing
304
- # @ws.fit_to_page = true
305
- # assert_equal(@ws.fit_to_page?, false)
306
- # end
307
-
308
- # def test_to_xml_string_selected
309
- # @ws.selected = true
310
- # doc = Nokogiri::XML(@ws.to_xml_string)
311
- # assert_equal(doc.xpath('//xmlns:worksheet/xmlns:sheetViews/xmlns:sheetView[@tabSelected=1]').size, 1)
312
- # end
313
-
314
- # def test_to_xml_string_show_gridlines
315
- # @ws.show_gridlines = false
316
- # doc = Nokogiri::XML(@ws.to_xml_string)
317
- # assert_equal(doc.xpath('//xmlns:worksheet/xmlns:sheetViews/xmlns:sheetView[@showGridLines=0]').size, 1)
318
- # end
319
-
320
- def test_to_xml_string_auto_fit_data
321
- @ws.add_row [1, "two"]
322
- doc = Nokogiri::XML(@ws.to_xml_string)
323
- assert_equal(doc.xpath('//xmlns:worksheet/xmlns:cols/xmlns:col').size, 2)
324
- end
325
-
326
- def test_to_xml_string_sheet_data
327
- @ws.add_row [1, "two"]
328
- doc = Nokogiri::XML(@ws.to_xml_string)
329
- assert_equal(doc.xpath('//xmlns:worksheet/xmlns:sheetData/xmlns:row').size, 1)
330
- end
331
-
332
- def test_to_xml_string_auto_filter
333
- @ws.add_row [1, "two"]
334
- @ws.auto_filter.range = "A1:B1"
335
- doc = Nokogiri::XML(@ws.to_xml_string)
336
- assert_equal(doc.xpath('//xmlns:worksheet/xmlns:autoFilter[@ref="A1:B1"]').size, 1)
337
- end
338
-
339
- def test_to_xml_string_merge_cells
340
- @ws.add_row [1, "two"]
341
- @ws.merge_cells "A1:D1"
342
- @ws.merge_cells "E1:F1"
343
- doc = Nokogiri::XML(@ws.to_xml_string)
344
- assert_equal(doc.xpath('//xmlns:worksheet/xmlns:mergeCells/xmlns:mergeCell[@ref="A1:D1"]').size, 1)
345
- assert_equal(doc.xpath('//xmlns:worksheet/xmlns:mergeCells/xmlns:mergeCell[@ref="E1:F1"]').size, 1)
346
- end
347
-
348
- def test_to_xml_string_merge_cells_row
349
- row = @ws.add_row [1, "two"]
350
- @ws.merge_cells row
351
- doc = Nokogiri::XML(@ws.to_xml_string)
352
- assert_equal(doc.xpath('//xmlns:worksheet/xmlns:mergeCells/xmlns:mergeCell[@ref="A1:B1"]').size, 1)
353
- end
354
-
355
- def test_to_xml_string_row_breaks
356
- @ws.add_page_break("A1")
357
- doc = Nokogiri::XML(@ws.to_xml_string)
358
- assert_equal(doc.xpath('//xmlns:worksheet/xmlns:rowBreaks/xmlns:brk[@id="0"]').size, 1)
359
- end
360
-
361
- def test_to_xml_string_sheet_protection
362
- @ws.sheet_protection.password = 'fish'
363
- doc = Nokogiri::XML(@ws.to_xml_string)
364
- assert(doc.xpath('//xmlns:sheetProtection'))
365
- end
366
-
367
- def test_to_xml_string_page_margins
368
- @ws.page_margins do |pm|
369
- pm.left = 9
370
- pm.right = 7
371
- end
372
- doc = Nokogiri::XML(@ws.to_xml_string)
373
- assert_equal(doc.xpath('//xmlns:worksheet/xmlns:pageMargins[@left="9"][@right="7"]').size, 1)
374
- end
375
-
376
- def test_to_xml_string_page_setup
377
- @ws.page_setup do |ps|
378
- ps.paper_width = "210mm"
379
- ps.scale = 80
380
- end
381
- doc = Nokogiri::XML(@ws.to_xml_string)
382
- assert_equal(doc.xpath('//xmlns:worksheet/xmlns:pageSetup[@paperWidth="210mm"][@scale="80"]').size, 1)
383
- end
384
-
385
- def test_to_xml_string_print_options
386
- @ws.print_options do |po|
387
- po.grid_lines = true
388
- po.horizontal_centered = true
389
- end
390
- doc = Nokogiri::XML(@ws.to_xml_string)
391
- assert_equal(doc.xpath('//xmlns:worksheet/xmlns:printOptions[@gridLines=1][@horizontalCentered=1]').size, 1)
392
- end
393
-
394
- def test_to_xml_string_header_footer
395
- @ws.header_footer do |hf|
396
- hf.different_first = false
397
- hf.different_odd_even = false
398
- hf.odd_header = 'Test Header'
399
- end
400
- doc = Nokogiri::XML(@ws.to_xml_string)
401
- assert_equal(doc.xpath('//xmlns:worksheet/xmlns:headerFooter[@differentFirst=0][@differentOddEven=0]').size, 1)
402
- end
403
-
404
- def test_to_xml_string_drawing
405
- @ws.add_chart Axlsx::Pie3DChart
406
- doc = Nokogiri::XML(@ws.to_xml_string)
407
- assert_equal @ws.send(:worksheet_drawing).relationship.Id, doc.xpath('//xmlns:worksheet/xmlns:drawing').first["r:id"]
408
- end
409
-
410
- def test_to_xml_string_tables
411
- @ws.add_row ["one", "two"]
412
- @ws.add_row [1, 2]
413
- table = @ws.add_table "A1:B2"
414
- doc = Nokogiri::XML(@ws.to_xml_string)
415
- assert_equal(doc.xpath('//xmlns:worksheet/xmlns:tableParts[@count="1"]').size, 1)
416
- assert_equal table.rId, doc.xpath('//xmlns:worksheet/xmlns:tableParts/xmlns:tablePart').first["r:id"]
417
- end
418
-
419
- def test_to_xml_string
420
- schema = Nokogiri::XML::Schema(File.open(Axlsx::SML_XSD))
421
- doc = Nokogiri::XML(@ws.to_xml_string)
422
- assert(schema.validate(doc).map{ |e| puts e.message; e }.empty?, "error free validation")
423
- end
424
-
425
- def test_styles
426
- assert(@ws.styles.is_a?(Axlsx::Styles), 'worksheet provides access to styles')
427
- end
428
-
429
- def test_to_xml_string_with_illegal_chars
430
- old = Axlsx::trust_input
431
- Axlsx::trust_input = false
432
- nasties = "\v\u2028\u0001\u0002\u0003\u0004\u0005\u0006\u0007\u0008\u001f"
433
- @ws.add_row [nasties]
434
- assert_equal(0, @ws.rows.last.cells.last.value.index("\v"))
435
- assert_equal(nil, @ws.to_xml_string.index("\v"))
436
- Axlsx::trust_input = old
437
- end
438
-
439
- def test_to_xml_string_with_newlines
440
- cell_with_newline = "foo\n\r\nbar"
441
- @ws.add_row [cell_with_newline]
442
- assert_equal("foo\n\r\nbar", @ws.rows.last.cells.last.value)
443
- assert_not_nil(@ws.to_xml_string.index("foo\n\r\nbar"))
444
- end
445
- # Make sure the XML for all optional elements (like pageMargins, autoFilter, ...)
446
- # is generated in correct order.
447
- def test_valid_with_optional_elements
448
- @ws.page_margins.set :left => 9
449
- @ws.page_setup.set :fit_to_width => 1
450
- @ws.print_options.set :headings => true
451
- @ws.auto_filter.range = "A1:C3"
452
- @ws.merge_cells "A4:A5"
453
- @ws.add_chart Axlsx::Pie3DChart
454
- @ws.add_table "E1:F3"
455
- @ws.add_pivot_table 'G5:G6', 'A1:D10'
456
- schema = Nokogiri::XML::Schema(File.open(Axlsx::SML_XSD))
457
- doc = Nokogiri::XML(@ws.to_xml_string)
458
- assert(schema.validate(doc).map { |e| puts e.message; e }.empty?, schema.validate(doc).map { |e| e.message }.join('\n'))
459
- end
460
-
461
- def test_relationships
462
- @ws.add_row [1,2,3]
463
- assert(@ws.relationships.empty?, "No Drawing relationship until you add a chart")
464
- @ws.add_chart Axlsx::Pie3DChart
465
- assert_equal(@ws.relationships.size, 1, "adding a chart creates the relationship")
466
- @ws.add_chart Axlsx::Pie3DChart
467
- assert_equal(@ws.relationships.size, 1, "multiple charts still only result in one relationship")
468
- @ws.add_comment :text => 'builder', :author => 'bob', :ref => @ws.rows.last.cells.last
469
- assert_equal(@ws.relationships.size, 3, "adding a comment adds 2 relationships")
470
- @ws.add_comment :text => 'not that is a comment!', :author => 'travis', :ref => "A1"
471
- assert_equal(@ws.relationships.size, 3, "adding multiple comments in the same worksheet should not add any additional comment relationships")
472
- @ws.add_pivot_table 'G5:G6', 'A1:D10'
473
- assert_equal(@ws.relationships.size, 4, "adding a pivot table adds 1 relationship")
474
- end
475
-
476
- def test_set_fixed_width_column
477
- @ws.add_row ["mule", "donkey", "horse"], :widths => [20, :ignore, nil]
478
- assert(@ws.column_info.size == 3, "a data item for each column")
479
- assert_equal(20, @ws.column_info[0].width, "adding a row with fixed width updates :fixed attribute")
480
- assert_equal(@ws.column_info[1].width, nil, ":ignore does not set any data")
481
- end
482
-
483
- def test_fixed_height
484
- @ws.add_row [1, 2, 3], :height => 40
485
- assert_equal(40, @ws.rows[-1].height)
486
- end
487
-
488
- def test_set_column_width
489
- @ws.add_row ["chasing windmills", "penut"]
490
- @ws.column_widths nil, 0.5
491
- assert_equal(@ws.column_info[1].width, 0.5, 'eat my width')
492
- assert_raise(ArgumentError, 'only accept unsigned ints') { @ws.column_widths 2, 7, -1 }
493
- assert_raise(ArgumentError, 'only accept Integer or Float') { @ws.column_widths 2, 7, "-1" }
494
- end
495
-
496
- def test_protect_range
497
- assert(@ws.send(:protected_ranges).is_a?(Axlsx::SimpleTypedList))
498
- assert_equal(0, @ws.send(:protected_ranges).size)
499
- @ws.protect_range('A1:A3')
500
- assert_equal('A1:A3', @ws.send(:protected_ranges).last.sqref)
501
- end
502
-
503
- def test_protect_range_with_cells
504
- @ws.add_row [1, 2, 3]
505
- assert_nothing_raised {@ws.protect_range(@ws.rows.first.cells) }
506
- assert_equal('A1:C1', @ws.send(:protected_ranges).last.sqref)
507
-
508
- end
509
- def test_merge_cells
510
- @ws.add_row [1,2,3]
511
- @ws.add_row [4,5,6]
512
- @ws.add_row [7,8,9]
513
- @ws.merge_cells "A1:A2"
514
- @ws.merge_cells "B2:C3"
515
- @ws.merge_cells @ws.rows.last.cells[(0..1)]
516
- assert_equal(@ws.send(:merged_cells).size, 3)
517
- assert_equal(@ws.send(:merged_cells).last, "A3:B3")
518
- end
519
-
520
- def test_merge_cells_sorts_correctly_by_row_when_given_array
521
- 10.times do |i|
522
- @ws.add_row [i]
523
- end
524
- @ws.merge_cells [@ws.rows[8].cells.first, @ws.rows[9].cells.first]
525
- assert_equal "A9:A10", @ws.send(:merged_cells).first
526
- end
527
-
528
- def test_auto_filter
529
- assert(@ws.auto_filter.range.nil?)
530
- assert(@wb.defined_names.none?{|defined_name| defined_name.name=='_xlnm._FilterDatabase'})
531
- assert_raise(ArgumentError) { @ws.auto_filter = 123 }
532
- @ws.auto_filter.range = "A1:D9"
533
- assert_equal(@ws.auto_filter.range, "A1:D9")
534
- @ws.to_xml_string
535
- assert(@wb.defined_names.any?{|defined_name| defined_name.name=='_xlnm._FilterDatabase'})
536
- end
537
-
538
- def test_auto_filter_assign
539
- other_ws = @wb.add_worksheet
540
-
541
- assert(@ws.auto_filter.range.nil?)
542
- assert(other_ws.auto_filter.range.nil?)
543
- assert(@wb.defined_names.none?{|defined_name| defined_name.name=='_xlnm._FilterDatabase'})
544
- assert_raise(ArgumentError) { @ws.auto_filter = 123 }
545
-
546
- @ws.auto_filter = "A1:D9"
547
- assert_equal(@ws.auto_filter.range, "A1:D9")
548
-
549
- other_ws.auto_filter = "A1:D2"
550
- assert_equal(other_ws.auto_filter.range, "A1:D2")
551
-
552
- @ws.to_xml_string
553
- other_ws.to_xml_string
554
-
555
- filter_database = @wb.defined_names.select{|defined_name| defined_name.name=='_xlnm._FilterDatabase'}
556
- assert_equal(2, filter_database.size)
557
- assert_equal(@ws.index, filter_database[0].local_sheet_id)
558
- assert_equal(other_ws.index, filter_database[1].local_sheet_id)
559
- end
560
-
561
-
562
- def test_sheet_pr_for_auto_filter
563
- @ws.auto_filter.range = 'A1:D9'
564
- @ws.auto_filter.add_column 0, :filters, :filter_items => [1]
565
- doc = Nokogiri::XML(@ws.to_xml_string)
566
- assert(doc.xpath('//sheetPr[@filterMode=1]'))
567
- end
568
-
569
- def test_outline_level_rows
570
- 3.times { @ws.add_row [1,2,3] }
571
- @ws.outline_level_rows 0, 2
572
- assert_equal(1, @ws.rows[0].outline_level)
573
- assert_equal(true, @ws.rows[2].hidden)
574
- assert_equal(true, @ws.sheet_view.show_outline_symbols)
575
- end
576
-
577
- def test_outline_level_columns
578
- 3.times { @ws.add_row [1,2,3] }
579
- @ws.outline_level_columns 0, 2
580
- assert_equal(1, @ws.column_info[0].outline_level)
581
- assert_equal(true, @ws.column_info[2].hidden)
582
- assert_equal(true, @ws.sheet_view.show_outline_symbols)
583
- end
584
-
585
- def test_worksheet_does_not_get_added_to_workbook_on_initialize_failure
586
- assert_equal(1, @wb.worksheets.size)
587
- assert_raise(ArgumentError) { @wb.add_worksheet(:name => 'Sheet1') }
588
- assert_equal(1, @wb.worksheets.size)
589
- end
590
-
591
- def test_worksheet_only_includes_outline_pr_when_set
592
- doc = Nokogiri::XML(@ws.to_xml_string)
593
- assert_equal(doc.xpath('//xmlns:worksheet/xmlns:sheetPr/xmlns:outlinePr').size, 0)
594
-
595
- @ws.sheet_pr.outline_pr.summary_below = false
596
- @ws.sheet_pr.outline_pr.summary_right = true
597
- doc = Nokogiri::XML(@ws.to_xml_string)
598
- assert_equal(doc.xpath('//xmlns:worksheet/xmlns:sheetPr/xmlns:outlinePr').size, 1)
599
- assert_equal(doc.xpath('//xmlns:worksheet/xmlns:sheetPr/xmlns:outlinePr[@summaryBelow=0][@summaryRight=1]').size, 1)
600
- end
601
- end
1
+ require 'tc_helper.rb'
2
+
3
+ class TestWorksheet < Test::Unit::TestCase
4
+ def setup
5
+ @p = Axlsx::Package.new
6
+ @wb = @p.workbook
7
+ @ws = @wb.add_worksheet
8
+ end
9
+
10
+
11
+ def test_pn
12
+ assert_equal(@ws.pn, "worksheets/sheet1.xml")
13
+ ws = @ws.workbook.add_worksheet
14
+ assert_equal(ws.pn, "worksheets/sheet2.xml")
15
+ end
16
+
17
+ def test_name_is_html_encoded
18
+ @ws.name = '<foo> & <bar>'
19
+ assert_equal(@ws.name, '&lt;foo&gt; &amp; &lt;bar&gt;')
20
+ end
21
+
22
+ def test_name_exception_on_invalid_character
23
+ assert_raises(ArgumentError) { @ws.name = 'foo:bar' }
24
+ assert_raises(ArgumentError) { @ws.name = 'foo[bar' }
25
+ assert_raises(ArgumentError) { @ws.name = 'foo]bar' }
26
+ assert_raises(ArgumentError) { @ws.name = 'foo*bar' }
27
+ assert_raises(ArgumentError) { @ws.name = 'foo/bar' }
28
+ assert_raises(ArgumentError) { @ws.name = 'foo\bar' }
29
+ assert_raises(ArgumentError) { @ws.name = 'foo?bar' }
30
+ end
31
+
32
+ def test_name_unique
33
+ assert_raise(ArgumentError, "worksheet name must be unique") { n = @ws.name; @ws.workbook.add_worksheet(:name=> n) }
34
+ end
35
+
36
+ def test_name_unique_only_checks_other_worksheet_names
37
+ assert_nothing_raised { @ws.name = @ws.name }
38
+ assert_nothing_raised { Axlsx::Package.new.workbook.add_worksheet :name => 'Sheet1' }
39
+ end
40
+
41
+ def test_exception_if_name_too_long
42
+ assert_nothing_raised { @ws.name = 'x' * 31 }
43
+ assert_raises(ArgumentError) { @ws.name = 'x' * 32 }
44
+ end
45
+
46
+ def test_exception_if_name_too_long_because_of_multibyte_characters
47
+ four_characters_for_excel = "\u{1F1EB 1F1F7}" # french flag emoji
48
+ assert_raises(ArgumentError, "name too long!") do
49
+ @ws.name = four_characters_for_excel + "x" * 28
50
+ end
51
+ assert_nothing_raised { @ws.name = "#{four_characters_for_excel}123456789012345678901234567" }
52
+ assert_nothing_raised { @ws.name = "123456789012345678901234567890…" }
53
+ assert_nothing_raised { @ws.name = "123456789012345678901234567890✔" }
54
+ end
55
+
56
+ def test_page_margins
57
+ assert(@ws.page_margins.is_a? Axlsx::PageMargins)
58
+ end
59
+
60
+ def test_page_margins_yeild
61
+ @ws.page_margins do |pm|
62
+ assert(pm.is_a? Axlsx::PageMargins)
63
+ assert(@ws.page_margins == pm)
64
+ end
65
+ end
66
+
67
+ def test_page_setup
68
+ assert(@ws.page_setup.is_a? Axlsx::PageSetup)
69
+ end
70
+
71
+ def test_page_setup_yield
72
+ @ws.page_setup do |ps|
73
+ assert(ps.is_a? Axlsx::PageSetup)
74
+ assert(@ws.page_setup == ps)
75
+ end
76
+ end
77
+
78
+ def test_print_options
79
+ assert(@ws.print_options.is_a? Axlsx::PrintOptions)
80
+ end
81
+
82
+ def test_print_options_yield
83
+ @ws.print_options do |po|
84
+ assert(po.is_a? Axlsx::PrintOptions)
85
+ assert(@ws.print_options == po)
86
+ end
87
+ end
88
+
89
+ def test_header_footer
90
+ assert(@ws.header_footer.is_a? Axlsx::HeaderFooter)
91
+ end
92
+
93
+ def test_header_footer_yield
94
+ @ws.header_footer do |hf|
95
+ assert(hf.is_a? Axlsx::HeaderFooter)
96
+ assert(@ws.header_footer == hf)
97
+ end
98
+ end
99
+
100
+ def test_state
101
+ assert_equal(:visible, @ws.state)
102
+ end
103
+
104
+ def test_state_validation
105
+ assert_raise(ArgumentError) { @ws.state = :dead }
106
+ assert_nothing_raised { @ws.state = :very_hidden }
107
+ end
108
+
109
+ def test_no_autowidth
110
+ @ws.workbook.use_autowidth = false
111
+ @ws.add_row [1,2,3,4]
112
+ assert_equal(@ws.column_info[0].width, nil)
113
+ end
114
+
115
+ def test_initialization_options
116
+ page_margins = {:left => 2, :right => 2, :bottom => 2, :top => 2, :header => 2, :footer => 2}
117
+ page_setup = {:fit_to_height => 1, :fit_to_width => 1, :orientation => :landscape, :paper_width => "210mm", :paper_height => "297mm", :scale => 80}
118
+ print_options = {:grid_lines => true, :headings => true, :horizontal_centered => true, :vertical_centered => true}
119
+ header_footer = {:different_first => false, :different_odd_even => false, :odd_header => 'Header'}
120
+ optioned = @ws.workbook.add_worksheet(:name => 'bob', :page_margins => page_margins, :page_setup => page_setup, :print_options => print_options, :header_footer => header_footer)
121
+ page_margins.keys.each do |key|
122
+ assert_equal(page_margins[key], optioned.page_margins.send(key))
123
+ end
124
+ page_setup.keys.each do |key|
125
+ assert_equal(page_setup[key], optioned.page_setup.send(key))
126
+ end
127
+ print_options.keys.each do |key|
128
+ assert_equal(print_options[key], optioned.print_options.send(key))
129
+ end
130
+ header_footer.keys.each do |key|
131
+ assert_equal(header_footer[key], optioned.header_footer.send(key))
132
+ end
133
+ assert_equal(optioned.name, 'bob')
134
+
135
+ end
136
+
137
+
138
+ # def test_use_gridlines
139
+ # assert_raise(ArgumentError) { @ws.show_gridlines = -1.1 }
140
+ # assert_nothing_raised { @ws.show_gridlines = false }
141
+ # assert_equal(@ws.show_gridlines, false)
142
+ # end
143
+
144
+ # def test_selected
145
+ # assert_raise(ArgumentError) { @ws.selected = -1.1 }
146
+ # assert_nothing_raised { @ws.selected = true }
147
+ # assert_equal(@ws.selected, true)
148
+ # end
149
+
150
+ def test_rels_pn
151
+ assert_equal(@ws.rels_pn, "worksheets/_rels/sheet1.xml.rels")
152
+ ws = @ws.workbook.add_worksheet
153
+ assert_equal(ws.rels_pn, "worksheets/_rels/sheet2.xml.rels")
154
+ end
155
+
156
+ def test_rId
157
+ assert_equal @ws.workbook.relationships.for(@ws).Id, @ws.rId
158
+ end
159
+
160
+ def test_index
161
+ assert_equal(@ws.index, @ws.workbook.worksheets.index(@ws))
162
+ end
163
+
164
+ def test_dimension
165
+ @ws.add_row [1, 2, 3]
166
+ @ws.add_row [4, 5, 6]
167
+ assert_equal @ws.dimension.sqref, "A1:C2"
168
+ end
169
+
170
+ def test_dimension_with_empty_row
171
+ @ws.add_row
172
+ assert_equal "A1:AA200", @ws.dimension.sqref
173
+ end
174
+
175
+ def test_referencing
176
+ @ws.add_row [1, 2, 3]
177
+ @ws.add_row [4, 5, 6]
178
+ range = @ws["A1:C2"]
179
+ first_row = @ws[0]
180
+ last_row = @ws[1]
181
+ assert_equal(@ws.rows[0],first_row)
182
+ assert_equal(@ws.rows[1],last_row)
183
+ assert_equal(range.size, 6)
184
+ assert_equal(range.first, @ws.rows.first.cells.first)
185
+ assert_equal(range.last, @ws.rows.last.cells.last)
186
+ end
187
+
188
+ def test_add_row
189
+ assert(@ws.rows.empty?, "sheet has no rows by default")
190
+ r = @ws.add_row([1,2,3])
191
+ assert_equal(@ws.rows.size, 1, "add_row adds a row")
192
+ assert_equal(@ws.rows.first, r, "the row returned is the row added")
193
+ end
194
+
195
+ def test_add_chart
196
+ assert(@ws.workbook.charts.empty?, "the sheet's workbook should not have any charts by default")
197
+ @ws.add_chart Axlsx::Pie3DChart
198
+ assert_equal(@ws.workbook.charts.size, 1, "add_chart adds a chart to the workbook")
199
+ end
200
+
201
+ def test_add_page_break_with_string_cell_ref
202
+ assert(@ws.row_breaks.empty?)
203
+ assert(@ws.col_breaks.empty?)
204
+ @ws.add_page_break("B1")
205
+ assert_equal(1, @ws.row_breaks.size)
206
+ assert_equal(1, @ws.col_breaks.size)
207
+ end
208
+
209
+ def test_add_page_break_with_cell
210
+ @ws.add_row [1, 2, 3, 4]
211
+ @ws.add_row [1, 2, 3, 4]
212
+
213
+
214
+ assert(@ws.row_breaks.empty?)
215
+ assert(@ws.col_breaks.empty?)
216
+ @ws.add_page_break(@ws.rows.last.cells[1])
217
+ assert_equal(1, @ws.row_breaks.size)
218
+ assert_equal(1, @ws.col_breaks.size)
219
+ end
220
+
221
+
222
+ def test_drawing
223
+ assert @ws.drawing == nil
224
+ @ws.add_chart(Axlsx::Pie3DChart)
225
+ assert @ws.drawing.is_a?(Axlsx::Drawing)
226
+ end
227
+
228
+ def test_add_pivot_table
229
+ assert(@ws.workbook.pivot_tables.empty?, "the sheet's workbook should not have any pivot tables by default")
230
+ @ws.add_pivot_table 'G5:G6', 'A1:D:10'
231
+ assert_equal(@ws.workbook.pivot_tables.size, 1, "add_pivot_tables adds a pivot_table to the workbook")
232
+ end
233
+
234
+ def test_col_style
235
+ @ws.add_row [1,2,3,4]
236
+ @ws.add_row [1,2,3,4]
237
+ @ws.add_row [1,2,3,4]
238
+ @ws.add_row [1,2,3,4]
239
+ @ws.col_style( (1..2), 1, :row_offset=>1)
240
+ @ws.rows[(1..-1)].each do | r |
241
+ assert_equal(r.cells[1].style, 1)
242
+ assert_equal(r.cells[2].style, 1)
243
+ end
244
+ assert_equal(@ws.rows.first.cells[1].style, 0)
245
+ assert_equal(@ws.rows.first.cells[0].style, 0)
246
+ end
247
+
248
+ def test_col_style_with_empty_column
249
+ @ws.add_row [1,2,3,4]
250
+ @ws.add_row [1]
251
+ @ws.add_row [1,2,3,4]
252
+ assert_nothing_raised {@ws.col_style(1, 1)}
253
+ end
254
+
255
+ def test_cols
256
+ @ws.add_row [1,2,3,4]
257
+ @ws.add_row [1,2,3,4]
258
+ @ws.add_row [1,2,3]
259
+ @ws.add_row [1,2,3,4]
260
+ c = @ws.cols[1]
261
+ assert_equal(c.size, 4)
262
+ assert_equal(c[0].value, 2)
263
+ end
264
+
265
+ def test_cols_with_block
266
+ @ws.add_row [1,2,3]
267
+ @ws.add_row [1]
268
+ cols = @ws.cols {|row, column| :foo }
269
+ assert_equal(:foo, cols[1][1])
270
+ end
271
+
272
+ def test_row_style
273
+ @ws.add_row [1,2,3,4]
274
+ @ws.add_row [1,2,3,4]
275
+ @ws.add_row [1,2,3,4]
276
+ @ws.add_row [1,2,3,4]
277
+ @ws.row_style 1, 1, :col_offset=>1
278
+ @ws.rows[1].cells[(1..-1)].each do | c |
279
+ assert_equal(c.style, 1)
280
+ end
281
+ assert_equal(@ws.rows[1].cells[0].style, 0)
282
+ assert_equal(@ws.rows[2].cells[1].style, 0)
283
+ @ws.row_style( 1..2, 1, :col_offset => 2)
284
+ @ws.rows[(1..2)].each do |r|
285
+ r.cells[(2..-1)].each do |c|
286
+ assert_equal(c.style, 1)
287
+ end
288
+ end
289
+ end
290
+
291
+ # def test_to_xml_string_fit_to_page
292
+ # @ws.page_setup.fit_to_width = 1
293
+ # doc = Nokogiri::XML(@ws.to_xml_string)
294
+ # assert_equal(doc.xpath('//xmlns:worksheet/xmlns:sheetPr/xmlns:pageSetUpPr[@fitToPage=1]').size, 1)
295
+ # end
296
+
297
+ def test_to_xml_string_dimensions
298
+ @ws.add_row [1,2,3]
299
+ doc = Nokogiri::XML(@ws.to_xml_string)
300
+ assert_equal(doc.xpath('//xmlns:worksheet/xmlns:dimension[@ref="A1:C1"]').size, 1)
301
+ end
302
+
303
+ # def test_fit_to_page_assignation_does_nothing
304
+ # @ws.fit_to_page = true
305
+ # assert_equal(@ws.fit_to_page?, false)
306
+ # end
307
+
308
+ # def test_to_xml_string_selected
309
+ # @ws.selected = true
310
+ # doc = Nokogiri::XML(@ws.to_xml_string)
311
+ # assert_equal(doc.xpath('//xmlns:worksheet/xmlns:sheetViews/xmlns:sheetView[@tabSelected=1]').size, 1)
312
+ # end
313
+
314
+ # def test_to_xml_string_show_gridlines
315
+ # @ws.show_gridlines = false
316
+ # doc = Nokogiri::XML(@ws.to_xml_string)
317
+ # assert_equal(doc.xpath('//xmlns:worksheet/xmlns:sheetViews/xmlns:sheetView[@showGridLines=0]').size, 1)
318
+ # end
319
+
320
+ def test_to_xml_string_auto_fit_data
321
+ @ws.add_row [1, "two"]
322
+ doc = Nokogiri::XML(@ws.to_xml_string)
323
+ assert_equal(doc.xpath('//xmlns:worksheet/xmlns:cols/xmlns:col').size, 2)
324
+ end
325
+
326
+ def test_to_xml_string_sheet_data
327
+ @ws.add_row [1, "two"]
328
+ doc = Nokogiri::XML(@ws.to_xml_string)
329
+ assert_equal(doc.xpath('//xmlns:worksheet/xmlns:sheetData/xmlns:row').size, 1)
330
+ end
331
+
332
+ def test_to_xml_string_auto_filter
333
+ @ws.add_row [1, "two"]
334
+ @ws.auto_filter.range = "A1:B1"
335
+ doc = Nokogiri::XML(@ws.to_xml_string)
336
+ assert_equal(doc.xpath('//xmlns:worksheet/xmlns:autoFilter[@ref="A1:B1"]').size, 1)
337
+ end
338
+
339
+ def test_to_xml_string_merge_cells
340
+ @ws.add_row [1, "two"]
341
+ @ws.merge_cells "A1:D1"
342
+ @ws.merge_cells "E1:F1"
343
+ doc = Nokogiri::XML(@ws.to_xml_string)
344
+ assert_equal(doc.xpath('//xmlns:worksheet/xmlns:mergeCells/xmlns:mergeCell[@ref="A1:D1"]').size, 1)
345
+ assert_equal(doc.xpath('//xmlns:worksheet/xmlns:mergeCells/xmlns:mergeCell[@ref="E1:F1"]').size, 1)
346
+ end
347
+
348
+ def test_to_xml_string_merge_cells_row
349
+ row = @ws.add_row [1, "two"]
350
+ @ws.merge_cells row
351
+ doc = Nokogiri::XML(@ws.to_xml_string)
352
+ assert_equal(doc.xpath('//xmlns:worksheet/xmlns:mergeCells/xmlns:mergeCell[@ref="A1:B1"]').size, 1)
353
+ end
354
+
355
+ def test_to_xml_string_row_breaks
356
+ @ws.add_page_break("A1")
357
+ doc = Nokogiri::XML(@ws.to_xml_string)
358
+ assert_equal(doc.xpath('//xmlns:worksheet/xmlns:rowBreaks/xmlns:brk[@id="0"]').size, 1)
359
+ end
360
+
361
+ def test_to_xml_string_sheet_protection
362
+ @ws.sheet_protection.password = 'fish'
363
+ doc = Nokogiri::XML(@ws.to_xml_string)
364
+ assert(doc.xpath('//xmlns:sheetProtection'))
365
+ end
366
+
367
+ def test_to_xml_string_page_margins
368
+ @ws.page_margins do |pm|
369
+ pm.left = 9
370
+ pm.right = 7
371
+ end
372
+ doc = Nokogiri::XML(@ws.to_xml_string)
373
+ assert_equal(doc.xpath('//xmlns:worksheet/xmlns:pageMargins[@left="9"][@right="7"]').size, 1)
374
+ end
375
+
376
+ def test_to_xml_string_page_setup
377
+ @ws.page_setup do |ps|
378
+ ps.paper_width = "210mm"
379
+ ps.scale = 80
380
+ end
381
+ doc = Nokogiri::XML(@ws.to_xml_string)
382
+ assert_equal(doc.xpath('//xmlns:worksheet/xmlns:pageSetup[@paperWidth="210mm"][@scale="80"]').size, 1)
383
+ end
384
+
385
+ def test_to_xml_string_print_options
386
+ @ws.print_options do |po|
387
+ po.grid_lines = true
388
+ po.horizontal_centered = true
389
+ end
390
+ doc = Nokogiri::XML(@ws.to_xml_string)
391
+ assert_equal(doc.xpath('//xmlns:worksheet/xmlns:printOptions[@gridLines=1][@horizontalCentered=1]').size, 1)
392
+ end
393
+
394
+ def test_to_xml_string_header_footer
395
+ @ws.header_footer do |hf|
396
+ hf.different_first = false
397
+ hf.different_odd_even = false
398
+ hf.odd_header = 'Test Header'
399
+ end
400
+ doc = Nokogiri::XML(@ws.to_xml_string)
401
+ assert_equal(doc.xpath('//xmlns:worksheet/xmlns:headerFooter[@differentFirst=0][@differentOddEven=0]').size, 1)
402
+ end
403
+
404
+ def test_to_xml_string_drawing
405
+ @ws.add_chart Axlsx::Pie3DChart
406
+ doc = Nokogiri::XML(@ws.to_xml_string)
407
+ assert_equal @ws.send(:worksheet_drawing).relationship.Id, doc.xpath('//xmlns:worksheet/xmlns:drawing').first["r:id"]
408
+ end
409
+
410
+ def test_to_xml_string_tables
411
+ @ws.add_row ["one", "two"]
412
+ @ws.add_row [1, 2]
413
+ table = @ws.add_table "A1:B2"
414
+ doc = Nokogiri::XML(@ws.to_xml_string)
415
+ assert_equal(doc.xpath('//xmlns:worksheet/xmlns:tableParts[@count="1"]').size, 1)
416
+ assert_equal table.rId, doc.xpath('//xmlns:worksheet/xmlns:tableParts/xmlns:tablePart').first["r:id"]
417
+ end
418
+
419
+ def test_to_xml_string
420
+ schema = Nokogiri::XML::Schema(File.open(Axlsx::SML_XSD))
421
+ doc = Nokogiri::XML(@ws.to_xml_string)
422
+ assert(schema.validate(doc).map{ |e| puts e.message; e }.empty?, "error free validation")
423
+ end
424
+
425
+ def test_styles
426
+ assert(@ws.styles.is_a?(Axlsx::Styles), 'worksheet provides access to styles')
427
+ end
428
+
429
+ def test_to_xml_string_with_illegal_chars
430
+ old = Axlsx::trust_input
431
+ Axlsx::trust_input = false
432
+ nasties = "\v\u2028\u0001\u0002\u0003\u0004\u0005\u0006\u0007\u0008\u001f"
433
+ @ws.add_row [nasties]
434
+ assert_equal(0, @ws.rows.last.cells.last.value.index("\v"))
435
+ assert_equal(nil, @ws.to_xml_string.index("\v"))
436
+ Axlsx::trust_input = old
437
+ end
438
+
439
+ def test_to_xml_string_with_newlines
440
+ cell_with_newline = "foo\n\r\nbar"
441
+ @ws.add_row [cell_with_newline]
442
+ assert_equal("foo\n\r\nbar", @ws.rows.last.cells.last.value)
443
+ assert_not_nil(@ws.to_xml_string.index("foo\n\r\nbar"))
444
+ end
445
+ # Make sure the XML for all optional elements (like pageMargins, autoFilter, ...)
446
+ # is generated in correct order.
447
+ def test_valid_with_optional_elements
448
+ @ws.page_margins.set :left => 9
449
+ @ws.page_setup.set :fit_to_width => 1
450
+ @ws.print_options.set :headings => true
451
+ @ws.auto_filter.range = "A1:C3"
452
+ @ws.merge_cells "A4:A5"
453
+ @ws.add_chart Axlsx::Pie3DChart
454
+ @ws.add_table "E1:F3"
455
+ @ws.add_pivot_table 'G5:G6', 'A1:D10'
456
+ schema = Nokogiri::XML::Schema(File.open(Axlsx::SML_XSD))
457
+ doc = Nokogiri::XML(@ws.to_xml_string)
458
+ assert(schema.validate(doc).map { |e| puts e.message; e }.empty?, schema.validate(doc).map { |e| e.message }.join('\n'))
459
+ end
460
+
461
+ def test_relationships
462
+ @ws.add_row [1,2,3]
463
+ assert(@ws.relationships.empty?, "No Drawing relationship until you add a chart")
464
+ @ws.add_chart Axlsx::Pie3DChart
465
+ assert_equal(@ws.relationships.size, 1, "adding a chart creates the relationship")
466
+ @ws.add_chart Axlsx::Pie3DChart
467
+ assert_equal(@ws.relationships.size, 1, "multiple charts still only result in one relationship")
468
+ @ws.add_comment :text => 'builder', :author => 'bob', :ref => @ws.rows.last.cells.last
469
+ assert_equal(@ws.relationships.size, 3, "adding a comment adds 2 relationships")
470
+ @ws.add_comment :text => 'not that is a comment!', :author => 'travis', :ref => "A1"
471
+ assert_equal(@ws.relationships.size, 3, "adding multiple comments in the same worksheet should not add any additional comment relationships")
472
+ @ws.add_pivot_table 'G5:G6', 'A1:D10'
473
+ assert_equal(@ws.relationships.size, 4, "adding a pivot table adds 1 relationship")
474
+ end
475
+
476
+ def test_set_fixed_width_column
477
+ @ws.add_row ["mule", "donkey", "horse"], :widths => [20, :ignore, nil]
478
+ assert(@ws.column_info.size == 3, "a data item for each column")
479
+ assert_equal(20, @ws.column_info[0].width, "adding a row with fixed width updates :fixed attribute")
480
+ assert_equal(@ws.column_info[1].width, nil, ":ignore does not set any data")
481
+ end
482
+
483
+ def test_fixed_height
484
+ @ws.add_row [1, 2, 3], :height => 40
485
+ assert_equal(40, @ws.rows[-1].height)
486
+ end
487
+
488
+ def test_set_column_width
489
+ @ws.add_row ["chasing windmills", "penut"]
490
+ @ws.column_widths nil, 0.5
491
+ assert_equal(@ws.column_info[1].width, 0.5, 'eat my width')
492
+ assert_raise(ArgumentError, 'only accept unsigned ints') { @ws.column_widths 2, 7, -1 }
493
+ assert_raise(ArgumentError, 'only accept Integer or Float') { @ws.column_widths 2, 7, "-1" }
494
+ end
495
+
496
+ def test_ranges
497
+ @ws.add_row(["foo", "bar"])
498
+ @ws.add_row(["a", "b", "c", "d", "e"])
499
+
500
+ valid_range = "A1:B2"
501
+ @ws[valid_range]
502
+
503
+ valid_range = "A1:E2" ### Missing middle cells (C1 - E1), still allowed
504
+ @ws[valid_range]
505
+
506
+ assert_raise ArgumentError, "Invalid cell definition" do
507
+ invalid_range_format = "A1:B2:C3"
508
+ @ws[invalid_range_format]
509
+ end
510
+
511
+ assert_raise ArgumentError, "Missing cell `Z1` for the specified range." do
512
+ invalid_row_range = "A1:Z1"
513
+ @ws[invalid_row_range]
514
+ end
515
+
516
+ assert_raise ArgumentError, "Missing cell `D1` for the specified range." do
517
+ invalid_cell_range = "D1:E2" ### Missing start cell, not allowed
518
+ @ws[invalid_cell_range]
519
+ end
520
+
521
+ assert_raise ArgumentError, "Missing cell `A99` for the specified range." do
522
+ invalid_cell_range = "A1:A99" ### Missing end cell, not allowed
523
+ @ws[invalid_cell_range]
524
+ end
525
+ end
526
+
527
+ def test_protect_range
528
+ assert(@ws.send(:protected_ranges).is_a?(Axlsx::SimpleTypedList))
529
+ assert_equal(0, @ws.send(:protected_ranges).size)
530
+ @ws.protect_range('A1:A3')
531
+ assert_equal('A1:A3', @ws.send(:protected_ranges).last.sqref)
532
+ end
533
+
534
+ def test_protect_range_with_cells
535
+ @ws.add_row [1, 2, 3]
536
+ assert_nothing_raised {@ws.protect_range(@ws.rows.first.cells) }
537
+ assert_equal('A1:C1', @ws.send(:protected_ranges).last.sqref)
538
+ end
539
+
540
+ def test_merge_cells
541
+ @ws.add_row [1,2,3]
542
+ @ws.add_row [4,5,6]
543
+ @ws.add_row [7,8,9]
544
+ @ws.merge_cells "A1:A2"
545
+ @ws.merge_cells "B2:C3"
546
+ @ws.merge_cells @ws.rows.last.cells[(0..1)]
547
+ assert_equal(@ws.send(:merged_cells).size, 3)
548
+ assert_equal(@ws.send(:merged_cells).last, "A3:B3")
549
+ end
550
+
551
+ def test_merge_cells_sorts_correctly_by_row_when_given_array
552
+ 10.times do |i|
553
+ @ws.add_row [i]
554
+ end
555
+ @ws.merge_cells [@ws.rows[8].cells.first, @ws.rows[9].cells.first]
556
+ assert_equal "A9:A10", @ws.send(:merged_cells).first
557
+ end
558
+
559
+ def test_auto_filter
560
+ assert(@ws.auto_filter.range.nil?)
561
+ assert(@wb.defined_names.none?{|defined_name| defined_name.name=='_xlnm._FilterDatabase'})
562
+ assert_raise(ArgumentError) { @ws.auto_filter = 123 }
563
+ @ws.auto_filter.range = "A1:D9"
564
+ assert_equal(@ws.auto_filter.range, "A1:D9")
565
+ @ws.to_xml_string
566
+ assert(@wb.defined_names.any?{|defined_name| defined_name.name=='_xlnm._FilterDatabase'})
567
+ end
568
+
569
+ def test_auto_filter_assign
570
+ other_ws = @wb.add_worksheet
571
+
572
+ assert(@ws.auto_filter.range.nil?)
573
+ assert(other_ws.auto_filter.range.nil?)
574
+ assert(@wb.defined_names.none?{|defined_name| defined_name.name=='_xlnm._FilterDatabase'})
575
+ assert_raise(ArgumentError) { @ws.auto_filter = 123 }
576
+
577
+ @ws.auto_filter = "A1:D9"
578
+ assert_equal(@ws.auto_filter.range, "A1:D9")
579
+
580
+ other_ws.auto_filter = "A1:D2"
581
+ assert_equal(other_ws.auto_filter.range, "A1:D2")
582
+
583
+ @ws.to_xml_string
584
+ other_ws.to_xml_string
585
+
586
+ filter_database = @wb.defined_names.select{|defined_name| defined_name.name=='_xlnm._FilterDatabase'}
587
+ assert_equal(2, filter_database.size)
588
+ assert_equal(@ws.index, filter_database[0].local_sheet_id)
589
+ assert_equal(other_ws.index, filter_database[1].local_sheet_id)
590
+ end
591
+
592
+
593
+ def test_sheet_pr_for_auto_filter
594
+ @ws.auto_filter.range = 'A1:D9'
595
+ @ws.auto_filter.add_column 0, :filters, :filter_items => [1]
596
+ doc = Nokogiri::XML(@ws.to_xml_string)
597
+ assert(doc.xpath('//sheetPr[@filterMode=1]'))
598
+ end
599
+
600
+ def test_outline_level_rows
601
+ 3.times { @ws.add_row [1,2,3] }
602
+ @ws.outline_level_rows 0, 2
603
+ assert_equal(1, @ws.rows[0].outline_level)
604
+ assert_equal(true, @ws.rows[2].hidden)
605
+ assert_equal(true, @ws.sheet_view.show_outline_symbols)
606
+ end
607
+
608
+ def test_outline_level_columns
609
+ 3.times { @ws.add_row [1,2,3] }
610
+ @ws.outline_level_columns 0, 2
611
+ assert_equal(1, @ws.column_info[0].outline_level)
612
+ assert_equal(true, @ws.column_info[2].hidden)
613
+ assert_equal(true, @ws.sheet_view.show_outline_symbols)
614
+ end
615
+
616
+ def test_worksheet_does_not_get_added_to_workbook_on_initialize_failure
617
+ assert_equal(1, @wb.worksheets.size)
618
+ assert_raise(ArgumentError) { @wb.add_worksheet(:name => 'Sheet1') }
619
+ assert_equal(1, @wb.worksheets.size)
620
+ end
621
+
622
+ def test_worksheet_only_includes_outline_pr_when_set
623
+ doc = Nokogiri::XML(@ws.to_xml_string)
624
+ assert_equal(doc.xpath('//xmlns:worksheet/xmlns:sheetPr/xmlns:outlinePr').size, 0)
625
+
626
+ @ws.sheet_pr.outline_pr.summary_below = false
627
+ @ws.sheet_pr.outline_pr.summary_right = true
628
+ doc = Nokogiri::XML(@ws.to_xml_string)
629
+ assert_equal(doc.xpath('//xmlns:worksheet/xmlns:sheetPr/xmlns:outlinePr').size, 1)
630
+ assert_equal(doc.xpath('//xmlns:worksheet/xmlns:sheetPr/xmlns:outlinePr[@summaryBelow=0][@summaryRight=1]').size, 1)
631
+ end
632
+ end