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,20 +1,18 @@
1
-
2
- require 'axlsx/workbook/worksheet/auto_filter/filter_column.rb'
3
- require 'axlsx/workbook/worksheet/auto_filter/filters.rb'
1
+ # frozen_string_literal: true
4
2
 
5
3
  module Axlsx
6
-
7
- #This class represents an auto filter range in a worksheet
4
+ # This class represents an auto filter range in a worksheet
8
5
  class AutoFilter
9
-
10
6
  # creates a new Autofilter object
11
7
  # @param [Worksheet] worksheet
12
8
  def initialize(worksheet)
13
9
  raise ArgumentError, 'you must provide a worksheet' unless worksheet.is_a?(Worksheet)
10
+
14
11
  @worksheet = worksheet
12
+ @sort_on_generate = true
15
13
  end
16
14
 
17
- attr_reader :worksheet
15
+ attr_reader :worksheet, :sort_on_generate
18
16
 
19
17
  # The range the autofilter should be applied to.
20
18
  # This should be a string like 'A1:B8'
@@ -27,7 +25,26 @@ module Axlsx
27
25
  # @return [String]
28
26
  def defined_name
29
27
  return unless range
30
- Axlsx.cell_range(range.split(':').collect { |name| worksheet.name_to_cell(name)})
28
+
29
+ cells = range.split(':').collect do |name|
30
+ cell = worksheet.name_to_cell(name)
31
+ next cell if cell
32
+
33
+ # We're calculating the defined_name for the AutoFilter just before serializing,
34
+ # so in theory adding new rows or columns should not cause weird offset issues
35
+ col_index, row_index = *Axlsx.name_to_indices(name)
36
+ while (row = worksheet.rows[row_index]).nil?
37
+ worksheet.add_row
38
+ end
39
+
40
+ while (cell = row[col_index]).nil?
41
+ row.add_cell
42
+ end
43
+
44
+ cell
45
+ end
46
+
47
+ Axlsx.cell_range(cells)
31
48
  end
32
49
 
33
50
  # A collection of filterColumns for this auto_filter
@@ -47,31 +64,86 @@ module Axlsx
47
64
  columns.last
48
65
  end
49
66
 
50
- # actually performs the filtering of rows who's cells do not
51
- # match the filter.
67
+ # Performs the sorting of the rows based on the sort_state conditions. Then it actually performs
68
+ # the filtering of rows who's cells do not match the filter.
52
69
  def apply
53
70
  first_cell, last_cell = range.split(':')
54
- start_point = Axlsx::name_to_indices(first_cell)
55
- end_point = Axlsx::name_to_indices(last_cell)
71
+ start_point = Axlsx.name_to_indices(first_cell)
72
+ end_point = Axlsx.name_to_indices(last_cell)
56
73
  # The +1 is so we skip the header row with the filter drop downs
57
- rows = worksheet.rows[(start_point.last+1)..end_point.last] || []
74
+ rows = worksheet.rows[(start_point.last + 1)..end_point.last] || []
75
+
76
+ # the sorting of the rows if sort_conditions are available.
77
+ if !sort_state.sort_conditions.empty? && sort_on_generate
78
+ sort_conditions = sort_state.sort_conditions
79
+ sorted_rows = rows.sort do |row1, row2|
80
+ comparison = 0
81
+
82
+ sort_conditions.each do |condition|
83
+ cell_value_row1 = row1.cells[condition.column_index + start_point.first].value
84
+ cell_value_row2 = row2.cells[condition.column_index + start_point.first].value
85
+ custom_list = condition.custom_list
86
+ comparison = if cell_value_row1.nil? || cell_value_row2.nil?
87
+ cell_value_row1.nil? ? 1 : -1
88
+ elsif custom_list.empty?
89
+ condition.order == :asc ? cell_value_row1 <=> cell_value_row2 : cell_value_row2 <=> cell_value_row1
90
+ else
91
+ index1 = custom_list.index(cell_value_row1) || custom_list.size
92
+ index2 = custom_list.index(cell_value_row2) || custom_list.size
93
+
94
+ condition.order == :asc ? index1 <=> index2 : index2 <=> index1
95
+ end
96
+
97
+ break unless comparison == 0
98
+ end
99
+
100
+ comparison
101
+ end
102
+ insert_index = start_point.last + 1
103
+
104
+ sorted_rows.each do |row|
105
+ # Insert the row at the specified index
106
+ worksheet.rows[insert_index] = row
107
+ insert_index += 1
108
+ end
109
+ end
58
110
 
