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,26 +1,28 @@
1
- # encoding: UTF-8
1
+ # frozen_string_literal: true
2
+
2
3
  module Axlsx
3
- require 'axlsx/stylesheet/border.rb'
4
- require 'axlsx/stylesheet/border_pr.rb'
5
- require 'axlsx/stylesheet/cell_alignment.rb'
6
- require 'axlsx/stylesheet/cell_style.rb'
7
- require 'axlsx/stylesheet/color.rb'
8
- require 'axlsx/stylesheet/fill.rb'
9
- require 'axlsx/stylesheet/font.rb'
10
- require 'axlsx/stylesheet/gradient_fill.rb'
11
- require 'axlsx/stylesheet/gradient_stop.rb'
12
- require 'axlsx/stylesheet/num_fmt.rb'
13
- require 'axlsx/stylesheet/pattern_fill.rb'
14
- require 'axlsx/stylesheet/table_style.rb'
15
- require 'axlsx/stylesheet/table_styles.rb'
16
- require 'axlsx/stylesheet/table_style_element.rb'
17
- require 'axlsx/stylesheet/dxf.rb'
18
- require 'axlsx/stylesheet/xf.rb'
19
- require 'axlsx/stylesheet/cell_protection.rb'
20
-
21
- #The Styles class manages worksheet styles
4
+ autoload :Border, 'axlsx/stylesheet/border'
5
+ autoload :BorderPr, 'axlsx/stylesheet/border_pr'
6
+ autoload :CellAlignment, 'axlsx/stylesheet/cell_alignment'
7
+ autoload :CellStyle, 'axlsx/stylesheet/cell_style'
8
+ autoload :Color, 'axlsx/stylesheet/color'
9
+ autoload :Fill, 'axlsx/stylesheet/fill'
10
+ autoload :Font, 'axlsx/stylesheet/font'
11
+ autoload :GradientFill, 'axlsx/stylesheet/gradient_fill'
12
+ autoload :GradientStop, 'axlsx/stylesheet/gradient_stop'
13
+ autoload :NumFmt, 'axlsx/stylesheet/num_fmt'
14
+ autoload :PatternFill, 'axlsx/stylesheet/pattern_fill'
15
+ autoload :TableStyle, 'axlsx/stylesheet/table_style'
16
+ autoload :TableStyles, 'axlsx/stylesheet/table_styles'
17
+ autoload :TableStyleElement, 'axlsx/stylesheet/table_style_element'
18
+ autoload :Theme, 'axlsx/stylesheet/theme'
19
+ autoload :Dxf, 'axlsx/stylesheet/dxf'
20
+ autoload :Xf, 'axlsx/stylesheet/xf'
21
+ autoload :CellProtection, 'axlsx/stylesheet/cell_protection'
22
+
23
+ # The Styles class manages worksheet styles
22
24
  # In addition to creating the require style objects for a valid xlsx package, this class provides the key mechanism for adding styles to your workbook, and safely applying them to the cells of your worksheet.
23
- # All portions of the stylesheet are implemented here exception colors, which specify legacy and modified pallete colors, and exLst, whic is used as a future feature data storage area.
25
+ # All portions of the stylesheet are implemented here exception colors, which specify legacy and modified palette colors, and exLst, which is used as a future feature data storage area.
24
26
  # @see Office Open XML Part 1 18.8.11 for gory details on how this stuff gets put together
25
27
  # @see Styles#add_style
26
28
  # @note The recommended way to manage styles is with add_style
@@ -109,14 +111,14 @@ module Axlsx
109
111
  # @see Styles#add_style
110
112
  attr_reader :dxfs
111
113
 
112
- # The collection of table styles that will be available to the user in the excel UI
114
+ # The collection of table styles that will be available to the user in the Excel UI
113
115
  # @return [SimpleTypedList]
114
116
  # @note The recommended way to manage styles is with add_style
115
117
  # @see Styles#add_style
116
118
  attr_reader :tableStyles
117
119
 
118
120
  # Creates a new Styles object and prepopulates it with the requires objects to generate a valid package style part.
119
- def initialize()
121
+ def initialize
120
122
  load_default_styles
121
123
  end
