Sha256: 9e75079b9d973e6d6a0a64bcb93626edfa03cdeb24408b9f1be603baac0ef179

Contents?: true

Size: 781 Bytes

Versions: 6

Compression:

Stored size: 781 Bytes

Contents

# -*- coding: utf-8 -*-
require 'singleton'

class ColName
  include Singleton

  def initialize
    @col_str_table = Hash.new
  end

  def col_str(col)
    @col_str_table[col] = col_str_build(col) unless @col_str_table[col]
    @col_str_table[col]
  end

  private

  def col_str_build(col)
    # Change from 0-indexed to 1 indexed.
    col += 1
    col_str = ''

    while col > 0
      # Set remainder from 1 .. 26
      remainder = col % 26
      remainder = 26 if remainder == 0

      # Convert the remainder to a character. C-ishly.
      col_letter = ("A".ord + remainder - 1).chr

      # Accumulate the column letters, right to left.
      col_str = col_letter + col_str

      # Get the next order of magnitude.
      col = (col - 1) / 26
    end

    col_str
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
write_xlsx-0.76.1 lib/write_xlsx/col_name.rb
write_xlsx-0.76.0 lib/write_xlsx/col_name.rb
write_xlsx-0.75.0 lib/write_xlsx/col_name.rb
write_xlsx-0.74.0 lib/write_xlsx/col_name.rb
write_xlsx-0.73.0 lib/write_xlsx/col_name.rb
write_xlsx-0.72.3.beta1 lib/write_xlsx/col_name.rb