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,9 +1,6 @@
1
- # encoding: UTF-8
2
-
3
- require_relative "border_creator"
1
+ # frozen_string_literal: true
4
2
 
5
3
  module Axlsx
6
-
7
4
  # The Worksheet class represents a worksheet in the workbook.
8
5
  class Worksheet
9
6
  include Axlsx::OptionsParser
@@ -16,12 +13,15 @@ module Axlsx
16
13
  # @option options [Hash] page_margins A hash containing page margins for this worksheet. @see PageMargins
17
14
  # @option options [Hash] print_options A hash containing print options for this worksheet. @see PrintOptions
18
15
  # @option options [Hash] header_footer A hash containing header/footer options for this worksheet. @see HeaderFooter
19
- # @option options [Boolean] show_gridlines indicates if gridlines should be shown for this sheet.
20
- def initialize(wb, options={})
16
+ # @option options [Boolean] show_gridlines Whether gridlines should be shown for this sheet.
17
+ # @option options [Boolean] escape_formulas Whether formulas should be escaped by default. Can be overridden at a
18
+ # row/cell level.
19
+ def initialize(wb, options = {})
21
20
  self.workbook = wb
22
21
  @sheet_protection = nil
23
22
  initialize_page_options(options)
24
23
  parse_options options
24
+ self.escape_formulas = wb.escape_formulas unless defined? @escape_formulas
25
25
  @workbook.worksheets << self
26
26
  @sheet_id = index + 1
27
27
  yield self if block_given?
@@ -29,11 +29,11 @@ module Axlsx
29
29
 
30
30
  serializable_attributes :sheet_id, :state
31
31
 
32
- # Initalizes page margin, setup and print options
32
+ # Initializes page margin, setup and print options
33
33
  # @param [Hash] options Options passed in from the initializer
34
34
  def initialize_page_options(options)
35
35
  @page_margins = PageMargins.new options[:page_margins] if options[:page_margins]
36
- @page_setup = PageSetup.new options[:page_setup] if options[:page_setup]
36
+ @page_setup = PageSetup.new options[:page_setup] if options[:page_setup]
37
37
  @print_options = PrintOptions.new options[:print_options] if options[:print_options]
38
38
  @header_footer = HeaderFooter.new options[:header_footer] if options[:header_footer]
39
39
  @row_breaks = RowBreaks.new
@@ -43,13 +43,27 @@ module Axlsx
43
43
  # The name of the worksheet
44
44
  # @return [String]
45
45
  def name
46
- @name ||= "Sheet" + (index+1).to_s
46
+ @name ||= "Sheet#{index + 1}"
47
+ end
48
+
49
+ # Whether to treat values starting with an equals sign as formulas or as literal strings.
50
+ # Allowing user-generated data to be interpreted as formulas is a security risk.
51
+ # See https://www.owasp.org/index.php/CSV_Injection for details.
52
+ # @return [Boolean]
53
+ attr_reader :escape_formulas
54
+
55
+ # Sets whether to treat values starting with an equals sign as formulas or as literal strings.
56
+ # @param [Boolean] value The value to set.
57
+ # @return [Boolean]
58
+ def escape_formulas=(value)
59
+ Axlsx.validate_boolean(value)
60
+ @escape_formulas = value
47
61
  end
48
62
 
49
63
  # Specifies the visible state of this sheet. Allowed states are
50
64
  # :visible, :hidden or :very_hidden. The default value is :visible.
51
65
  #
52
- # Worksheets in the :hidden state can be shown using the sheet formatting properties in excel.
66
+ # Worksheets in the :hidden state can be shown using the sheet formatting properties in Excel.
53
67
  # :very_hidden sheets should be inaccessible to end users.
54
68
  # @param [Symbol] sheet_state The visible state for this sheet.
55
69
  def state=(sheet_state)
@@ -102,13 +116,13 @@ module Axlsx
102
116
  # The tables in this worksheet
103
117
  # @return [Array] of Table
104
118
  def tables
105
- @tables ||= Tables.new self
119
+ @tables ||= Tables.new self
106
120
  end
107
121
 
108
122
  # The pivot tables in this worksheet