122
124
 
@@ -137,7 +139,7 @@ module Axlsx
137
139
  # @option options [Integer] family The font family to use.
138
140
  # @option options [String] font_name The name of the font to use
139
141
  # @option options [Integer] num_fmt The number format to apply
140
- # @option options [String] format_code The formatting to apply.
142
+ # @option options [String] format_code The formatting to apply.
141
143
  # @option options [Integer|Hash] border The border style to use.
142
144
  # borders support style, color and edges options @see parse_border_options
143
145
  # @option options [String] bg_color The background color to apply to the cell
@@ -155,7 +157,7 @@ module Axlsx
155
157
  # ws = p.workbook.add_worksheet
156
158
  #
157
159
  # # black text on a white background at 14pt with thin borders!
158
- # title = ws.styles.add_style(:bg_color => "FFFF0000", :fg_color=>"#FF000000", :sz=>14, :border=> {:style => :thin, :color => "FFFF0000"}
160
+ # title = ws.styles.add_style(:bg_color => "FFFF0000", :fg_color=>"FF000000", :sz=>14, :border=> {:style => :thin, :color => "FFFF0000"}
159
161
  #
160
162
  # ws.add_row ["Least Popular Pets"]
161
163
  # ws.add_row ["", "Dry Skinned Reptiles", "Bald Cats", "Violent Parrots"], :style=>title
@@ -173,7 +175,7 @@ module Axlsx
173
175
  #
174
176
  # # define your styles
175
177
  # title = ws.styles.add_style(:bg_color => "FFFF0000",
176
- # :fg_color=>"#FF000000",
178
+ # :fg_color=>"FF000000",
177
179
  # :border=>Axlsx::STYLE_THIN_BORDER,
178
180
  # :alignment=>{:horizontal => :center})
179
181
  #
@@ -208,10 +210,10 @@ module Axlsx
208
210
  #
209
211
  # # define your styles
210
212
  # profitable = wb.styles.add_style(:bg_color => "FFFF0000",
211
- # :fg_color=>"#FF000000",
213
+ # :fg_color=>"FF000000",
212
214
  # :type => :dxf)
213
215
  #
214
- # ws.add_row ["Genreated At:", Time.now], :styles=>[nil, date_time]
216
+ # ws.add_row ["Generated At:", Time.now], :styles=>[nil, date_time]
215
217
  # ws.add_row ["Previous Year Quarterly Profits (JPY)"], :style=>title
216
218
  # ws.add_row ["Quarter", "Profit", "% of Total"], :style=>title
217
219
  # ws.add_row ["Q1", 4000, 40], :style=>[title, currency, percent]
@@ -225,22 +227,28 @@ module Axlsx
225
227
  #
226
228
  # An index for cell styles where keys are styles codes as per Axlsx::Style and values are Cell#raw_style
227
229
  # The reason for the backward key/value ordering is that style lookup must be most efficient, while `add_style` can be less efficient
228
- def add_style(options={})
230
+ def add_style(options = {})
231
+ options = options.dup
232
+
229
233
  # Default to :xf
230
234
  options[:type] ||= :xf
231
235
 
232
- raise ArgumentError, "Type must be one of [:xf, :dxf]" unless [:xf, :dxf].include?(options[:type] )
236
+ raise ArgumentError, "Type must be one of [:xf, :dxf]" unless [:xf, :dxf].include?(options[:type])
233
237
 
234
- if options[:border].is_a?(Hash) && options[:border][:edges] == :all
235
- options[:border][:edges] = Axlsx::Border::EDGES
238
+ if options[:border].is_a?(Hash)
239
+ if options[:border][:edges] == :all
240
+ options[:border][:edges] = Axlsx::Border::EDGES
241
+ elsif options[:border][:edges]
242
+ options[:border][:edges] = options[:border][:edges].map(&:to_sym) ### normalize for style caching
243
+ end
236
244
  end
237
245
 
238
246
  if options[:type] == :xf
239
247
  # Check to see if style in cache already
240
-
241
- font_defaults = {name: @fonts.first.name, sz: @fonts.first.sz, family: @fonts.first.family}
242
248
 
