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 36
def initialize(cell, row)
  @cell = cell
  @row  = row
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/poi/workbook/cell.rb, line 41
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 92
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 49
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
        
    # breaks Law of Demeter by reaching into the Row's Worksheet, but it makes sense to do in this case
    value_of(@row.worksheet.workbook.formula_evaluator.evaluate(poi_cell))
  else
    nil
  end
end
formula() click to toggle source
# File lib/poi/workbook/cell.rb, line 78
def formula
  poi_cell.cell_formula
end
formula=(new_value) click to toggle source
# File lib/poi/workbook/cell.rb, line 72
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 63
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 96
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 115
def poi_cell
  @cell
end
style!(options) click to toggle source
# File lib/poi/workbook/cell.rb, line 122
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 104
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 67
def value
  return nil if poi_cell.nil?
  value_of(cell_value_for_type(poi_cell.cell_type))
end
value=(new_value) click to toggle source
# File lib/poi/workbook/cell.rb, line 82
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

cell_value_for_type(cell_type) click to toggle source
# File lib/poi/workbook/cell.rb, line 141
def cell_value_for_type(cell_type)
  return nil if cell_type.nil?
  begin
    case cell_type
    when CELL_TYPE_BLANK then nil
    when CELL_TYPE_BOOLEAN then CELL_VALUE.value_of(poi_cell.boolean_cell_value)
    when CELL_TYPE_FORMULA then cell_value_for_type(poi_cell.cached_formula_result_type)
    when CELL_TYPE_STRING then CELL_VALUE.new(poi_cell.string_cell_value)
    when CELL_TYPE_ERROR, CELL_TYPE_NUMERIC then CELL_VALUE.new(poi_cell.numeric_cell_value)
    else
      raise "unhandled cell type[#{poi_cell.cell_type}]"
    end
  rescue
    nil
  end
end
error_value_from(cell_value) click to toggle source
# File lib/poi/workbook/cell.rb, line 158
def error_value_from(cell_value)
  org.apache.poi.ss.usermodel.ErrorConstants.text(cell_value)
end
formula_evaluator_for(workbook) click to toggle source
# File lib/poi/workbook/cell.rb, line 162
def formula_evaluator_for(workbook)
  workbook.creation_helper.create_formula_evaluator
end
numeric_value_from(cell_value) click to toggle source
# File lib/poi/workbook/cell.rb, line 166
def numeric_value_from(cell_value)
  if DATE_UTIL.cell_date_formatted(poi_cell)
    Date.parse(DATE_UTIL.get_java_date(cell_value.number_value).to_s)
  else
    cell_value.number_value
  end
end
value_of(cell_value) click to toggle source
# File lib/poi/workbook/cell.rb, line 127
def value_of(cell_value)
  return nil if cell_value.nil?
  
  case cell_value.cell_type
  when CELL_TYPE_BLANK then nil
  when CELL_TYPE_BOOLEAN then cell_value.boolean_value
  when CELL_TYPE_ERROR then error_value_from(cell_value.error_value)
  when CELL_TYPE_NUMERIC then numeric_value_from(cell_value)
  when CELL_TYPE_STRING then cell_value.string_value
  else
    raise "unhandled cell type[#{cell_value.cell_type}]"
  end
end