59
111
  column_offset = start_point.first
60
112
  columns.each do |column|
61
113
  rows.each do |row|
62
114
  next if row.hidden
115
+
63
116
  column.apply(row, column_offset)
64
117
  end
65
118
  end
66
119
  end
120
+
121
+ # the SortState object for this AutoFilter
122
+ # @return [SortState]
123
+ def sort_state
124
+ @sort_state ||= SortState.new self
125
+ end
126
+
127
+ # @param [Boolean] v Flag indicating whether the AutoFilter should sort the rows when generating the
128
+ # file. If false, the sorting rules will need to be applied manually after generating to alter
129
+ # the order of the rows.
130
+ # @return [Boolean]
131
+ def sort_on_generate=(v)
132
+ Axlsx.validate_boolean v
133
+ @sort_on_generate = v
134
+ end
135
+
67
136
  # serialize the object
68
137
  # @return [String]
69
- def to_xml_string(str='')
138
+ def to_xml_string(str = +'')
70
139
  return unless range
140
+
71
141
  str << "<autoFilter ref='#{range}'>"
72
142
  columns.each { |filter_column| filter_column.to_xml_string(str) }
143
+ unless @sort_state.nil?
144
+ @sort_state.to_xml_string(str)
145
+ end
73
146
  str << "</autoFilter>"
74
147
  end
75
-
76
148
  end
77
149
  end
@@ -1,15 +1,16 @@
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
 
11
12
  # Creates a new FilterColumn object
12
- # @note This class yeilds its filter object as that is where the vast majority of processing will be done
13
+ # @note This class yields its filter object as that is where the vast majority of processing will be done
13
14
  # @param [Integer|Cell] col_id The zero based index for the column to which this filter will be applied
14
15
  # @param [Symbol] filter_type The symbolized class name of the filter to apply to this column.
15
16
  # @param [Hash] options options for this object and the filter
@@ -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]
@@ -42,7 +43,9 @@ module Axlsx
42
43
  # the filter button can be hidden, and not drawn.
43
44
  # @return [Boolean]
44
45
  def show_button
45
- @show_button ||= true
46
+ return @show_button if defined?(@show_button)
47
+
48
+ true
46
49
  end
47
50
 
48
51
  # Flag indicating whether the AutoFilter button for this column is hidden.
@@ -52,7 +55,7 @@ module Axlsx
52
55
  end
53
56
 
54
57
  # 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.
58
+ # @param [Integer | Cell] column_index The zero based index of the column to which this filter applies.
56
59
  # When you specify a cell, the column index will be read off the cell
57
60
  # @return [Integer]
58
61
  def col_id=(column_index)
@@ -65,8 +68,9 @@ module Axlsx
65
68
  # @param [Array] row A row from a worksheet that needs to be
66
69
  # filtered.
67
70
  def apply(row, offset)
68
- row.hidden = @filter.apply(row.cells[offset+col_id.to_i])
71
+ row.hidden = @filter.apply(row.cells[offset + col_id.to_i])
69
72
  end
73
+
70
74
  # @param [Boolean] hidden Flag indicating whether the AutoFilter button for this column is hidden.
71
75
  # @return [Boolean]
72
76
  def hidden_button=(hidden)
@@ -81,12 +85,14 @@ module Axlsx
81
85
  # @return [Boolean]
82
86
  def show_button=(show)
83
87
  Axlsx.validate_boolean show
84
- @show_botton = show
88
+ @show_button = show
85
89
  end
86
90
 
87
91
  # Serialize the object to xml
88
- def to_xml_string(str='')
89
- str << "<filterColumn #{serialized_attributes}>"
92
+ def to_xml_string(str = +'')
93
+ str << '<filterColumn '
94
+ serialized_attributes(str)
95
+ str << '>'
90
96
  @filter.to_xml_string(str)
91
97
  str << "</filterColumn>"
92
98
  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
@@ -12,36 +13,37 @@ module Axlsx
12
13
  # @option [Boolean] blank @see blank