243
- raw_style = {type: :xf}.merge(font_defaults).merge(options)
249
+ font_defaults = { name: @fonts.first.name, sz: @fonts.first.sz, family: @fonts.first.family }
250
+
251
+ raw_style = { type: :xf }.merge(font_defaults, options)
244
252
 
245
253
  if raw_style[:format_code]
246
254
  raw_style.delete(:num_fmt)
@@ -260,24 +268,22 @@ module Axlsx
260
268
  alignment = parse_alignment_options options
261
269
  protection = parse_protection_options options
262
270
 
263
- case options[:type]
264
- when :dxf
265
- style = Dxf.new :fill => fill, :font => font, :numFmt => numFmt, :border => border, :alignment => alignment, :protection => protection
266
- else
267
- style = Xf.new :fillId=>fill || 0, :fontId=>font || 0, :numFmtId=>numFmt || 0, :borderId=>border || 0, :alignment => alignment, :protection => protection, :applyFill=>!fill.nil?, :applyFont=>!font.nil?, :applyNumberFormat =>!numFmt.nil?, :applyBorder=>!border.nil?, :applyAlignment => !alignment.nil?, :applyProtection => !protection.nil?
268
- end
271
+ style = case options[:type]
272
+ when :dxf
273
+ Dxf.new fill: fill, font: font, numFmt: numFmt, border: border, alignment: alignment, protection: protection
274
+ else
275
+ Xf.new fillId: fill || 0, fontId: font || 0, numFmtId: numFmt || 0, borderId: border || 0, alignment: alignment, protection: protection, applyFill: !fill.nil?, applyFont: !font.nil?, applyNumberFormat: !numFmt.nil?, applyBorder: !border.nil?, applyAlignment: !alignment.nil?, applyProtection: !protection.nil?
276
+ end
269
277
 
270
278
  if options[:type] == :xf
271
279
  xf_index = (cellXfs << style)
272
280
 
273
- # Add styles to style_index cache for re-use
281
+ # Add styles to style_index cache for reuse
274
282
  style_index[xf_index] = raw_style
275
283
 
276
- return xf_index
284
+ xf_index
277
285
  else
278
- dxf_index = (dxfs << style)
279
-
280
- return dxf_index
286
+ dxfs << style
281
287
  end
282
288
  end
283
289
 
@@ -287,18 +293,20 @@ module Axlsx
287
293
  # @option options [Boolean] hide boolean value defining cell protection attribute for hiding.
288
294
  # @option options [Boolean] locked boolean value defining cell protection attribute for locking.
289
295
  # @return [CellProtection]
290
- def parse_protection_options(options={})
296
+ def parse_protection_options(options = {})
291
297
  return if (options.keys & [:hidden, :locked]).empty?
298
+
292
299
  CellProtection.new(options)
293
300
  end
294
301
 
295
302
  # parses add_style options for alignment
296
303
  # noop if options hash does not include :alignment key
297
- # @option options [Hash] alignment A hash of options to prive the CellAlignment intializer
304
+ # @option options [Hash] alignment A hash of options to prive the CellAlignment initializer
298
305
  # @return [CellAlignment]
299
306
  # @see CellAlignment
300
- def parse_alignment_options(options={})
307
+ def parse_alignment_options(options = {})
301
308
  return unless options[:alignment]
309
+
302
310
  CellAlignment.new options[:alignment]
303
311
  end
304
312
 
@@ -317,53 +325,88 @@ module Axlsx
317
325
  # @option options [Integer] family The font family to use.
318
326
  # @option options [String] font_name The name of the font to use
319
327
  # @return [Font|Integer]
320
- def parse_font_options(options={})
328
+ def parse_font_options(options = {})
321
329
  return if (options.keys & [:fg_color, :sz, :b, :i, :u, :strike, :outline, :shadow, :charset, :family, :font_name]).empty?
322
- Axlsx.instance_values_for(fonts.first).each do |key, value|
323
- # Thanks for that 1.8.7 - cant do a simple merge...
324
- options[key.to_sym] = value unless options.keys.include?(key.to_sym)
325
- end
326
- font = Font.new(options)
327
- font.color = Color.new(:rgb => options[:fg_color]) if options[:fg_color]
330
+
331
+ font = Font.new(Axlsx.instance_values_for(fonts.first).merge(options))
332
+ font.color = Color.new(rgb: options[:fg_color]) if options[:fg_color]
328
333
  font.name = options[:font_name] if options[:font_name]
