Class: Xlsxrb::Elements::Cell
- Inherits:
-
Data
- Object
- Data
- Xlsxrb::Elements::Cell
- Defined in:
- lib/xlsxrb/elements/cell.rb
Overview
Represents a single cell in a worksheet. All indices are 0-based.
Instance Attribute Summary collapse
-
#column_index ⇒ Object
readonly
Returns the value of attribute column_index.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#formula ⇒ Object
readonly
Returns the value of attribute formula.
-
#row_index ⇒ Object
readonly
Returns the value of attribute row_index.
-
#style_index ⇒ Object
readonly
Returns the value of attribute style_index.
-
#unmapped_data ⇒ Object
readonly
Returns the value of attribute unmapped_data.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
-
.column_letter(index) ⇒ Object
Converts a 0-based column index to a letter (0 -> "A", 25 -> "Z", 26 -> "AA").
-
.parse_ref(ref) ⇒ Object
Parses an Excel-style reference to [row_index, col_index] (both 0-based).
- .validate(row_index, column_index, value) ⇒ Object
Instance Method Summary collapse
-
#initialize(row_index:, column_index:, value: nil, formula: nil, style_index: nil, unmapped_data: {}, errors: nil) ⇒ Cell
constructor
A new instance of Cell.
-
#ref ⇒ Object
Excel-style reference (e.g. "A1").
- #valid? ⇒ Boolean
Constructor Details
#initialize(row_index:, column_index:, value: nil, formula: nil, style_index: nil, unmapped_data: {}, errors: nil) ⇒ Cell
Returns a new instance of Cell.
8 9 10 11 12 13 |
# File 'lib/xlsxrb/elements/cell.rb', line 8 def initialize(row_index:, column_index:, value: nil, formula: nil, style_index: nil, unmapped_data: {}, errors: nil) computed_errors = errors || self.class.validate(row_index, column_index, value) computed_errors = computed_errors.freeze unless computed_errors.frozen? super(row_index: row_index, column_index: column_index, value: value, formula: formula, style_index: style_index, unmapped_data: unmapped_data, errors: computed_errors) end |
Instance Attribute Details
#column_index ⇒ Object (readonly)
Returns the value of attribute column_index
7 8 9 |
# File 'lib/xlsxrb/elements/cell.rb', line 7 def column_index @column_index end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors
7 8 9 |
# File 'lib/xlsxrb/elements/cell.rb', line 7 def errors @errors end |
#formula ⇒ Object (readonly)
Returns the value of attribute formula
7 8 9 |
# File 'lib/xlsxrb/elements/cell.rb', line 7 def formula @formula end |
#row_index ⇒ Object (readonly)
Returns the value of attribute row_index
7 8 9 |
# File 'lib/xlsxrb/elements/cell.rb', line 7 def row_index @row_index end |
#style_index ⇒ Object (readonly)
Returns the value of attribute style_index
7 8 9 |
# File 'lib/xlsxrb/elements/cell.rb', line 7 def style_index @style_index end |
#unmapped_data ⇒ Object (readonly)
Returns the value of attribute unmapped_data
7 8 9 |
# File 'lib/xlsxrb/elements/cell.rb', line 7 def unmapped_data @unmapped_data end |
#value ⇒ Object (readonly)
Returns the value of attribute value
7 8 9 |
# File 'lib/xlsxrb/elements/cell.rb', line 7 def value @value end |
Class Method Details
.column_letter(index) ⇒ Object
Converts a 0-based column index to a letter (0 -> "A", 25 -> "Z", 26 -> "AA").
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/xlsxrb/elements/cell.rb', line 37 def self.column_letter(index) @column_letters[index] || begin result = +"" i = index loop do result.prepend(("A".ord + (i % 26)).chr) i = (i / 26) - 1 break if i.negative? end result end end |
.parse_ref(ref) ⇒ Object
Parses an Excel-style reference to [row_index, col_index] (both 0-based).
51 52 53 54 55 56 57 58 |
# File 'lib/xlsxrb/elements/cell.rb', line 51 def self.parse_ref(ref) match = ref.match(/\A([A-Z]+)(\d+)\z/) return nil unless match col = match[1].chars.reduce(0) { |acc, c| (acc * 26) + (c.ord - "A".ord + 1) } - 1 row = match[2].to_i - 1 [row, col] end |
.validate(row_index, column_index, value) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/xlsxrb/elements/cell.rb', line 60 def self.validate(row_index, column_index, value) if row_index.is_a?(Integer) && row_index >= 0 && row_index < 1_048_576 && column_index.is_a?(Integer) && column_index >= 0 && column_index < 16_384 && (value.nil? || value.is_a?(String) || value.is_a?(Numeric) || value == true || value == false || value.is_a?(Date) || value.is_a?(Time)) return [].freeze end errs = [] errs << "row_index must be a non-negative Integer (got #{row_index.inspect})" if !row_index.is_a?(Integer) || row_index.negative? errs << "column_index must be a non-negative Integer (got #{column_index.inspect})" if !column_index.is_a?(Integer) || column_index.negative? errs << "row_index must be < 1048576 (got #{row_index}, max row is 1048575)" if row_index.is_a?(Integer) && row_index >= 1_048_576 errs << "column_index must be < 16384 (got #{column_index}, max column is XFD=16383)" if column_index.is_a?(Integer) && column_index >= 16_384 errs << "unsupported value type: #{value.class} (#{value.inspect}) — supported types: String, Numeric, true/false, Date, Time, or nil" unless value.nil? || value.is_a?(String) || value.is_a?(Numeric) || value == true || value == false || value.is_a?(Date) || value.is_a?(Time) errs end |
Instance Method Details
#ref ⇒ Object
Excel-style reference (e.g. "A1").
20 21 22 |
# File 'lib/xlsxrb/elements/cell.rb', line 20 def ref "#{self.class.column_letter(column_index)}#{row_index + 1}" end |
#valid? ⇒ Boolean
15 16 17 |
# File 'lib/xlsxrb/elements/cell.rb', line 15 def valid? errors.empty? end |