class Roo::Excelx::Cell

Attributes

coordinate[R]
excelx_type[R]
excelx_value[R]
formula[R]
style[R]
value[RW]

Public Class Methods

cell_class(type) click to toggle source
# File lib/roo/excelx/cell.rb, line 46
def self.cell_class(type)
  case type
  when :string
    Cell::String
  when :boolean
    Cell::Boolean
  when :number
    Cell::Number
  when :date
    Cell::Date
  when :datetime
    Cell::DateTime
  when :time
    Cell::Time
  end
end
create_cell(type, *values) click to toggle source
# File lib/roo/excelx/cell.rb, line 42
def self.create_cell(type, *values)
  cell_class(type)&.new(*values)
end
new(value, type, formula, excelx_type, excelx_value, style, hyperlink, base_date, coordinate) click to toggle source

DEPRECATED: Please use Cell.create_cell instead.

# File lib/roo/excelx/cell.rb, line 18
def initialize(value, type, formula, excelx_type, excelx_value, style, hyperlink, base_date, coordinate)
  warn '[DEPRECATION] `Cell.new` is deprecated.  Please use `Cell.create_cell` instead.'
  @type = type
  @formula = formula
  @base_date = base_date if [:date, :datetime].include?(@type)
  @excelx_type = excelx_type
  @excelx_value = excelx_value
  @style = style
  @value = type_cast_value(value)
  @value = Roo::Link.new(hyperlink, @value.to_s) if hyperlink
  @coordinate = coordinate
end

Public Instance Methods

type() click to toggle source
# File lib/roo/excelx/cell.rb, line 31
def type
  case
  when @formula
    :formula
  when @value.is_a?(Roo::Link)
    :link
  else
    @type
  end
end

Private Instance Methods

create_date(date) click to toggle source
# File lib/roo/excelx/cell.rb, line 90
def create_date(date)
  yyyy, mm, dd = date.strftime('%Y-%m-%d').split('-')

  ::Date.new(yyyy.to_i, mm.to_i, dd.to_i)
end
create_datetime(date) click to toggle source
# File lib/roo/excelx/cell.rb, line 96
def create_datetime(date)
  datetime_string = date.strftime('%Y-%m-%d %H:%M:%S.%N')
  t = round_datetime(datetime_string)

  ::DateTime.civil(t.year, t.month, t.day, t.hour, t.min, t.sec)
end
round_datetime(datetime_string) click to toggle source
# File lib/roo/excelx/cell.rb, line 103
def round_datetime(datetime_string)
  /(?<yyyy>\d+)-(?<mm>\d+)-(?<dd>\d+) (?<hh>\d+):(?<mi>\d+):(?<ss>\d+.\d+)/ =~ datetime_string

  ::Time.new(yyyy.to_i, mm.to_i, dd.to_i, hh.to_i, mi.to_i, ss.to_r).round(0)
end
type_cast_value(value) click to toggle source
# File lib/roo/excelx/cell.rb, line 75
def type_cast_value(value)
  case @type
  when :float, :percentage
    value.to_f
  when :date
    create_date(@base_date + value.to_i)
  when :datetime
    create_datetime(@base_date + value.to_f.round(6))
  when :time
    value.to_f * 86_400
  else
    value
  end
end