329
334
  options[:type] == :dxf ? font : fonts << font
330
335
  end
331
336
 
332
337
  # parses add_style options for fills. If the options hash contains :type => :dxf we return a Fill object. If not, we return the index of the fill after being added to the fills collection.
333
- # @note noop if :bg_color is not specified in options
334
- # @option options [String] bg_color The rgb color to apply to the fill
338
+ # @note noop unless at least one of the documented attributes is specified in options
339
+ # @option options [String] bg_color The rgb color to apply to the fill. An alias for pattern_bg_color if you need only a solid background
340
+ # @option options [String] pattern_type The fill pattern to apply to the fill
341
+ # @option options [String] pattern_bg_color The rgb color to apply to the fill as the first color
342
+ # @option options [String] pattern_fg_color The rgb color to apply to the fill as the second color
335
343
  # @return [Fill|Integer]
336
- def parse_fill_options(options={})
337
- return unless options[:bg_color]
338
- color = Color.new(:rgb=>options[:bg_color])
344
+ def parse_fill_options(options = {})
345
+ return unless options[:bg_color] || options[:pattern_type] || options[:pattern_bg_color] || options[:pattern_fg_color]
346
+
347
+ pattern_type = options[:pattern_type] || :solid
339
348
  dxf = options[:type] == :dxf
340
- color_key = dxf ? :bgColor : :fgColor
341
- pattern = PatternFill.new(:patternType =>:solid, color_key=>color)
349
+
350
+ pattern_options = {
351
+ patternType: pattern_type
352
+ }
353
+
354
+ if options[:pattern_bg_color] && options[:bg_color]
355
+ warn 'Both `bg_color` and `pattern_bg_color` got defined. To get a solid background without defining it in `patter_type`, use only `bg_color`, otherwise use only `pattern_bg_color` to avoid confusion.'
356
+ end
357
+
358
+ bg_color = options[:pattern_bg_color] || options[:bg_color]
359
+ fg_color = options[:pattern_fg_color]
360
+
361
+ # Both bgColor and fgColor happens to configure the background of the cell.
362
+ # One of them sets the "background" of the cell, while the other one is
363
+ # responsible for the "pattern" of the cell. When you pick "solid" pattern for
364
+ # a normal xf style, then it's a rectangle covering all bgColor with fgColor,
365
+ # which means we need to to set the given background color to fgColor as well.
366
+ # For some reason I wasn't able find, it works the opposite for dxf styles
367
+ # (differential formatting records), so to get the expected color, we need
368
+ # to put it into bgColor. We only need these cross-assignments when using
369
+ # "solid" pattern and the user provided only one color to get the least
370
+ # amount of surprise
371
+
372
+ if bg_color
373
+ pattern_options[:bgColor] = Color.new(rgb: bg_color)
374
+ elsif pattern_type == :solid && fg_color
375
+ pattern_options[:bgColor] = Color.new(rgb: fg_color)
376
+ end
377
+
378
+ if fg_color
379
+ pattern_options[:fgColor] = Color.new(rgb: fg_color)
380
+ elsif pattern_type == :solid && bg_color
381
+ pattern_options[:fgColor] = Color.new(rgb: bg_color)
382
+ end
383
+
384
+ pattern = PatternFill.new(pattern_options)
342
385
  fill = Fill.new(pattern)
343
386
  dxf ? fill : fills << fill
344
387
  end
345
388
 
346
389
  # parses Style#add_style options for borders.
347
390
  # @note noop if :border is not specified in options
348
- # @option options [Hash|Integer] A border style definition hash or the index of an existing border.
349
- # Border style definition hashes must include :style and :color key-value entries and
350
- # may include an :edges entry that references an array of symbols identifying which border edges
391
+ # @option options [Hash|Integer] A border style definition hash or the index of an existing border.
392
+ # Border style definition hashes must include :style and :color key-value entries and
393
+ # may include an :edges entry that references an array of symbols identifying which border edges
351
394
  # you wish to apply the style or any other valid Border initializer options.
