Class: OpenXml::Xlsx::Parts::Workbook

Inherits:
Part
  • Object
show all
Defined in:
lib/openxml/xlsx/parts/workbook.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(package) ⇒ Workbook

Returns a new instance of Workbook.



7
8
9
10
11
12
13
# File 'lib/openxml/xlsx/parts/workbook.rb', line 7

def initialize(package)
  @package = package
  @worksheets = []
  @tables = []
  @defined_names =[]
  add_worksheet
end

Instance Attribute Details

#defined_namesObject (readonly)

Returns the value of attribute defined_names.



5
6
7
# File 'lib/openxml/xlsx/parts/workbook.rb', line 5

def defined_names
  @defined_names
end

#packageObject (readonly)

Returns the value of attribute package.



5
6
7
# File 'lib/openxml/xlsx/parts/workbook.rb', line 5

def package
  @package
end

#tablesObject (readonly)

Returns the value of attribute tables.



5
6
7
# File 'lib/openxml/xlsx/parts/workbook.rb', line 5

def tables
  @tables
end

#worksheetsObject (readonly)

Returns the value of attribute worksheets.



5
6
7
# File 'lib/openxml/xlsx/parts/workbook.rb', line 5

def worksheets
  @worksheets
end

Instance Method Details

#add_defined_name(attributes) ⇒ Object



38
39
40
# File 'lib/openxml/xlsx/parts/workbook.rb', line 38

def add_defined_name(attributes)
  defined_names.push Xlsx::Elements::DefinedName.new(attributes[:name], attributes[:formula])
end

#add_defined_names(*defined_names) ⇒ Object



32
33
34
35
36
# File 'lib/openxml/xlsx/parts/workbook.rb', line 32

def add_defined_names(*defined_names)
  defined_names.flatten.each do |attributes|
    add_defined_name attributes
  end
end

#add_table(table) ⇒ Object



26
27
28
29
30
# File 'lib/openxml/xlsx/parts/workbook.rb', line 26

def add_table(table)
  package.content_types.add_override "/xl/tables/#{table.filename}", TYPE_TABLE
  package.add_part "xl/tables/#{table.filename}", table
  tables.push table
end

#add_worksheetObject



15
16
17
18
19
20
21
22
23
24
# File 'lib/openxml/xlsx/parts/workbook.rb', line 15

def add_worksheet
  worksheet = Worksheet.new(self, worksheets.length + 1)
  package.xl_rels.add_relationship(
    REL_WORKSHEET,
    "worksheets/sheet#{worksheet.index}.xml",
    "rId#{worksheet.index}")
  package.add_part "xl/worksheets/_rels/sheet#{worksheet.index}.xml.rels", worksheet.rels
  package.add_part "xl/worksheets/sheet#{worksheet.index}.xml", worksheet
  worksheets.push worksheet
end

#to_xmlObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/openxml/xlsx/parts/workbook.rb', line 42

def to_xml
  build_standalone_xml do |xml|
    xml.workbook(root_namespaces) {
      xml.bookViews {
        xml.workbookView
      }
      xml.sheets { worksheets.each { |worksheet|
        xml.sheet(
          "name" => worksheet.name,
          "sheetId" => worksheet.index,
          "r:id" => "rId#{worksheet.index}")
      } }
      xml.definedNames do
        defined_names.each { |defined_name| defined_name.to_xml(xml) }
      end if defined_names.any?
    }
  end
end