caxlsx 3.3.0 → 4.0.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 (151) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +25 -0
  3. data/README.md +25 -7
  4. data/Rakefile +7 -6
  5. data/lib/axlsx/content_type/abstract_content_type.rb +11 -9
  6. data/lib/axlsx/content_type/content_type.rb +7 -9
  7. data/lib/axlsx/content_type/default.rb +4 -6
  8. data/lib/axlsx/content_type/override.rb +3 -5
  9. data/lib/axlsx/doc_props/app.rb +27 -30
  10. data/lib/axlsx/doc_props/core.rb +9 -12
  11. data/lib/axlsx/drawing/area_chart.rb +13 -14
  12. data/lib/axlsx/drawing/area_series.rb +13 -14
  13. data/lib/axlsx/drawing/ax_data_source.rb +3 -6
  14. data/lib/axlsx/drawing/axes.rb +10 -9
  15. data/lib/axlsx/drawing/axis.rb +27 -30
  16. data/lib/axlsx/drawing/bar_3D_chart.rb +17 -18
  17. data/lib/axlsx/drawing/bar_chart.rb +16 -17
  18. data/lib/axlsx/drawing/bar_series.rb +9 -13
  19. data/lib/axlsx/drawing/bubble_chart.rb +8 -9
  20. data/lib/axlsx/drawing/bubble_series.rb +9 -10
  21. data/lib/axlsx/drawing/cat_axis.rb +14 -17
  22. data/lib/axlsx/drawing/chart.rb +25 -28
  23. data/lib/axlsx/drawing/d_lbls.rb +29 -26
  24. data/lib/axlsx/drawing/drawing.rb +60 -62
  25. data/lib/axlsx/drawing/graphic_frame.rb +6 -7
  26. data/lib/axlsx/drawing/hyperlink.rb +12 -13
  27. data/lib/axlsx/drawing/line_3D_chart.rb +13 -15
  28. data/lib/axlsx/drawing/line_chart.rb +13 -14
  29. data/lib/axlsx/drawing/line_series.rb +13 -14
  30. data/lib/axlsx/drawing/marker.rb +14 -16
  31. data/lib/axlsx/drawing/num_data.rb +13 -16
  32. data/lib/axlsx/drawing/num_data_source.rb +11 -13
  33. data/lib/axlsx/drawing/num_val.rb +9 -10
  34. data/lib/axlsx/drawing/one_cell_anchor.rb +10 -10
  35. data/lib/axlsx/drawing/pic.rb +57 -22
  36. data/lib/axlsx/drawing/picture_locking.rb +6 -7
  37. data/lib/axlsx/drawing/pie_3D_chart.rb +6 -9
  38. data/lib/axlsx/drawing/pie_series.rb +9 -12
  39. data/lib/axlsx/drawing/scaling.rb +9 -10
  40. data/lib/axlsx/drawing/scatter_chart.rb +9 -10
  41. data/lib/axlsx/drawing/scatter_series.rb +15 -16
  42. data/lib/axlsx/drawing/ser_axis.rb +9 -11
  43. data/lib/axlsx/drawing/series.rb +8 -8
  44. data/lib/axlsx/drawing/series_title.rb +6 -6
  45. data/lib/axlsx/drawing/str_data.rb +10 -13
  46. data/lib/axlsx/drawing/str_val.rb +8 -9
  47. data/lib/axlsx/drawing/title.rb +23 -27
  48. data/lib/axlsx/drawing/two_cell_anchor.rb +8 -8
  49. data/lib/axlsx/drawing/val_axis.rb +5 -6
  50. data/lib/axlsx/drawing/view_3D.rb +32 -30
  51. data/lib/axlsx/drawing/vml_drawing.rb +19 -20
  52. data/lib/axlsx/drawing/vml_shape.rb +25 -26
  53. data/lib/axlsx/package.rb +81 -79
  54. data/lib/axlsx/rels/relationship.rb +30 -28
  55. data/lib/axlsx/rels/relationships.rb +7 -8
  56. data/lib/axlsx/stylesheet/border.rb +7 -8
  57. data/lib/axlsx/stylesheet/border_pr.rb +8 -8
  58. data/lib/axlsx/stylesheet/cell_alignment.rb +14 -20
  59. data/lib/axlsx/stylesheet/cell_protection.rb +6 -7
  60. data/lib/axlsx/stylesheet/cell_style.rb +12 -14
  61. data/lib/axlsx/stylesheet/color.rb +15 -12
  62. data/lib/axlsx/stylesheet/dxf.rb +7 -9
  63. data/lib/axlsx/stylesheet/fill.rb +3 -5
  64. data/lib/axlsx/stylesheet/font.rb +24 -21
  65. data/lib/axlsx/stylesheet/gradient_fill.rb +9 -9
  66. data/lib/axlsx/stylesheet/gradient_stop.rb +7 -6
  67. data/lib/axlsx/stylesheet/num_fmt.rb +9 -14
  68. data/lib/axlsx/stylesheet/pattern_fill.rb +8 -8
  69. data/lib/axlsx/stylesheet/styles.rb +104 -98
  70. data/lib/axlsx/stylesheet/table_style.rb +8 -9
  71. data/lib/axlsx/stylesheet/table_style_element.rb +7 -8
  72. data/lib/axlsx/stylesheet/table_styles.rb +8 -10
  73. data/lib/axlsx/stylesheet/xf.rb +21 -22
  74. data/lib/axlsx/util/accessors.rb +6 -6
  75. data/lib/axlsx/util/buffered_zip_output_stream.rb +60 -0
  76. data/lib/axlsx/util/constants.rb +119 -108
  77. data/lib/axlsx/util/mime_type_utils.rb +11 -0
  78. data/lib/axlsx/util/options_parser.rb +4 -3
  79. data/lib/axlsx/util/serialized_attributes.rb +45 -21
  80. data/lib/axlsx/util/simple_typed_list.rb +58 -57
  81. data/lib/axlsx/util/storage.rb +38 -41
  82. data/lib/axlsx/util/validators.rb +107 -44
  83. data/lib/axlsx/util/zip_command.rb +10 -12
  84. data/lib/axlsx/version.rb +3 -2
  85. data/lib/axlsx/workbook/defined_name.rb +11 -8
  86. data/lib/axlsx/workbook/defined_names.rb +4 -3
  87. data/lib/axlsx/workbook/shared_strings_table.rb +10 -11
  88. data/lib/axlsx/workbook/workbook.rb +121 -114
  89. data/lib/axlsx/workbook/workbook_view.rb +8 -11
  90. data/lib/axlsx/workbook/workbook_views.rb +4 -4
  91. data/lib/axlsx/workbook/worksheet/auto_filter/auto_filter.rb +72 -14
  92. data/lib/axlsx/workbook/worksheet/auto_filter/filter_column.rb +11 -7
  93. data/lib/axlsx/workbook/worksheet/auto_filter/filters.rb +24 -21
  94. data/lib/axlsx/workbook/worksheet/auto_filter/sort_condition.rb +51 -0
  95. data/lib/axlsx/workbook/worksheet/auto_filter/sort_state.rb +56 -0
  96. data/lib/axlsx/workbook/worksheet/border_creator.rb +30 -25
  97. data/lib/axlsx/workbook/worksheet/break.rb +4 -5
  98. data/lib/axlsx/workbook/worksheet/cell.rb +92 -65
  99. data/lib/axlsx/workbook/worksheet/cell_serializer.rb +32 -28
  100. data/lib/axlsx/workbook/worksheet/cfvo.rb +7 -5
  101. data/lib/axlsx/workbook/worksheet/cfvos.rb +5 -5
  102. data/lib/axlsx/workbook/worksheet/col.rb +9 -10
  103. data/lib/axlsx/workbook/worksheet/col_breaks.rb +8 -7
  104. data/lib/axlsx/workbook/worksheet/color_scale.rb +16 -16
  105. data/lib/axlsx/workbook/worksheet/cols.rb +9 -7
  106. data/lib/axlsx/workbook/worksheet/comment.rb +12 -11
  107. data/lib/axlsx/workbook/worksheet/comments.rb +10 -12
  108. data/lib/axlsx/workbook/worksheet/conditional_formatting.rb +12 -8
  109. data/lib/axlsx/workbook/worksheet/conditional_formatting_rule.rb +19 -21
  110. data/lib/axlsx/workbook/worksheet/conditional_formattings.rb +5 -5
  111. data/lib/axlsx/workbook/worksheet/data_bar.rb +29 -30
  112. data/lib/axlsx/workbook/worksheet/data_validation.rb +34 -33
  113. data/lib/axlsx/workbook/worksheet/data_validations.rb +5 -6
  114. data/lib/axlsx/workbook/worksheet/date_time_converter.rb +8 -8
  115. data/lib/axlsx/workbook/worksheet/dimension.rb +9 -6
  116. data/lib/axlsx/workbook/worksheet/header_footer.rb +4 -3
  117. data/lib/axlsx/workbook/worksheet/icon_set.rb +24 -8
  118. data/lib/axlsx/workbook/worksheet/merged_cells.rb +10 -10
  119. data/lib/axlsx/workbook/worksheet/outline_pr.rb +6 -3
  120. data/lib/axlsx/workbook/worksheet/page_margins.rb +17 -12
  121. data/lib/axlsx/workbook/worksheet/page_set_up_pr.rb +6 -4
  122. data/lib/axlsx/workbook/worksheet/page_setup.rb +128 -129
  123. data/lib/axlsx/workbook/worksheet/pane.rb +27 -26
  124. data/lib/axlsx/workbook/worksheet/pivot_table.rb +23 -25
  125. data/lib/axlsx/workbook/worksheet/pivot_table_cache_definition.rb +12 -13
  126. data/lib/axlsx/workbook/worksheet/pivot_tables.rb +5 -4
  127. data/lib/axlsx/workbook/worksheet/print_options.rb +3 -2
  128. data/lib/axlsx/workbook/worksheet/protected_range.rb +6 -5
  129. data/lib/axlsx/workbook/worksheet/protected_ranges.rb +12 -10
  130. data/lib/axlsx/workbook/worksheet/rich_text.rb +6 -6
  131. data/lib/axlsx/workbook/worksheet/rich_text_run.rb +35 -17
  132. data/lib/axlsx/workbook/worksheet/row.rb +30 -22
  133. data/lib/axlsx/workbook/worksheet/row_breaks.rb +8 -7
  134. data/lib/axlsx/workbook/worksheet/selection.rb +16 -16
  135. data/lib/axlsx/workbook/worksheet/sheet_calc_pr.rb +10 -7
  136. data/lib/axlsx/workbook/worksheet/sheet_data.rb +7 -7
  137. data/lib/axlsx/workbook/worksheet/sheet_format_pr.rb +23 -19
  138. data/lib/axlsx/workbook/worksheet/sheet_pr.rb +11 -7
  139. data/lib/axlsx/workbook/worksheet/sheet_protection.rb +21 -20
  140. data/lib/axlsx/workbook/worksheet/sheet_view.rb +48 -53
  141. data/lib/axlsx/workbook/worksheet/table.rb +13 -13
  142. data/lib/axlsx/workbook/worksheet/table_style_info.rb +6 -5
  143. data/lib/axlsx/workbook/worksheet/tables.rb +7 -5
  144. data/lib/axlsx/workbook/worksheet/worksheet.rb +92 -63
  145. data/lib/axlsx/workbook/worksheet/worksheet_comments.rb +10 -8
  146. data/lib/axlsx/workbook/worksheet/worksheet_drawing.rb +11 -4
  147. data/lib/axlsx/workbook/worksheet/worksheet_hyperlink.rb +9 -8
  148. data/lib/axlsx/workbook/worksheet/worksheet_hyperlinks.rb +7 -5
  149. data/lib/axlsx.rb +75 -47
  150. data/lib/caxlsx.rb +3 -2
  151. metadata +50 -44
