Class: Xlsxrb::Elements::Workbook
- Inherits:
-
Data
- Object
- Data
- Xlsxrb::Elements::Workbook
- Defined in:
- lib/xlsxrb/elements/workbook.rb
Overview
Represents an entire XLSX workbook.
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#shared_strings ⇒ Object
readonly
Returns the value of attribute shared_strings.
-
#sheets ⇒ Object
readonly
Returns the value of attribute sheets.
-
#styles ⇒ Object
readonly
Returns the value of attribute styles.
-
#unmapped_data ⇒ Object
readonly
Returns the value of attribute unmapped_data.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(sheets: [], shared_strings: [], styles: {}, unmapped_data: {}, errors: nil) ⇒ Workbook
constructor
A new instance of Workbook.
-
#sheet(identifier = 0) ⇒ Object
Returns the sheet at the given 0-based index or by name.
-
#sheet_names ⇒ Object
Returns sheet names.
- #valid? ⇒ Boolean
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
#errors ⇒ Object (readonly)
Returns the value of attribute errors
6 7 8 |
# File 'lib/xlsxrb/elements/workbook.rb', line 6 def errors @errors end |
#shared_strings ⇒ Object (readonly)
Returns the value of attribute shared_strings
6 7 8 |
# File 'lib/xlsxrb/elements/workbook.rb', line 6 def shared_strings @shared_strings end |
#sheets ⇒ Object (readonly)
Returns the value of attribute sheets
6 7 8 |
# File 'lib/xlsxrb/elements/workbook.rb', line 6 def sheets @sheets end |
#styles ⇒ Object (readonly)
Returns the value of attribute styles
6 7 8 |
# File 'lib/xlsxrb/elements/workbook.rb', line 6 def styles @styles end |
#unmapped_data ⇒ Object (readonly)
Returns the value of attribute 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_names ⇒ Object
Returns sheet names.
28 29 30 |
# File 'lib/xlsxrb/elements/workbook.rb', line 28 def sheet_names sheets.map(&:name) end |
#valid? ⇒ Boolean
13 14 15 |
# File 'lib/xlsxrb/elements/workbook.rb', line 13 def valid? errors.empty? end |