352
395
  # If the :edges entity is not provided the style is applied to all edges of cells that reference this style.
353
- # Also available :border_top, :border_right, :border_bottom and :border_left options with :style and/or :color
396
+ # Also available :border_top, :border_right, :border_bottom and :border_left options with :style and/or :color
354
397
  # key-value entries, which override :border values.
355
398
  # @example
356
399
  # #apply a thick red border to the top and bottom
357
400
  # { :border => { :style => :thick, :color => "FFFF0000", :edges => [:top, :bottom] }
358
401
  # @return [Border|Integer]
359
- def parse_border_options(options={})
360
- if options[:border].nil? && Border::EDGES.all?{|x| options["border_#{x}".to_sym].nil? }
402
+ def parse_border_options(options = {})
403
+ if options[:border].nil? && Border::EDGES.all? { |x| options[:"border_#{x}"].nil? }
361
404
  return nil
362
405
  end
363
406
 
364
407
  if options[:border].is_a?(Integer)
365
408
  if options[:border] >= borders.size
366
- raise ArgumentError, (ERR_INVALID_BORDER_ID % options[:border])
409
+ raise ArgumentError, format(ERR_INVALID_BORDER_ID, options[:border])
367
410
  end
368
411
 
369
412
  if options[:type] == :dxf
@@ -373,9 +416,9 @@ module Axlsx
373
416
  end
374
417
  end
375
418
 
376
- validate_border_hash = ->(val){
377
- if !(val.keys.include?(:style) && val.keys.include?(:color))
378
- raise ArgumentError, (ERR_INVALID_BORDER_OPTIONS % options[:border])
419
+ validate_border_hash = lambda { |val|
420
+ unless val.key?(:style) && val.key?(:color)
421
+ raise ArgumentError, format(ERR_INVALID_BORDER_OPTIONS, options[:border])
379
422
  end
380
423
  }
381
424
 
@@ -383,28 +426,26 @@ module Axlsx
383
426
 
384
427
  if options[:border].nil?
385
428
  base_border_opts = {}
386
- else
387
- if options[:border].is_a?(Array)
388
- borders_array += options[:border]
429
+ elsif options[:border].is_a?(Array)
430
+ borders_array += options[:border]
389
431
 
390
- base_border_opts = {}
432
+ base_border_opts = {}
391
433
 
392
- options[:border].each do |b_opts|
393
- if b_opts[:edges].nil?
394
- base_border_opts = base_border_opts.merge(b_opts)
395
- end
434
+ options[:border].each do |b_opts|
435
+ if b_opts[:edges].nil?
436
+ base_border_opts = base_border_opts.merge(b_opts)
396
437
  end
397
- else
398
- borders_array << options[:border]
438
+ end
439
+ else
440
+ borders_array << options[:border]
399
441
 
400
- base_border_opts = options[:border]
442
+ base_border_opts = options[:border]
401
443
 
402
- validate_border_hash.call(base_border_opts)
403
- end
444
+ validate_border_hash.call(base_border_opts)
404
445
  end
405
446
 
406
447
  Border::EDGES.each do |edge|
407
- val = options["border_#{edge}".to_sym]
448
+ val = options[:"border_#{edge}"]
408
449
 
409
450
  if val
410
451
  borders_array << val.merge(edges: [edge])
@@ -425,8 +466,8 @@ module Axlsx
425
466
  end
426
467
  end
427
468
 
428
- if options["border_#{edge}".to_sym]
429
- edge_b_opts = edge_b_opts.merge(options["border_#{edge}".to_sym])
469
+ if options[:"border_#{edge}"]
470
+ edge_b_opts = edge_b_opts.merge(options[:"border_#{edge}"])
430
471
  skip_edge = false
431
472
  end
432
473
 
@@ -434,23 +475,23 @@ module Axlsx
434
475
  next
435
476
  end
436
477
 
437
- if !edge_b_opts.empty?
478
+ unless edge_b_opts.empty?
438
479
  if base_border_opts.empty?
439
480
  validate_border_hash.call(edge_b_opts)
440
481
  end
441
482
 
442
483
  border.prs << BorderPr.new({
443
- :name => edge,
444
- :style => edge_b_opts[:style],
445
- :color => Color.new(:rgb => edge_b_opts[:color]) },
446
- )
484
+ name: edge,
485
+ style: edge_b_opts[:style],
486
+ color: Color.new(rgb: edge_b_opts[:color])
487
+ })
447
488
  end
