axlsx-alt 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (330) hide show
  1. checksums.yaml +7 -0
  2. data/.yardopts +9 -0
  3. data/.yardopts_guide +19 -0
  4. data/CHANGELOG.md +244 -0
  5. data/LICENSE +22 -0
  6. data/README.md +180 -0
  7. data/Rakefile +29 -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 +67 -0
  20. data/examples/example.rb +885 -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 +171 -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/area_chart.rb +99 -0
  43. data/lib/axlsx/drawing/area_series.rb +110 -0
  44. data/lib/axlsx/drawing/ax_data_source.rb +26 -0
  45. data/lib/axlsx/drawing/axes.rb +61 -0
  46. data/lib/axlsx/drawing/axis.rb +190 -0
  47. data/lib/axlsx/drawing/bar_3D_chart.rb +151 -0
  48. data/lib/axlsx/drawing/bar_chart.rb +143 -0
  49. data/lib/axlsx/drawing/bar_series.rb +82 -0
  50. data/lib/axlsx/drawing/bubble_chart.rb +59 -0
  51. data/lib/axlsx/drawing/bubble_series.rb +63 -0
  52. data/lib/axlsx/drawing/cat_axis.rb +85 -0
  53. data/lib/axlsx/drawing/chart.rb +276 -0
  54. data/lib/axlsx/drawing/d_lbls.rb +90 -0
  55. data/lib/axlsx/drawing/drawing.rb +167 -0
  56. data/lib/axlsx/drawing/graphic_frame.rb +54 -0
  57. data/lib/axlsx/drawing/hyperlink.rb +100 -0
  58. data/lib/axlsx/drawing/line_3D_chart.rb +68 -0
  59. data/lib/axlsx/drawing/line_chart.rb +99 -0
  60. data/lib/axlsx/drawing/line_series.rb +110 -0
  61. data/lib/axlsx/drawing/marker.rb +84 -0
  62. data/lib/axlsx/drawing/num_data.rb +52 -0
  63. data/lib/axlsx/drawing/num_data_source.rb +62 -0
  64. data/lib/axlsx/drawing/num_val.rb +34 -0
  65. data/lib/axlsx/drawing/one_cell_anchor.rb +99 -0
  66. data/lib/axlsx/drawing/pic.rb +211 -0
  67. data/lib/axlsx/drawing/picture_locking.rb +42 -0
  68. data/lib/axlsx/drawing/pie_3D_chart.rb +47 -0
  69. data/lib/axlsx/drawing/pie_series.rb +74 -0
  70. data/lib/axlsx/drawing/scaling.rb +60 -0
  71. data/lib/axlsx/drawing/scatter_chart.rb +74 -0
  72. data/lib/axlsx/drawing/scatter_series.rb +98 -0
  73. data/lib/axlsx/drawing/ser_axis.rb +45 -0
  74. data/lib/axlsx/drawing/series.rb +69 -0
  75. data/lib/axlsx/drawing/series_title.rb +23 -0
  76. data/lib/axlsx/drawing/str_data.rb +42 -0
  77. data/lib/axlsx/drawing/str_val.rb +34 -0
  78. data/lib/axlsx/drawing/title.rb +96 -0
  79. data/lib/axlsx/drawing/two_cell_anchor.rb +97 -0
  80. data/lib/axlsx/drawing/val_axis.rb +37 -0
  81. data/lib/axlsx/drawing/view_3D.rb +115 -0
  82. data/lib/axlsx/drawing/vml_drawing.rb +42 -0
  83. data/lib/axlsx/drawing/vml_shape.rb +66 -0
  84. data/lib/axlsx/package.rb +358 -0
  85. data/lib/axlsx/rels/relationship.rb +137 -0
  86. data/lib/axlsx/rels/relationships.rb +32 -0
  87. data/lib/axlsx/stylesheet/border.rb +71 -0
  88. data/lib/axlsx/stylesheet/border_pr.rb +71 -0
  89. data/lib/axlsx/stylesheet/cell_alignment.rb +132 -0
  90. data/lib/axlsx/stylesheet/cell_protection.rb +41 -0
  91. data/lib/axlsx/stylesheet/cell_style.rb +72 -0
  92. data/lib/axlsx/stylesheet/color.rb +76 -0
  93. data/lib/axlsx/stylesheet/dxf.rb +79 -0
  94. data/lib/axlsx/stylesheet/fill.rb +35 -0
  95. data/lib/axlsx/stylesheet/font.rb +148 -0
  96. data/lib/axlsx/stylesheet/gradient_fill.rb +103 -0
  97. data/lib/axlsx/stylesheet/gradient_stop.rb +37 -0
  98. data/lib/axlsx/stylesheet/num_fmt.rb +86 -0
  99. data/lib/axlsx/stylesheet/pattern_fill.rb +73 -0
  100. data/lib/axlsx/stylesheet/styles.rb +420 -0
  101. data/lib/axlsx/stylesheet/table_style.rb +54 -0
  102. data/lib/axlsx/stylesheet/table_style_element.rb +77 -0
  103. data/lib/axlsx/stylesheet/table_styles.rb +46 -0
  104. data/lib/axlsx/stylesheet/xf.rb +147 -0
  105. data/lib/axlsx/util/accessors.rb +64 -0
  106. data/lib/axlsx/util/constants.rb +400 -0
  107. data/lib/axlsx/util/mime_type_utils.rb +11 -0
  108. data/lib/axlsx/util/options_parser.rb +16 -0
  109. data/lib/axlsx/util/parser.rb +44 -0
  110. data/lib/axlsx/util/serialized_attributes.rb +89 -0
  111. data/lib/axlsx/util/simple_typed_list.rb +179 -0
  112. data/lib/axlsx/util/storage.rb +146 -0
  113. data/lib/axlsx/util/validators.rb +312 -0
  114. data/lib/axlsx/version.rb +3 -0
  115. data/lib/axlsx/workbook/defined_name.rb +128 -0
  116. data/lib/axlsx/workbook/defined_names.rb +21 -0
  117. data/lib/axlsx/workbook/shared_strings_table.rb +77 -0
  118. data/lib/axlsx/workbook/workbook.rb +379 -0
  119. data/lib/axlsx/workbook/workbook_view.rb +80 -0
  120. data/lib/axlsx/workbook/workbook_views.rb +22 -0
  121. data/lib/axlsx/workbook/worksheet/auto_filter/auto_filter.rb +77 -0
  122. data/lib/axlsx/workbook/worksheet/auto_filter/filter_column.rb +94 -0
  123. data/lib/axlsx/workbook/worksheet/auto_filter/filters.rb +244 -0
  124. data/lib/axlsx/workbook/worksheet/break.rb +35 -0
  125. data/lib/axlsx/workbook/worksheet/cell.rb +478 -0
  126. data/lib/axlsx/workbook/worksheet/cell_serializer.rb +164 -0
  127. data/lib/axlsx/workbook/worksheet/cfvo.rb +60 -0
  128. data/lib/axlsx/workbook/worksheet/cfvos.rb +18 -0
  129. data/lib/axlsx/workbook/worksheet/col.rb +141 -0
  130. data/lib/axlsx/workbook/worksheet/col_breaks.rb +35 -0
  131. data/lib/axlsx/workbook/worksheet/color_scale.rb +110 -0
  132. data/lib/axlsx/workbook/worksheet/cols.rb +23 -0
  133. data/lib/axlsx/workbook/worksheet/comment.rb +90 -0
  134. data/lib/axlsx/workbook/worksheet/comments.rb +97 -0
  135. data/lib/axlsx/workbook/worksheet/conditional_formatting.rb +82 -0
  136. data/lib/axlsx/workbook/worksheet/conditional_formatting_rule.rb +220 -0
  137. data/lib/axlsx/workbook/worksheet/conditional_formattings.rb +25 -0
  138. data/lib/axlsx/workbook/worksheet/data_bar.rb +129 -0
  139. data/lib/axlsx/workbook/worksheet/data_validation.rb +246 -0
  140. data/lib/axlsx/workbook/worksheet/data_validations.rb +28 -0
  141. data/lib/axlsx/workbook/worksheet/date_time_converter.rb +30 -0
  142. data/lib/axlsx/workbook/worksheet/dimension.rb +64 -0
  143. data/lib/axlsx/workbook/worksheet/header_footer.rb +52 -0
  144. data/lib/axlsx/workbook/worksheet/icon_set.rb +81 -0
  145. data/lib/axlsx/workbook/worksheet/merged_cells.rb +37 -0
  146. data/lib/axlsx/workbook/worksheet/outline_pr.rb +33 -0
  147. data/lib/axlsx/workbook/worksheet/page_margins.rb +97 -0
  148. data/lib/axlsx/workbook/worksheet/page_set_up_pr.rb +44 -0
  149. data/lib/axlsx/workbook/worksheet/page_setup.rb +240 -0
  150. data/lib/axlsx/workbook/worksheet/pane.rb +139 -0
  151. data/lib/axlsx/workbook/worksheet/pivot_table.rb +289 -0
  152. data/lib/axlsx/workbook/worksheet/pivot_table_cache_definition.rb +66 -0
  153. data/lib/axlsx/workbook/worksheet/pivot_tables.rb +24 -0
  154. data/lib/axlsx/workbook/worksheet/print_options.rb +39 -0
  155. data/lib/axlsx/workbook/worksheet/protected_range.rb +47 -0
  156. data/lib/axlsx/workbook/worksheet/protected_ranges.rb +37 -0
  157. data/lib/axlsx/workbook/worksheet/rich_text.rb +55 -0
  158. data/lib/axlsx/workbook/worksheet/rich_text_run.rb +250 -0
  159. data/lib/axlsx/workbook/worksheet/row.rb +161 -0
  160. data/lib/axlsx/workbook/worksheet/row_breaks.rb +33 -0
  161. data/lib/axlsx/workbook/worksheet/selection.rb +101 -0
  162. data/lib/axlsx/workbook/worksheet/sheet_calc_pr.rb +29 -0
  163. data/lib/axlsx/workbook/worksheet/sheet_data.rb +27 -0
  164. data/lib/axlsx/workbook/worksheet/sheet_format_pr.rb +60 -0
  165. data/lib/axlsx/workbook/worksheet/sheet_pr.rb +87 -0
  166. data/lib/axlsx/workbook/worksheet/sheet_protection.rb +118 -0
  167. data/lib/axlsx/workbook/worksheet/sheet_view.rb +213 -0
  168. data/lib/axlsx/workbook/worksheet/table.rb +102 -0
  169. data/lib/axlsx/workbook/worksheet/table_style_info.rb +49 -0
  170. data/lib/axlsx/workbook/worksheet/tables.rb +34 -0
  171. data/lib/axlsx/workbook/worksheet/worksheet.rb +756 -0
  172. data/lib/axlsx/workbook/worksheet/worksheet_comments.rb +58 -0
  173. data/lib/axlsx/workbook/worksheet/worksheet_drawing.rb +58 -0
  174. data/lib/axlsx/workbook/worksheet/worksheet_hyperlink.rb +74 -0
  175. data/lib/axlsx/workbook/worksheet/worksheet_hyperlinks.rb +38 -0
  176. data/lib/schema/dc.xsd +118 -0
  177. data/lib/schema/dcmitype.xsd +52 -0
  178. data/lib/schema/dcterms.xsd +331 -0
  179. data/lib/schema/dml-chart.xsd +1499 -0
  180. data/lib/schema/dml-chartDrawing.xsd +146 -0
  181. data/lib/schema/dml-compatibility.xsd +14 -0
  182. data/lib/schema/dml-diagram.xsd +1091 -0
  183. data/lib/schema/dml-lockedCanvas.xsd +11 -0
  184. data/lib/schema/dml-main.xsd +3048 -0
  185. data/lib/schema/dml-picture.xsd +23 -0
  186. data/lib/schema/dml-spreadsheetDrawing.xsd +185 -0
  187. data/lib/schema/dml-wordprocessingDrawing.xsd +185 -0
  188. data/lib/schema/opc-contentTypes.xsd +42 -0
  189. data/lib/schema/opc-coreProperties.xsd +54 -0
  190. data/lib/schema/opc-digSig.xsd +49 -0
  191. data/lib/schema/opc-relationships.xsd +33 -0
  192. data/lib/schema/pml.xsd +1676 -0
  193. data/lib/schema/shared-additionalCharacteristics.xsd +28 -0
  194. data/lib/schema/shared-bibliography.xsd +144 -0
  195. data/lib/schema/shared-commonSimpleTypes.xsd +166 -0
  196. data/lib/schema/shared-customXmlDataProperties.xsd +25 -0
  197. data/lib/schema/shared-customXmlSchemaProperties.xsd +18 -0
  198. data/lib/schema/shared-documentPropertiesCustom.xsd +59 -0
  199. data/lib/schema/shared-documentPropertiesExtended.xsd +56 -0
  200. data/lib/schema/shared-documentPropertiesVariantTypes.xsd +195 -0
  201. data/lib/schema/shared-math.xsd +582 -0
  202. data/lib/schema/shared-relationshipReference.xsd +25 -0
  203. data/lib/schema/sml.xsd +4434 -0
  204. data/lib/schema/vml-main.xsd +569 -0
  205. data/lib/schema/vml-officeDrawing.xsd +509 -0
  206. data/lib/schema/vml-presentationDrawing.xsd +12 -0
  207. data/lib/schema/vml-spreadsheetDrawing.xsd +108 -0
  208. data/lib/schema/vml-wordprocessingDrawing.xsd +96 -0
  209. data/lib/schema/wml.xsd +3644 -0
  210. data/lib/schema/xml.xsd +116 -0
  211. data/test/benchmark.rb +72 -0
  212. data/test/content_type/tc_content_type.rb +76 -0
  213. data/test/content_type/tc_default.rb +16 -0
  214. data/test/content_type/tc_override.rb +14 -0
  215. data/test/doc_props/tc_app.rb +43 -0
  216. data/test/doc_props/tc_core.rb +42 -0
  217. data/test/drawing/tc_area_chart.rb +39 -0
  218. data/test/drawing/tc_area_series.rb +71 -0
  219. data/test/drawing/tc_axes.rb +8 -0
  220. data/test/drawing/tc_axis.rb +112 -0
  221. data/test/drawing/tc_bar_3D_chart.rb +71 -0
  222. data/test/drawing/tc_bar_chart.rb +71 -0
  223. data/test/drawing/tc_bar_series.rb +37 -0
  224. data/test/drawing/tc_bubble_chart.rb +44 -0
  225. data/test/drawing/tc_bubble_series.rb +21 -0
  226. data/test/drawing/tc_cat_axis.rb +31 -0
  227. data/test/drawing/tc_cat_axis_data.rb +27 -0
  228. data/test/drawing/tc_chart.rb +123 -0
  229. data/test/drawing/tc_d_lbls.rb +57 -0
  230. data/test/drawing/tc_data_source.rb +23 -0
  231. data/test/drawing/tc_drawing.rb +80 -0
  232. data/test/drawing/tc_graphic_frame.rb +27 -0
  233. data/test/drawing/tc_hyperlink.rb +64 -0
  234. data/test/drawing/tc_line_3d_chart.rb +47 -0
  235. data/test/drawing/tc_line_chart.rb +39 -0
  236. data/test/drawing/tc_line_series.rb +71 -0
  237. data/test/drawing/tc_marker.rb +44 -0
  238. data/test/drawing/tc_named_axis_data.rb +27 -0
  239. data/test/drawing/tc_num_data.rb +31 -0
  240. data/test/drawing/tc_num_val.rb +29 -0
  241. data/test/drawing/tc_one_cell_anchor.rb +66 -0
  242. data/test/drawing/tc_pic.rb +103 -0
  243. data/test/drawing/tc_picture_locking.rb +72 -0
  244. data/test/drawing/tc_pie_3D_chart.rb +28 -0
  245. data/test/drawing/tc_pie_series.rb +32 -0
  246. data/test/drawing/tc_scaling.rb +36 -0
  247. data/test/drawing/tc_scatter_chart.rb +48 -0
  248. data/test/drawing/tc_scatter_series.rb +56 -0
  249. data/test/drawing/tc_ser_axis.rb +31 -0
  250. data/test/drawing/tc_series.rb +23 -0
  251. data/test/drawing/tc_series_title.rb +33 -0
  252. data/test/drawing/tc_str_data.rb +18 -0
  253. data/test/drawing/tc_str_val.rb +30 -0
  254. data/test/drawing/tc_title.rb +54 -0
  255. data/test/drawing/tc_two_cell_anchor.rb +36 -0
  256. data/test/drawing/tc_val_axis.rb +24 -0
  257. data/test/drawing/tc_view_3D.rb +54 -0
  258. data/test/drawing/tc_vml_drawing.rb +25 -0
  259. data/test/drawing/tc_vml_shape.rb +106 -0
  260. data/test/profile.rb +24 -0
  261. data/test/rels/tc_relationship.rb +44 -0
  262. data/test/rels/tc_relationships.rb +37 -0
  263. data/test/stylesheet/tc_border.rb +37 -0
  264. data/test/stylesheet/tc_border_pr.rb +32 -0
  265. data/test/stylesheet/tc_cell_alignment.rb +81 -0
  266. data/test/stylesheet/tc_cell_protection.rb +29 -0
  267. data/test/stylesheet/tc_cell_style.rb +57 -0
  268. data/test/stylesheet/tc_color.rb +43 -0
  269. data/test/stylesheet/tc_dxf.rb +81 -0
  270. data/test/stylesheet/tc_fill.rb +18 -0
  271. data/test/stylesheet/tc_font.rb +121 -0
  272. data/test/stylesheet/tc_gradient_fill.rb +72 -0
  273. data/test/stylesheet/tc_gradient_stop.rb +31 -0
  274. data/test/stylesheet/tc_num_fmt.rb +30 -0
  275. data/test/stylesheet/tc_pattern_fill.rb +43 -0
  276. data/test/stylesheet/tc_styles.rb +235 -0
  277. data/test/stylesheet/tc_table_style.rb +44 -0
  278. data/test/stylesheet/tc_table_style_element.rb +45 -0
  279. data/test/stylesheet/tc_table_styles.rb +29 -0
  280. data/test/stylesheet/tc_xf.rb +120 -0
  281. data/test/tc_axlsx.rb +103 -0
  282. data/test/tc_helper.rb +12 -0
  283. data/test/tc_package.rb +245 -0
  284. data/test/util/tc_mime_type_utils.rb +13 -0
  285. data/test/util/tc_serialized_attributes.rb +19 -0
  286. data/test/util/tc_simple_typed_list.rb +77 -0
  287. data/test/util/tc_validators.rb +210 -0
  288. data/test/workbook/tc_defined_name.rb +49 -0
  289. data/test/workbook/tc_shared_strings_table.rb +59 -0
  290. data/test/workbook/tc_workbook.rb +160 -0
  291. data/test/workbook/tc_workbook_view.rb +50 -0
  292. data/test/workbook/worksheet/auto_filter/tc_auto_filter.rb +38 -0
  293. data/test/workbook/worksheet/auto_filter/tc_filter_column.rb +76 -0
  294. data/test/workbook/worksheet/auto_filter/tc_filters.rb +50 -0
  295. data/test/workbook/worksheet/tc_break.rb +49 -0
  296. data/test/workbook/worksheet/tc_cell.rb +387 -0
  297. data/test/workbook/worksheet/tc_cfvo.rb +31 -0
  298. data/test/workbook/worksheet/tc_col.rb +78 -0
  299. data/test/workbook/worksheet/tc_color_scale.rb +58 -0
  300. data/test/workbook/worksheet/tc_comment.rb +72 -0
  301. data/test/workbook/worksheet/tc_comments.rb +57 -0
  302. data/test/workbook/worksheet/tc_conditional_formatting.rb +224 -0
  303. data/test/workbook/worksheet/tc_data_bar.rb +46 -0
  304. data/test/workbook/worksheet/tc_data_validation.rb +265 -0
  305. data/test/workbook/worksheet/tc_date_time_converter.rb +124 -0
  306. data/test/workbook/worksheet/tc_header_footer.rb +151 -0
  307. data/test/workbook/worksheet/tc_icon_set.rb +45 -0
  308. data/test/workbook/worksheet/tc_outline_pr.rb +19 -0
  309. data/test/workbook/worksheet/tc_page_margins.rb +97 -0
  310. data/test/workbook/worksheet/tc_page_set_up_pr.rb +15 -0
  311. data/test/workbook/worksheet/tc_page_setup.rb +143 -0
  312. data/test/workbook/worksheet/tc_pane.rb +54 -0
  313. data/test/workbook/worksheet/tc_pivot_table.rb +135 -0
  314. data/test/workbook/worksheet/tc_pivot_table_cache_definition.rb +54 -0
  315. data/test/workbook/worksheet/tc_print_options.rb +72 -0
  316. data/test/workbook/worksheet/tc_protected_range.rb +17 -0
  317. data/test/workbook/worksheet/tc_rich_text.rb +44 -0
  318. data/test/workbook/worksheet/tc_rich_text_run.rb +172 -0
  319. data/test/workbook/worksheet/tc_row.rb +122 -0
  320. data/test/workbook/worksheet/tc_selection.rb +55 -0
  321. data/test/workbook/worksheet/tc_sheet_calc_pr.rb +18 -0
  322. data/test/workbook/worksheet/tc_sheet_format_pr.rb +88 -0
  323. data/test/workbook/worksheet/tc_sheet_pr.rb +49 -0
  324. data/test/workbook/worksheet/tc_sheet_protection.rb +117 -0
  325. data/test/workbook/worksheet/tc_sheet_view.rb +214 -0
  326. data/test/workbook/worksheet/tc_table.rb +67 -0
  327. data/test/workbook/worksheet/tc_table_style_info.rb +53 -0
  328. data/test/workbook/worksheet/tc_worksheet.rb +597 -0
  329. data/test/workbook/worksheet/tc_worksheet_hyperlink.rb +55 -0
  330. metadata +601 -0
