Class: Xlsxrb::Elements::Workbook

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

Overview

Represents an entire XLSX workbook.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sheets: [], shared_strings: [], styles: {}, unmapped_data: {}, errors: nil) ⇒ Workbook

Returns a new instance of Workbook.



7
8
9
10
11
# File 'lib/xlsxrb/elements/workbook.rb', line 7

def initialize(sheets: [], shared_strings: [], styles: {}, unmapped_data: {}, errors: nil)
  computed_errors = errors || self.class.validate(sheets)
  super(sheets: sheets.freeze, shared_strings: shared_strings.freeze, styles: styles,
        unmapped_data: unmapped_data, errors: computed_errors.freeze)
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors

Returns:

  • (Object)

    the current value of errors



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

def errors
  @errors
end

#shared_stringsObject (readonly)

Returns the value of attribute shared_strings

Returns:

  • (Object)

    the current value of shared_strings



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

def shared_strings
  @shared_strings
end

#sheetsObject (readonly)

Returns the value of attribute sheets

Returns:

  • (Object)

    the current value of sheets



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

def sheets
  @sheets
end

#stylesObject (readonly)

Returns the value of attribute styles

Returns:

  • (Object)

    the current value of styles



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

def styles
  @styles
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/workbook.rb', line 6

def unmapped_data
  @unmapped_data
end

Class Method Details

.validate(sheets) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/xlsxrb/elements/workbook.rb', line 32

def self.validate(sheets)
  errs = []
  errs << "sheets must be an Array (got #{sheets.class})" unless sheets.is_a?(Array)
  if sheets.is_a?(Array)
    errs << "workbook must have at least one sheet" if sheets.empty?
    names = sheets.map(&:name)
    if names.uniq.size != names.size
      dups = names.select { |n| names.count(n) > 1 }.uniq
      errs << "duplicate sheet name: #{dups.map(&:inspect).join(", ")} — sheet names must be unique"
    end
  end
  errs
end

Instance Method Details

#sheet(identifier = 0) ⇒ Object

Returns the sheet at the given 0-based index or by name.



18
19
20
21
22
23
24
25
# File 'lib/xlsxrb/elements/workbook.rb', line 18

def sheet(identifier = 0)
  case identifier
  when Integer
    sheets[identifier]
  when String
    sheets.find { |s| s.name == identifier }
  end
end

#sheet_namesObject

Returns sheet names.



28
29
30
# File 'lib/xlsxrb/elements/workbook.rb', line 28

def sheet_names
  sheets.map(&:name)
end

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
  errors.empty?
end