448
489
  end
449
490
 
450
491
  if options[:type] == :dxf
451
- return border
492
+ border
452
493
  else
453
- return borders << border
494
+ borders << border
454
495
  end
455
496
  end
456
497
 
@@ -458,16 +499,21 @@ module Axlsx
458
499
  # noop if neither :format_code or :num_format options are set.
459
500
  # @option options [Hash] A hash describing the :format_code and/or :num_fmt integer for the style.
460
501
  # @return [NumFmt|Integer]
461
- def parse_num_fmt_options(options={})
502
+ def parse_num_fmt_options(options = {})
462
503
  return if (options.keys & [:format_code, :num_fmt]).empty?
463
504
 
464
- #When the user provides format_code - we always need to create a new numFmt object
465
- #When the type is :dxf we always need to create a new numFmt object
505
+ # When the user provides format_code - we always need to create a new numFmt object
506
+ # When the type is :dxf we always need to create a new numFmt object
466
507
  if options[:format_code] || options[:type] == :dxf
467
- #If this is a standard xf we pull from numFmts the highest current and increment for num_fmt
468
- options[:num_fmt] ||= (@numFmts.map{ |num_fmt| num_fmt.numFmtId }.max + 1) if options[:type] != :dxf
469
- numFmt = NumFmt.new(:numFmtId => options[:num_fmt] || 0, :formatCode=> options[:format_code].to_s)
470
- options[:type] == :dxf ? numFmt : (numFmts << numFmt; numFmt.numFmtId)
508
+ # If this is a standard xf we pull from numFmts the highest current and increment for num_fmt
509
+ options[:num_fmt] ||= (@numFmts.map(&:numFmtId).max + 1) if options[:type] != :dxf
510
+ numFmt = NumFmt.new(numFmtId: options[:num_fmt] || 0, formatCode: options[:format_code].to_s)
511
+ if options[:type] == :dxf
512
+ numFmt
513
+ else
514
+ numFmts << numFmt
515
+ numFmt.numFmtId
516
+ end
471
517
  else
472
518
  options[:num_fmt]
473
519
  end
@@ -476,8 +522,8 @@ module Axlsx
476
522
  # Serializes the object
477
523
  # @param [String] str
478
524
  # @return [String]
479
- def to_xml_string(str = '')
480
- str << ('<styleSheet xmlns="' << XML_NS << '">')
525
+ def to_xml_string(str = +'')
526
+ str << '<styleSheet xmlns="' << XML_NS << '">'
481
527
  instance_vals = Axlsx.instance_values_for(self)
482
528
  [:numFmts, :fonts, :fills, :borders, :cellStyleXfs, :cellXfs, :cellStyles, :dxfs, :tableStyles].each do |key|
483
529
  instance_vals[key.to_s].to_xml_string(str) unless instance_vals[key.to_s].nil?
@@ -486,50 +532,53 @@ module Axlsx
486
532
  end
487
533
 
488
534
  private
535
+
489
536
  # Creates the default set of styles the exel requires to be valid as well as setting up the
490
537
  # Axlsx::STYLE_THIN_BORDER
491
538
  def load_default_styles
492
539
  @numFmts = SimpleTypedList.new NumFmt, 'numFmts'
493
- @numFmts << NumFmt.new(:numFmtId => NUM_FMT_YYYYMMDD, :formatCode=> "yyyy/mm/dd")
494
- @numFmts << NumFmt.new(:numFmtId => NUM_FMT_YYYYMMDDHHMMSS, :formatCode=> "yyyy/mm/dd hh:mm:ss")
540
+ @numFmts << NumFmt.new(numFmtId: NUM_FMT_YYYYMMDD, formatCode: "yyyy/mm/dd")
541
+ @numFmts << NumFmt.new(numFmtId: NUM_FMT_YYYYMMDDHHMMSS, formatCode: "yyyy/mm/dd hh:mm:ss")
495
542
 
