Class: RubyXL::Cell

Inherits:
OOXMLObject show all
Includes:
LegacyCell
Defined in:
lib/rubyXL/objects/sheet_data.rb

Overview

Constant Summary collapse

NUMBER_REGEXP =
/\A-?\d+((?:\.\d+)?(?:e[+-]?\d+)?)?\Z/i

Instance Attribute Summary collapse

Attributes included from OOXMLObjectInstanceMethods

#local_namespaces

Instance Method Summary collapse

Methods included from LegacyCell

#workbook

Methods included from OOXMLObjectInstanceMethods

#==, #before_write_xml, included, #init_child_nodes, #initialize, #write_xml

Instance Attribute Details

#worksheet ⇒ Object

Returns the value of attribute worksheet.



51
52
53
# File 'lib/rubyXL/objects/sheet_data.rb', line 51

def worksheet
  @worksheet
end

Instance Method Details

#column ⇒ Object



65
66
67
# File 'lib/rubyXL/objects/sheet_data.rb', line 65

def column
  r&.first_col
end

#column=(v) ⇒ Object



69
70
71
# File 'lib/rubyXL/objects/sheet_data.rb', line 69

def column=(v)
  self.r = RubyXL::Reference.new(row || 0, v)
end

#error? ⇒ Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/rubyXL/objects/sheet_data.rb', line 133

def error?
  datatype == RubyXL::DataType::ERROR
end

#get_cell_border ⇒ Object



90
91
92
# File 'lib/rubyXL/objects/sheet_data.rb', line 90

def get_cell_border
  workbook.stylesheet.borders[get_cell_xf.border_id]
end

#get_cell_font ⇒ Object



86
87
88
# File 'lib/rubyXL/objects/sheet_data.rb', line 86

def get_cell_font
  workbook.stylesheet.fonts[get_cell_xf.font_id]
end

#get_cell_xf ⇒ Object



82
83
84
# File 'lib/rubyXL/objects/sheet_data.rb', line 82

def get_cell_xf
  workbook.stylesheet.cell_xfs[self.style_index || 0]
end

#index_in_collection ⇒ Object



53
54
55
# File 'lib/rubyXL/objects/sheet_data.rb', line 53

def index_in_collection
  r.first_col
end

#inspect ⇒ Object



137
138
139
140
141
142
# File 'lib/rubyXL/objects/sheet_data.rb', line 137

def inspect
  str = "#<#{self.class}(#{row},#{column}): #{raw_value.inspect}"
  str << " =#{self.formula.expression}" if self.formula
  str << ", datatype=#{self.datatype.inspect}, style_index=#{self.style_index.inspect}>"
  return str
end

#is_date? ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
101
102
103
104
105
106
107
# File 'lib/rubyXL/objects/sheet_data.rb', line 98

def is_date?
  return false unless # Only fully numeric values can be dates
    case raw_value
    when Numeric then true
    when String  then raw_value =~ NUMBER_REGEXP
    else false
    end

  self.number_format&.is_date_format?
end

#number_format ⇒ Object



94
95
96
# File 'lib/rubyXL/objects/sheet_data.rb', line 94

def number_format
  workbook.stylesheet.get_number_format_by_id(get_cell_xf.num_fmt_id)
end

#raw_value ⇒ Object



73
74
75
# File 'lib/rubyXL/objects/sheet_data.rb', line 73

def raw_value
  value_container&.value
end

#raw_value=(v) ⇒ Object



77
78
79
80
# File 'lib/rubyXL/objects/sheet_data.rb', line 77

def raw_value=(v)
  self.value_container ||= RubyXL::CellValue.new
  value_container.value = v
end

#row ⇒ Object



57
58
59
# File 'lib/rubyXL/objects/sheet_data.rb', line 57

def row
  r&.first_row
end

#row=(v) ⇒ Object



61
62
63
# File 'lib/rubyXL/objects/sheet_data.rb', line 61

def row=(v)
  self.r = RubyXL::Reference.new(v, column || 0)
end

#value(args = {}) ⇒ Object

Gets massaged value of the cell, converting datatypes to those known to Ruby (that includes stripping any special formatting from RichText).



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/rubyXL/objects/sheet_data.rb', line 111

def value(args = {})
  r = self.raw_value

  case datatype
  when RubyXL::DataType::SHARED_STRING then workbook.shared_strings_container[r.to_i].to_s
  when RubyXL::DataType::INLINE_STRING then is.to_s
  when RubyXL::DataType::RAW_STRING    then
    raw_value&.gsub(RubyXL::Text::ESCAPED_UNICODE) { Regexp.last_match(1).hex.chr(::Encoding::UTF_8) }
  when RubyXL::DataType::DATE          then raw_value && DateTime.parse(raw_value)
  when RubyXL::DataType::ERROR         then nil
  else
    if is then is.to_s
    elsif is_date? then workbook.num_to_date(r.to_f)
    elsif r.is_a?(String) && (r =~ NUMBER_REGEXP) then # Numeric
      if Regexp.last_match(1) != '' then r.to_f
      else r.to_i
      end
    else r
    end
  end
end