caxlsx 3.2.0 → 3.3.0

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