@@ -1,10 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Axlsx
2
4
  # The filterColumn collection identifies a particular column in the AutoFilter
3
5
  # range and specifies filter information that has been applied to this column.
4
6
  # If a column in the AutoFilter range has no criteria specified,
5
7
  # then there is no corresponding filterColumn collection expressed for that column.
6
8
  class FilterColumn
7
-
8
9
  include Axlsx::OptionsParser
9
10
  include Axlsx::SerializedAttributes
10
11
 
@@ -17,7 +18,7 @@ module Axlsx
17
18
  # @option [Boolean] show_button @see show_button
18
19
  def initialize(col_id, filter_type, options = {})
19
20
  RestrictionValidator.validate 'FilterColumn.filter', FILTERS, filter_type
20
- #Axlsx::validate_unsigned_int(col_id)
21
+ # Axlsx::validate_unsigned_int(col_id)
21
22
  self.col_id = col_id
22
23
  parse_options options
23
24
  @filter = Axlsx.const_get(Axlsx.camel(filter_type)).new(options)
@@ -27,7 +28,7 @@ module Axlsx
27
28
  serializable_attributes :col_id, :hidden_button, :show_button
28
29
 
29
30
  # Allowed filters
30
- FILTERS = [:filters] #, :top10, :custom_filters, :dynamic_filters, :color_filters, :icon_filters]
31
+ FILTERS = [:filters].freeze # , :top10, :custom_filters, :dynamic_filters, :color_filters, :icon_filters]
31
32
 
