caxlsx 3.3.0 → 4.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (155) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +87 -11
  3. data/README.md +43 -16
  4. data/Rakefile +5 -11
  5. data/examples/generate.rb +3 -1
  6. data/lib/axlsx/content_type/abstract_content_type.rb +15 -10
  7. data/lib/axlsx/content_type/content_type.rb +8 -10
  8. data/lib/axlsx/content_type/default.rb +7 -6
  9. data/lib/axlsx/content_type/override.rb +6 -5
  10. data/lib/axlsx/doc_props/app.rb +96 -32
  11. data/lib/axlsx/doc_props/core.rb +9 -12
  12. data/lib/axlsx/drawing/area_chart.rb +16 -17
  13. data/lib/axlsx/drawing/area_series.rb +23 -18
  14. data/lib/axlsx/drawing/ax_data_source.rb +4 -7
  15. data/lib/axlsx/drawing/axes.rb +11 -10
  16. data/lib/axlsx/drawing/axis.rb +83 -36
  17. data/lib/axlsx/drawing/bar_3D_chart.rb +30 -21
  18. data/lib/axlsx/drawing/bar_chart.rb +29 -20
  19. data/lib/axlsx/drawing/bar_series.rb +23 -18
  20. data/lib/axlsx/drawing/bubble_chart.rb +9 -10
  21. data/lib/axlsx/drawing/bubble_series.rb +11 -12
  22. data/lib/axlsx/drawing/cat_axis.rb +38 -28
  23. data/lib/axlsx/drawing/chart.rb +52 -30
  24. data/lib/axlsx/drawing/d_lbls.rb +36 -32
  25. data/lib/axlsx/drawing/drawing.rb +61 -62
  26. data/lib/axlsx/drawing/graphic_frame.rb +6 -7
  27. data/lib/axlsx/drawing/hyperlink.rb +22 -14
  28. data/lib/axlsx/drawing/line_3D_chart.rb +15 -17
  29. data/lib/axlsx/drawing/line_chart.rb +16 -17
  30. data/lib/axlsx/drawing/line_series.rb +23 -18
  31. data/lib/axlsx/drawing/marker.rb +29 -16
  32. data/lib/axlsx/drawing/num_data.rb +13 -16
  33. data/lib/axlsx/drawing/num_data_source.rb +11 -13
  34. data/lib/axlsx/drawing/num_val.rb +10 -11
  35. data/lib/axlsx/drawing/one_cell_anchor.rb +16 -10
  36. data/lib/axlsx/drawing/pic.rb +68 -24
  37. data/lib/axlsx/drawing/picture_locking.rb +6 -7
  38. data/lib/axlsx/drawing/pie_3D_chart.rb +9 -12
  39. data/lib/axlsx/drawing/pie_chart.rb +36 -0
  40. data/lib/axlsx/drawing/pie_series.rb +26 -17
  41. data/lib/axlsx/drawing/scaling.rb +26 -13
  42. data/lib/axlsx/drawing/scatter_chart.rb +10 -11
  43. data/lib/axlsx/drawing/scatter_series.rb +17 -18
  44. data/lib/axlsx/drawing/ser_axis.rb +22 -19
  45. data/lib/axlsx/drawing/series.rb +14 -8
  46. data/lib/axlsx/drawing/series_title.rb +6 -6
  47. data/lib/axlsx/drawing/str_data.rb +10 -13
  48. data/lib/axlsx/drawing/str_val.rb +8 -9
  49. data/lib/axlsx/drawing/title.rb +23 -27
  50. data/lib/axlsx/drawing/two_cell_anchor.rb +9 -9
  51. data/lib/axlsx/drawing/val_axis.rb +7 -8
  52. data/lib/axlsx/drawing/view_3D.rb +38 -30
  53. data/lib/axlsx/drawing/vml_drawing.rb +19 -20
  54. data/lib/axlsx/drawing/vml_shape.rb +25 -26
  55. data/lib/axlsx/package.rb +134 -99
  56. data/lib/axlsx/rels/relationship.rb +42 -30
  57. data/lib/axlsx/rels/relationships.rb +9 -10
  58. data/lib/axlsx/stylesheet/border.rb +16 -8
  59. data/lib/axlsx/stylesheet/border_pr.rb +24 -12
  60. data/lib/axlsx/stylesheet/cell_alignment.rb +44 -21
  61. data/lib/axlsx/stylesheet/cell_protection.rb +13 -7
  62. data/lib/axlsx/stylesheet/cell_style.rb +36 -15
  63. data/lib/axlsx/stylesheet/color.rb +23 -14
  64. data/lib/axlsx/stylesheet/dxf.rb +36 -15
  65. data/lib/axlsx/stylesheet/fill.rb +7 -6
  66. data/lib/axlsx/stylesheet/font.rb +71 -22
  67. data/lib/axlsx/stylesheet/gradient_fill.rb +16 -10
  68. data/lib/axlsx/stylesheet/gradient_stop.rb +14 -6
  69. data/lib/axlsx/stylesheet/num_fmt.rb +15 -14
  70. data/lib/axlsx/stylesheet/pattern_fill.rb +20 -8
  71. data/lib/axlsx/stylesheet/styles.rb +167 -118
  72. data/lib/axlsx/stylesheet/table_style.rb +20 -10
  73. data/lib/axlsx/stylesheet/table_style_element.rb +16 -8
  74. data/lib/axlsx/stylesheet/table_styles.rb +16 -11
  75. data/lib/axlsx/stylesheet/theme.rb +163 -0
  76. data/lib/axlsx/stylesheet/xf.rb +78 -25
  77. data/lib/axlsx/util/accessors.rb +13 -11
  78. data/lib/axlsx/util/buffered_zip_output_stream.rb +64 -0
  79. data/lib/axlsx/util/constants.rb +133 -110
  80. data/lib/axlsx/util/mime_type_utils.rb +75 -5
  81. data/lib/axlsx/util/options_parser.rb +4 -3
  82. data/lib/axlsx/util/serialized_attributes.rb +47 -23
  83. data/lib/axlsx/util/simple_typed_list.rb +77 -64
  84. data/lib/axlsx/util/storage.rb +38 -41
  85. data/lib/axlsx/util/uri_utils.rb +70 -0
  86. data/lib/axlsx/util/validators.rb +111 -48
  87. data/lib/axlsx/util/zip_command.rb +10 -12
  88. data/lib/axlsx/version.rb +3 -2
  89. data/lib/axlsx/workbook/defined_name.rb +13 -9
  90. data/lib/axlsx/workbook/defined_names.rb +5 -4
  91. data/lib/axlsx/workbook/shared_strings_table.rb +13 -14
  92. data/lib/axlsx/workbook/workbook.rb +152 -120
  93. data/lib/axlsx/workbook/workbook_view.rb +9 -12
  94. data/lib/axlsx/workbook/workbook_views.rb +5 -5
  95. data/lib/axlsx/workbook/worksheet/auto_filter/auto_filter.rb +87 -15
  96. data/lib/axlsx/workbook/worksheet/auto_filter/filter_column.rb +16 -10
  97. data/lib/axlsx/workbook/worksheet/auto_filter/filters.rb +28 -25
  98. data/lib/axlsx/workbook/worksheet/auto_filter/sort_condition.rb +51 -0
  99. data/lib/axlsx/workbook/worksheet/auto_filter/sort_state.rb +54 -0
  100. data/lib/axlsx/workbook/worksheet/border_creator.rb +34 -29
  101. data/lib/axlsx/workbook/worksheet/break.rb +4 -5
  102. data/lib/axlsx/workbook/worksheet/cell.rb +125 -78
  103. data/lib/axlsx/workbook/worksheet/cell_serializer.rb +33 -29
  104. data/lib/axlsx/workbook/worksheet/cfvo.rb +13 -5
  105. data/lib/axlsx/workbook/worksheet/cfvos.rb +5 -5
  106. data/lib/axlsx/workbook/worksheet/col.rb +32 -19
  107. data/lib/axlsx/workbook/worksheet/col_breaks.rb +9 -8
  108. data/lib/axlsx/workbook/worksheet/color_scale.rb +16 -16
  109. data/lib/axlsx/workbook/worksheet/cols.rb +10 -8
  110. data/lib/axlsx/workbook/worksheet/comment.rb +14 -13
  111. data/lib/axlsx/workbook/worksheet/comments.rb +11 -13
  112. data/lib/axlsx/workbook/worksheet/conditional_formatting.rb +19 -9
  113. data/lib/axlsx/workbook/worksheet/conditional_formatting_rule.rb +75 -22
  114. data/lib/axlsx/workbook/worksheet/conditional_formattings.rb +8 -8
  115. data/lib/axlsx/workbook/worksheet/data_bar.rb +29 -30
  116. data/lib/axlsx/workbook/worksheet/data_validation.rb +73 -33
  117. data/lib/axlsx/workbook/worksheet/data_validations.rb +8 -9
  118. data/lib/axlsx/workbook/worksheet/date_time_converter.rb +8 -8
  119. data/lib/axlsx/workbook/worksheet/dimension.rb +10 -7
  120. data/lib/axlsx/workbook/worksheet/header_footer.rb +5 -4
  121. data/lib/axlsx/workbook/worksheet/icon_set.rb +40 -12
  122. data/lib/axlsx/workbook/worksheet/merged_cells.rb +11 -11
  123. data/lib/axlsx/workbook/worksheet/outline_pr.rb +7 -4
  124. data/lib/axlsx/workbook/worksheet/page_margins.rb +41 -13
  125. data/lib/axlsx/workbook/worksheet/page_set_up_pr.rb +7 -6
  126. data/lib/axlsx/workbook/worksheet/page_setup.rb +153 -131
  127. data/lib/axlsx/workbook/worksheet/pane.rb +34 -26
  128. data/lib/axlsx/workbook/worksheet/pivot_table.rb +78 -29
  129. data/lib/axlsx/workbook/worksheet/pivot_table_cache_definition.rb +12 -13
  130. data/lib/axlsx/workbook/worksheet/pivot_tables.rb +6 -5
  131. data/lib/axlsx/workbook/worksheet/print_options.rb +4 -2
  132. data/lib/axlsx/workbook/worksheet/protected_range.rb +6 -5
  133. data/lib/axlsx/workbook/worksheet/protected_ranges.rb +13 -11
  134. data/lib/axlsx/workbook/worksheet/rich_text.rb +6 -6
  135. data/lib/axlsx/workbook/worksheet/rich_text_run.rb +66 -28
  136. data/lib/axlsx/workbook/worksheet/row.rb +34 -23
  137. data/lib/axlsx/workbook/worksheet/row_breaks.rb +9 -8
  138. data/lib/axlsx/workbook/worksheet/selection.rb +22 -16
  139. data/lib/axlsx/workbook/worksheet/sheet_calc_pr.rb +11 -7
  140. data/lib/axlsx/workbook/worksheet/sheet_data.rb +7 -7
  141. data/lib/axlsx/workbook/worksheet/sheet_format_pr.rb +23 -19
  142. data/lib/axlsx/workbook/worksheet/sheet_pr.rb +12 -8
  143. data/lib/axlsx/workbook/worksheet/sheet_protection.rb +22 -21
  144. data/lib/axlsx/workbook/worksheet/sheet_view.rb +71 -55
  145. data/lib/axlsx/workbook/worksheet/table.rb +13 -13
  146. data/lib/axlsx/workbook/worksheet/table_style_info.rb +8 -6
  147. data/lib/axlsx/workbook/worksheet/tables.rb +8 -6
  148. data/lib/axlsx/workbook/worksheet/worksheet.rb +120 -79
  149. data/lib/axlsx/workbook/worksheet/worksheet_comments.rb +13 -11
  150. data/lib/axlsx/workbook/worksheet/worksheet_drawing.rb +11 -4
  151. data/lib/axlsx/workbook/worksheet/worksheet_hyperlink.rb +12 -10
  152. data/lib/axlsx/workbook/worksheet/worksheet_hyperlinks.rb +9 -7
  153. data/lib/axlsx.rb +120 -60
  154. data/lib/caxlsx.rb +3 -2
  155. metadata +42 -75