13
14
  # @option [String] calendar_type @see calendar_type
14
15
  # @option [Array] filter_items An array of values that will be used to create filter objects.
15
- # @option [Array] date_group_items An array of hases defining date group item filters to apply.
16
+ # @option [Array] date_group_items An array of hashes defining date group item filters to apply.
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.
36
37
  attr_reader :calendar_type
37
38
 
38
- # Tells us if the row of the cell provided should be filterd as it
39
+ # Tells us if the row of the cell provided should be filtered as it
39
40
  # does not meet any of the specified filter_items or
40
41
  # date_group_items restrictions.
41
42
  # @param [Cell] cell The cell to test against items
42
43
  # TODO implement this for date filters as well!
43
- def apply(cell)
44
+ def apply(cell) # rubocop:disable Naming/PredicateMethod
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,11 +134,11 @@ 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
139
- # instanciating the object
141
+ # instantiating the object
140
142
  # @option [String] date_time_grouping the part of the date this
141
143
  # filter should apply for grouping
142
144
  # @option [Integer|String] year @see year
@@ -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 containing 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,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Axlsx
4
+ # This class performs sorting on a range in a worksheet
5
+ class SortState
6
+ # creates a new SortState object
7
+ # @param [AutoFilter] auto_filter the auto_filter that this sort_state belongs to
8
+ def initialize(auto_filter)
9
+ @auto_filter = auto_filter
10
+ end
11
+
12
+ # A collection of SortConditions for this sort_state
13
+ # @return [SimpleTypedList]
14
+ def sort_conditions
15
+ @sort_conditions ||= SimpleTypedList.new SortCondition
16
+ end
17
+
18
+ # Adds a SortCondition to the sort_state. This is the recommended way to add conditions to it.
19
+ # It requires a column_index for the sorting, descending and the custom order are optional.
20
+ # @param [Integer] column_index Zero-based index indicating the AutoFilter column to which the sorting should be applied to
21
+ # @param [Symbol] order The order the column should be sorted on, can only be :asc or :desc
22
+ # @param [Array] custom_list An array containing a custom sorting list in order.
23
+ # @return [SortCondition]
24
+ def add_sort_condition(column_index:, order: :asc, custom_list: [])
25
+ sort_conditions << SortCondition.new(column_index: column_index, order: order, custom_list: custom_list)
26
+ sort_conditions.last
27
+ end
28
+
29
+ # method to increment the String representing the first cell of the range of the autofilter by 1 row for the sortCondition
30
+ # xml string
31
+ def increment_cell_value(str)
32
+ letter = str[/[A-Za-z]+/]
33
+ number = str[/\d+/].to_i
34
+
35
+ incremented_number = number + 1
36
+
37
+ "#{letter}#{incremented_number}"
38
+ end
39
+
40
+ # serialize the object
41
+ # @return [String]
42
+ def to_xml_string(str = +'')
43
+ return if sort_conditions.empty?
44
+
45
+ ref = @auto_filter.range
46
+ first_cell, last_cell = ref.split(':')
47
+ ref = "#{increment_cell_value(first_cell)}:#{last_cell}"
48
+
49
+ str << "<sortState xmlns:xlrd2='http://schemas.microsoft.com/office/spreadsheetml/2017/richdata2' ref='#{ref}'>"
50
+ sort_conditions.each { |sort_condition| sort_condition.to_xml_string(str, ref) }
51
+ str << "</sortState>"
52
+ end
53
+ end
54
+ 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,33 +50,32 @@ 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
60
- @first_row ||= first_cell.scan(/\d+/).first
66
+ @first_row ||= first_cell[/\d+/]
61
67
  end
62
68
 
63
69
  def first_col
64
- @first_col ||= first_cell.scan(/\D+/).first
70
+ @first_col ||= first_cell[/\D+/]
65
71
  end
66
72
 
67
73
  def last_row
68
- @last_row ||= last_cell.scan(/\d+/).first
74
+ @last_row ||= last_cell[/\d+/]
69
75
  end
70
76
 
71
77
  def last_col
72
- @last_col ||= last_cell.scan(/\D+/).first
78
+ @last_col ||= last_cell[/\D+/]
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
-