caxlsx 3.2.0 → 3.4.0

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