109
123
  # @return [Array] of Table
110
124
  def pivot_tables
111
- @pivot_tables ||= PivotTables.new self
125
+ @pivot_tables ||= PivotTables.new self
112
126
  end
113
127
 
114
128
  # A collection of column breaks added to this worksheet
@@ -157,7 +171,7 @@ module Axlsx
157
171
  @rows.transpose(&block)
158
172
  end
159
173
 
160
- # A range that excel will apply an auto-filter to "A1:B3"
174
+ # A range that Excel will apply an auto-filter to "A1:B3"
161
175
  # This will turn filtering on for the cells in the range.
162
176
  # The first row is considered the header, while subsequent rows are considered to be data.
163
177
  # @return String
@@ -165,17 +179,17 @@ module Axlsx
165
179
  @auto_filter ||= AutoFilter.new self
166
180
  end
167
181
 
168
- # Indicates if the worksheet will be fit by witdh or height to a specific number of pages.
182
+ # Indicates if the worksheet will be fit by width or height to a specific number of pages.
169
183
  # To alter the width or height for page fitting, please use page_setup.fit_to_widht or page_setup.fit_to_height.
170
184
  # If you want the worksheet to fit on more pages (e.g. 2x2), set {PageSetup#fit_to_width} and {PageSetup#fit_to_height} accordingly.
171
185
  # @return Boolean
172
186
  # @see #page_setup
173
187
  def fit_to_page?
174
- return false unless Axlsx.instance_values_for(self).keys.include?('page_setup')
188
+ return false unless Axlsx.instance_values_for(self).key?('page_setup')
189
+
175
190
  page_setup.fit_to_page?
176
191
  end
177
192
 
178
-
179
193
  # Column info for the sheet
180
194
  # @return [SimpleTypedList]
181
195
  def column_info
@@ -311,7 +325,7 @@ module Axlsx
311
325
  # @param [String] name
312
326
  def name=(name)
313
327
  validate_sheet_name name
314
- @name=Axlsx::coder.encode(name)
328
+ @name = Axlsx.coder.encode(name)
315
329
  end
316
330
 
317
331
  # The auto filter range for the worksheet
@@ -329,13 +343,13 @@ module Axlsx
329
343
  # The part name of this worksheet
330
344
  # @return [String]
331
345
  def pn
332
- "#{WORKSHEET_PN % (index+1)}"
346
+ format(WORKSHEET_PN, index + 1)
333
347
  end
334
348
 
335
349
  # The relationship part name of this worksheet
336
350
  # @return [String]
337
351
  def rels_pn
338
- "#{WORKSHEET_RELS_PN % (index+1)}"
352
+ format(WORKSHEET_RELS_PN, index + 1)
339
353
  end
340
354
 
341
355
  # The relationship id of this worksheet.
@@ -405,14 +419,14 @@ module Axlsx
405
419
  # @option options [Array] values
406
420
  # @option options [Array, Symbol] types
407
421
  # @option options [Array, Integer] style
408
- # @option options [Array] widths each member of the widths array will affect how auto_fit behavies.
422
+ # @option options [Array] widths each member of the widths array will affect how auto_fit behaves.
409
423
  # @option options [Float] height the row's height (in points)
410
424
  # @option options [Integer] offset - add empty columns before values
411
425
  # @option options [Array, Boolean] escape_formulas - Whether to treat a value starting with an equal
412
426
  # sign as formula (default) or as simple string.
413
427
  # Allowing user generated data to be interpreted as formulas can be dangerous
414
428
  # (see https://www.owasp.org/index.php/CSV_Injection for details).
415
- def add_row(values=[], options={})
429
+ def add_row(values = [], options = {})
416
430
  row = Row.new(self, values, options)
417
431
  update_column_info row, options.delete(:widths)
418
432
  yield row if block_given?
@@ -432,7 +446,7 @@ module Axlsx
432
446
  # @see ConditionalFormattingRule#initialize
433
447
  # @see file:examples/example_conditional_formatting.rb
434
448
  def add_conditional_formatting(cells, rules)
435
- cf = ConditionalFormatting.new( :sqref => cells )
449
+ cf = ConditionalFormatting.new(sqref: cells)
436
450
  cf.add_rules rules