@@ -0,0 +1,34 @@
1
+ module Axlsx
2
+
3
+ # A simple, self serializing class for storing tables
4
+ class Tables < SimpleTypedList
5
+
6
+ # creates a new Tables object
7
+ def initialize(worksheet)
8
+ raise ArgumentError, "you must provide a worksheet" unless worksheet.is_a?(Worksheet)
9
+ super Table
10
+ @worksheet = worksheet
11
+ end
12
+
13
+ # The worksheet that owns this collection of tables
14
+ # @return [Worksheet]
15
+ attr_reader :worksheet
16
+
17
+ # returns the relationships required by this collection
18
+ def relationships
19
+ return [] if empty?
20
+ map{ |table| Relationship.new(table, TABLE_R, "../#{table.pn}") }
21
+ end
22
+
23
+ # renders the tables xml
24
+ # @param [String] str
25
+ # @return [String]
26
+ def to_xml_string(str = "")
27
+ return if empty?
28
+ str << "<tableParts count='#{size}'>"
29
+ each { |table| str << "<tablePart r:id='#{table.rId}'/>" }
30
+ str << '</tableParts>'
31
+ end
32
+ end
33
+
34
+ end
@@ -0,0 +1,756 @@
1
+ # encoding: UTF-8
2
+ module Axlsx
3
+
4
+ # The Worksheet class represents a worksheet in the workbook.
5
+ class Worksheet
6
+ include Axlsx::OptionsParser
7
+ include Axlsx::SerializedAttributes
8
+ # definition of characters which are less than the maximum width of 0-9 in the default font for use in String#count.
9
+ # This is used for autowidth calculations
10
+ THIN_CHARS = '^.acfijklrstxzFIJL()-'.freeze
11
+
12
+ # Creates a new worksheet.
13
+ # @note the recommended way to manage worksheets is Workbook#add_worksheet
14
+ # @see Workbook#add_worksheet
15
+ # @option options [String] name The name of this worksheet.
16
+ # @option options [Hash] page_margins A hash containing page margins for this worksheet. @see PageMargins
17
+ # @option options [Hash] print_options A hash containing print options for this worksheet. @see PrintOptions
18
+ # @option options [Hash] header_footer A hash containing header/footer options for this worksheet. @see HeaderFooter
19
+ # @option options [Boolean] show_gridlines indicates if gridlines should be shown for this sheet.
20
+ def initialize(wb, options={})
21
+ self.workbook = wb
22
+ @sheet_protection = nil
23
+ initialize_page_options(options)
24
+ parse_options options
25
+ @workbook.worksheets << self
26
+ @sheet_id = index + 1
27
+ name
28
+ yield self if block_given?
29
+ end
30
+
31
+ serializable_attributes :sheet_id, :state
32
+
33
+ # Initalizes page margin, setup and print options
34
+ # @param [Hash] options Options passed in from the initializer
35
+ def initialize_page_options(options)
36
+ @page_margins = PageMargins.new options[:page_margins] if options[:page_margins]
37
+ @page_setup = PageSetup.new options[:page_setup] if options[:page_setup]
38
+ @print_options = PrintOptions.new options[:print_options] if options[:print_options]
39
+ @header_footer = HeaderFooter.new options[:header_footer] if options[:header_footer]
40
+ @row_breaks = RowBreaks.new
41
+ @col_breaks = ColBreaks.new
42
+ end
43
+
44
+ # The name of the worksheet
45
+ # @return [String]
46
+ def name
47
+ @name ||= "Sheet" + (index+1).to_s
48
+ end
49
+
50
+ # Specifies the visible state of this sheet. Allowed states are
51
+ # :visible, :hidden or :very_hidden. The default value is :visible.
52
+ #
53
+ # Worksheets in the :hidden state can be shown using the sheet formatting properties in excel.
54
+ # :very_hidden sheets should be inaccessible to end users.
55
+ # @param [Symbol] sheet_state The visible state for this sheet.
56
+ def state=(sheet_state)
57
+ RestrictionValidator.validate :worksheet_state, [:visible, :hidden, :very_hidden], sheet_state
58
+ @state = sheet_state
59
+ end
60
+
61
+ # The visibility of this sheet
62
+ def state
63
+ @state ||= :visible
64
+ end
65
+
66
+ # The sheet calculation properties
67
+ # @return [SheetCalcPr]
68
+ def sheet_calc_pr
69
+ @sheet_calc_pr ||= SheetCalcPr.new
70
+ end
71
+
72
+ # The sheet protection object for this workbook
73
+ # @return [SheetProtection]
74
+ # @see SheetProtection
75
+ def sheet_protection
76
+ @sheet_protection ||= SheetProtection.new
77
+ yield @sheet_protection if block_given?
78
+ @sheet_protection
79
+ end
80
+
81
+ # The sheet view object for this worksheet
82
+ # @return [SheetView]
83
+ # @see [SheetView]
84
+ def sheet_view
85
+ @sheet_view ||= SheetView.new
86
+ yield @sheet_view if block_given?
87
+ @sheet_view
88
+ end
89
+
90
+ # The sheet format pr for this worksheet
91
+ # @return [SheetFormatPr]
92
+ # @see [SheetFormatPr]
93
+ def sheet_format_pr
94
+ @sheet_format_pr ||= SheetFormatPr.new
95
+ yield @sheet_format_pr if block_given?
96
+ @sheet_format_pr
97
+ end
98
+
99
+ # The workbook that owns this worksheet
100
+ # @return [Workbook]
101
+ attr_reader :workbook
102
+
103
+ # The tables in this worksheet
104
+ # @return [Array] of Table
105
+ def tables
106
+ @tables ||= Tables.new self
107
+ end
108
+
109
+ # The pivot tables in this worksheet
110
+ # @return [Array] of Table
111
+ def pivot_tables
112
+ @pivot_tables ||= PivotTables.new self
113
+ end
114
+
115
+ # A collection of column breaks added to this worksheet
116
+ # @note Please do not use this directly. Instead use
117
+ # add_page_break
118
+ # @see Worksheet#add_page_break
119
+ def col_breaks
120
+ @col_breaks ||= ColBreaks.new
121
+ end
122
+
123
+ # A collection of row breaks added to this worksheet
124
+ # @note Please do not use this directly. Instead use
125
+ # add_page_break
126
+ # @see Worksheet#add_page_break
127
+ def row_breaks
128
+ @row_breaks ||= RowBreaks.new
129
+ end
130
+
131
+ # A typed collection of hyperlinks associated with this worksheet
132
+ # @return [WorksheetHyperlinks]
133
+ def hyperlinks
134
+ @hyperlinks ||= WorksheetHyperlinks.new self
135
+ end
136
+
137
+ # The a shortcut to the worksheet_comments list of comments
138
+ # @return [Array|SimpleTypedList]
139
+ def comments
140
+ worksheet_comments.comments if worksheet_comments.has_comments?
141
+ end
142
+
143
+ # The rows in this worksheet
144
+ # @note The recommended way to manage rows is Worksheet#add_row
145
+ # @return [SimpleTypedList]
146
+ # @see Worksheet#add_row
147
+ def rows
148
+ @rows ||= SimpleTypedList.new Row
149
+ end
150
+
151
+ # returns the sheet data as columns
152
+ # If you pass a block, it will be evaluated whenever a row does not have a
153
+ # cell at a specific index. The block will be called with the row and column
154
+ # index in the missing cell was found.
155
+ # @example
156
+ # cols { |row_index, column_index| puts "warn - row #{row_index} does not have a cell at #{column_index}" }
157
+ def cols(&block)
158
+ @rows.transpose(&block)
159
+ end
160
+
161
+ # A range that excel will apply an auto-filter to "A1:B3"
162
+ # This will turn filtering on for the cells in the range.
163
+ # The first row is considered the header, while subsequent rows are considered to be data.
164
+ # @return String
165
+ def auto_filter
166
+ @auto_filter ||= AutoFilter.new self
167
+ end
168
+
169
+ # Indicates if the worksheet will be fit by witdh or height to a specific number of pages.
170
+ # To alter the width or height for page fitting, please use page_setup.fit_to_widht or page_setup.fit_to_height.
171
+ # If you want the worksheet to fit on more pages (e.g. 2x2), set {PageSetup#fit_to_width} and {PageSetup#fit_to_height} accordingly.
172
+ # @return Boolean
173
+ # @see #page_setup
174
+ def fit_to_page?
175
+ return false unless self.instance_values.keys.include?('page_setup')
176
+ page_setup.fit_to_page?
177
+ end
178
+
179
+
180
+ # Column info for the sheet
181
+ # @return [SimpleTypedList]
182
+ def column_info
183
+ @column_info ||= Cols.new self
184
+ end
185
+
186
+ # Page margins for printing the worksheet.
187
+ # @example
188
+ # wb = Axlsx::Package.new.workbook
189
+ # # using options when creating the worksheet.
190
+ # ws = wb.add_worksheet :page_margins => {:left => 1.9, :header => 0.1}
191
+ #
192
+ # # use the set method of the page_margins object
193
+ # ws.page_margins.set(:bottom => 3, :footer => 0.7)
194
+ #
195
+ # # set page margins in a block
196
+ # ws.page_margins do |margins|
197
+ # margins.right = 6
198
+ # margins.top = 0.2
199
+ # end
200
+ # @see PageMargins#initialize
201
+ # @return [PageMargins]
202
+ def page_margins
203
+ @page_margins ||= PageMargins.new
204
+ yield @page_margins if block_given?
205
+ @page_margins
206
+ end
207
+
208
+ # Page setup settings for printing the worksheet.
209
+ # @example
210
+ # wb = Axlsx::Package.new.workbook
211
+ #
212
+ # # using options when creating the worksheet.
213
+ # ws = wb.add_worksheet :page_setup => {:fit_to_width => 2, :orientation => :landscape}
214
+ #
215
+ # # use the set method of the page_setup object
216
+ # ws.page_setup.set(:paper_width => "297mm", :paper_height => "210mm")
217
+ #
218
+ # # setup page in a block
219
+ # ws.page_setup do |page|
220
+ # page.scale = 80
221
+ # page.orientation = :portrait
222
+ # end
223
+ # @see PageSetup#initialize
224
+ # @return [PageSetup]
225
+ def page_setup
226
+ @page_setup ||= PageSetup.new
227
+ yield @page_setup if block_given?
228
+ @page_setup
229
+ end
230
+
231
+ # Options for printing the worksheet.
232
+ # @example
233
+ # wb = Axlsx::Package.new.workbook
234
+ # # using options when creating the worksheet.
235
+ # ws = wb.add_worksheet :print_options => {:grid_lines => true, :horizontal_centered => true}
236
+ #
237
+ # # use the set method of the page_margins object
238
+ # ws.print_options.set(:headings => true)
239
+ #
240
+ # # set page margins in a block
241
+ # ws.print_options do |options|
242
+ # options.horizontal_centered = true
243
+ # options.vertical_centered = true
244
+ # end
245
+ # @see PrintOptions#initialize
246
+ # @return [PrintOptions]
247
+ def print_options
248
+ @print_options ||= PrintOptions.new
249
+ yield @print_options if block_given?
250
+ @print_options
251
+ end
252
+
253
+ # Options for headers and footers.
254
+ # @example
255
+ # wb = Axlsx::Package.new.workbook
256
+ # # would generate something like: "file.xlsx : sheet_name 2 of 7 date with timestamp"
257
+ # header = {:different_odd_ => false, :odd_header => "&L&F : &A&C&Pof%N%R%D %T"}
258
+ # ws = wb.add_worksheet :header_footer => header
259
+ #
260
+ # @see HeaderFooter#initialize
261
+ # @return [HeaderFooter]
262
+ def header_footer
263
+ @header_footer ||= HeaderFooter.new
264
+ yield @header_footer if block_given?
265
+ @header_footer
266
+ end
267
+
268
+ # convenience method to access all cells in this worksheet
269
+ # @return [Array] cells
270
+ def cells
271
+ rows.flatten
272
+ end
273
+
274
+ # Creates merge information for this worksheet.
275
+ # Cells can be merged by calling the merge_cells method on a worksheet.
276
+ # @example This would merge the three cells C1..E1 #
277
+ # worksheet.merge_cells "C1:E1"
278
+ # # you can also provide an array of cells to be merged
279
+ # worksheet.merge_cells worksheet.rows.first.cells[(2..4)]
280
+ # #alternatively you can do it from a single cell
281
+ # worksheet["C1"].merge worksheet["E1"]
282
+ # @param [Array, string] cells
283
+ def merge_cells(cells)
284
+ merged_cells.add cells
285
+ end
286
+
287
+ # Adds a new protected cell range to the worksheet. Note that protected ranges are only in effect when sheet protection is enabled.
288
+ # @param [String|Array] cells The string reference for the cells to protect or an array of cells.
289
+ # @return [ProtectedRange]
290
+ # @note When using an array of cells, a contiguous range is created from the minimum top left to the maximum top bottom of the cells provided.
291
+ def protect_range(cells)
292
+ protected_ranges.add_range(cells)
293
+ end
294
+
295
+ # The dimensions of a worksheet. This is not actually a required element by the spec,
296
+ # but at least a few other document readers expect this for conversion
297
+ # @return [Dimension]
298
+ def dimension
299
+ @dimension ||= Dimension.new self
300
+ end
301
+
302
+ # The sheet properties for this workbook.
303
+ # Currently only pageSetUpPr -> fitToPage is implemented
304
+ # @return [SheetPr]
305
+ def sheet_pr
306
+ @sheet_pr ||= SheetPr.new self
307
+ end
308
+
309
+ # The name of the worksheet
310
+ # The name of a worksheet must be unique in the workbook, and must not exceed 31 characters
311
+ # @param [String] name
312
+ def name=(name)
313
+ validate_sheet_name name
314
+ @name=Axlsx::coder.encode(name)
315
+ end
316
+
317
+ # The auto filter range for the worksheet
318
+ # @param [String] v
319
+ # @see auto_filter
320
+ def auto_filter=(v)
321
+ DataTypeValidator.validate :worksheet_auto_filter, String, v
322
+ auto_filter.range = v
323
+ end
324
+
325
+ # Accessor for controlling whether leading and trailing spaces in cells are
326
+ # preserved or ignored. The default is to preserve spaces.
327
+ attr_accessor :preserve_spaces
328
+
329
+ # The part name of this worksheet
330
+ # @return [String]
331
+ def pn
332
+ "#{WORKSHEET_PN % (index+1)}"
333
+ end
334
+
335
+ # The relationship part name of this worksheet
336
+ # @return [String]
337
+ def rels_pn
338
+ "#{WORKSHEET_RELS_PN % (index+1)}"
339
+ end
340
+
341
+ # The relationship id of this worksheet.
342
+ # @return [String]
343
+ # @see Relationship#Id
344
+ def rId
345
+ @workbook.relationships.for(self).Id
346
+ end
347
+
348
+ # The index of this worksheet in the owning Workbook's worksheets list.
349
+ # @return [Integer]
350
+ def index
351
+ @workbook.worksheets.index(self)
352
+ end
353
+
354
+ # The drawing associated with this worksheet.
355
+ # @note the recommended way to work with drawings and charts is Worksheet#add_chart
356
+ # @return [Drawing]
357
+ # @see Worksheet#add_chart
358
+ def drawing
359
+ worksheet_drawing.drawing
360
+ end
361
+
362
+ # Adds a row to the worksheet and updates auto fit data.
363
+ # @example - put a vanilla row in your spreadsheet
364
+ # ws.add_row [1, 'fish on my pl', '8']
365
+ #
366
+ # @example - specify a fixed width for a column in your spreadsheet
367
+ # # The first column will ignore the content of this cell when calculating column autowidth.
368
+ # # The second column will include this text in calculating the columns autowidth
369
+ # # The third cell will set a fixed with of 80 for the column.
370
+ # # If you need to un-fix a column width, use :auto. That will recalculate the column width based on all content in the column
371
+ #
372
+ # ws.add_row ['I wish', 'for a fish', 'on my fish wish dish'], :widths=>[:ignore, :auto, 80]
373
+ #
374
+ # @example - specify a fixed height for a row
375
+ # ws.add_row ['I wish', 'for a fish', 'on my fish wish dish'], :height => 40
376
+ #
377
+ # @example - create and use a style for all cells in the row
378
+ # blue = ws.styles.add_style :color => "#00FF00"
379
+ # ws.add_row [1, 2, 3], :style=>blue
380
+ #
381
+ # @example - only style some cells
382
+ # blue = ws.styles.add_style :color => "#00FF00"
383
+ # red = ws.styles.add_style :color => "#FF0000"
384
+ # big = ws.styles.add_style :sz => 40
385
+ # ws.add_row ["red fish", "blue fish", "one fish", "two fish"], :style=>[red, blue, nil, big] # the last nil is optional
386
+ #
387
+ #
388
+ # @example - force the second cell to be a float value
389
+ # ws.add_row [3, 4, 5], :types => [nil, :float]
390
+ #
391
+ # @example - use << alias
392
+ # ws << [3, 4, 5], :types => [nil, :float]
393
+ #
394
+ # @see Worksheet#column_widths
395
+ # @return [Row]
396
+ # @option options [Array] values
397
+ # @option options [Array, Symbol] types
398
+ # @option options [Array, Integer] style
399
+ # @option options [Array] widths each member of the widths array will affect how auto_fit behavies.
400
+ # @option options [Float] height the row's height (in points)
401
+ def add_row(values=[], options={})
402
+ row = Row.new(self, values, options)
403
+ update_column_info row, options.delete(:widths)
404
+ yield row if block_given?
405
+ row
406
+ end
407
+
408
+ alias :<< :add_row
409
+
410
+ # Add conditional formatting to this worksheet.
411
+ #
412
+ # @param [String] cells The range to apply the formatting to
413
+ # @param [Array|Hash] rules An array of hashes (or just one) to create Conditional formatting rules from.
414
+ # @example This would format column A whenever it is FALSE.
415
+ # # for a longer example, see examples/example_conditional_formatting.rb (link below)
416
+ # worksheet.add_conditional_formatting( "A1:A1048576", { :type => :cellIs, :operator => :equal, :formula => "FALSE", :dxfId => 1, :priority => 1 }
417
+ #
418
+ # @see ConditionalFormattingRule#initialize
419
+ # @see file:examples/example_conditional_formatting.rb
420
+ def add_conditional_formatting(cells, rules)
421
+ cf = ConditionalFormatting.new( :sqref => cells )
422
+ cf.add_rules rules
423
+ conditional_formattings << cf
424
+ conditional_formattings
425
+ end
426
+
427
+ # Add data validation to this worksheet.
428
+ #
429
+ # @param [String] cells The cells the validation will apply to.
430
+ # @param [hash] data_validation options defining the validation to apply.
431
+ # @see examples/data_validation.rb for an example
432
+ def add_data_validation(cells, data_validation)
433
+ dv = DataValidation.new(data_validation)
434
+ dv.sqref = cells
435
+ data_validations << dv
436
+ end
437
+
438
+ # Adds a new hyperlink to the worksheet
439
+ # @param [Hash] options for the hyperlink
440
+ # @see WorksheetHyperlink for a list of options
441
+ # @return [WorksheetHyperlink]
442
+ def add_hyperlink(options={})
443
+ hyperlinks.add(options)
444
+ end
445
+
446
+ # Adds a chart to this worksheets drawing. This is the recommended way to create charts for your worksheet. This method wraps the complexity of dealing with ooxml drawing, anchors, markers graphic frames chart objects and all the other dirty details.
447
+ # @param [Class] chart_type
448
+ # @option options [Array] start_at
449
+ # @option options [Array] end_at
450
+ # @option options [Cell, String] title
451
+ # @option options [Boolean] show_legend
452
+ # @option options [Integer] style
453
+ # @note each chart type also specifies additional options
454
+ # @see Chart
455
+ # @see Pie3DChart
456
+ # @see Bar3DChart
457
+ # @see Line3DChart
458
+ # @see README for examples
459
+ def add_chart(chart_type, options={})
460
+ chart = worksheet_drawing.add_chart(chart_type, options)
461
+ yield chart if block_given?
462
+ chart
463
+ end
464
+
465
+ # needs documentation
466
+ def add_table(ref, options={})
467
+ tables << Table.new(ref, self, options)
468
+ yield tables.last if block_given?
469
+ tables.last
470
+ end
471
+
472
+ def add_pivot_table(ref, range, options={})
473
+ pivot_tables << PivotTable.new(ref, range, self, options)
474
+ yield pivot_tables.last if block_given?
475
+ pivot_tables.last
476
+ end
477
+
478
+ # Shortcut to worsksheet_comments#add_comment
479
+ def add_comment(options={})
480
+ worksheet_comments.add_comment(options)
481
+ end
482
+
483
+ # Adds a media item to the worksheets drawing
484
+ # @option [Hash] options options passed to drawing.add_image
485
+ def add_image(options={})
486
+ image = worksheet_drawing.add_image(options)
487
+ yield image if block_given?
488
+ image
489
+ end
490
+
491
+ # Adds a page break (row break) to the worksheet
492
+ # @param cell A Cell object or excel style string reference indicating where the break
493
+ # should be added to the sheet.
494
+ # @example
495
+ # ws.add_page_break("A4")
496
+ def add_page_break(cell)
497
+ DataTypeValidator.validate :worksheet_page_break, [String, Cell], cell
498
+ column_index, row_index = if cell.is_a?(String)
499
+ Axlsx.name_to_indices(cell)
500
+ else
501
+ cell.pos
502
+ end
503
+ if column_index > 0
504
+ col_breaks.add_break(:id => column_index)
505
+ end
506
+ row_breaks.add_break(:id => row_index)
507
+ end
508
+
509
+ # This is a helper method that Lets you specify a fixed width for multiple columns in a worksheet in one go.
510
+ # Note that you must call column_widths AFTER adding data, otherwise the width will not be set successfully.
511
+ # Setting a fixed column width to nil will revert the behaviour back to calculating the width for you on the next call to add_row.
512
+ # @example This would set the first and third column widhts but leave the second column in autofit state.
513
+ # ws.column_widths 7.2, nil, 3
514
+ # @note For updating only a single column it is probably easier to just set the width of the ws.column_info[col_index].width directly
515
+ # @param [Integer|Float|nil] widths
516
+ def column_widths(*widths)
517
+ widths.each_with_index do |value, index|
518
+ next if value == nil
519
+ Axlsx::validate_unsigned_numeric(value) unless value == nil
520
+ find_or_create_column_info(index).width = value
521
+ end
522
+ end
523
+
524
+ # Set the style for cells in a specific column
525
+ # @param [Integer] index the index of the column
526
+ # @param [Integer] style the cellXfs index
527
+ # @param [Hash] options
528
+ # @option [Integer] :row_offset only cells after this column will be updated.
529
+ # @note You can also specify the style for specific columns in the call to add_row by using an array for the :styles option
530
+ # @see Worksheet#add_row
531
+ # @see README.md for an example
532
+ def col_style(index, style, options={})
533
+ offset = options.delete(:row_offset) || 0
534
+ cells = @rows[(offset..-1)].map { |row| row[index] }.flatten.compact
535
+ cells.each { |cell| cell.style = style }
536
+ end
537
+
538
+ # Set the style for cells in a specific row
539
+ # @param [Integer] index or range of indexes in the table
540
+ # @param [Integer] style the cellXfs index
541
+ # @param [Hash] options the options used when applying the style
542
+ # @option [Integer] :col_offset only cells after this column will be updated.
543
+ # @note You can also specify the style in the add_row call
544
+ # @see Worksheet#add_row
545
+ # @see README.md for an example
546
+ def row_style(index, style, options={})
547
+ offset = options.delete(:col_offset) || 0
548
+ cells = cols[(offset..-1)].map { |column| column[index] }.flatten.compact
549
+ cells.each { |cell| cell.style = style }
550
+ end
551
+
552
+ # Returns a sheet node serialization for this sheet in the workbook.
553
+ def to_sheet_node_xml_string(str='')
554
+ add_autofilter_defined_name_to_workbook
555
+ str << '<sheet '
556
+ serialized_attributes str
557
+ str << ('name="' << name << '" ')
558
+ str << ('r:id="' << rId << '"></sheet>')
559
+ end
560
+
561
+ # Serializes the worksheet object to an xml string
562
+ # This intentionally does not use nokogiri for performance reasons
563
+ # @return [String]
564
+ def to_xml_string str=''
565
+ add_autofilter_defined_name_to_workbook
566
+ auto_filter.apply if auto_filter.range
567
+ str << '<?xml version="1.0" encoding="UTF-8"?>'
568
+ str << worksheet_node
569
+ serializable_parts.each do |item|
570
+ item.to_xml_string(str) if item
571
+ end
572
+ str << '</worksheet>'
573
+ end
574
+
575
+ # The worksheet relationships. This is managed automatically by the worksheet
576
+ # @return [Relationships]
577
+ def relationships
578
+ r = Relationships.new
579
+ r + [tables.relationships,
580
+ worksheet_comments.relationships,
581
+ hyperlinks.relationships,
582
+ worksheet_drawing.relationship,
583
+ pivot_tables.relationships].flatten.compact || []
584
+ r
585
+ end
586
+
587
+ # Returns the cell or cells defined using excel style A1:B3 references.
588
+ # @param [String|Integer] cell_def the string defining the cell or range of cells, or the rownumber
589
+ # @return [Cell, Array]
590
+ def [](cell_def)
591
+ return rows[cell_def] if cell_def.is_a?(Integer)
592
+ parts = cell_def.split(':').map{ |part| name_to_cell part }
593
+ if parts.size == 1
594
+ parts.first
595
+ else
596
+ range(*parts)
597
+ end
598
+ end
599
+
600
+ # returns the column and row index for a named based cell
601
+ # @param [String] name The cell or cell range to return. "A1" will return the first cell of the first row.
602
+ # @return [Cell]
603
+ def name_to_cell(name)
604
+ col_index, row_index = *Axlsx::name_to_indices(name)
605
+ r = rows[row_index]
606
+ r[col_index] if r
607
+ end
608
+
609
+ # shortcut method to access styles direclty from the worksheet
610
+ # This lets us do stuff like:
611
+ # @example
612
+ # p = Axlsx::Package.new
613
+ # p.workbook.add_worksheet(:name => 'foo') do |sheet|
614
+ # my_style = sheet.styles.add_style { :bg_color => "FF0000" }
615
+ # sheet.add_row ['Oh No!'], :styles => my_style
616
+ # end
617
+ # p.serialize 'foo.xlsx'
618
+ def styles
619
+ @styles ||= self.workbook.styles
620
+ end
621
+
622
+ # shortcut level to specify the outline level for a series of rows
623
+ # Oulining is what lets you add collapse and expand to a data set.
624
+ # @param [Integer] start_index The zero based index of the first row of outlining.
625
+ # @param [Integer] end_index The zero based index of the last row to be outlined
626
+ # @param [integer] level The level of outline to apply
627
+ # @param [Integer] collapsed The initial collapsed state of the outline group
628
+ def outline_level_rows(start_index, end_index, level = 1, collapsed = true)
629
+ outline rows, (start_index..end_index), level, collapsed
630
+ end
631
+
632
+ # shortcut level to specify the outline level for a series of columns
633
+ # Oulining is what lets you add collapse and expand to a data set.
634
+ # @param [Integer] start_index The zero based index of the first column of outlining.
635
+ # @param [Integer] end_index The zero based index of the last column to be outlined
636
+ # @param [integer] level The level of outline to apply
637
+ # @param [Integer] collapsed The initial collapsed state of the outline group
638
+ def outline_level_columns(start_index, end_index, level = 1, collapsed = true)
639
+ outline column_info, (start_index..end_index), level, collapsed
640
+ end
641
+
642
+ private
643
+
644
+ def xml_space
645
+ workbook.xml_space
646
+ end
647
+
648
+ def outline(collection, range, level = 1, collapsed = true)
649
+ range.each do |index|
650
+ unless (item = collection[index]).nil?
651
+ item.outline_level = level
652
+ item.hidden = collapsed
653
+ end
654
+ sheet_view.show_outline_symbols = true
655
+ end
656
+ end
657
+
658
+ def validate_sheet_name(name)
659
+ DataTypeValidator.validate :worksheet_name, String, name
660
+ raise ArgumentError, (ERR_SHEET_NAME_TOO_LONG % name) if name.bytesize > 31
661
+ raise ArgumentError, (ERR_SHEET_NAME_CHARACTER_FORBIDDEN % name) if '[]*/\?:'.chars.any? { |char| name.include? char }
662
+ name = Axlsx::coder.encode(name)
663
+ sheet_names = @workbook.worksheets.reject { |s| s == self }.map { |s| s.name }
664
+ raise ArgumentError, (ERR_DUPLICATE_SHEET_NAME % name) if sheet_names.include?(name)
665
+ end
666
+
667
+ def serializable_parts
668
+ [sheet_pr, dimension, sheet_view, sheet_format_pr, column_info,
669
+ sheet_data, sheet_calc_pr, @sheet_protection, protected_ranges,
670
+ auto_filter, merged_cells, conditional_formattings,
671
+ data_validations, hyperlinks, print_options, page_margins,
672
+ page_setup, header_footer, row_breaks, col_breaks, worksheet_drawing, worksheet_comments,
673
+ tables]
674
+ end
675
+
676
+ def range(*cell_def)
677
+ first, last = cell_def
678
+ cells = []
679
+ rows[(first.row.row_index..last.row.row_index)].each do |r|
680
+ r[(first.index..last.index)].each do |c|
681
+ cells << c
682
+ end
683
+ end
684
+ cells
685
+ end
686
+
687
+ # A collection of protected ranges in the worksheet
688
+ # @note The recommended way to manage protected ranges is with Worksheet#protect_range
689
+ # @see Worksheet#protect_range
690
+ # @return [SimpleTypedList] The protected ranges for this worksheet
691
+ def protected_ranges
692
+ @protected_ranges ||= ProtectedRanges.new self
693
+ # SimpleTypedList.new ProtectedRange
694
+ end
695
+
696
+ # conditional formattings
697
+ # @return [Array]
698
+ def conditional_formattings
699
+ @conditional_formattings ||= ConditionalFormattings.new self
700
+ end
701
+
702
+ # data validations array
703
+ # @return [Array]
704
+ def data_validations
705
+ @data_validations ||= DataValidations.new self
706
+ end
707
+
708
+ # merged cells array
709
+ # @return [Array]
710
+ def merged_cells
711
+ @merged_cells ||= MergedCells.new self
712
+ end
713
+
714
+
715
+ # Helper method for parsingout the root node for worksheet
716
+ # @return [String]
717
+ def worksheet_node
718
+ "<worksheet xmlns=\"#{XML_NS}\" xmlns:r=\"#{XML_NS_R}\" xml:space=\"#{xml_space}\">"
719
+ end
720
+
721
+ def sheet_data
722
+ @sheet_data ||= SheetData.new self
723
+ end
724
+
725
+ def worksheet_drawing
726
+ @worksheet_drawing ||= WorksheetDrawing.new self
727
+ end
728
+
729
+ # The comments associated with this worksheet
730
+ # @return [SimpleTypedList]
731
+ def worksheet_comments
732
+ @worksheet_comments ||= WorksheetComments.new self
733
+ end
734
+
735
+ def workbook=(v) DataTypeValidator.validate "Worksheet.workbook", Workbook, v; @workbook = v; end
736
+
737
+ def update_column_info(cells, widths=nil)
738
+ cells.each_with_index do |cell, index|
739
+ width = widths ? widths[index] : nil
740
+ col = find_or_create_column_info(index)
741
+ next if width == :ignore
742
+ col.update_width(cell, width, workbook.use_autowidth)
743
+ end
744
+ end
745
+
746
+ def find_or_create_column_info(index)
747
+ column_info[index] ||= Col.new(index + 1, index + 1)
748
+ end
749
+
750
+ def add_autofilter_defined_name_to_workbook
751
+ return if !auto_filter.range
752
+ workbook.add_defined_name auto_filter.defined_name, name: '_xlnm._FilterDatabase', local_sheet_id: index, hidden: 1
753
+ end
754
+
755
+ end
756
+ end