32
33
  # Zero-based index indicating the AutoFilter column to which this filter information applies.
33
34
  # @return [Integer]
@@ -52,7 +53,7 @@ module Axlsx
52
53
  end
53
54
 
54
55
  # Sets the col_id attribute for this filter column.
55
- # @param [Integer | Cell] column_index The zero based index of the column to which this filter applies.
56
+ # @param [Integer | Cell] column_index The zero based index of the column to which this filter applies.
56
57
  # When you specify a cell, the column index will be read off the cell
57
58
  # @return [Integer]
58
59
  def col_id=(column_index)
@@ -65,8 +66,9 @@ module Axlsx
65
66
  # @param [Array] row A row from a worksheet that needs to be
66
67
  # filtered.
67
68
  def apply(row, offset)
68
- row.hidden = @filter.apply(row.cells[offset+col_id.to_i])
69
+ row.hidden = @filter.apply(row.cells[offset + col_id.to_i])
69
70
  end
71
+
70
72
  # @param [Boolean] hidden Flag indicating whether the AutoFilter button for this column is hidden.
71
73
  # @return [Boolean]
72
74
  def hidden_button=(hidden)
@@ -85,8 +87,10 @@ module Axlsx
85
87
  end
86
88
 
87
89
  # Serialize the object to xml