496
543
  @numFmts.lock
497
544
 
498
545
  @fonts = SimpleTypedList.new Font, 'fonts'
499
- @fonts << Font.new(:name => "Arial", :sz => 11, :family=>1)
546
+ @fonts << Font.new(name: "Arial", sz: 11, family: 1)
500
547
  @fonts.lock
501
548
 
502
549
  @fills = SimpleTypedList.new Fill, 'fills'
503
- @fills << Fill.new(Axlsx::PatternFill.new(:patternType=>:none))
504
- @fills << Fill.new(Axlsx::PatternFill.new(:patternType=>:gray125))
550
+ @fills << Fill.new(Axlsx::PatternFill.new(patternType: :none))
551
+ @fills << Fill.new(Axlsx::PatternFill.new(patternType: :gray125))
505
552
  @fills.lock
506
553
 
507
554
  @borders = SimpleTypedList.new Border, 'borders'
508
555
  @borders << Border.new
509
556
  black_border = Border.new
510
557
  [:left, :right, :top, :bottom].each do |item|
511
- black_border.prs << BorderPr.new(:name=>item, :style=>:thin, :color=>Color.new(:rgb=>"FF000000"))
558
+ black_border.prs << BorderPr.new(name: item, style: :thin, color: Color.new(rgb: "FF000000"))
512
559
  end
513
560
  @borders << black_border
514
561
  @borders.lock
515
562
 
516
563
  @cellStyleXfs = SimpleTypedList.new Xf, "cellStyleXfs"
517
- @cellStyleXfs << Xf.new(:borderId=>0, :numFmtId=>0, :fontId=>0, :fillId=>0)
564
+ @cellStyleXfs << Xf.new(borderId: 0, numFmtId: 0, fontId: 0, fillId: 0)
518
565
  @cellStyleXfs.lock
519
566
 
520
567
  @cellStyles = SimpleTypedList.new CellStyle, 'cellStyles'
521
- @cellStyles << CellStyle.new(:name =>"Normal", :builtinId =>0, :xfId=>0)
568
+ @cellStyles << CellStyle.new(name: "Normal", builtinId: 0, xfId: 0)
522
569
  @cellStyles.lock
523
570
 
524
571
  @cellXfs = SimpleTypedList.new Xf, "cellXfs"
525
- @cellXfs << Xf.new(:borderId=>0, :xfId=>0, :numFmtId=>0, :fontId=>0, :fillId=>0)
526
- @cellXfs << Xf.new(:borderId=>1, :xfId=>0, :numFmtId=>0, :fontId=>0, :fillId=>0)
572
+ @cellXfs << Xf.new(borderId: 0, xfId: 0, numFmtId: 0, fontId: 0, fillId: 0)
573
+ @cellXfs << Xf.new(borderId: 1, xfId: 0, numFmtId: 0, fontId: 0, fillId: 0)
527
574
  # default date formatting
528
- @cellXfs << Xf.new(:borderId=>0, :xfId=>0, :numFmtId=>14, :fontId=>0, :fillId=>0, :applyNumberFormat=>1)
575
+ @cellXfs << Xf.new(borderId: 0, xfId: 0, numFmtId: 14, fontId: 0, fillId: 0, applyNumberFormat: 1)
529
576
  @cellXfs.lock
530
577
 
531
- @dxfs = SimpleTypedList.new(Dxf, "dxfs"); @dxfs.lock
532
- @tableStyles = TableStyles.new(:defaultTableStyle => "TableStyleMedium9", :defaultPivotStyle => "PivotStyleLight16"); @tableStyles.lock
578
+ @dxfs = SimpleTypedList.new(Dxf, "dxfs")
579
+ @dxfs.lock
580
+ @tableStyles = TableStyles.new(defaultTableStyle: "TableStyleMedium9", defaultPivotStyle: "PivotStyleLight16")
581
+ @tableStyles.lock
533
582
  end
534
583
  end
535
584
  end
