bonio-axlsx 2.2.1

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