Class: POI::Cell

Inherits:
Facade
  • Object
show all
Defined in:
lib/poi/workbook/cell.rb

Constant Summary collapse

DATE_UTIL =
Java::org.apache.poi.ss.usermodel.DateUtil
CELL =
Java::org.apache.poi.ss.usermodel.Cell
CELL_VALUE =
Java::org.apache.poi.ss.usermodel.CellValue
CELL_TYPE_BLANK =
CELL::CELL_TYPE_BLANK
CELL_TYPE_BOOLEAN =
CELL::CELL_TYPE_BOOLEAN
CELL_TYPE_ERROR =
CELL::CELL_TYPE_ERROR
CELL_TYPE_FORMULA =
CELL::CELL_TYPE_FORMULA
CELL_TYPE_NUMERIC =
CELL::CELL_TYPE_NUMERIC
CELL_TYPE_STRING =
CELL::CELL_TYPE_STRING

Instance Method Summary collapse

Constructor Details

#initialize(cell, row) ⇒ Cell

Returns a new instance of Cell.



36
37
38
39
# File 'lib/poi/workbook/cell.rb', line 36

def initialize(cell, row)
  @cell = cell
  @row  = row
end

Instance Method Details

#<=>(other) ⇒ Object



41
42
43
44
# File 'lib/poi/workbook/cell.rb', line 41

def <=> other
  return 1 if other.nil?
  return self.index <=> other.index
end

#commentObject



92
93
94
# File 'lib/poi/workbook/cell.rb', line 92

def comment
  poi_cell.cell_comment
end

#error_valueObject

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.



49
50
51
52
53
54
55
56
57
58
59
60
# 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

#formulaObject



78
79
80
# File 'lib/poi/workbook/cell.rb', line 78

def formula
  poi_cell.cell_formula
end

#formula=(new_value) ⇒ Object



72
73
74
75
76
# 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_valueObject

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



63
64
65
# File 'lib/poi/workbook/cell.rb', line 63

def formula_value
  poi_cell.cell_type == CELL_TYPE_FORMULA ? poi_cell.cell_formula : nil
end

#indexObject



96
97
98
# File 'lib/poi/workbook/cell.rb', line 96

def index
  poi_cell.column_index 
end

#poi_cellObject

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



115
116
117
# File 'lib/poi/workbook/cell.rb', line 115

def poi_cell
  @cell
end

#style!(options) ⇒ Object



122
123
124
# 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) ⇒ Object

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.



104
105
106
107
108
109
110
111
112
# 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

#valueObject



67
68
69
70
# 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) ⇒ Object



82
83
84
85
86
87
88
89
90
# 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