88
- def to_xml_string(str='')
89
- str << "<filterColumn #{serialized_attributes}>"
90
+ def to_xml_string(str = +'')
91
+ str << '<filterColumn '
92
+ serialized_attributes(str)
93
+ str << '>'
90
94
  @filter.to_xml_string(str)
91
95
  str << "</filterColumn>"
92
96
  end
@@ -1,6 +1,7 @@
1
- module Axlsx
1
+ # frozen_string_literal: true
2
2
 
3
- # When multiple values are chosen to filter by, or when a group of date values are chosen to filter by,
3
+ module Axlsx
4
+ # When multiple values are chosen to filter by, or when a group of date values are chosen to filter by,
4
5
  # this object groups those criteria together.
5
6
  class Filters
6
7
  include Axlsx::OptionsParser
@@ -16,20 +17,20 @@ module Axlsx
16
17
  # @note The recommended way to interact with filter objects is via AutoFilter#add_column
17
18
  # @example
18
19
  # ws.auto_filter.add_column(0, :filters, :blank => true, :calendar_type => 'japan', :filter_items => [100, 'a'])
19
- def initialize(options={})
20
+ def initialize(options = {})
20
21
  parse_options options
21
22
  end
22
23
 
23
24
  serializable_attributes :blank, :calendar_type
