dg-axlsx 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (324) 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 +259 -0
  7. data/Rakefile +30 -0
  8. data/examples/2010_comments.rb +17 -0
  9. data/examples/anchor_swapping.rb +28 -0
  10. data/examples/auto_filter.rb +25 -0
  11. data/examples/basic_charts.rb +58 -0
  12. data/examples/chart_colors.rb +88 -0
  13. data/examples/colored_links.rb +59 -0
  14. data/examples/conditional_formatting/example_conditional_formatting.rb +89 -0
  15. data/examples/conditional_formatting/getting_barred.rb +37 -0
  16. data/examples/conditional_formatting/hitting_the_high_notes.rb +37 -0
  17. data/examples/conditional_formatting/scaled_colors.rb +39 -0
  18. data/examples/conditional_formatting/stop_and_go.rb +37 -0
  19. data/examples/data_validation.rb +50 -0
  20. data/examples/example.rb +875 -0
  21. data/examples/extractive.rb +45 -0
  22. data/examples/image1.jpeg +0 -0
  23. data/examples/ios_preview.rb +14 -0
  24. data/examples/merge_cells.rb +17 -0
  25. data/examples/no_grid_with_borders.rb +18 -0
  26. data/examples/page_setup.rb +11 -0
  27. data/examples/pivot_table.rb +39 -0
  28. data/examples/pivot_test.rb +63 -0
  29. data/examples/sheet_protection.rb +10 -0
  30. data/examples/skydrive/real_example.rb +63 -0
  31. data/examples/split.rb +16 -0
  32. data/examples/styles.rb +66 -0
  33. data/examples/underline.rb +13 -0
  34. data/examples/wrap_text.rb +21 -0
  35. data/lib/axlsx.rb +168 -0
  36. data/lib/axlsx/content_type/abstract_content_type.rb +32 -0
  37. data/lib/axlsx/content_type/content_type.rb +26 -0
  38. data/lib/axlsx/content_type/default.rb +25 -0
  39. data/lib/axlsx/content_type/override.rb +25 -0
  40. data/lib/axlsx/doc_props/app.rb +235 -0
  41. data/lib/axlsx/doc_props/core.rb +39 -0
  42. data/lib/axlsx/drawing/ax_data_source.rb +26 -0
  43. data/lib/axlsx/drawing/axes.rb +61 -0
  44. data/lib/axlsx/drawing/axis.rb +190 -0
  45. data/lib/axlsx/drawing/bar_3D_chart.rb +151 -0
  46. data/lib/axlsx/drawing/bar_series.rb +82 -0
  47. data/lib/axlsx/drawing/bubble_chart.rb +59 -0
  48. data/lib/axlsx/drawing/bubble_series.rb +63 -0
  49. data/lib/axlsx/drawing/cat_axis.rb +85 -0
  50. data/lib/axlsx/drawing/chart.rb +269 -0
  51. data/lib/axlsx/drawing/d_lbls.rb +90 -0
  52. data/lib/axlsx/drawing/drawing.rb +164 -0
  53. data/lib/axlsx/drawing/graphic_frame.rb +54 -0
  54. data/lib/axlsx/drawing/hyperlink.rb +100 -0
  55. data/lib/axlsx/drawing/line_3D_chart.rb +68 -0
  56. data/lib/axlsx/drawing/line_chart.rb +99 -0
  57. data/lib/axlsx/drawing/line_series.rb +110 -0
  58. data/lib/axlsx/drawing/marker.rb +84 -0
  59. data/lib/axlsx/drawing/num_data.rb +52 -0
  60. data/lib/axlsx/drawing/num_data_source.rb +62 -0
  61. data/lib/axlsx/drawing/num_val.rb +32 -0
  62. data/lib/axlsx/drawing/one_cell_anchor.rb +98 -0
  63. data/lib/axlsx/drawing/pic.rb +213 -0
  64. data/lib/axlsx/drawing/picture_locking.rb +42 -0
  65. data/lib/axlsx/drawing/pie_3D_chart.rb +47 -0
  66. data/lib/axlsx/drawing/pie_series.rb +74 -0
  67. data/lib/axlsx/drawing/scaling.rb +60 -0
  68. data/lib/axlsx/drawing/scatter_chart.rb +74 -0
  69. data/lib/axlsx/drawing/scatter_series.rb +84 -0
  70. data/lib/axlsx/drawing/ser_axis.rb +45 -0
  71. data/lib/axlsx/drawing/series.rb +69 -0
  72. data/lib/axlsx/drawing/series_title.rb +23 -0
  73. data/lib/axlsx/drawing/str_data.rb +42 -0
  74. data/lib/axlsx/drawing/str_val.rb +32 -0
  75. data/lib/axlsx/drawing/title.rb +78 -0
  76. data/lib/axlsx/drawing/two_cell_anchor.rb +92 -0
  77. data/lib/axlsx/drawing/val_axis.rb +37 -0
  78. data/lib/axlsx/drawing/view_3D.rb +115 -0
  79. data/lib/axlsx/drawing/vml_drawing.rb +42 -0
  80. data/lib/axlsx/drawing/vml_shape.rb +66 -0
  81. data/lib/axlsx/package.rb +363 -0
  82. data/lib/axlsx/rels/relationship.rb +129 -0
  83. data/lib/axlsx/rels/relationships.rb +29 -0
  84. data/lib/axlsx/stylesheet/border.rb +71 -0
  85. data/lib/axlsx/stylesheet/border_pr.rb +71 -0
  86. data/lib/axlsx/stylesheet/cell_alignment.rb +132 -0
  87. data/lib/axlsx/stylesheet/cell_protection.rb +41 -0
  88. data/lib/axlsx/stylesheet/cell_style.rb +72 -0
  89. data/lib/axlsx/stylesheet/color.rb +76 -0
  90. data/lib/axlsx/stylesheet/dxf.rb +79 -0
  91. data/lib/axlsx/stylesheet/fill.rb +35 -0
  92. data/lib/axlsx/stylesheet/font.rb +148 -0
  93. data/lib/axlsx/stylesheet/gradient_fill.rb +103 -0
  94. data/lib/axlsx/stylesheet/gradient_stop.rb +37 -0
  95. data/lib/axlsx/stylesheet/num_fmt.rb +77 -0
  96. data/lib/axlsx/stylesheet/pattern_fill.rb +73 -0
  97. data/lib/axlsx/stylesheet/styles.rb +420 -0
  98. data/lib/axlsx/stylesheet/table_style.rb +54 -0
  99. data/lib/axlsx/stylesheet/table_style_element.rb +77 -0
  100. data/lib/axlsx/stylesheet/table_styles.rb +46 -0
  101. data/lib/axlsx/stylesheet/xf.rb +147 -0
  102. data/lib/axlsx/util/accessors.rb +64 -0
  103. data/lib/axlsx/util/constants.rb +397 -0
  104. data/lib/axlsx/util/mime_type_utils.rb +11 -0
  105. data/lib/axlsx/util/options_parser.rb +16 -0
  106. data/lib/axlsx/util/parser.rb +44 -0
  107. data/lib/axlsx/util/serialized_attributes.rb +89 -0
  108. data/lib/axlsx/util/simple_typed_list.rb +179 -0
  109. data/lib/axlsx/util/storage.rb +146 -0
  110. data/lib/axlsx/util/string.rb +7 -0
  111. data/lib/axlsx/util/validators.rb +311 -0
  112. data/lib/axlsx/version.rb +5 -0
  113. data/lib/axlsx/workbook/defined_name.rb +128 -0
  114. data/lib/axlsx/workbook/defined_names.rb +21 -0
  115. data/lib/axlsx/workbook/shared_strings_table.rb +77 -0
  116. data/lib/axlsx/workbook/workbook.rb +361 -0
  117. data/lib/axlsx/workbook/workbook_view.rb +78 -0
  118. data/lib/axlsx/workbook/workbook_views.rb +22 -0
  119. data/lib/axlsx/workbook/worksheet/auto_filter/auto_filter.rb +77 -0
  120. data/lib/axlsx/workbook/worksheet/auto_filter/filter_column.rb +94 -0
  121. data/lib/axlsx/workbook/worksheet/auto_filter/filters.rb +244 -0
  122. data/lib/axlsx/workbook/worksheet/break.rb +35 -0
  123. data/lib/axlsx/workbook/worksheet/cell.rb +475 -0
  124. data/lib/axlsx/workbook/worksheet/cell_serializer.rb +154 -0
  125. data/lib/axlsx/workbook/worksheet/cfvo.rb +60 -0
  126. data/lib/axlsx/workbook/worksheet/cfvos.rb +15 -0
  127. data/lib/axlsx/workbook/worksheet/col.rb +141 -0
  128. data/lib/axlsx/workbook/worksheet/col_breaks.rb +35 -0
  129. data/lib/axlsx/workbook/worksheet/color_scale.rb +110 -0
  130. data/lib/axlsx/workbook/worksheet/cols.rb +20 -0
  131. data/lib/axlsx/workbook/worksheet/comment.rb +91 -0
  132. data/lib/axlsx/workbook/worksheet/comments.rb +82 -0
  133. data/lib/axlsx/workbook/worksheet/conditional_formatting.rb +82 -0
  134. data/lib/axlsx/workbook/worksheet/conditional_formatting_rule.rb +220 -0
  135. data/lib/axlsx/workbook/worksheet/conditional_formattings.rb +25 -0
  136. data/lib/axlsx/workbook/worksheet/data_bar.rb +129 -0
  137. data/lib/axlsx/workbook/worksheet/data_validation.rb +246 -0
  138. data/lib/axlsx/workbook/worksheet/data_validations.rb +28 -0
  139. data/lib/axlsx/workbook/worksheet/date_time_converter.rb +30 -0
  140. data/lib/axlsx/workbook/worksheet/dimension.rb +64 -0
  141. data/lib/axlsx/workbook/worksheet/header_footer.rb +52 -0
  142. data/lib/axlsx/workbook/worksheet/icon_set.rb +81 -0
  143. data/lib/axlsx/workbook/worksheet/merged_cells.rb +35 -0
  144. data/lib/axlsx/workbook/worksheet/page_margins.rb +97 -0
  145. data/lib/axlsx/workbook/worksheet/page_set_up_pr.rb +44 -0
  146. data/lib/axlsx/workbook/worksheet/page_setup.rb +240 -0
  147. data/lib/axlsx/workbook/worksheet/pane.rb +139 -0
  148. data/lib/axlsx/workbook/worksheet/pivot_table.rb +266 -0
  149. data/lib/axlsx/workbook/worksheet/pivot_table_cache_definition.rb +66 -0
  150. data/lib/axlsx/workbook/worksheet/pivot_tables.rb +24 -0
  151. data/lib/axlsx/workbook/worksheet/print_options.rb +39 -0
  152. data/lib/axlsx/workbook/worksheet/protected_range.rb +47 -0
  153. data/lib/axlsx/workbook/worksheet/protected_ranges.rb +34 -0
  154. data/lib/axlsx/workbook/worksheet/rich_text.rb +35 -0
  155. data/lib/axlsx/workbook/worksheet/rich_text_run.rb +254 -0
  156. data/lib/axlsx/workbook/worksheet/row.rb +154 -0
  157. data/lib/axlsx/workbook/worksheet/row_breaks.rb +33 -0
  158. data/lib/axlsx/workbook/worksheet/selection.rb +101 -0
  159. data/lib/axlsx/workbook/worksheet/sheet_calc_pr.rb +29 -0
  160. data/lib/axlsx/workbook/worksheet/sheet_data.rb +27 -0
  161. data/lib/axlsx/workbook/worksheet/sheet_format_pr.rb +60 -0
  162. data/lib/axlsx/workbook/worksheet/sheet_pr.rb +79 -0
  163. data/lib/axlsx/workbook/worksheet/sheet_protection.rb +118 -0
  164. data/lib/axlsx/workbook/worksheet/sheet_view.rb +213 -0
  165. data/lib/axlsx/workbook/worksheet/table.rb +102 -0
  166. data/lib/axlsx/workbook/worksheet/table_style_info.rb +49 -0
  167. data/lib/axlsx/workbook/worksheet/tables.rb +31 -0
  168. data/lib/axlsx/workbook/worksheet/worksheet.rb +798 -0
  169. data/lib/axlsx/workbook/worksheet/worksheet_comments.rb +58 -0
  170. data/lib/axlsx/workbook/worksheet/worksheet_drawing.rb +58 -0
  171. data/lib/axlsx/workbook/worksheet/worksheet_hyperlink.rb +74 -0
  172. data/lib/axlsx/workbook/worksheet/worksheet_hyperlinks.rb +38 -0
  173. data/lib/dg-axlsx.rb +1 -0
  174. data/lib/schema/dc.xsd +118 -0
  175. data/lib/schema/dcmitype.xsd +52 -0
  176. data/lib/schema/dcterms.xsd +331 -0
  177. data/lib/schema/dml-chart.xsd +1499 -0
  178. data/lib/schema/dml-chartDrawing.xsd +146 -0
  179. data/lib/schema/dml-compatibility.xsd +14 -0
  180. data/lib/schema/dml-diagram.xsd +1091 -0
  181. data/lib/schema/dml-lockedCanvas.xsd +11 -0
  182. data/lib/schema/dml-main.xsd +3048 -0
  183. data/lib/schema/dml-picture.xsd +23 -0
  184. data/lib/schema/dml-spreadsheetDrawing.xsd +185 -0
  185. data/lib/schema/dml-wordprocessingDrawing.xsd +185 -0
  186. data/lib/schema/opc-contentTypes.xsd +42 -0
  187. data/lib/schema/opc-coreProperties.xsd +54 -0
  188. data/lib/schema/opc-digSig.xsd +49 -0
  189. data/lib/schema/opc-relationships.xsd +33 -0
  190. data/lib/schema/pml.xsd +1676 -0
  191. data/lib/schema/shared-additionalCharacteristics.xsd +28 -0
  192. data/lib/schema/shared-bibliography.xsd +144 -0
  193. data/lib/schema/shared-commonSimpleTypes.xsd +166 -0
  194. data/lib/schema/shared-customXmlDataProperties.xsd +25 -0
  195. data/lib/schema/shared-customXmlSchemaProperties.xsd +18 -0
  196. data/lib/schema/shared-documentPropertiesCustom.xsd +59 -0
  197. data/lib/schema/shared-documentPropertiesExtended.xsd +56 -0
  198. data/lib/schema/shared-documentPropertiesVariantTypes.xsd +195 -0
  199. data/lib/schema/shared-math.xsd +582 -0
  200. data/lib/schema/shared-relationshipReference.xsd +25 -0
  201. data/lib/schema/sml.xsd +4434 -0
  202. data/lib/schema/vml-main.xsd +569 -0
  203. data/lib/schema/vml-officeDrawing.xsd +509 -0
  204. data/lib/schema/vml-presentationDrawing.xsd +12 -0
  205. data/lib/schema/vml-spreadsheetDrawing.xsd +108 -0
  206. data/lib/schema/vml-wordprocessingDrawing.xsd +96 -0
  207. data/lib/schema/wml.xsd +3644 -0
  208. data/lib/schema/xml.xsd +116 -0
  209. data/test/benchmark.rb +72 -0
  210. data/test/content_type/tc_content_type.rb +76 -0
  211. data/test/content_type/tc_default.rb +16 -0
  212. data/test/content_type/tc_override.rb +14 -0
  213. data/test/doc_props/tc_app.rb +43 -0
  214. data/test/doc_props/tc_core.rb +42 -0
  215. data/test/drawing/tc_axes.rb +8 -0
  216. data/test/drawing/tc_axis.rb +112 -0
  217. data/test/drawing/tc_bar_3D_chart.rb +71 -0
  218. data/test/drawing/tc_bar_series.rb +37 -0
  219. data/test/drawing/tc_bubble_chart.rb +44 -0
  220. data/test/drawing/tc_bubble_series.rb +21 -0
  221. data/test/drawing/tc_cat_axis.rb +31 -0
  222. data/test/drawing/tc_cat_axis_data.rb +27 -0
  223. data/test/drawing/tc_chart.rb +110 -0
  224. data/test/drawing/tc_d_lbls.rb +57 -0
  225. data/test/drawing/tc_data_source.rb +23 -0
  226. data/test/drawing/tc_drawing.rb +80 -0
  227. data/test/drawing/tc_graphic_frame.rb +27 -0
  228. data/test/drawing/tc_hyperlink.rb +64 -0
  229. data/test/drawing/tc_line_3d_chart.rb +47 -0
  230. data/test/drawing/tc_line_chart.rb +39 -0
  231. data/test/drawing/tc_line_series.rb +71 -0
  232. data/test/drawing/tc_marker.rb +44 -0
  233. data/test/drawing/tc_named_axis_data.rb +27 -0
  234. data/test/drawing/tc_num_data.rb +31 -0
  235. data/test/drawing/tc_num_val.rb +29 -0
  236. data/test/drawing/tc_one_cell_anchor.rb +66 -0
  237. data/test/drawing/tc_pic.rb +106 -0
  238. data/test/drawing/tc_picture_locking.rb +72 -0
  239. data/test/drawing/tc_pie_3D_chart.rb +28 -0
  240. data/test/drawing/tc_pie_series.rb +32 -0
  241. data/test/drawing/tc_scaling.rb +36 -0
  242. data/test/drawing/tc_scatter_chart.rb +48 -0
  243. data/test/drawing/tc_scatter_series.rb +45 -0
  244. data/test/drawing/tc_ser_axis.rb +31 -0
  245. data/test/drawing/tc_series.rb +23 -0
  246. data/test/drawing/tc_series_title.rb +33 -0
  247. data/test/drawing/tc_str_data.rb +18 -0
  248. data/test/drawing/tc_str_val.rb +21 -0
  249. data/test/drawing/tc_title.rb +49 -0
  250. data/test/drawing/tc_two_cell_anchor.rb +36 -0
  251. data/test/drawing/tc_val_axis.rb +24 -0
  252. data/test/drawing/tc_view_3D.rb +54 -0
  253. data/test/drawing/tc_vml_drawing.rb +25 -0
  254. data/test/drawing/tc_vml_shape.rb +106 -0
  255. data/test/profile.rb +24 -0
  256. data/test/rels/tc_relationship.rb +44 -0
  257. data/test/rels/tc_relationships.rb +37 -0
  258. data/test/stylesheet/tc_border.rb +37 -0
  259. data/test/stylesheet/tc_border_pr.rb +32 -0
  260. data/test/stylesheet/tc_cell_alignment.rb +81 -0
  261. data/test/stylesheet/tc_cell_protection.rb +29 -0
  262. data/test/stylesheet/tc_cell_style.rb +57 -0
  263. data/test/stylesheet/tc_color.rb +43 -0
  264. data/test/stylesheet/tc_dxf.rb +81 -0
  265. data/test/stylesheet/tc_fill.rb +18 -0
  266. data/test/stylesheet/tc_font.rb +121 -0
  267. data/test/stylesheet/tc_gradient_fill.rb +72 -0
  268. data/test/stylesheet/tc_gradient_stop.rb +31 -0
  269. data/test/stylesheet/tc_num_fmt.rb +30 -0
  270. data/test/stylesheet/tc_pattern_fill.rb +43 -0
  271. data/test/stylesheet/tc_styles.rb +235 -0
  272. data/test/stylesheet/tc_table_style.rb +44 -0
  273. data/test/stylesheet/tc_table_style_element.rb +45 -0
  274. data/test/stylesheet/tc_table_styles.rb +29 -0
  275. data/test/stylesheet/tc_xf.rb +120 -0
  276. data/test/tc_axlsx.rb +72 -0
  277. data/test/tc_helper.rb +10 -0
  278. data/test/tc_package.rb +233 -0
  279. data/test/util/tc_mime_type_utils.rb +13 -0
  280. data/test/util/tc_serialized_attributes.rb +19 -0
  281. data/test/util/tc_simple_typed_list.rb +77 -0
  282. data/test/util/tc_validators.rb +210 -0
  283. data/test/workbook/tc_defined_name.rb +49 -0
  284. data/test/workbook/tc_shared_strings_table.rb +58 -0
  285. data/test/workbook/tc_workbook.rb +139 -0
  286. data/test/workbook/tc_workbook_view.rb +50 -0
  287. data/test/workbook/worksheet/auto_filter/tc_auto_filter.rb +38 -0
  288. data/test/workbook/worksheet/auto_filter/tc_filter_column.rb +76 -0
  289. data/test/workbook/worksheet/auto_filter/tc_filters.rb +50 -0
  290. data/test/workbook/worksheet/tc_break.rb +49 -0
  291. data/test/workbook/worksheet/tc_cell.rb +358 -0
  292. data/test/workbook/worksheet/tc_cfvo.rb +31 -0
  293. data/test/workbook/worksheet/tc_col.rb +78 -0
  294. data/test/workbook/worksheet/tc_color_scale.rb +58 -0
  295. data/test/workbook/worksheet/tc_comment.rb +72 -0
  296. data/test/workbook/worksheet/tc_comments.rb +57 -0
  297. data/test/workbook/worksheet/tc_conditional_formatting.rb +224 -0
  298. data/test/workbook/worksheet/tc_data_bar.rb +46 -0
  299. data/test/workbook/worksheet/tc_data_validation.rb +265 -0
  300. data/test/workbook/worksheet/tc_date_time_converter.rb +124 -0
  301. data/test/workbook/worksheet/tc_header_footer.rb +151 -0
  302. data/test/workbook/worksheet/tc_icon_set.rb +45 -0
  303. data/test/workbook/worksheet/tc_page_margins.rb +97 -0
  304. data/test/workbook/worksheet/tc_page_set_up_pr.rb +15 -0
  305. data/test/workbook/worksheet/tc_page_setup.rb +143 -0
  306. data/test/workbook/worksheet/tc_pane.rb +54 -0
  307. data/test/workbook/worksheet/tc_pivot_table.rb +120 -0
  308. data/test/workbook/worksheet/tc_pivot_table_cache_definition.rb +54 -0
  309. data/test/workbook/worksheet/tc_print_options.rb +72 -0
  310. data/test/workbook/worksheet/tc_protected_range.rb +17 -0
  311. data/test/workbook/worksheet/tc_rich_text.rb +44 -0
  312. data/test/workbook/worksheet/tc_rich_text_run.rb +172 -0
  313. data/test/workbook/worksheet/tc_row.rb +117 -0
  314. data/test/workbook/worksheet/tc_selection.rb +55 -0
  315. data/test/workbook/worksheet/tc_sheet_calc_pr.rb +18 -0
  316. data/test/workbook/worksheet/tc_sheet_format_pr.rb +88 -0
  317. data/test/workbook/worksheet/tc_sheet_pr.rb +49 -0
  318. data/test/workbook/worksheet/tc_sheet_protection.rb +117 -0
  319. data/test/workbook/worksheet/tc_sheet_view.rb +214 -0
  320. data/test/workbook/worksheet/tc_table.rb +68 -0
  321. data/test/workbook/worksheet/tc_table_style_info.rb +53 -0
  322. data/test/workbook/worksheet/tc_worksheet.rb +577 -0
  323. data/test/workbook/worksheet/tc_worksheet_hyperlink.rb +55 -0
  324. metadata +583 -0