437
451
  conditional_formattings << cf
438
452
  conditional_formattings
@@ -453,7 +467,7 @@ module Axlsx
453
467
  # @param [Hash] options for the hyperlink
454
468
  # @see WorksheetHyperlink for a list of options
455
469
  # @return [WorksheetHyperlink]
456
- def add_hyperlink(options={})
470
+ def add_hyperlink(options = {})
457
471
  hyperlinks.add(options)
458
472
  end
459
473
 
@@ -470,54 +484,54 @@ module Axlsx
470
484
  # @see Bar3DChart
471
485
  # @see Line3DChart
472
486
  # @see README for examples
473
- def add_chart(chart_type, options={})
487
+ def add_chart(chart_type, options = {})
474
488
  chart = worksheet_drawing.add_chart(chart_type, options)
475
489
  yield chart if block_given?
476
490
  chart
477
491
  end
478
492
 
479
493
  # needs documentation
480
- def add_table(ref, options={})
494
+ def add_table(ref, options = {})
481
495
  tables << Table.new(ref, self, options)
482
496
  yield tables.last if block_given?
483
497
  tables.last
484
498
  end
485
499
 
486
- def add_pivot_table(ref, range, options={})
500
+ def add_pivot_table(ref, range, options = {})
487
501
  pivot_tables << PivotTable.new(ref, range, self, options)
488
502
  yield pivot_tables.last if block_given?
489
503
  pivot_tables.last
490
504
  end
491
505
 
492
506
  # Shortcut to worsksheet_comments#add_comment
493
- def add_comment(options={})
507
+ def add_comment(options = {})
494
508
  worksheet_comments.add_comment(options)
495
509
  end
496
510
 
497
511
  # Adds a media item to the worksheets drawing
498
512
  # @option [Hash] options options passed to drawing.add_image
499
- def add_image(options={})
513
+ def add_image(options = {})
500
514
  image = worksheet_drawing.add_image(options)
501
515
  yield image if block_given?
502
516
  image
503
517
  end
504
518
 
505
519
  # Adds a page break (row break) to the worksheet
506
- # @param cell A Cell object or excel style string reference indicating where the break
520
+ # @param cell A Cell object or Excel style string reference indicating where the break
507
521
  # should be added to the sheet.
508
522
  # @example
509
523
  # ws.add_page_break("A4")
510
524
  def add_page_break(cell)
511
525
  DataTypeValidator.validate :worksheet_page_break, [String, Cell], cell
512
526
  column_index, row_index = if cell.is_a?(String)
513
- Axlsx.name_to_indices(cell)
514
- else
515
- cell.pos
516
- end
527
+ Axlsx.name_to_indices(cell)
528
+ else
529
+ cell.pos
530
+ end
517
531
  if column_index > 0
518
- col_breaks.add_break(:id => column_index)
532
+ col_breaks.add_break(id: column_index)
519
533
  end
520
- row_breaks.add_break(:id => row_index)
534
+ row_breaks.add_break(id: row_index)
521
535
  end
522
536
 
523
537
  # This is a helper method that Lets you specify a fixed width for multiple columns in a worksheet in one go.
@@ -529,12 +543,26 @@ module Axlsx
529
543
  # @param [Integer|Float|nil] widths
530
544
  def column_widths(*widths)
531
545
  widths.each_with_index do |value, index|
532
- next if value == nil
533
- Axlsx::validate_unsigned_numeric(value) unless value == nil
546
+ next if value.nil?
547
+
548
+ Axlsx.validate_unsigned_numeric(value) unless value.nil?
534
549
  find_or_create_column_info(index).width = value
535
550
  end
536
551
  end
537
552
 
553
+ # This is a helper method that lets you specify a default style for multiple columns in a worksheet in one go.
554
+ # This style will be applied for all not-yet-defined cells in a column.
555
+ # Note that you must call column_styles BEFORE adding data, otherwise the styles will not be set successfully for new cells.
556
+ # @param [Integer] styles the cellXfs indexes
557
+ def column_styles(*styles)
558
+ styles.each_with_index do |style, index|
559
+ next if style.nil?
560
+
561
+ Axlsx.validate_unsigned_int(style)
562
+ find_or_create_column_info(index).style = style
563
+ end
564
+ end
565
+
538
566
  # Set the style for cells in a specific column