@@ -1,13 +1,12 @@
1
- # encoding: UTF-8
2
- require 'cgi'
1
+ # frozen_string_literal: true
2
+
3
3
  module Axlsx
4
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.
5
+ # Cell stores information required to serialize a single worksheet cell to xml. You must provide 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
6
  # @note The recommended way to generate cells is via Worksheet#add_row
7
7
  #
8
8
  # @see Worksheet#add_row
9
9
  class Cell
10
-
11
10
  include Axlsx::OptionsParser
12
11
 
13
12
  # @param [Row] row The row this cell belongs to.
@@ -30,10 +29,9 @@ module Axlsx
30
29
  # @option options [String] color an 8 letter rgb specification
31
30
  # @option options [Number] formula_value The value to cache for a formula cell.
32
31
  # @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).
32
+ # @option options [Boolean] escape_formulas Whether to treat values starting with an equals
33
+ # sign as formulas or as literal strings. Allowing user-generated data to be interpreted as
34
+ # formulas is a security risk. See https://www.owasp.org/index.php/CSV_Injection for details.
37
35
  def initialize(row, value = nil, options = {})
38
36
  @row = row
39
37
  # Do not use instance vars if not needed to use less RAM
@@ -42,15 +40,14 @@ module Axlsx
42
40
  type = options.delete(:type) || cell_type_from_value(value)
