Class: OOXL::Cell

Inherits:
Object
  • Object
show all
Extended by:
Util
Defined in:
lib/ooxl/xl_objects/cell.rb

Overview

where, r = reference s = style t = type v = value <c r=“A1” s=“227” t=“s”>

<v>113944</v>

</c>

Direct Known Subclasses

BlankCell

Constant Summary

Constants included from Util

Util::COLUMN_LETTERS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

column_letter_to_number, column_number_to_letter, letter_equivalent, letter_index, node_attribute_value, node_value_extractor, to_column_letter, uniform_reference

Constructor Details

#initialize(**attrs) ⇒ Cell

Returns a new instance of Cell.



14
15
16
# File 'lib/ooxl/xl_objects/cell.rb', line 14

def initialize(**attrs)
  attrs.each { |property, value| send("#{property}=", value)}
end

Instance Attribute Details

#formulaObject

Returns the value of attribute formula.



12
13
14
# File 'lib/ooxl/xl_objects/cell.rb', line 12

def formula
  @formula
end

#idObject

Returns the value of attribute id.



12
13
14
# File 'lib/ooxl/xl_objects/cell.rb', line 12

def id
  @id
end

#shared_stringsObject

Returns the value of attribute shared_strings.



12
13
14
# File 'lib/ooxl/xl_objects/cell.rb', line 12

def shared_strings
  @shared_strings
end

#style_idObject

Returns the value of attribute style_id.



12
13
14
# File 'lib/ooxl/xl_objects/cell.rb', line 12

def style_id
  @style_id
end

#stylesObject

Returns the value of attribute styles.



12
13
14
# File 'lib/ooxl/xl_objects/cell.rb', line 12

def styles
  @styles
end

#type_idObject

Returns the value of attribute type_id.



12
13
14
# File 'lib/ooxl/xl_objects/cell.rb', line 12

def type_id
  @type_id
end

#valueObject

Returns the value of attribute value.



12
13
14
# File 'lib/ooxl/xl_objects/cell.rb', line 12

def value
  @value
end

Class Method Details

.extract_value(type_id, cell_node, shared_strings) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/ooxl/xl_objects/cell.rb', line 103

def self.extract_value(type_id, cell_node, shared_strings)
  value_id = cell_node.at('v').try(:text)

  case type_id

  when 's'
    (value_id.present?) ? shared_strings[ Integer(value_id) ] : nil

  when 'inlineStr'
    value = []
    cell_node.xpath('is').each do |text_node|
      value << text_node.xpath('r/t|t').map { |value_node| value_node.text}.join('')
    end
    value.join('')
  else
    value_id
  end

end

.load_from_node(cell_node, shared_strings, styles) ⇒ Object



93
94
95
96
97
98
99
100
101
# File 'lib/ooxl/xl_objects/cell.rb', line 93

def self.load_from_node(cell_node, shared_strings, styles)
  type_id = node_attribute_value(cell_node, 't')
  new(id: node_attribute_value(cell_node, 'r'),
      type_id: type_id,
      style_id: node_attribute_value(cell_node, 's'),
      value: extract_value(type_id, cell_node, shared_strings),
      styles: styles,
      formula: cell_node.at('f').try(:content))
end

Instance Method Details

#columnObject



18
19
20
# File 'lib/ooxl/xl_objects/cell.rb', line 18

def column
  @column ||= id.gsub(/\d+/, '')
end

#fillObject



89
90
91
# File 'lib/ooxl/xl_objects/cell.rb', line 89

def fill
  (style.present?) ? style[:fill]: nil
end

#fontObject



85
86
87
# File 'lib/ooxl/xl_objects/cell.rb', line 85

def font
  (style.present?) ? style[:font] : nil
end

#next_id(offset: 1, location: "bottom") ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ooxl/xl_objects/cell.rb', line 26

def next_id(offset: 1, location: "bottom")
  _, column_letter, column_index = id.partition(/[A-Z]+/)

  # ensure that all are numbers
  column_index = column_index.to_i
  offset = offset.to_i if offset.is_a?(String)

  # increment based on specified location
  case location
  when "top"
    if column_index == 1 || column_index < offset
      column_index = 1
    else
      column_index -= offset
    end
  when "bottom"
    column_index += offset
  when "left"
    return id if column_letter == 'A'
    1.upto(offset) { |count| column_letter = (column_letter.ord-1).chr unless column_letter == 'A' }
  when "right"
    1.upto(offset) { |count| column_letter.next! }
  else
    id
  end

  "#{column_letter}#{column_index}"
end

#number_formatObject



78
79
80
81
82
83
# File 'lib/ooxl/xl_objects/cell.rb', line 78

def number_format
  if (style.present?)
    nf = style[:number_format]
    (nf.present?) ? nf.gsub("\\", "") : nil
  end
end

#rowObject



22
23
24
# File 'lib/ooxl/xl_objects/cell.rb', line 22

def row
  @row ||= id.gsub(/[^\d+]/, '')
end

#styleObject



70
71
72
73
74
75
76
# File 'lib/ooxl/xl_objects/cell.rb', line 70

def style
  @style ||= begin
    if style_id.present?
      style = styles.by_id(style_id.to_i)
    end
  end
end

#typeObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ooxl/xl_objects/cell.rb', line 55

def type
  @type ||= begin
    case type_id
    when 's' then :string
    when 'n' then :number
    when 'b' then :boolean
    when 'd' then :date
    when 'str' then :formula
    when 'inlineStr' then :inline_str
    else
      :error
    end
  end
end