539
567
  # @param [Integer] index the index of the column
540
568
  # @param [Integer] style the cellXfs index
@@ -543,7 +571,7 @@ module Axlsx
543
571
  # @note You can also specify the style for specific columns in the call to add_row by using an array for the :styles option
544
572
  # @see Worksheet#add_row
545
573
  # @see README.md for an example
546
- def col_style(index, style, options={})
574
+ def col_style(index, style, options = {})
547
575
  offset = options.delete(:row_offset) || 0
548
576
  cells = @rows[(offset..-1)].map { |row| row[index] }.flatten.compact
549
577
  cells.each { |cell| cell.style = style }
@@ -557,17 +585,17 @@ module Axlsx
557
585
  # @note You can also specify the style in the add_row call
558
586
  # @see Worksheet#add_row
559
587
  # @see README.md for an example
560
- def row_style(index, style, options={})
588
+ def row_style(index, style, options = {})
561
589
  offset = options.delete(:col_offset) || 0
562
590
  cells = cols[(offset..-1)].map { |column| column[index] }.flatten.compact
563
591
  cells.each { |cell| cell.style = style }
564
592
  end
565
593
 
566
594
  # Set the style for cells in a specific column
567
- # @param [String|Array] cell references
595
+ # @param [String|Array] cell_refs Cell references
568
596
  # @param [Hash] styles
569
597
  def add_style(cell_refs, *styles)
570
- if !cell_refs.is_a?(Array)
598
+ unless cell_refs.is_a?(Array)
571
599
  cell_refs = [cell_refs]
572
600
  end
573
601
 
@@ -585,32 +613,43 @@ module Axlsx
585
613
  end
586
614
 
587
615
  # Set the style for cells in a specific column
588
- # @param [String|Array] cell references
589
- # @param [Hash|Array|Symbol] border options
590
- def add_border(cell_refs, options = Axlsx::Border::EDGES)
591
- if !cell_refs.is_a?(Array)
616
+ # @param [String|Array] cell_refs Cell references
617
+ # @param [Hash|Array|Symbol] options border options
618
+ def add_border(cell_refs, options = nil)
619
+ if options.is_a?(Hash)
620
+ border_edges = options[:edges]
621
+ border_style = options[:style]
622
+ border_color = options[:color]
623
+ else
624
+ border_edges = options
625
+ end
626
+
627
+ unless cell_refs.is_a?(Array)
592
628
  cell_refs = [cell_refs]
593
629
  end
594
630
 
595
631
  cell_refs.each do |cell_ref|
596
- cells = self[cell_ref]
597
- Axlsx::BorderCreator.new(self, cells, options).draw
632
+ item = self[cell_ref]
633
+
634
+ cells = item.is_a?(Array) ? item : [item]
635
+
636
+ Axlsx::BorderCreator.new(worksheet: self, cells: cells, edges: border_edges, style: border_style, color: border_color).draw
598
637
  end
599
638
  end
600
639
 
601
640
  # Returns a sheet node serialization for this sheet in the workbook.
602
- def to_sheet_node_xml_string(str='')
641
+ def to_sheet_node_xml_string(str = +'')
603
642
  add_autofilter_defined_name_to_workbook
604
643
  str << '<sheet '
605
644
  serialized_attributes str
606
- str << ('name="' << name << '" ')
607
- str << ('r:id="' << rId << '"></sheet>')
645
+ str << 'name="' << name << '" '
646
+ str << 'r:id="' << rId << '"></sheet>'
608
647
  end
609
648
 
610
649
  # Serializes the worksheet object to an xml string
611
650
  # This intentionally does not use nokogiri for performance reasons
612
651
  # @return [String]
613
- def to_xml_string str=''
652
+ def to_xml_string(str = +'')
614
653
  add_autofilter_defined_name_to_workbook
615
654
  auto_filter.apply if auto_filter.range
