caxlsx 3.0.4 → 3.2.0

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