Class: Xlsxrb::Elements::Worksheet

Inherits:
Data
  • Object
show all
Defined in:
lib/xlsxrb/elements/worksheet.rb

Overview

Represents a single worksheet in a workbook.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#chartsObject (readonly)

Returns the value of attribute charts

Returns:

  • (Object)

    the current value of charts



6
7
8
# File 'lib/xlsxrb/elements/worksheet.rb', line 6

def charts
  @charts
end

#columnsObject (readonly)

Returns the value of attribute columns

Returns:

  • (Object)

    the current value of columns



6
7
8
# File 'lib/xlsxrb/elements/worksheet.rb', line 6

def columns
  @columns
end

#errorsObject (readonly)

Returns the value of attribute errors

Returns:

  • (Object)

    the current value of errors



6
7
8
# File 'lib/xlsxrb/elements/worksheet.rb', line 6

def errors
  @errors
end

#nameObject (readonly)

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



6
7
8
# File 'lib/xlsxrb/elements/worksheet.rb', line 6

def name
  @name
end

#rowsObject (readonly)

Returns the value of attribute rows

Returns:

  • (Object)

    the current value of rows



6
7
8
# File 'lib/xlsxrb/elements/worksheet.rb', line 6

def rows
  @rows
end

#unmapped_dataObject (readonly)

Returns the value of attribute unmapped_data

Returns:

  • (Object)

    the current value of 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

Returns:

  • (Boolean)


13
14
15
# File 'lib/xlsxrb/elements/worksheet.rb', line 13

def valid?
  errors.empty?
end