616
655
  str << '<?xml version="1.0" encoding="UTF-8"?>'
@@ -625,31 +664,30 @@ module Axlsx
625
664
  # @return [Relationships]
626
665
  def relationships
627
666
  r = Relationships.new
628
- r + [tables.relationships,
629
- worksheet_comments.relationships,
630
- hyperlinks.relationships,
631
- worksheet_drawing.relationship,
632
- pivot_tables.relationships].flatten.compact || []
633
- r
667
+ r.concat [tables.relationships,
668
+ worksheet_comments.relationships,
669
+ hyperlinks.relationships,
670
+ worksheet_drawing.relationship,
671
+ pivot_tables.relationships].flatten.compact
634
672
  end
635
673
 
636
- # Returns the cell or cells defined using excel style A1:B3 references.
674
+ # Returns the cell or cells defined using Excel style A1:B3 references.
637
675
  # @param [String|Integer] cell_def the string defining the cell or range of cells, or the rownumber
638
676
  # @return [Cell, Array]
639
677
  def [](cell_def)
640
678
  return rows[cell_def] if cell_def.is_a?(Integer)
641
679
 
642
- parts = cell_def.split(':').map{ |part| name_to_cell part }
680
+ parts = cell_def.split(':').map { |part| name_to_cell part }
643
681
 
644
682
  if parts.size == 1
645
683
  parts.first
646
684
  else
647
685
  if parts.size > 2
648
- raise ArgumentError, (ERR_CELL_REFERENCE_INVALID % cell_def)
686
+ raise ArgumentError, format(ERR_CELL_REFERENCE_INVALID, cell_def)
649
687
  elsif parts.first.nil?
650
- raise ArgumentError, (ERR_CELL_REFERENCE_MISSING_CELL % [cell_def.split(":").first, cell_def])
688
+ raise ArgumentError, format(ERR_CELL_REFERENCE_MISSING_CELL, cell_def.split(":").first, cell_def)
651
689
  elsif parts.last.nil?
652
- raise ArgumentError, (ERR_CELL_REFERENCE_MISSING_CELL % [cell_def.split(":").last, cell_def])
690
+ raise ArgumentError, format(ERR_CELL_REFERENCE_MISSING_CELL, cell_def.split(":").last, cell_def)
653
691
  end
654
692
 
655
693
  range(*parts)
@@ -660,12 +698,12 @@ module Axlsx
660
698
  # @param [String] name The cell or cell range to return. "A1" will return the first cell of the first row.
661
699
  # @return [Cell]
662
700
  def name_to_cell(name)
663
- col_index, row_index = *Axlsx::name_to_indices(name)
701
+ col_index, row_index = *Axlsx.name_to_indices(name)
664
702
 
665
703
  r = rows[row_index]
666
704
 
667
705
  if r
668
- return r[col_index]
706
+ r[col_index]
669
707
  end
670
708
  end
671
709
 
@@ -683,11 +721,11 @@ module Axlsx
683
721
  # @note The XLSX format does not support worksheet-specific styles. Even when using this method
684
722
  # you're still working with the single global {Axlsx::Styles} object in the workbook.
685
723
  def styles
686
- @styles ||= self.workbook.styles
724
+ @styles ||= workbook.styles
687
725
  end
688
726
 
689
727
  # shortcut level to specify the outline level for a series of rows
690
- # Oulining is what lets you add collapse and expand to a data set.
728
+ # Outlining is what lets you add collapse and expand to a data set.
691
729
  # @param [Integer] start_index The zero based index of the first row of outlining.
692
730
  # @param [Integer] end_index The zero based index of the last row to be outlined
693
731
  # @param [integer] level The level of outline to apply
@@ -697,7 +735,7 @@ module Axlsx
697
735
  end
698
736
 
699
737
  # shortcut level to specify the outline level for a series of columns
700
- # Oulining is what lets you add collapse and expand to a data set.
738
+ # Outlining is what lets you add collapse and expand to a data set.
701
739
  # @param [Integer] start_index The zero based index of the first column of outlining.
702
740
  # @param [Integer] end_index The zero based index of the last column to be outlined
703
741
  # @param [integer] level The level of outline to apply