43
41
  self.type = type unless type == :string
44
42
 
45
- escape_formulas = options[:escape_formulas]
46
- self.escape_formulas = escape_formulas unless escape_formulas.nil?
47
-
48
43
  val = options.delete(:style)
49
44
  self.style = val unless val.nil? || val == 0
50
45
  val = options.delete(:formula_value)
51
46
  self.formula_value = val unless val.nil?
47
+ val = options.delete(:escape_formulas)
48
+ self.escape_formulas = val unless val.nil?
52
49
 
53
- parse_options(options)
50
+ parse_options(options) unless options.empty?
54
51
 
55
52
  self.value = value
56
53
  value.cell = self if contains_rich_text?
@@ -60,11 +57,11 @@ module Axlsx
60
57
  # you need to set this.
61
58
  attr_accessor :formula_value
62
59
 
63
- # An array of available inline styes.
60
+ # An array of available inline styles.
64
61
  # 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.
62
+ # then move it out to a module so we can reuse in in other classes.
66
63
  # 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
64
+ # set_attr method that kicks the supplied validator and updates the instance_variable
68
65
  # for the key
69
66
  INLINE_STYLES = [:value, :type, :font_name, :charset,
70
67
  :family, :b, :i, :strike, :outline,
@@ -75,6 +72,9 @@ module Axlsx
75
72
  CELL_TYPES = [:date, :time, :float, :integer, :richtext,
76
73
  :string, :boolean, :iso_8601, :text].freeze
77
74
 
75
+ # A regular expression to match the alpha(column)numeric(row) reference of a cell
76
+ CELL_REFERENCE_REGEX = /([A-Z]+)([0-9]+)/.freeze
77
+
78
78
  # The index of the cellXfs item to be applied to this cell.
79
79
  # @return [Integer]
80
80
  # @see Axlsx::Styles
@@ -82,10 +82,15 @@ module Axlsx
82
82
  defined?(@style) ? @style : 0
83
83
  end
84
84
 
85
+ # Internal
86
+ def style_str
87
+ defined?(@style) ? @style.to_s : '0'
88
+ end
89
+
85
90
  attr_accessor :raw_style
86
91
 
87
92
  # The index of the cellXfs item to be applied to this cell.
88
- # @param [Hash] styles
93
+ # @param [Hash] style
89
94
  # @see Axlsx::Styles
90
95
  def add_style(style)
91
96
  self.raw_style ||= {}
@@ -104,7 +109,7 @@ module Axlsx
104
109
  self.raw_style = new_style
105
110
 
106
111
  wb = row.worksheet.workbook
107
-
112
+
108
113
  wb.styled_cells << self
109
114
  end
110
115
 
@@ -117,7 +122,7 @@ module Axlsx
117
122
  # automatically determed.
118
123
  # @see Cell#cell_type_from_value
119
124
  # @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]
125
+ # @raise [ArgumentError] Cell.type must be one of [:date, time, :float, :integer, :string, :boolean]
121
126
  # @note
122
127
  # If the value provided cannot be cast into the type specified, type is changed to :string and the following logic is applied.
123
128
  # :string to :integer or :float, type conversions always return 0 or 0.0
@@ -134,16 +139,19 @@ module Axlsx
134
139
  self.value = @value unless !defined?(@value) || @value.nil?
135
140
  end
136
141
 
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).
142
+ # Whether to treat values starting with an equals sign as formulas or as literal strings.
143
+ # Allowing user-generated data to be interpreted as formulas is a security risk.
144
+ # See https://www.owasp.org/index.php/CSV_Injection for details.
141
145
  # @return [Boolean]