24
25
 
25
26
  # Allowed calendar types
26
- CALENDAR_TYPES = %w(gregorian gregorianUs gregorianMeFrench gregorianArabic hijri hebrew taiwan japan thai korea saka gregorianXlitEnglish gregorianXlitFrench none)
27
+ CALENDAR_TYPES = %w(gregorian gregorianUs gregorianMeFrench gregorianArabic hijri hebrew taiwan japan thai korea saka gregorianXlitEnglish gregorianXlitFrench none).freeze
27
28
 
28
29
  # Flag indicating whether to filter by blank.
29
30
  # @return [Boolean]
30
31
  attr_reader :blank
31
32
 
32
- # Calendar type for date grouped items.
33
+ # Calendar type for date grouped items.
33
34
  # Used to interpret the values in dateGroupItem.
34
35
  # This is the calendar type used to evaluate all dates in the filter column,
35
36
  # even when those dates are not using the same calendar system / date formatting.
@@ -42,6 +43,7 @@ module Axlsx
42
43
  # TODO implement this for date filters as well!
43
44
  def apply(cell)
44
45
  return false unless cell
46
+
45
47
  filter_items.each do |filter|
46
48
  return false if cell.value == filter.val
47
49
  end
@@ -74,14 +76,16 @@ module Axlsx
74
76
  end
75
77
 
76
78
  # Serialize the object to xml
77
- def to_xml_string(str = '')
78
- str << "<filters #{serialized_attributes}>"
79
- filter_items.each { |filter| filter.to_xml_string(str) }
79
+ def to_xml_string(str = +'')
80
+ str << '<filters '
81
+ serialized_attributes(str)
82
+ str << '>'
83
+ filter_items.each { |filter| filter.to_xml_string(str) }
80
84
  date_group_items.each { |date_group_item| date_group_item.to_xml_string(str) }
81
85
  str << '</filters>'
82
86
  end
83
87
 
84
- # not entirely happy with this.
88
+ # not entirely happy with this.
85
89
  # filter_items should be a simple typed list that overrides << etc
86
90
  # to create Filter objects from the inserted values. However this
87
91
  # is most likely so rarely used...(really? do you know that?)
@@ -98,13 +102,13 @@ module Axlsx
98
102
  def date_group_items=(options)
99
103
  options.each do |date_group|
100
104
  raise ArgumentError, "date_group_items should be an array of hashes specifying the options for each date_group_item" unless date_group.is_a?(Hash)
105
+
101
106
  date_group_items << DateGroupItem.new(date_group)
102
107
  end
103
108
  end
104
109
 
105
110
  # This class expresses a filter criteria value.
106
111
  class Filter
107
-
108
112
  # Creates a new filter value object
109
113
  # @param [Any] value The value of the filter. This is not restricted, but
110
114
  # will be serialized via to_s so if you are passing an object
@@ -113,18 +117,16 @@ module Axlsx
113
117
  @val = value
114
118
  end
115
119
 
116
-
117
- #Filter value used in the criteria.
120
+ # Filter value used in the criteria.
118
121
  attr_accessor :val
119
122
 
120
123
  # Serializes the filter value object
121
124
  # @param [String] str The string to concact the serialization information to.
122
- def to_xml_string(str = '')
123
- str << "<filter val='#{@val.to_s}' />"
125
+ def to_xml_string(str = +'')
126
+ str << "<filter val='#{@val}' />"
124
127
  end
