caxlsx 2.0.2

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