142
- attr_reader :escape_formulas
146
+ def escape_formulas
147
+ defined?(@escape_formulas) ? @escape_formulas : row.worksheet.escape_formulas
148
+ end
143
149
 
144
- def escape_formulas=(v)
145
- Axlsx.validate_boolean(v)
146
- @escape_formulas = v
150
+ # Sets whether to treat values starting with an equals sign as formulas or as literal strings.
151
+ # @param [Boolean] value The value to set.
152
+ def escape_formulas=(value)
153
+ Axlsx.validate_boolean(value)
154
+ @escape_formulas = value
147
155
  end
148
156
 
149
157
  # The value of this cell.
@@ -152,13 +160,13 @@ module Axlsx
152
160
 
153
161
  # @see value
154
162
  def value=(v)
155
- #TODO: consider doing value based type determination first?
163
+ # TODO: consider doing value based type determination first?
156
164
  @value = cast_value(v)
157
165
  end
158
166
 
159
167
  # Indicates that the cell has one or more of the custom cell styles applied.
160
168
  # @return [Boolean]
161
- def is_text_run?
169
+ def is_text_run? # rubocop:disable Naming/PredicatePrefix
162
170
  defined?(@is_text_run) && @is_text_run && !contains_rich_text?
163
171
  end
164
172
 
@@ -168,18 +176,22 @@ module Axlsx
168
176
 
169
177
  # Indicates if the cell is good for shared string table
170
178
  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
179
+ (type == :string || type == :text) && # String typed
180
+ !value.nil? &&
181
+ !value.empty? &&
182
+ !is_text_run? && # No inline styles
183
+ !is_formula? &&
184
+ !is_array_formula?
176
185
  end
177
186
 
178
187
  # The inline font_name property for the cell
179
188
  # @return [String]
180
189
  attr_reader :font_name
190
+
181
191
  # @see font_name
182
- def font_name=(v) set_run_style :validate_string, :font_name, v; end
192
+ def font_name=(v)
193
+ set_run_style :validate_string, :font_name, v
194
+ end
183
195
 
184
196
  # The inline charset property for the cell
185
197
  # As far as I can tell, this is pretty much ignored. However, based on the spec it should be one of the following:
@@ -204,8 +216,11 @@ module Axlsx
204
216
  # 255  OEM_CHARSET
205
217
  # @return [String]
206
218
  attr_reader :charset
219
+
207
220
  # @see charset
208
- def charset=(v) set_run_style :validate_unsigned_int, :charset, v; end
221
+ def charset=(v)
222
+ set_run_style :validate_unsigned_int, :charset, v
223
+ end
209
224
 
210
225
  # The inline family property for the cell
211
226
  # @return [Integer]
@@ -215,6 +230,7 @@ module Axlsx
215
230
  # 4 Script
216
231
  # 5 Decorative
217
232
  attr_reader :family
233
+
218
234
  # @see family
219
235
  def family=(v)
220
236
  set_run_style :validate_family, :family, v.to_i
@@ -223,76 +239,101 @@ module Axlsx
223
239
  # The inline bold property for the cell
224
240
  # @return [Boolean]
225
241
  attr_reader :b
242
+
226
243
  # @see b
227
- def b=(v) set_run_style :validate_boolean, :b, v; end
244
+ def b=(v)
245
+ set_run_style :validate_boolean, :b, v
246
+ end
228
247
 
229
248
  # The inline italic property for the cell
