Sha256: f10f1b2791671dc8424bcd91d8ce45b15c0aaa0ee07b812fd99222decdeaf38c

Contents?: true

Size: 1.72 KB

Versions: 5

Compression:

Stored size: 1.72 KB

Contents

# frozen_string_literal: true

module Axlsx
  # This class manages the dimensions for a worksheet.
  # While this node is optional in the specification some readers like
  # LibraOffice require this node to render the sheet
  class Dimension
    # the default value for the first cell in the dimension
    # @return [String]
    def self.default_first
      @@default_first ||= 'A1'
    end

    # the default value for the last cell in the dimension
    # @return [String]
    def self.default_last
      @@default_last ||= 'AA200'
    end

    # Creates a new dimension object
    # @param[Worksheet] worksheet - the worksheet this dimension applies
    # to.
    def initialize(worksheet)
      raise ArgumentError, "you must provide a worksheet" unless worksheet.is_a?(Worksheet)

      @worksheet = worksheet
    end

    attr_reader :worksheet

    # the full refernece for this dimension
    # @return [String]
    def sqref
      "#{first_cell_reference}:#{last_cell_reference}"
    end

    # serialize the object
    # @return [String]
    def to_xml_string(str = +'')
      return if worksheet.rows.empty?

      str << '<dimension ref="' << sqref << '"></dimension>'
    end

    # The first cell in the dimension
    # @return [String]
    def first_cell_reference
      dimension_reference(worksheet.rows.first.first, Dimension.default_first)
    end

    # the last cell in the dimension
    # @return [String]
    def last_cell_reference
      dimension_reference(worksheet.rows.last.last, Dimension.default_last)
    end

    private

    # Returns the reference of a cell or the default specified
    # @return [String]
    def dimension_reference(cell, default)
      return default unless cell.respond_to?(:r)

      cell.r
    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/dimension.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/caxlsx-4.1.0/lib/axlsx/workbook/worksheet/dimension.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/caxlsx-4.1.0/lib/axlsx/workbook/worksheet/dimension.rb
caxlsx-4.1.0 lib/axlsx/workbook/worksheet/dimension.rb
caxlsx-4.0.0 lib/axlsx/workbook/worksheet/dimension.rb