125
128
  end
126
129
 
127
-
128
130
  # This collection is used to express a group of dates or times which are
129
131
  # used in an AutoFilter criteria. Values are always written in the calendar
130
132
  # type of the first date encountered in the filter range, so that all
@@ -132,7 +134,7 @@ module Axlsx
132
134
  # types, can be correctly compared for the purposes of filtering.
133
135
  class DateGroupItem
134
136
  include Axlsx::OptionsParser
135
- include Axlsx::SerializedAttributes
137
+ include Axlsx::SerializedAttributes
136
138
 
137
139
  # Creates a new DateGroupItem
138
140
  # @param [Hash] options A hash of options to use when
@@ -145,16 +147,17 @@ include Axlsx::SerializedAttributes
145
147
  # @option [Integer] hour @see hour
146
148
  # @option [Integer] minute @see minute
147
149
  # @option [Integer] second @see second
148
- def initialize(options={})
149
- raise ArgumentError, "You must specify a year for date time grouping" unless options[:year]
150
+ def initialize(options = {})
151
+ raise ArgumentError, "You must specify a year for date time grouping" unless options[:year]
150
152
  raise ArgumentError, "You must specify a date_time_grouping when creating a DateGroupItem for auto filter" unless options[:date_time_grouping]
153
+
151
154
  parse_options options
152
155
  end
153
156
 
154
157
  serializable_attributes :date_time_grouping, :year, :month, :day, :hour, :minute, :second
155
158
 
156
159
  # Allowed date time groupings
157
- DATE_TIME_GROUPING = %w(year month day hour minute second)
160
+ DATE_TIME_GROUPING = %w(year month day hour minute second).freeze
158
161
 
159
162
  # Grouping level
160
163
  # This must be one of year, month, day, hour, minute or second.
@@ -200,7 +203,7 @@ include Axlsx::SerializedAttributes
200
203
  end
201
204
 
202
205
  # The day value for the date group item
203
- # This must be between 1 and 31
206
+ # This must be between 1 and 31
204
207
  # @note no attempt is made to ensure the date value is valid for any given month
205
208
  def day=(value)
206
209
  RangeValidator.validate "DateGroupItem.day", 0, 31, value
@@ -236,7 +239,7 @@ include Axlsx::SerializedAttributes
236
239
 
237
240
  # Serialize the object to xml
238
241
  # @param [String] str The string object this serialization will be concatenated to.
239
- def to_xml_string(str = '')
242
+ def to_xml_string(str = +'')
240
243
  serialized_tag('dateGroupItem', str)
241
244
  end
242
245
  end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Axlsx
