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.



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

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

Instance Method Details

#<=>(other) ⇒ Object



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

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

#commentObject



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

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.



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

#formulaObject



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

def formula
  poi_cell.cell_formula
end

#formula=(new_value) ⇒ Object



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

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



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

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

#indexObject



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

def index
  poi_cell.column_index 
end

#poi_cellObject

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



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

def poi_cell
  @cell
end

#style!(options) ⇒ Object



123
124
125
# 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) ⇒ 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.



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

#valueObject



68
69
70
71
# File 'lib/poi/workbook/cell.rb', line 68

def value
  return nil if poi_cell.nil?
  cast_value
end

#value=(new_value) ⇒ Object



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