Class: OpenXml::Xlsx::Elements::Cell

Inherits:
Object
  • Object
show all
Defined in:
lib/openxml/xlsx/elements/cell.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(row, options = {}) ⇒ Cell

Returns a new instance of Cell.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 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 String
    @type = :string
    @string_id = package.string_ref(value)
  when Date then
    @type = :date
    @serial_date = to_serial_date(value)
  when Time then
    @type = :time
    @serial_time = to_serial_time(value)
  else
    @type = :general
  end
  @style = package.style_ref(options[:style]) if options.key? :style
  @formula = options[:formula]
end

Instance Attribute Details

#columnObject (readonly)

Returns the value of attribute column.



7
8
9
# File 'lib/openxml/xlsx/elements/cell.rb', line 7

def column
  @column
end

#formulaObject (readonly)

Returns the value of attribute formula.



7
8
9
# File 'lib/openxml/xlsx/elements/cell.rb', line 7

def formula
  @formula
end

#rowObject (readonly)

Returns the value of attribute row.



7
8
9
# File 'lib/openxml/xlsx/elements/cell.rb', line 7

def row
  @row
end

#styleObject (readonly)

Returns the value of attribute style.



7
8
9
# File 'lib/openxml/xlsx/elements/cell.rb', line 7

def style
  @style
end

#typeObject (readonly)

Returns the value of attribute type.



7
8
9
# File 'lib/openxml/xlsx/elements/cell.rb', line 7

def type
  @type
end

#valueObject (readonly)

Returns the value of attribute value.



7
8
9
# File 'lib/openxml/xlsx/elements/cell.rb', line 7

def value
  @value
end

Instance Method Details

#column_letterObject



34
35
36
37
38
39
40
41
42
# File 'lib/openxml/xlsx/elements/cell.rb', line 34

def column_letter
  bytes = []
  remaining = column
  while remaining > 0
    bytes.unshift (remaining - 1) % 26 + 65
    remaining = (remaining - 1) / 26
  end
  bytes.pack "c*"
end

#idObject



30
31
32
# File 'lib/openxml/xlsx/elements/cell.rb', line 30

def id
  "#{column_letter}#{row.number}"
end

#packageObject



52
53
54
# File 'lib/openxml/xlsx/elements/cell.rb', line 52

def package
  workbook.package
end

#to_xml(xml) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/openxml/xlsx/elements/cell.rb', line 56

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

#workbookObject



48
49
50
# File 'lib/openxml/xlsx/elements/cell.rb', line 48

def workbook
  worksheet.workbook
end

#worksheetObject



44
45
46
# File 'lib/openxml/xlsx/elements/cell.rb', line 44

def worksheet
  row.worksheet
end