230
249
  # @return [Boolean]
231
250
  attr_reader :i
251
+
232
252
  # @see i
233
- def i=(v) set_run_style :validate_boolean, :i, v; end
253
+ def i=(v)
254
+ set_run_style :validate_boolean, :i, v
255
+ end
234
256
 
235
257
  # The inline strike property for the cell
236
258
  # @return [Boolean]
237
259
  attr_reader :strike
260
+
238
261
  # @see strike
239
- def strike=(v) set_run_style :validate_boolean, :strike, v; end
262
+ def strike=(v)
263
+ set_run_style :validate_boolean, :strike, v
264
+ end
240
265
 
241
266
  # The inline outline property for the cell
242
267
  # @return [Boolean]
243
268
  attr_reader :outline
269
+
244
270
  # @see outline
245
- def outline=(v) set_run_style :validate_boolean, :outline, v; end
271
+ def outline=(v)
272
+ set_run_style :validate_boolean, :outline, v
273
+ end
246
274
 
247
275
  # The inline shadow property for the cell
248
276
  # @return [Boolean]
249
277
  attr_reader :shadow
278
+
250
279
  # @see shadow
251
- def shadow=(v) set_run_style :validate_boolean, :shadow, v; end
280
+ def shadow=(v)
281
+ set_run_style :validate_boolean, :shadow, v
282
+ end
252
283
 
253
284
  # The inline condense property for the cell
254
285
  # @return [Boolean]
255
286
  attr_reader :condense
287
+
256
288
  # @see condense
257
- def condense=(v) set_run_style :validate_boolean, :condense, v; end
289
+ def condense=(v)
290
+ set_run_style :validate_boolean, :condense, v
291
+ end
258
292
 
259
293
  # The inline extend property for the cell
260
294
  # @return [Boolean]
261
295
  attr_reader :extend
296
+
262
297
  # @see extend
263
- def extend=(v) set_run_style :validate_boolean, :extend, v; end
298
+ def extend=(v)
299
+ set_run_style :validate_boolean, :extend, v
300
+ end
264
301
 
265
302
  # The inline underline property for the cell.
266
- # It must be one of :none, :single, :double, :singleAccounting, :doubleAccounting, true
303
+ # It must be one of :none, :single, :double, :singleAccounting, :doubleAccounting
267
304
  # @return [Boolean]
268
305
  # @return [String]
269
- # @note true is for backwards compatability and is reassigned to :single
270
306
  attr_reader :u
307
+
271
308
  # @see u
272
309
  def u=(v)
273
- v = :single if (v == true || v == 1 || v == :true || v == 'true')
274
310
  set_run_style :validate_cell_u, :u, v
275
311
  end
276
312
 
277
313
  # The inline color property for the cell
278
314
  # @return [Color]
279
315
  attr_reader :color
316
+
280
317
  # @param [String] v The 8 character representation for an rgb color #FFFFFFFF"
281
318
  def color=(v)
282
- @color = v.is_a?(Color) ? v : Color.new(:rgb=>v)
319
+ @color = v.is_a?(Color) ? v : Color.new(rgb: v)
283
320
  @is_text_run = true
284
321
  end
285
322
 
286
323
  # The inline sz property for the cell
287
324
  # @return [Inteter]
288
325
  attr_reader :sz
326
+
289
327
  # @see sz
290
- def sz=(v) set_run_style :validate_unsigned_int, :sz, v; end
328
+ def sz=(v)
329
+ set_run_style :validate_unsigned_int, :sz, v
330
+ end
291
331
 
292
332
  # The inline vertical alignment property for the cell
293
333
  # this must be one of [:baseline, :subscript, :superscript]
294
334
  # @return [Symbol]
295
335
  attr_reader :vertAlign
336
+
296
337
  # @see vertAlign
297
338
  def vertAlign=(v)
298
339
  RestrictionValidator.validate :cell_vertAlign, [:baseline, :subscript, :superscript], v
@@ -303,6 +344,7 @@ module Axlsx
303
344
  # this must be one of [:none, major, minor]
304
345
  # @return [Symbol]
305
346
  attr_reader :scheme
347
+
306
348
  # @see scheme
307
349
  def scheme=(v)
308
350
  RestrictionValidator.validate :cell_scheme, [:none, :major, :minor], v
@@ -322,22 +364,23 @@ module Axlsx
322
364
  # @example Relative Cell Reference
323
365
  # ws.rows.first.cells.first.r #=> "A1"
324
366
  def r
325
- Axlsx::cell_r index, @row.row_index
367
+ Axlsx.cell_r index, @row.row_index
326
368
  end
327
369
 
328
- # @return [String] The absolute alpha(column)numeric(row) reference for this sell.
370
+ # @return [String] The absolute alpha(column)numeric(row) reference for this cell.
329
371
  # @example Absolute Cell Reference
330
372
  # ws.rows.first.cells.first.r #=> "$A$1"
331
373
  def r_abs
332
- "$#{r.match(%r{([A-Z]+)([0-9]+)})[1,2].join('$')}"
374
+ "$#{CELL_REFERENCE_REGEX.match(r)[1, 2].join('$')}"
333
375
  end
