class POI::Cell

Constants

CELL
CELL_TYPE_BLANK
CELL_TYPE_BOOLEAN
CELL_TYPE_ERROR
CELL_TYPE_FORMULA
CELL_TYPE_NUMERIC
CELL_TYPE_STRING
CELL_VALUE
DATE_UTIL

Public Class Methods

new(cell, row) click to toggle source
# File lib/poi/workbook/cell.rb, line 37
def initialize(cell, row)
  @cell = cell
  @row  = row
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/poi/workbook/cell.rb, line 42
def <=> other
  return 1 if other.nil?
  return self.index <=> other.index
end
comment() click to toggle source
# File lib/poi/workbook/cell.rb, line 93
def comment
  poi_cell.cell_comment
end
error_value() click to toggle source

This is NOT an inexpensive operation. The purpose of this method is merely to get more information out of cell when one thinks the value returned is incorrect. It may have more value in development than in production.

# File lib/poi/workbook/cell.rb, line 50
def error_value
  if poi_cell.cell_type == CELL_TYPE_ERROR
    error_value_from(poi_cell.error_cell_value)
  elsif poi_cell.cell_type == CELL_TYPE_FORMULA && 
        poi_cell.cached_formula_result_type == CELL_TYPE_ERROR
        
    cell_value = formula_evaluator.evaluate(poi_cell)
    cell_value && error_value_from(cell_value.error_value)
  else
    nil
  end
end
formula() click to toggle source
# File lib/poi/workbook/cell.rb, line 79
def formula
  poi_cell.cell_formula
end
formula=(new_value) click to toggle source
# File lib/poi/workbook/cell.rb, line 73
def formula= new_value
  poi_cell.cell_formula = new_value
  @row.worksheet.workbook.on_formula_update self
  self
end
formula_value() click to toggle source

returns the formula for this Cell if it has one, otherwise nil

# File lib/poi/workbook/cell.rb, line 64
def formula_value
  poi_cell.cell_type == CELL_TYPE_FORMULA ? poi_cell.cell_formula : nil
end
index() click to toggle source
# File lib/poi/workbook/cell.rb, line 97
def index
  poi_cell.column_index 
end
poi_cell() click to toggle source

returns the underlying org.apache.poi.ss.usermodel.Cell

# File lib/poi/workbook/cell.rb, line 116
def poi_cell
  @cell
end
style!(options) click to toggle source
# File lib/poi/workbook/cell.rb, line 123
def style! options
  self.style = @row.worksheet.workbook.create_style(options)
end
to_s(evaluate_formulas=true) click to toggle source

Get the String representation of this Cell's value.

If this Cell is a formula you can pass a false to this method and get the formula instead of the String representation.

# File lib/poi/workbook/cell.rb, line 105
def to_s(evaluate_formulas=true)
  return '' if poi_cell.nil?

  if poi_cell.cell_type == CELL_TYPE_FORMULA && evaluate_formulas == false
    formula_value
  else
    value.to_s
  end
end
value() click to toggle source
# File lib/poi/workbook/cell.rb, line 68
def value
  return nil if poi_cell.nil?
  cast_value
end
value=(new_value) click to toggle source
# File lib/poi/workbook/cell.rb, line 83
def value= new_value
  set_cell_value new_value
  if new_value.nil?
    @row.worksheet.workbook.on_delete self
  else
    @row.worksheet.workbook.on_update self
  end
  self
end

Private Instance Methods

cast_value(type = cell_type) click to toggle source
# File lib/poi/workbook/cell.rb, line 128
def cast_value(type = cell_type)
  case type
  when CELL_TYPE_BLANK   then nil
  when CELL_TYPE_BOOLEAN then get_boolean_cell_value
  when CELL_TYPE_ERROR   then nil
  when CELL_TYPE_FORMULA then cast_value(poi_cell.cached_formula_result_type)
  when CELL_TYPE_STRING  then get_string_cell_value
  when CELL_TYPE_NUMERIC
    if DATE_UTIL.cell_date_formatted(poi_cell)
      DateTime.parse(get_date_cell_value.to_s)
    else
      get_numeric_cell_value
    end
  else
    raise "unhandled cell type[#{type}]"
  end
end
error_value_from(cell_value) click to toggle source
# File lib/poi/workbook/cell.rb, line 154
def error_value_from(cell_value)
  #org.apache.poi.ss.usermodel.ErrorConstants.get_text(cell_value)
  org.apache.poi.ss.usermodel.FormulaError.forInt(cell_value).getString
end
formula_evaluator() click to toggle source
# File lib/poi/workbook/cell.rb, line 150
def formula_evaluator
  workbook.formula_evaluator
end
workbook() click to toggle source
# File lib/poi/workbook/cell.rb, line 146
def workbook
  @row.worksheet.workbook
end