Sha256: 3c694931d711a918d424a1ed71a701c2a9de3b8f03bc53e6483597115853a4df

Contents?: true

Size: 1.78 KB

Versions: 5

Compression:

Stored size: 1.78 KB

Contents

# frozen_string_literal: true

module Axlsx
  # This class represents a individual sort condition belonging to the sort state of an auto filter
  class SortCondition
    # Creates a new SortCondition object
    # @param [Integer] column_index Zero-based index indicating the AutoFilter column to which the sorting should be applied to
    # @param [Symbol] order The order the column should be sorted on, can only be :asc or :desc
    # @param [Array] custom_list An array containg a custom sorting list in order.
    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

    attr_reader :column_index, :order, :custom_list

    # 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.
    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

    # serialize the object
    # @return [String]
    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
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/caxlsx-4.1.0/lib/axlsx/workbook/worksheet/auto_filter/sort_condition.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/caxlsx-4.1.0/lib/axlsx/workbook/worksheet/auto_filter/sort_condition.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/caxlsx-4.1.0/lib/axlsx/workbook/worksheet/auto_filter/sort_condition.rb
caxlsx-4.1.0 lib/axlsx/workbook/worksheet/auto_filter/sort_condition.rb
caxlsx-4.0.0 lib/axlsx/workbook/worksheet/auto_filter/sort_condition.rb