Class: Axlsx::SortCondition

Inherits:
Object
  • Object
show all
Defined in:
lib/axlsx/workbook/worksheet/auto_filter/sort_condition.rb

Overview

This class represents a individual sort condition belonging to the sort state of an auto filter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(column_index:, order:, custom_list:) ⇒ SortCondition

Creates a new SortCondition object

Parameters:

  • column_index (Integer)

    Zero-based index indicating the AutoFilter column to which the sorting should be applied to

  • order (Symbol)

    The order the column should be sorted on, can only be :asc or :desc

  • custom_list (Array)

    An array containing a custom sorting list in order.



10
11
12
13
14
15
16
17
18
19
# File 'lib/axlsx/workbook/worksheet/auto_filter/sort_condition.rb', line 10

def initialize(column_index:, order:, custom_list:)
  Axlsx.validate_int column_index
  @column_index = column_index

  RestrictionValidator.validate 'SortCondition.order', [:asc, :desc], order
  @order = order

  DataTypeValidator.validate :sort_condition_custom_list, Array, custom_list
  @custom_list = custom_list
end

Instance Attribute Details

#column_indexObject (readonly)

Returns the value of attribute column_index.



21
22
23
# File 'lib/axlsx/workbook/worksheet/auto_filter/sort_condition.rb', line 21

def column_index
  @column_index
end

#custom_listObject (readonly)

Returns the value of attribute custom_list.



21
22
23
# File 'lib/axlsx/workbook/worksheet/auto_filter/sort_condition.rb', line 21

def custom_list
  @custom_list
end

#orderObject (readonly)

Returns the value of attribute order.



21
22
23
# File 'lib/axlsx/workbook/worksheet/auto_filter/sort_condition.rb', line 21

def order
  @order
end

Instance Method Details

#ref_to_single_column(ref, column_index) ⇒ Object

converts the ref String from the sort_state to a string representing the ref of a single column for the xml string to be returned.



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/axlsx/workbook/worksheet/auto_filter/sort_condition.rb', line 25

def ref_to_single_column(ref, column_index)
  first_cell, last_cell = ref.split(':')

  start_point = Axlsx.name_to_indices(first_cell)

  first_row = first_cell[/\d+/]
  last_row = last_cell[/\d+/]

  first_column = Axlsx.col_ref(column_index + start_point.first)
  last_column = first_column

  "#{first_column}#{first_row}:#{last_column}#{last_row}"
end

#to_xml_string(str, ref) ⇒ String

serialize the object

Returns:

  • (String)


41
42
43
44
45
46
47
48
49
# File 'lib/axlsx/workbook/worksheet/auto_filter/sort_condition.rb', line 41

def to_xml_string(str, ref)
  ref = ref_to_single_column(ref, column_index)

  str << "<sortCondition "
  str << "descending='1' " if order == :desc
  str << "ref='#{ref}' "
  str << "customList='#{custom_list.join(',')}' " unless custom_list.empty?
  str << "/>"
end