@@ -713,7 +751,7 @@ module Axlsx
713
751
  end
714
752
 
715
753
  def outline(collection, range, level = 1, collapsed = true)
716
- range.each do |index|
754
+ range.each do |index|
717
755
  unless (item = collection[index]).nil?
718
756
  item.outline_level = level
719
757
  item.hidden = collapsed
@@ -725,13 +763,15 @@ module Axlsx
725
763
  def validate_sheet_name(name)
726
764
  DataTypeValidator.validate :worksheet_name, String, name
727
765
  # ignore first character (BOM) after encoding to utf16 because Excel does so, too.
728
- raise ArgumentError, (ERR_SHEET_NAME_EMPTY) if name.empty?
766
+ raise ArgumentError, ERR_SHEET_NAME_EMPTY if name.empty?
767
+
729
768
  character_length = name.encode("utf-16")[1..-1].encode("utf-16").bytesize / 2
730
- raise ArgumentError, (ERR_SHEET_NAME_TOO_LONG % name) if character_length > WORKSHEET_MAX_NAME_LENGTH
731
- raise ArgumentError, (ERR_SHEET_NAME_CHARACTER_FORBIDDEN % name) if WORKSHEET_NAME_FORBIDDEN_CHARS.any? { |char| name.include? char }
732
- name = Axlsx::coder.encode(name)
733
- sheet_names = @workbook.worksheets.reject { |s| s == self }.map { |s| s.name }
734
- raise ArgumentError, (ERR_DUPLICATE_SHEET_NAME % name) if sheet_names.include?(name)
769
+ raise ArgumentError, format(ERR_SHEET_NAME_TOO_LONG, name) if character_length > WORKSHEET_MAX_NAME_LENGTH && Axlsx.validate_sheet_name_length
770
+ raise ArgumentError, format(ERR_SHEET_NAME_CHARACTER_FORBIDDEN, name) if WORKSHEET_NAME_FORBIDDEN_CHARS.any? { |char| name.include? char }
771
+
772
+ name = Axlsx.coder.encode(name)
773
+ sheet_names = @workbook.worksheets.reject { |s| s == self }.map(&:name)
774
+ raise ArgumentError, format(ERR_DUPLICATE_SHEET_NAME, name) if sheet_names.include?(name)
735
775
  end
736
776
 
737
777
  def serializable_parts
@@ -783,7 +823,6 @@ module Axlsx
783
823
  @merged_cells ||= MergedCells.new self
784
824
  end
785
825
 
786
-
787
826
  # Helper method for parsingout the root node for worksheet
788
827
  # @return [String]
789
828
  def worksheet_node
@@ -804,13 +843,15 @@ module Axlsx
804
843
  @worksheet_comments ||= WorksheetComments.new self
805
844
  end
806
845
 
807
- def workbook=(v) DataTypeValidator.validate "Worksheet.workbook", Workbook, v; @workbook = v; end
846
+ def workbook=(v)
847
+ DataTypeValidator.validate "Worksheet.workbook", Workbook, v
848
+ @workbook = v
849
+ end
808
850
 
809
851
  def update_column_info(cells, widths = nil)
810
852
  cells.each_with_index do |cell, index|
811
853
  width = widths ? widths[index] : nil
812
854
  col = find_or_create_column_info(index)
813
- next if width == :ignore
814
855
 
815
856
  col.update_width(cell, width, workbook.use_autowidth)
816
857
  end
@@ -821,9 +862,9 @@ module Axlsx
821
862
  end
822
863
 
823
864
  def add_autofilter_defined_name_to_workbook
824
- return if !auto_filter.range
865
+ return unless auto_filter.range
866
+
825
867
  workbook.add_defined_name auto_filter.defined_name, name: '_xlnm._FilterDatabase', local_sheet_id: index, hidden: 1
826
868
  end
827
-
828
869
  end
829
870
  end
@@ -1,13 +1,14 @@
1
- module Axlsx
1
+ # frozen_string_literal: true
2
2
 
3
- # A wraper class for comments that defines its on worksheet
4
- # serailization
3
+ module Axlsx
4
+ # A wrapper class for comments that defines its on worksheet
5
+ # serialization
5
6
  class WorksheetComments