334
376
 
335
377
  # @return [Integer] The cellXfs item index applied to this cell.
336
378
  # @raise [ArgumentError] Invalid cellXfs id if the value provided is not within cellXfs items range.
337
379
  def style=(v)
338
- Axlsx::validate_unsigned_int(v)
380
+ Axlsx.validate_unsigned_int(v)
339
381
  count = styles.cellXfs.size
340
382
  raise ArgumentError, "Invalid cellXfs id" unless v < count
383
+
341
384
  @style = v
342
385
  end
343
386
 
@@ -351,11 +394,11 @@ module Axlsx
351
394
  # @param [Cell, String] target The last cell, or str ref for the cell in the merge range
352
395
  def merge(target)
353
396
  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 }
397
+ [r, target]
398
+ elsif target.is_a?(Cell)
399
+ Axlsx.sort_cells([self, target]).map(&:r)
357
400
  end
358
- self.row.worksheet.merge_cells "#{start}:#{stop}" unless stop.nil?
401
+ row.worksheet.merge_cells "#{start}:#{stop}" unless stop.nil?
359
402
  end
360
403
 
361
404
  # Serializes the cell
@@ -363,18 +406,22 @@ module Axlsx
363
406
  # @param [Integer] c_index The cell index in the row.
364
407
  # @param [String] str The string index the cell content will be appended to. Defaults to empty string.
365
408
  # @return [String] xml text for the cell
366
- def to_xml_string(r_index, c_index, str = '')
409
+ def to_xml_string(r_index, c_index, str = +'')
367
410
  CellSerializer.to_xml_string r_index, c_index, self, str
368
411
  end
369
412
 
370
- def is_formula?
413
+ def is_formula? # rubocop:disable Naming/PredicatePrefix
371
414
  return false if escape_formulas
372
415
 
373
- type == :string && @value.to_s.start_with?(?=)
416
+ type == :string && @value.to_s.start_with?(FORMULA_PREFIX)
374
417
  end
375
418
 
376
- def is_array_formula?
377
- type == :string && @value.to_s.start_with?('{=') && @value.to_s.end_with?('}')
419
+ def is_array_formula? # rubocop:disable Naming/PredicatePrefix
420
+ return false if escape_formulas
421
+
422
+ type == :string &&
423
+ @value.to_s.start_with?(ARRAY_FORMULA_PREFIX) &&
424
+ @value.to_s.end_with?(ARRAY_FORMULA_SUFFIX)
378
425
  end
379
426
 
380
427
  # returns the absolute or relative string style reference for
@@ -382,7 +429,7 @@ module Axlsx
382
429
  # @param [Boolean] absolute -when false a relative reference will be
383
430
  # returned.
384
431
  # @return [String]
385
- def reference(absolute=true)
432
+ def reference(absolute = true)
386
433
  absolute ? r_abs : r
387
434
  end
388
435
 
@@ -398,7 +445,7 @@ module Axlsx
398
445
  # Attempts to determine the correct width for this cell's content
399
446
  # @return [Float]
400
447
  def autowidth
401
- return if is_formula? || value.nil?
448
+ return if value.nil? || is_formula?
402
449
 
403
450
  if contains_rich_text?
404
451
  string_width('', font_size) + value.autowidth
@@ -414,12 +461,13 @@ module Axlsx
414
461
  end
415
462
  end
416
463
 
417
- # Returns the sanatized value
418
- # TODO find a better way to do this as it accounts for 30% of
464
+ # Returns the sanitized value
465
+ # TODO: find a better way to do this as it accounts for 30% of
419
466
  # processing time in benchmarking...
467
+ # @return [String] The sanitized value
420
468
  def clean_value
421
- if (type == :string || type == :text) && !Axlsx::trust_input
422
- Axlsx::sanitize(::CGI.escapeHTML(@value.to_s))
469
+ if (type == :string || type == :text) && !Axlsx.trust_input
470
+ Axlsx.sanitize(::CGI.escapeHTML(@value.to_s))
423
471
  else
424
472
  @value.to_s
425
473
  end
@@ -452,14 +500,15 @@ module Axlsx
452
500
  # Utility method for setting inline style attributes
453
501
  def set_run_style(validator, attr, value)
454
502
  return unless INLINE_STYLES.include?(attr.to_sym)
503
+
455
504
  Axlsx.send(validator, value) unless validator.nil?
456
- self.instance_variable_set :"@#{attr.to_s}", value
505
+ instance_variable_set :"@#{attr}", value
457
506
  @is_text_run = true
458
507
  end
459
508
 
460
509
  # @see ssti
461
510
  def ssti=(v)
462
- Axlsx::validate_unsigned_int(v)
511
+ Axlsx.validate_unsigned_int(v)
463
512
  @ssti = v
464
513
  end
465
514
 
@@ -478,13 +527,11 @@ module Axlsx
478
527
  :time
479
528
  elsif v.is_a?(TrueClass) || v.is_a?(FalseClass)
480
529
  :boolean
481
- elsif v.to_s =~ Axlsx::NUMERIC_REGEX && v.respond_to?(:to_i)
530
+ elsif v.respond_to?(:to_i) && Axlsx::NUMERIC_REGEX.match?(v.to_s)
482
531
  :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)
