Sha256: 265070c2ef2826ad67ed3574a322faff530c301127475360d15daa7bdd3e72bd

Contents?: true

Size: 761 Bytes

Versions: 1

Compression:

Stored size: 761 Bytes

Contents

module Tolq::Parsers::CSV
  module ColumnHelper
    # Converts column to char, zero indexed
    def self.column_to_char(idx)
      dividend = idx + 1
      column_name = ""

      while dividend > 0
        modulo = (dividend - 1) % 26
        column_name = (65 + modulo).chr + column_name
        dividend = (dividend - modulo) / 26
      end

      column_name
    end

    # Converts char to column, zero indexed
    def self.char_to_column(char)
      char
        .split('')
        .reverse
        .map.with_index { |c,idx| (c.ord - 64) * (26**idx)}
        .inject(&:+) - 1
    end

    def self.from_char_notation(key)
      char, row = key.scan(/([aA-zZ]+)(\d+)/).first
      return  row.to_i - 1, ColumnHelper.char_to_column(char)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tolq-parsers-csv-0.2.3 lib/csv/column_helper.rb