class OpenXml::Xlsx::Elements::Cell

Constants

EXCEL_ANCHOR_DATE
SECONDS_PER_DAY

Attributes

column[R]
formula[R]
row[R]
serial_date[R]
serial_time[R]
string_id[R]
style[R]
type[R]
value[R]

Public Class Methods

new(row, options={}) click to toggle source
# File lib/openxml/xlsx/elements/cell.rb, line 9
def initialize(row, options={})
  @row = row
  @column = options.fetch(:column)
  @value = options[:value]
  case value
  when Numeric
    @type = :general
  when Date
    @type = :date
    @serial_date = to_serial_date(value)
  when Time
    @type = :time
    @serial_time = to_serial_time(value)
  else
    @value = value.to_s
    @type = :string
    @string_id = package.string_ref(value)
  end
  @style = package.style_ref(options[:style]) if options.key? :style
  @formula = options[:formula]
end

Public Instance Methods

column_letter() click to toggle source
# File lib/openxml/xlsx/elements/cell.rb, line 35
def column_letter
  bytes = []
  remaining = column
  while remaining > 0
    bytes.unshift (remaining - 1) % 26 + 65
    remaining = (remaining - 1) / 26
  end
  bytes.pack "c*"
end
id() click to toggle source
# File lib/openxml/xlsx/elements/cell.rb, line 31
def id
  "#{column_letter}#{row.number}"
end
package() click to toggle source
# File lib/openxml/xlsx/elements/cell.rb, line 53
def package
  workbook.package
end
to_xml(xml) click to toggle source
# File lib/openxml/xlsx/elements/cell.rb, line 57
def to_xml(xml)
  attributes = {"r" => id}
  attributes.merge!("s" => style) if style
  attributes.merge!("t" => "s") if type == :string

  value = self.value
  value = string_id if type == :string
  value = serial_date if type == :date
  value = serial_time if type == :time

  xml.c(attributes) do
    xml.f formula if formula
    xml.v value if value
  end
end
workbook() click to toggle source
# File lib/openxml/xlsx/elements/cell.rb, line 49
def workbook
  worksheet.workbook
end
worksheet() click to toggle source
# File lib/openxml/xlsx/elements/cell.rb, line 45
def worksheet
  row.worksheet
end

Private Instance Methods

to_serial_date(date) click to toggle source
# File lib/openxml/xlsx/elements/cell.rb, line 79
def to_serial_date(date)
  # Excel stores dates as the number of days since 1900-Jan-0
  # Excel behaves as if 1900 was a leap year, so the number is
  # generally 1 greater than you would expect.
  # http://www.cpearson.com/excel/datetime.htm
  (date - EXCEL_ANCHOR_DATE).to_i + 2
end
to_serial_time(time) click to toggle source
# File lib/openxml/xlsx/elements/cell.rb, line 87
def to_serial_time(time)
  date = to_serial_date(time.to_date)

  seconds_since_midnight = time.hour * 3600 + time.min * 60 + time.sec
  date + (seconds_since_midnight.to_f / SECONDS_PER_DAY)
end