@@ -1,9 +1,9 @@
1
- # encoding: UTF-8
1
+ # frozen_string_literal: true
2
+
2
3
  module Axlsx
3
4
  # A single table style definition and is a collection for tableStyleElements
4
5
  # @note Table are not supported in this version and only the defaults required for a valid workbook are created.
5
6
  class TableStyle < SimpleTypedList
6
-
7
7
  include Axlsx::OptionsParser
8
8
  include Axlsx::SerializedAttributes
9
9
 
@@ -12,10 +12,10 @@ module Axlsx
12
12
  # @param [String] name
13
13
  # @option options [Boolean] pivot
14
14
  # @option options [Boolean] table
15
- def initialize(name, options={})
15
+ def initialize(name, options = {})
16
16
  self.name = name
17
17
  parse_options options
18
- super TableStyleElement
18
+ super(TableStyleElement)
19
19
  end
20
20
 
21
21
  serializable_attributes :name, :pivot, :table
@@ -33,22 +33,32 @@ module Axlsx
33
33
  attr_reader :table
34
34
 
35
35
  # @see name
36
- def name=(v) Axlsx::validate_string v; @name=v end
36
+ def name=(v)
37
+ Axlsx.validate_string v
38
+ @name = v
39
+ end
40
+
37
41
  # @see pivot
38
- def pivot=(v) Axlsx::validate_boolean v; @pivot=v end
42
+ def pivot=(v)
43
+ Axlsx.validate_boolean v
44
+ @pivot = v
45
+ end
46
+
39
47
  # @see table
40
- def table=(v) Axlsx::validate_boolean v; @table=v end
48
+ def table=(v)
49
+ Axlsx.validate_boolean v
50
+ @table = v
51
+ end
41
52
 
42
53
  # Serializes the object
43
54
  # @param [String] str
44
55
  # @return [String]
45
- def to_xml_string(str = '')
56
+ def to_xml_string(str = +'')
46
57
  str << '<tableStyle '
47
- serialized_attributes str, {:count => self.size}
58
+ serialized_attributes str, { count: size }
48
59
  str << '>'
49
60
  each { |table_style_el| table_style_el.to_xml_string(str) }
50
61
  str << '</tableStyle>'
51
62
  end
52
-
53
63
  end
54
64
  end
@@ -1,9 +1,9 @@
1
- # encoding: UTF-8
1
+ # frozen_string_literal: true
2
+
2
3
  module Axlsx
3
4
  # an element of style that belongs to a table style.
4
5
  # @note tables and table styles are not supported in this version. This class exists in preparation for that support.
5
6
  class TableStyleElement
6
-
7
7
  include Axlsx::OptionsParser
8
8
  include Axlsx::SerializedAttributes
9
9
 
@@ -11,7 +11,7 @@ module Axlsx
11
11
  # @option options [Symbol] type
12
12
  # @option options [Integer] size
13
13
  # @option options [Integer] dxfId
14
- def initialize(options={})
14
+ def initialize(options = {})
15
15
  parse_options options
16
16
  end
17
17
 
@@ -58,20 +58,28 @@ module Axlsx
58
58
  attr_reader :dxfId
59
59
 
60
60
  # @see type
61
- def type=(v) Axlsx::validate_table_element_type v; @type = v end
61
+ def type=(v)
62
+ Axlsx.validate_table_element_type v
63
+ @type = v
64
+ end
62
65
 
63
66
  # @see size
64
- def size=(v) Axlsx::validate_unsigned_int v; @size = v end
67
+ def size=(v)
68
+ Axlsx.validate_unsigned_int v
69
+ @size = v
70
+ end
65
71
 
66
72
  # @see dxfId
67
- def dxfId=(v) Axlsx::validate_unsigned_int v; @dxfId = v end
73
+ def dxfId=(v)
74
+ Axlsx.validate_unsigned_int v
75
+ @dxfId = v
76
+ end
68
77
 
69
78
  # Serializes the object
70
79
  # @param [String] str
71
80
  # @return [String]
72
- def to_xml_string(str = '')
81
+ def to_xml_string(str = +'')
73
82
  serialized_tag('tableStyleElement', str)
74
83
  end
75
-
76
84
  end
77
85
  end