4
+ # This class represents a individual sort condition belonging to the sort state of an auto filter
5
+ class SortCondition
6
+ # Creates a new SortCondition object
7
+ # @param [Integer] column_index Zero-based index indicating the AutoFilter column to which the sorting should be applied to
8
+ # @param [Symbol] order The order the column should be sorted on, can only be :asc or :desc
9
+ # @param [Array] custom_list An array containg a custom sorting list in order.
10
+ def initialize(column_index:, order:, custom_list:)
11
+ Axlsx.validate_int column_index
12
+ @column_index = column_index
13
+
14
+ RestrictionValidator.validate 'SortCondition.order', [:asc, :desc], order
15
+ @order = order
16
+
17
+ DataTypeValidator.validate :sort_condition_custom_list, Array, custom_list
18
+ @custom_list = custom_list
19
+ end
20
+
21
+ attr_reader :column_index, :order, :custom_list
22
+
23
+ # converts the ref String from the sort_state to a string representing the ref of a single column
24
+ # for the xml string to be returned.
25
+ def ref_to_single_column(ref, column_index)
26
+ first_cell, last_cell = ref.split(':')
27
+
28
+ start_point = Axlsx.name_to_indices(first_cell)
29
+
30
+ first_row = first_cell[/\d+/]
31
+ last_row = last_cell[/\d+/]
32
+
33
+ first_column = Axlsx.col_ref(column_index + start_point.first)
34
+ last_column = first_column
35
+
36
+ "#{first_column}#{first_row}:#{last_column}#{last_row}"
37
+ end
38
+
39
+ # serialize the object
40
+ # @return [String]
41
+ def to_xml_string(str, ref)
42
+ ref = ref_to_single_column(ref, column_index)
43
+
44
+ str << "<sortCondition "
45
+ str << "descending='1' " if order == :desc
46
+ str << "ref='#{ref}' "
47
+ str << "customList='#{custom_list.join(',')}' " unless custom_list.empty?
48
+ str << "/>"
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'axlsx/workbook/worksheet/auto_filter/sort_condition'
4
+
5
+ module Axlsx
6
+ # This class performs sorting on a range in a worksheet
7
+ class SortState
8
+ # creates a new SortState object
9
+ # @param [AutoFilter] auto_filter the auto_filter that this sort_state belongs to
10
+ def initialize(auto_filter)
11
+ @auto_filter = auto_filter
12
+ end
13
+
14
+ # A collection of SortConditions for this sort_state
15
+ # @return [SimpleTypedList]
16
+ def sort_conditions
17
+ @sort_conditions ||= SimpleTypedList.new SortCondition
18
+ end
19
+
20
+ # Adds a SortCondition to the sort_state. This is the recommended way to add conditions to it.
21
+ # It requires a column_index for the sorting, descending and the custom order are optional.
22
+ # @param [Integer] column_index Zero-based index indicating the AutoFilter column to which the sorting should be applied to
23
+ # @param [Symbol] order The order the column should be sorted on, can only be :asc or :desc
24
+ # @param [Array] custom_list An array containg a custom sorting list in order.
25
+ # @return [SortCondition]
26
+ def add_sort_condition(column_index:, order: :asc, custom_list: [])
27
+ sort_conditions << SortCondition.new(column_index: column_index, order: order, custom_list: custom_list)
28
+ sort_conditions.last
29
+ end
30
+
31
+ # method to increment the String representing the first cell of the range of the autofilter by 1 row for the sortCondition
32
+ # xml string
33
+ def increment_cell_value(str)
34
+ letter = str[/[A-Za-z]+/]
35
+ number = str[/\d+/].to_i
36
+
37
+ incremented_number = number + 1
38
+
39
+ "#{letter}#{incremented_number}"
40
+ end
41
+
42
+ # serialize the object
43
+ # @return [String]
44
+ def to_xml_string(str = +'')
45
+ return if sort_conditions.empty?
46
+
47
+ ref = @auto_filter.range
48
+ first_cell, last_cell = ref.split(':')
49
+ ref = "#{increment_cell_value(first_cell)}:#{last_cell}"
50
+
51
+ str << "<sortState xmlns:xlrd2='http://schemas.microsoft.com/office/spreadsheetml/2017/richdata2' ref='#{ref}'>"
52
+ sort_conditions.each { |sort_condition| sort_condition.to_xml_string(str, ref) }
53
+ str << "</sortState>"
54
+ end
55
+ end
56
+ end
@@ -1,39 +1,45 @@
1
- # encoding: UTF-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Axlsx
4
4
  class BorderCreator
5
- attr_reader :worksheet, :cells, :edges, :width, :color
6
-
7
- def initialize(worksheet, cells, args)
5
+ def initialize(worksheet:, cells:, edges: nil, style: nil, color: nil)
8
6
  @worksheet = worksheet
9
- @cells = cells
10
- if args.is_a?(Hash)
11
- @edges = args[:edges] || Axlsx::Border::EDGES
12
- @width = args[:style] || :thin
13
- @color = args[:color] || '000000'
14
- else
15
- @edges = args || Axlsx::Border::Edges
16
- @width = :thin
17
- @color = '000000'
18
- end
7
+ @cells = cells
8
+
9
+ @edges = edges || :all
10
+ @style = style || :thin
11
+ @color = color || "000000"
19
12
 