532
+ elsif v.respond_to?(:to_f) && (Axlsx::SAFE_FLOAT_REGEX.match?(v.to_s) || ((matchdata = MAYBE_FLOAT_REGEX.match(v.to_s)) && matchdata[:exp].to_i.between?(Float::MIN_10_EXP, Float::MAX_10_EXP)))
486
533
  :float
487
- elsif v.to_s =~ Axlsx::ISO_8601_REGEX
534
+ elsif Axlsx::ISO_8601_REGEX.match?(v.to_s)
488
535
  :iso_8601
489
536
  elsif v.is_a? RichText
490
537
  :richtext
@@ -498,17 +545,18 @@ module Axlsx
498
545
  # 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
546
  # @see Axlsx#date1904
500
547
  def cast_value(v)
501
- return v if v.is_a?(RichText) || v.nil?
548
+ return v if v.nil? || v.is_a?(RichText)
549
+
502
550
  case type
503
551
  when :date
504
- self.style = STYLE_DATE if self.style == 0
552
+ self.style = STYLE_DATE if style == 0
505
553
  if !v.is_a?(Date) && v.respond_to?(:to_date)
506
554
  v.to_date
507
555
  else
508
556
  v
509
557
  end
510
558
  when :time
511
- self.style = STYLE_DATE if self.style == 0
559
+ self.style = STYLE_DATE if style == 0
512
560
  if !v.is_a?(Time) && v.respond_to?(:to_time)
513
561
  v.to_time
514
562
  else
@@ -521,12 +569,11 @@ module Axlsx
521
569
  when :boolean
522
570
  v ? 1 : 0
523
571
  when :iso_8601
524
- #consumer is responsible for ensuring the iso_8601 format when specifying this type
572
+ # consumer is responsible for ensuring the iso_8601 format when specifying this type
525
573
  v
526
574
  else
527
575
  v.to_s
528
576
  end
529
577
  end
530
-
531
578
  end
532
579
  end
@@ -1,34 +1,38 @@
1
- module Axlsx
1
+ # frozen_string_literal: true
2
2
 
3
+ module Axlsx
3
4
  # The Cell Serializer class contains the logic for serializing cells based on their type.
4
5
  class CellSerializer
5
6
  class << self
6
7
  # Calls the proper serialization method based on type.
7
8
  # @param [Integer] row_index The index of the cell's row
8
9
  # @param [Integer] column_index The index of the cell's column
9
- # @param [String] str The string to apend serialization to.
10
+ # @param [String] str The string to append serialization to.
10
11
  # @return [String]
11
- def to_xml_string(row_index, column_index, cell, str='')
12
- str << ('<c r="' << Axlsx::cell_r(column_index, row_index) << '" s="' << cell.style.to_s << '" ')
12
+ def to_xml_string(row_index, column_index, cell, str = +'')
13
+ str << '<c r="'
14
+ str << Axlsx.col_ref(column_index) << Axlsx.row_ref(row_index)
15
+ str << '" s="' << cell.style_str << '" '
13
16
  return str << '/>' if cell.value.nil?
17
+
14
18
  method = cell.type
15
- self.send(method, cell, str)
19
+ send(method, cell, str)
16
20
  str << '</c>'
17
21
  end
18
22
 
19
23
  # builds an xml text run based on this cells attributes.
20
24
  # @param [String] str The string instance this run will be concated to.
21
25
  # @return [String]
22
- def run_xml_string(cell, str = '')
26
+ def run_xml_string(cell, str = +'')
23
27
  if cell.is_text_run?
24
28
  valid = RichTextRun::INLINE_STYLES - [:value, :type]
25
- data = Hash[Axlsx.instance_values_for(cell).map{ |k, v| [k.to_sym, v] }]
26
- data = data.select { |key, value| valid.include?(key) && !value.nil? }
29
+ data = Axlsx.instance_values_for(cell).transform_keys(&:to_sym)
30
+ data = data.select { |key, value| !value.nil? && valid.include?(key) }
27
31
  RichText.new(cell.value.to_s, data).to_xml_string(str)
28
32
  elsif cell.contains_rich_text?
29
33
  cell.value.to_xml_string(str)
30
34
  else
31
- str << ('<t>' << cell.clean_value << '</t>')
35
+ str << '<t>' << cell.clean_value << '</t>'
32
36
  end
33
37
  str
34
38
  end
@@ -37,7 +41,7 @@ module Axlsx
37
41
  # @param [Cell] cell The cell that is being serialized
38
42
  # @param [String] str The string the serialized content will be appended to.
39
43
  # @return [String]
40
- def iso_8601(cell, str='')
44
+ def iso_8601(cell, str = +'')
41
45
  value_serialization 'd', cell.value, str
42
46
  end
43
47
 
@@ -45,23 +49,23 @@ module Axlsx
45
49
  # @param [Cell] cell The cell that is being serialized
46
50
  # @param [String] str The string the serialized content will be appended to.
47
51
  # @return [String]