@@ -0,0 +1,35 @@
1
+ module Axlsx
2
+
3
+ # The Break class stores the details for row and column page breaks.
4
+ # @see RowBreaks, ColBreaks
5
+ class Break
6
+
7
+ include Axlsx::OptionsParser
8
+ include Axlsx::Accessors
9
+ include Axlsx::SerializedAttributes
10
+
11
+ # Creates a new Break object
12
+ # @param options A hash of attribute options for this break.
13
+ # @option options [Integer] id Zero-based row or column Id of the page break. Breaks occur above the specified row and left of the specified column.
14
+ # @option options [Integer] min Zero-based index of start row or column of the break. For row breaks, specifies column index; for column breaks, specifies row index.
15
+ # @option options [Integer] max Zero-based index of end row or column of the break. For row breaks, specifies column index; for column breaks, specifies row index.
16
+ # @option options [Boolean] man Manual Break flag. 1 means the break is a manually inserted break.
17
+ # @option option [Boolean] pt Flag indicating that a PivotTable created this break.
18
+ def initialize(options={})
19
+ parse_options options
20
+ yield self if block_given?
21
+ end
22
+
23
+ unsigned_int_attr_accessor :id, :min, :max
24
+
25
+ boolean_attr_accessor :man, :pt
26
+
27
+ serializable_attributes :id, :min, :max, :man, :pt
28
+
29
+ # serializes the break to xml
30
+ def to_xml_string(str='')
31
+ serialized_tag('brk', str)
32
+ end
33
+ end
34
+ end
35
+
@@ -0,0 +1,475 @@
1
+ # encoding: UTF-8
2
+ require 'cgi'
3
+ module Axlsx
4
+ # A cell in a worksheet.
5
+ # Cell stores inforamation requried to serialize a single worksheet cell to xml. You must provde the Row that the cell belongs to and the cells value. The data type will automatically be determed if you do not specify the :type option. The default style will be applied if you do not supply the :style option. Changing the cell's type will recast the value to the type specified. Altering the cell's value via the property accessor will also automatically cast the provided value to the cell's type.
6
+ # @note The recommended way to generate cells is via Worksheet#add_row
7
+ #
8
+ # @see Worksheet#add_row
9
+ class Cell
10
+
11
+ include Axlsx::OptionsParser
12
+
13
+ # @param [Row] row The row this cell belongs to.
14
+ # @param [Any] value The value associated with this cell.
15
+ # @option options [Symbol] type The intended data type for this cell. If not specified the data type will be determined internally based on the vlue provided.
16
+ # @option options [Integer] style The index of the cellXfs item to be applied to this cell. If not specified, the default style (0) will be applied.
17
+ # @option options [String] font_name
18
+ # @option options [Integer] charset
19
+ # @option options [String] family
20
+ # @option options [Boolean] b
21
+ # @option options [Boolean] i
22
+ # @option options [Boolean] strike
23
+ # @option options [Boolean] outline
24
+ # @option options [Boolean] shadow
25
+ # @option options [Boolean] condense
26
+ # @option options [Boolean] extend
27
+ # @option options [Boolean] u
28
+ # @option options [Symbol] vertAlign must be one of :baseline, :subscript, :superscript
29
+ # @option options [Integer] sz
30
+ # @option options [String] color an 8 letter rgb specification
31
+ # @option options [Number] formula_value The value to cache for a formula cell.
32
+ # @option options [Symbol] scheme must be one of :none, major, :minor
33
+ def initialize(row, value = nil, options = {})
34
+ @row = row
35
+ # Do not use instance vars if not needed to use less RAM
36
+ # And do not call parse_options on frequently used options
37
+ # to get less GC cycles
38
+ type = options.delete(:type) || cell_type_from_value(value)
39
+ self.type = type unless type == :string
40
+
41
+
42
+ val = options.delete(:style)
43
+ self.style = val unless val.nil? || val == 0
44
+ val = options.delete(:formula_value)
45
+ self.formula_value = val unless val.nil?
46
+
47
+ parse_options(options)
48
+
49
+ self.value = value
50
+ value.cell = self if contains_rich_text?
51
+ end
52
+
53
+ # this is the cached value for formula cells. If you want the values to render in iOS/Mac OSX preview
54
+ # you need to set this.
55
+ attr_accessor :formula_value
56
+
57
+ # An array of available inline styes.
58
+ # TODO change this to a hash where each key defines attr name and validator (and any info the validator requires)
59
+ # then move it out to a module so we can re-use in in other classes.
60
+ # needs to define bla=(v) and bla methods on the class that hook into a
61
+ # set_attr method that kicks the suplied validator and updates the instance_variable
62
+ # for the key
63
+ INLINE_STYLES = [:value, :type, :font_name, :charset,
64
+ :family, :b, :i, :strike, :outline,
65
+ :shadow, :condense, :extend, :u,
66
+ :vertAlign, :sz, :color, :scheme].freeze
67
+
68
+ CELL_TYPES = [:date, :time, :float, :integer, :richtext,
69
+ :string, :boolean, :iso_8601].freeze
70
+
71
+ # The index of the cellXfs item to be applied to this cell.
72
+ # @return [Integer]
73
+ # @see Axlsx::Styles
74
+ def style
75
+ defined?(@style) ? @style : 0
76
+ end
77
+
78
+ # The row this cell belongs to.
79
+ # @return [Row]
80
+ attr_reader :row
81
+
82
+ # The cell's data type. Currently only six types are supported, :date, :time, :float, :integer, :string and :boolean.
83
+ # Changing the type for a cell will recast the value into that type. If no type option is specified in the constructor, the type is
84
+ # automatically determed.
85
+ # @see Cell#cell_type_from_value
86
+ # @return [Symbol] The type of data this cell's value is cast to.
87
+ # @raise [ArgumentExeption] Cell.type must be one of [:date, time, :float, :integer, :string, :boolean]
88
+ # @note
89
+ # If the value provided cannot be cast into the type specified, type is changed to :string and the following logic is applied.
90
+ # :string to :integer or :float, type conversions always return 0 or 0.0
91
+ # :string, :integer, or :float to :time conversions always return the original value as a string and set the cells type to :string.
92
+ # No support is currently implemented for parsing time strings.
93
+ def type
94
+ defined?(@type) ? @type : :string
95
+ end
96
+
97
+ # @see type
98
+ def type=(v)
99
+ RestrictionValidator.validate :cell_type, CELL_TYPES, v
100
+ @type = v
101
+ self.value = @value unless !defined?(@value) || @value.nil?
102
+ end
103
+
104
+ # The value of this cell.
105
+ # @return [String, Integer, Float, Time, Boolean] casted value based on cell's type attribute.
106
+ attr_reader :value
107
+
108
+ # @see value
109
+ def value=(v)
110
+ #TODO: consider doing value based type determination first?
111
+ @value = cast_value(v)
112
+ end
113
+
114
+ # Indicates that the cell has one or more of the custom cell styles applied.
115
+ # @return [Boolean]
116
+ def is_text_run?
117
+ defined?(@is_text_run) && @is_text_run && !contains_rich_text?
118
+ end
119
+
120
+ def contains_rich_text?
121
+ type == :richtext
122
+ end
123
+
124
+ # Indicates if the cell is good for shared string table
125
+ def plain_string?
126
+ type == :string && # String typed
127
+ !is_text_run? && # No inline styles
128
+ !@value.nil? && # Not nil
129
+ !@value.empty? && # Not empty
130
+ !@value.start_with?(?=) # Not a formula
131
+ end
132
+
133
+ # The inline font_name property for the cell
134
+ # @return [String]
135
+ attr_reader :font_name
136
+ # @see font_name
137
+ def font_name=(v) set_run_style :validate_string, :font_name, v; end
138
+
139
+ # The inline charset property for the cell
140
+ # As far as I can tell, this is pretty much ignored. However, based on the spec it should be one of the following:
141
+ # 0  ANSI_CHARSET
142
+ # 1 DEFAULT_CHARSET
143
+ # 2 SYMBOL_CHARSET
144
+ # 77 MAC_CHARSET
145
+ # 128 SHIFTJIS_CHARSET
146
+ # 129  HANGUL_CHARSET
147
+ # 130  JOHAB_CHARSET
148
+ # 134  GB2312_CHARSET
149
+ # 136  CHINESEBIG5_CHARSET
150
+ # 161  GREEK_CHARSET
151
+ # 162  TURKISH_CHARSET
152
+ # 163  VIETNAMESE_CHARSET
153
+ # 177  HEBREW_CHARSET
154
+ # 178  ARABIC_CHARSET
155
+ # 186  BALTIC_CHARSET
156
+ # 204  RUSSIAN_CHARSET
157
+ # 222  THAI_CHARSET
158
+ # 238  EASTEUROPE_CHARSET
159
+ # 255  OEM_CHARSET
160
+ # @return [String]
161
+ attr_reader :charset
162
+ # @see charset
163
+ def charset=(v) set_run_style :validate_unsigned_int, :charset, v; end
164
+
165
+ # The inline family property for the cell
166
+ # @return [Integer]
167
+ # 1 Roman
168
+ # 2 Swiss
169
+ # 3 Modern
170
+ # 4 Script
171
+ # 5 Decorative
172
+ attr_reader :family
173
+ # @see family
174
+ def family=(v)
175
+ set_run_style :validate_family, :family, v.to_i
176
+ end
177
+
178
+ # The inline bold property for the cell
179
+ # @return [Boolean]
180
+ attr_reader :b
181
+ # @see b
182
+ def b=(v) set_run_style :validate_boolean, :b, v; end
183
+
184
+ # The inline italic property for the cell
185
+ # @return [Boolean]
186
+ attr_reader :i
187
+ # @see i
188
+ def i=(v) set_run_style :validate_boolean, :i, v; end
189
+
190
+ # The inline strike property for the cell
191
+ # @return [Boolean]
192
+ attr_reader :strike
193
+ # @see strike
194
+ def strike=(v) set_run_style :validate_boolean, :strike, v; end
195
+
196
+ # The inline outline property for the cell
197
+ # @return [Boolean]
198
+ attr_reader :outline
199
+ # @see outline
200
+ def outline=(v) set_run_style :validate_boolean, :outline, v; end
201
+
202
+ # The inline shadow property for the cell
203
+ # @return [Boolean]
204
+ attr_reader :shadow
205
+ # @see shadow
206
+ def shadow=(v) set_run_style :validate_boolean, :shadow, v; end
207
+
208
+ # The inline condense property for the cell
209
+ # @return [Boolean]
210
+ attr_reader :condense
211
+ # @see condense
212
+ def condense=(v) set_run_style :validate_boolean, :condense, v; end
213
+
214
+ # The inline extend property for the cell
215
+ # @return [Boolean]
216
+ attr_reader :extend
217
+ # @see extend
218
+ def extend=(v) set_run_style :validate_boolean, :extend, v; end
219
+
220
+ # The inline underline property for the cell.
221
+ # It must be one of :none, :single, :double, :singleAccounting, :doubleAccounting, true
222
+ # @return [Boolean]
223
+ # @return [String]
224
+ # @note true is for backwards compatability and is reassigned to :single
225
+ attr_reader :u
226
+ # @see u
227
+ def u=(v)
228
+ v = :single if (v == true || v == 1 || v == :true || v == 'true')
229
+ set_run_style :validate_cell_u, :u, v
230
+ end
231
+
232
+ # The inline color property for the cell
233
+ # @return [Color]
234
+ attr_reader :color
235
+ # @param [String] v The 8 character representation for an rgb color #FFFFFFFF"
236
+ def color=(v)
237
+ @color = v.is_a?(Color) ? v : Color.new(:rgb=>v)
238
+ @is_text_run = true
239
+ end
240
+
241
+ # The inline sz property for the cell
242
+ # @return [Inteter]
243
+ attr_reader :sz
244
+ # @see sz
245
+ def sz=(v) set_run_style :validate_unsigned_int, :sz, v; end
246
+
247
+ # The inline vertical alignment property for the cell
248
+ # this must be one of [:baseline, :subscript, :superscript]
249
+ # @return [Symbol]
250
+ attr_reader :vertAlign
251
+ # @see vertAlign
252
+ def vertAlign=(v)
253
+ RestrictionValidator.validate :cell_vertAlign, [:baseline, :subscript, :superscript], v
254
+ set_run_style nil, :vertAlign, v
255
+ end
256
+
257
+ # The inline scheme property for the cell
258
+ # this must be one of [:none, major, minor]
259
+ # @return [Symbol]
260
+ attr_reader :scheme
261
+ # @see scheme
262
+ def scheme=(v)
263
+ RestrictionValidator.validate :cell_scheme, [:none, :major, :minor], v
264
+ set_run_style nil, :scheme, v
265
+ end
266
+
267
+ # The Shared Strings Table index for this cell
268
+ # @return [Integer]
269
+ attr_reader :ssti
270
+
271
+ # @return [Integer] The index of the cell in the containing row.
272
+ def index
273
+ @row.index(self)
274
+ end
275
+
276
+ # @return [String] The alpha(column)numeric(row) reference for this sell.
277
+ # @example Relative Cell Reference
278
+ # ws.rows.first.cells.first.r #=> "A1"
279
+ def r
280
+ Axlsx::cell_r index, @row.row_index
281
+ end
282
+
283
+ # @return [String] The absolute alpha(column)numeric(row) reference for this sell.
284
+ # @example Absolute Cell Reference
285
+ # ws.rows.first.cells.first.r #=> "$A$1"
286
+ def r_abs
287
+ "$#{r.match(%r{([A-Z]+)([0-9]+)})[1,2].join('$')}"
288
+ end
289
+
290
+ # @return [Integer] The cellXfs item index applied to this cell.
291
+ # @raise [ArgumentError] Invalid cellXfs id if the value provided is not within cellXfs items range.
292
+ def style=(v)
293
+ Axlsx::validate_unsigned_int(v)
294
+ count = styles.cellXfs.size
295
+ raise ArgumentError, "Invalid cellXfs id" unless v < count
296
+ @style = v
297
+ end
298
+
299
+ # @return [Array] of x/y coordinates in the sheet for this cell.
300
+ def pos
301
+ [index, row.row_index]
302
+ end
303
+
304
+ # Merges all the cells in a range created between this cell and the cell or string name for a cell provided
305
+ # @see worksheet.merge_cells
306
+ # @param [Cell, String] target The last cell, or str ref for the cell in the merge range
307
+ def merge(target)
308
+ start, stop = if target.is_a?(String)
309
+ [self.r, target]
310
+ elsif(target.is_a?(Cell))
311
+ Axlsx.sort_cells([self, target]).map { |c| c.r }
312
+ end
313
+ self.row.worksheet.merge_cells "#{start}:#{stop}" unless stop.nil?
314
+ end
315
+
316
+ # Serializes the cell
317
+ # @param [Integer] r_index The row index for the cell
318
+ # @param [Integer] c_index The cell index in the row.
319
+ # @param [String] str The string index the cell content will be appended to. Defaults to empty string.
320
+ # @return [String] xml text for the cell
321
+ def to_xml_string(r_index, c_index, str = '')
322
+ CellSerializer.to_xml_string r_index, c_index, self, str
323
+ end
324
+
325
+ def is_formula?
326
+ type == :string && @value.to_s.start_with?(?=)
327
+ end
328
+
329
+ def is_array_formula?
330
+ type == :string && @value.to_s.start_with?('{=') && @value.to_s.end_with?('}')
331
+ end
332
+
333
+ # returns the absolute or relative string style reference for
334
+ # this cell.
335
+ # @param [Boolean] absolute -when false a relative reference will be
336
+ # returned.
337
+ # @return [String]
338
+ def reference(absolute=true)
339
+ absolute ? r_abs : r
340
+ end
341
+
342
+ # Creates a defined name in the workbook for this cell.
343
+ def name=(label)
344
+ row.worksheet.workbook.add_defined_name "#{row.worksheet.name}!#{r_abs}", name: label
345
+ @name = label
346
+ end
347
+
348
+ # returns the name of the cell
349
+ attr_reader :name
350
+
351
+ def autowidth
352
+ return if is_formula? || value.nil?
353
+ if contains_rich_text?
354
+ string_width('', font_size) + value.autowidth
355
+ elsif styles.cellXfs[style].alignment && styles.cellXfs[style].alignment.wrap_text
356
+ max_width = 0
357
+ value.to_s.split(/\r?\n/).each do |line|
358
+ width = string_width(line, font_size)
359
+ max_width = width if width > max_width
360
+ end
361
+ max_width
362
+ else
363
+ string_width(value, font_size)
364
+ end
365
+ end
366
+
367
+ # Returns the sanatized value
368
+ # TODO find a better way to do this as it accounts for 30% of
369
+ # processing time in benchmarking...
370
+ def clean_value
371
+ if type == :string && !Axlsx::trust_input
372
+ Axlsx::sanitize(::CGI.escapeHTML(@value.to_s))
373
+ else
374
+ @value.to_s
375
+ end
376
+ end
377
+
378
+ private
379
+
380
+ def styles
381
+ row.worksheet.styles
382
+ end
383
+
384
+ # Returns the width of a string according to the current style
385
+ # This is still not perfect...
386
+ # - scaling is not linear as font sizes increase
387
+ def string_width(string, font_size)
388
+ font_scale = font_size / 10.0
389
+ (string.to_s.count(Worksheet::THIN_CHARS) + 3.0) * (font_size/10.0)
390
+ end
391
+
392
+ # we scale the font size if bold style is applied to either the style font or
393
+ # the cell itself. Yes, it is a bit of a hack, but it is much better than using
394
+ # imagemagick and loading metrics for every character.
395
+ def font_size
396
+ return sz if sz
397
+ font = styles.fonts[styles.cellXfs[style].fontId] || styles.fonts[0]
398
+ (font.b || (defined?(@b) && @b)) ? (font.sz * 1.5) : font.sz
399
+ end
400
+
401
+ # Utility method for setting inline style attributes
402
+ def set_run_style(validator, attr, value)
403
+ return unless INLINE_STYLES.include?(attr.to_sym)
404
+ Axlsx.send(validator, value) unless validator.nil?
405
+ self.instance_variable_set :"@#{attr.to_s}", value
406
+ @is_text_run = true
407
+ end
408
+
409
+ # @see ssti
410
+ def ssti=(v)
411
+ Axlsx::validate_unsigned_int(v)
412
+ @ssti = v
413
+ end
414
+
415
+ # Determines the cell type based on the cell value.
416
+ # @note This is only used when a cell is created but no :type option is specified, the following rules apply:
417
+ # 1. If the value is an instance of Date, the type is set to :date
418
+ # 2. If the value is an instance of Time, the type is set to :time
419
+ # 3. If the value is an instance of TrueClass or FalseClass, the type is set to :boolean
420
+ # 4. :float and :integer types are determined by regular expression matching.
421
+ # 5. Anything that does not meet either of the above is determined to be :string.
422
+ # @return [Symbol] The determined type
423
+ def cell_type_from_value(v)
424
+ if v.is_a?(Date)
425
+ :date
426
+ elsif v.is_a?(Time)
427
+ :time
428
+ elsif v.is_a?(TrueClass) || v.is_a?(FalseClass)
429
+ :boolean
430
+ elsif v.to_s =~ Axlsx::NUMERIC_REGEX
431
+ :integer
432
+ elsif v.to_s =~ Axlsx::FLOAT_REGEX
433
+ :float
434
+ elsif v.to_s =~ Axlsx::ISO_8601_REGEX
435
+ :iso_8601
436
+ elsif v.is_a? RichText
437
+ :richtext
438
+ else
439
+ :string
440
+ end
441
+ end
442
+
443
+ # Cast the value into this cells data type.
444
+ # @note
445
+ # About Time - Time in OOXML is *different* from what you might expect. The history as to why is interesting, but you can safely assume that if you are generating docs on a mac, you will want to specify Workbook.1904 as true when using time typed values.
446
+ # @see Axlsx#date1904
447
+ def cast_value(v)
448
+ return v if v.is_a?(RichText) || v.nil?
449
+ case type
450
+ when :date
451
+ self.style = STYLE_DATE if self.style == 0
452
+ v
453
+ when :time
454
+ self.style = STYLE_DATE if self.style == 0
455
+ if !v.is_a?(Time) && v.respond_to?(:to_time)
456
+ v.to_time
457
+ else
458
+ v
459
+ end
460
+ when :float
461
+ v.to_f
462
+ when :integer
463
+ v.to_i
464
+ when :boolean
465
+ v ? 1 : 0
466
+ when :iso_8601
467
+ #consumer is responsible for ensuring the iso_8601 format when specifying this type
468
+ v
469
+ else
470
+ v.to_s
471
+ end
472
+ end
473
+
474
+ end
475
+ end