Class: Xlsxrb::Elements::Worksheet
- Inherits:
-
Data
- Object
- Data
- Xlsxrb::Elements::Worksheet
- Defined in:
- lib/xlsxrb/elements/worksheet.rb
Overview
Represents a single worksheet in a workbook.
Instance Attribute Summary collapse
-
#charts ⇒ Object
readonly
Returns the value of attribute charts.
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#rows ⇒ Object
readonly
Returns the value of attribute rows.
-
#unmapped_data ⇒ Object
readonly
Returns the value of attribute unmapped_data.
Class Method Summary collapse
Instance Method Summary collapse
-
#cell_value(ref) ⇒ Object
Returns cell value at Excel-style reference (e.g. "A1").
-
#initialize(name:, rows: [], columns: [], charts: [], unmapped_data: {}, errors: nil) ⇒ Worksheet
constructor
A new instance of Worksheet.
-
#row_at(index) ⇒ Object
Returns the row at the given 0-based index, or nil.
- #valid? ⇒ Boolean
Constructor Details
#initialize(name:, rows: [], columns: [], charts: [], unmapped_data: {}, errors: nil) ⇒ Worksheet
Returns a new instance of Worksheet.
7 8 9 10 11 |
# File 'lib/xlsxrb/elements/worksheet.rb', line 7 def initialize(name:, rows: [], columns: [], charts: [], unmapped_data: {}, errors: nil) computed_errors = errors || self.class.validate(name, rows) super(name: name, rows: rows.freeze, columns: columns.freeze, charts: charts.freeze, unmapped_data: unmapped_data, errors: computed_errors.freeze) end |
Instance Attribute Details
#charts ⇒ Object (readonly)
Returns the value of attribute charts
6 7 8 |
# File 'lib/xlsxrb/elements/worksheet.rb', line 6 def charts @charts end |
#columns ⇒ Object (readonly)
Returns the value of attribute columns
6 7 8 |
# File 'lib/xlsxrb/elements/worksheet.rb', line 6 def columns @columns end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors
6 7 8 |
# File 'lib/xlsxrb/elements/worksheet.rb', line 6 def errors @errors end |
#name ⇒ Object (readonly)
Returns the value of attribute name
6 7 8 |
# File 'lib/xlsxrb/elements/worksheet.rb', line 6 def name @name end |
#rows ⇒ Object (readonly)
Returns the value of attribute rows
6 7 8 |
# File 'lib/xlsxrb/elements/worksheet.rb', line 6 def rows @rows end |
#unmapped_data ⇒ Object (readonly)
Returns the value of attribute unmapped_data
6 7 8 |
# File 'lib/xlsxrb/elements/worksheet.rb', line 6 def unmapped_data @unmapped_data end |
Class Method Details
.validate(name, rows) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/xlsxrb/elements/worksheet.rb', line 35 def self.validate(name, rows) errs = [] errs << "worksheet name must be a non-empty String (got #{name.inspect})" if name.nil? || (name.is_a?(String) && name.empty?) errs << "rows must be an Array (got #{rows.class})" unless rows.is_a?(Array) if rows.is_a?(Array) indices = rows.map(&:index) if indices.uniq.size != indices.size dups = indices.select { |i| indices.count(i) > 1 }.uniq errs << "duplicate row index: #{dups.join(", ")} — row indices within a sheet must be unique" end end errs end |
Instance Method Details
#cell_value(ref) ⇒ Object
Returns cell value at Excel-style reference (e.g. "A1").
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/xlsxrb/elements/worksheet.rb', line 23 def cell_value(ref) parsed = Cell.parse_ref(ref) return nil unless parsed row_idx, col_idx = parsed row = row_at(row_idx) return nil unless row cell = row.cell_at(col_idx) cell&.value end |
#row_at(index) ⇒ Object
Returns the row at the given 0-based index, or nil.
18 19 20 |
# File 'lib/xlsxrb/elements/worksheet.rb', line 18 def row_at(index) rows.find { |r| r.index == index } end |
#valid? ⇒ Boolean
13 14 15 |
# File 'lib/xlsxrb/elements/worksheet.rb', line 13 def valid? errors.empty? end |