6
-
7
7
  # Creates a new WorksheetComments object
8
- # param [Worksheet] worksheet The worksheet comments in thes object belong to
8
+ # param [Worksheet] worksheet The worksheet to which the comments belong
9
9
  def initialize(worksheet)
10
10
  raise ArugumentError, 'You must provide a worksheet' unless worksheet.is_a?(Worksheet)
11
+
11
12
  @worksheet = worksheet
12
13
  end
13
14
 
@@ -22,21 +23,21 @@ module Axlsx
22
23
  # Adds a comment
23
24
  # @param [Hash] options
24
25
  # @see Comments#add_comment
25
- def add_comment(options={})
26
+ def add_comment(options = {})
26
27
  comments.add_comment(options)
27
- end
28
+ end
28
29
 
29
30
  # The relationships defined by this objects comments collection
30
31
  # @return [Relationships]
31
32
  def relationships
32
33
  return [] unless has_comments?
34
+
33
35
  comments.relationships
34
36
  end
35
37
 
36
-
37
38
  # Helper method to tell us if there are comments in the comments collection
38
39
  # @return [Boolean]
39
- def has_comments?
40
+ def has_comments? # rubocop:disable Naming/PredicatePrefix
40
41
  !comments.empty?
41
42
  end
42
43
 
@@ -44,14 +45,15 @@ module Axlsx
44
45
  # @see Relationship#Id
45
46
  # @return [String]
46
47
  def drawing_rId
47
- comments.relationships.find{ |r| r.Type == VML_DRAWING_R }.Id
48
+ comments.relationships.find { |r| r.Type == VML_DRAWING_R }.Id
48
49
  end
49
50
 
50
51
  # Seraalize the object
51
52
  # @param [String] str
52
53
  # @return [String]
53
- def to_xml_string(str = '')
54
+ def to_xml_string(str = +'')
54
55
  return unless has_comments?
56
+
55
57
  str << "<legacyDrawing r:id='#{drawing_rId}' />"
56
58
  end
57
59
  end
@@ -1,21 +1,26 @@
1
- module Axlsx
1
+ # frozen_string_literal: true
2
2
 
3
+ module Axlsx
3
4
  # This is a utility class for serialing the drawing node in a
4
5
  # worksheet. Drawing objects have their own serialization that exports
5
6
  # a drawing document. This is only for the single node in the
6
7
  # worksheet
7
8
  class WorksheetDrawing
8
-
9
9
  # Creates a new WorksheetDrawing
10
10
  # @param [Worksheet] worksheet
11
11
  def initialize(worksheet)
12
12
  raise ArgumentError, 'you must provide a worksheet' unless worksheet.is_a?(Worksheet)
13
+
13
14
  @worksheet = worksheet
14
15
  @drawing = nil
15
16
  end
16
17
 
18
+ # The worksheet that owns the drawing
19
+ # @return [Worksheet]
17
20
  attr_reader :worksheet
18
21
 
22
+ # The drawing object
23
+ # @return [Drawing]
19
24
  attr_reader :drawing
20
25
 
21
26
  # adds a chart to the drawing object
@@ -37,7 +42,7 @@ module Axlsx
37
42
 
38
43
  # helper method to tell us if the drawing has something in it or not
39
44
  # @return [Boolean]
40
- def has_drawing?
45
+ def has_drawing? # rubocop:disable Naming/PredicatePrefix
41
46
  @drawing.is_a? Drawing
42
47
  end
43
48
 
@@ -45,13 +50,15 @@ module Axlsx
45
50
  # @return [Relationship]
46
51
  def relationship
47
52
  return unless has_drawing?
53
+
48
54
  Relationship.new(self, DRAWING_R, "../#{drawing.pn}")
49
55
  end
50
56
 
51
57
  # Serialize the drawing for the worksheet
52
58
  # @param [String] str
53
- def to_xml_string(str = '')
59
+ def to_xml_string(str = +'')
54
60
  return unless has_drawing?
61
+
55
62
  str << "<drawing r:id='#{relationship.Id}'/>"
56
63
  end
57
64
  end