20
13
  if @edges == :all
21
14
  @edges = Axlsx::Border::EDGES
22
- elsif @edges.is_a?(Array)
23
- @edges = (@edges.map(&:to_sym).uniq & Axlsx::Border::EDGES)
15
+ elsif !@edges.is_a?(Array)
16
+ raise ArgumentError, "Invalid edges provided, #{@edges}"
24
17
  else
25
- @edges = []
18
+ @edges = @edges.map { |x| x&.to_sym }.uniq
19
+
20
+ unless (@edges - Axlsx::Border::EDGES).empty?
21
+ raise ArgumentError, "Invalid edges provided, #{edges}"
22
+ end
26
23
  end
27
24
  end
28
25
 
29
26
  def draw
30
- @edges.each do |edge|
31
- worksheet.add_style(
32
- border_cells[edge],
27
+ if @cells.size == 1
28
+ @worksheet.add_style(
29
+ first_cell,
33
30
  {
34
- border: {style: @width, color: @color, edges: [edge]}
31
+ border: { style: @style, color: @color, edges: @edges }
35
32
  }
36
33
  )
34
+ else
35
+ @edges.each do |edge|
36
+ @worksheet.add_style(
37
+ border_cells[edge],
38
+ {
39
+ border: { style: @style, color: @color, edges: [edge] }
40
+ }
41
+ )
42
+ end
37
43
  end
38
44
  end
39
45
 
@@ -44,16 +50,16 @@ module Axlsx
44
50
  top: "#{first_cell}:#{last_col}#{first_row}",
45
51
  right: "#{last_col}#{first_row}:#{last_cell}",
46
52
  bottom: "#{first_col}#{last_row}:#{last_cell}",
47
- left: "#{first_cell}:#{first_col}#{last_row}",
53
+ left: "#{first_cell}:#{first_col}#{last_row}"
48
54
  }
49
55
  end
50
56
 
51
57
  def first_cell
52
- @first_cell ||= cells.first.r
58
+ @first_cell ||= @cells.first.r
53
59
  end
54
60
 
55
61
  def last_cell
56
- @last_cell ||= cells.last.r
62
+ @last_cell ||= @cells.last.r
57
63
  end
58
64
 
59
65
  def first_row
@@ -71,6 +77,5 @@ module Axlsx
71
77
  def last_col
72
78
  @last_col ||= last_cell.scan(/\D+/).first
73
79
  end
74
-
75
80
  end
76
81
  end
@@ -1,9 +1,9 @@
1
- module Axlsx
1
+ # frozen_string_literal: true
2
2
 
3
+ module Axlsx
3
4
  # The Break class stores the details for row and column page breaks.
4
5
  # @see RowBreaks, ColBreaks
5
6
  class Break
6
-
7
7
  include Axlsx::OptionsParser
8
8
  include Axlsx::Accessors
9
9
  include Axlsx::SerializedAttributes
@@ -15,7 +15,7 @@ module Axlsx
15
15
  # @option options [Integer] max Zero-based index of end row or column of the break. For row breaks, specifies column index; for column breaks, specifies row index.
16
16
  # @option options [Boolean] man Manual Break flag. 1 means the break is a manually inserted break.
17
17
  # @option option [Boolean] pt Flag indicating that a PivotTable created this break.
18
- def initialize(options={})
18
+ def initialize(options = {})
19
19
  parse_options options
20
20
  yield self if block_given?
21
21
  end
@@ -27,9 +27,8 @@ module Axlsx
27
27
  serializable_attributes :id, :min, :max, :man, :pt
28
28
 
29
29
  # serializes the break to xml
30
- def to_xml_string(str='')
30
+ def to_xml_string(str = +'')
31
31
  serialized_tag('brk', str)
32
32
  end
33
33
  end
34
34
  end
35
-