48
- def date(cell, str='')
49
- value_serialization false, DateTimeConverter::date_to_serial(cell.value).to_s, str
52
+ def date(cell, str = +'')
53
+ value_serialization false, DateTimeConverter.date_to_serial(cell.value).to_s, str
50
54
  end
51
55
 
52
56
  # Serializes cells that are type time
53
57
  # @param [Cell] cell The cell that is being serialized
54
58
  # @param [String] str The string the serialized content will be appended to.
55
59
  # @return [String]
56
- def time(cell, str='')
57
- value_serialization false, DateTimeConverter::time_to_serial(cell.value).to_s, str
60
+ def time(cell, str = +'')
61
+ value_serialization false, DateTimeConverter.time_to_serial(cell.value).to_s, str
58
62
  end
59
63
 
60
64
  # Serializes cells that are type boolean
61
65
  # @param [Cell] cell The cell that is being serialized
62
66
  # @param [String] str The string the serialized content will be appended to.
63
67
  # @return [String]
64
- def boolean(cell, str='')
68
+ def boolean(cell, str = +'')
65
69
  value_serialization 'b', cell.value.to_s, str
66
70
  end
67
71
 
@@ -69,7 +73,7 @@ module Axlsx
69
73
  # @param [Cell] cell The cell that is being serialized
70
74
  # @param [String] str The string the serialized content will be appended to.
71
75
  # @return [String]
72
- def float(cell, str='')
76
+ def float(cell, str = +'')
73
77
  numeric cell, str
74
78
  end
75
79
 
@@ -77,7 +81,7 @@ module Axlsx
77
81
  # @param [Cell] cell The cell that is being serialized
78
82
  # @param [String] str The string the serialized content will be appended to.
79
83
  # @return [String]
80
- def integer(cell, str = '')
84
+ def integer(cell, str = +'')
81
85
  numeric cell, str
82
86
  end
83
87
 
@@ -85,25 +89,25 @@ module Axlsx
85
89
  # @param [Cell] cell The cell that is being serialized
86
90
  # @param [String] str The string the serialized content will be appended to.
87
91
  # @return [String]
88
- def formula_serialization(cell, str='')
89
- str << ('t="str"><f>' << cell.clean_value.to_s.sub('=', '') << '</f>')
90
- str << ('<v>' << cell.formula_value.to_s << '</v>') unless cell.formula_value.nil?
92
+ def formula_serialization(cell, str = +'')
93
+ str << 't="str"><f>' << cell.clean_value.delete_prefix(FORMULA_PREFIX) << '</f>'
94
+ str << '<v>' << cell.formula_value.to_s << '</v>' unless cell.formula_value.nil?
91
95
  end
92
96
 
93
97
  # Serializes cells that are type array formula
94
98
  # @param [Cell] cell The cell that is being serialized
95
99
  # @param [String] str The string the serialized content will be appended to.
96
100
  # @return [String]
97
- def array_formula_serialization(cell, str='')
98
- str << ('t="str">' << '<f t="array" ref="' << cell.r << '">' << cell.clean_value.to_s.sub('{=', '').sub(/}$/, '') << '</f>')
99
- str << ('<v>' << cell.formula_value.to_s << '</v>') unless cell.formula_value.nil?
101
+ def array_formula_serialization(cell, str = +'')
102
+ str << 't="str">' << '<f t="array" ref="' << cell.r << '">' << cell.clean_value.delete_prefix(ARRAY_FORMULA_PREFIX).delete_suffix(ARRAY_FORMULA_SUFFIX) << '</f>'
103
+ str << '<v>' << cell.formula_value.to_s << '</v>' unless cell.formula_value.nil?
100
104
  end
101
105
 
102
106
  # Serializes cells that are type inline_string
103
107
  # @param [Cell] cell The cell that is being serialized
104
108
  # @param [String] str The string the serialized content will be appended to.
105
109
  # @return [String]
106
- def inline_string_serialization(cell, str = '')
110
+ def inline_string_serialization(cell, str = +'')
107
111
  str << 't="inlineStr"><is>'
108
112
  run_xml_string cell, str
109
113
  str << '</is>'
@@ -113,7 +117,7 @@ module Axlsx
113
117
  # @param [Cell] cell The cell that is being serialized
114
118
  # @param [String] str The string the serialized content will be appended to.
115
119
  # @return [String]
116
- def string(cell, str='')
120
+ def string(cell, str = +'')
117
121
  if cell.is_array_formula?
118
122
  array_formula_serialization cell, str
119
123
  elsif cell.is_formula?
@@ -151,13 +155,13 @@ module Axlsx
151
155
 
152
156
  private
153
157
 
154
- def numeric(cell, str = '')
158
+ def numeric(cell, str = +'')
155
159
  value_serialization 'n', cell.value, str
156
160
  end
157
161
 
158
- def value_serialization(serialization_type, serialization_value, str = '')
159
- str << ('t="' << serialization_type.to_s << '"') if serialization_type
160
- str << ('><v>' << serialization_value.to_s << '</v>')
162
+ def value_serialization(serialization_type, serialization_value, str = +'')
163
+ str << 't="' << serialization_type.to_s << '"' if serialization_type
164
+ str << '><v>' << serialization_value.to_s << '</v>'
161
165
  end
162
166
  end
163
167
  end