Class: Xlsxrb::Elements::Workbook

Inherits:
Data
  • Object
show all
Includes:
Enumerable
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.



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

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



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

def errors
  @errors
end

#shared_stringsObject (readonly)

Returns the value of attribute shared_strings

Returns:

  • (Object)

    the current value of shared_strings



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

def shared_strings
  @shared_strings
end

#sheetsObject (readonly)

Returns the value of attribute sheets

Returns:

  • (Object)

    the current value of sheets



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

def sheets
  @sheets
end

#stylesObject (readonly)

Returns the value of attribute styles

Returns:

  • (Object)

    the current value of styles



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

def styles
  @styles
end

#unmapped_dataObject (readonly)

Returns the value of attribute unmapped_data

Returns:

  • (Object)

    the current value of unmapped_data



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

def unmapped_data
  @unmapped_data
end

Class Method Details

.validate(sheets) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/xlsxrb/elements/workbook.rb', line 61

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

#eachObject



17
18
19
# File 'lib/xlsxrb/elements/workbook.rb', line 17

def each(&)
  sheets.each(&)
end

#save(filepath) ⇒ Object

Save the workbook to a file.



57
58
59
# File 'lib/xlsxrb/elements/workbook.rb', line 57

def save(filepath)
  Xlsxrb.write(filepath, self)
end

#sheet(identifier = 0) ⇒ Object Also known as: []

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



26
27
28
29
30
31
32
33
# File 'lib/xlsxrb/elements/workbook.rb', line 26

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.



52
53
54
# File 'lib/xlsxrb/elements/workbook.rb', line 52

def sheet_names
  sheets.map(&:name)
end

#update_sheet(identifier) ⇒ Object

Returns a new Workbook with the specified sheet updated. If a block is given, it yields the matched sheet and expects a new Worksheet back.

Raises:

  • (ArgumentError)


38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/xlsxrb/elements/workbook.rb', line 38

def update_sheet(identifier)
  raise ArgumentError, "block is required" unless block_given?

  sheet_to_update = sheet(identifier)
  raise ArgumentError, "sheet not found: #{identifier}" unless sheet_to_update

  new_sheet = yield sheet_to_update
  raise TypeError, "block must return a Worksheet" unless new_sheet.is_a?(Worksheet)

  new_sheets = sheets.map { |s| s == sheet_to_update ? new_sheet : s }
  with(sheets: new_sheets)
end

#valid?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/xlsxrb/elements/workbook.rb', line 21

def valid?
  errors.empty?
end