Class: RubyXL::WorkbookRoot

Inherits:
Object
  • Object
show all
Includes:
RelationshipSupport
Defined in:
lib/rubyXL/objects/root.rb

Constant Summary collapse

@@debug =
$DEBUG

Instance Attribute Summary collapse

Attributes included from RelationshipSupport

#generic_storage, #relationship_container

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RelationshipSupport

#attach_relationship, #collect_related_objects, included, #load_relationships, #store_relationship

Instance Attribute Details

#content_types ⇒ Object

Returns the value of attribute content_types.



13
14
15
# File 'lib/rubyXL/objects/root.rb', line 13

def content_types
  @content_types
end

#rels_hash ⇒ Object

Returns the value of attribute rels_hash.



13
14
15
# File 'lib/rubyXL/objects/root.rb', line 13

def rels_hash
  @rels_hash
end

#source_file_path ⇒ Object

Returns the value of attribute source_file_path.



13
14
15
# File 'lib/rubyXL/objects/root.rb', line 13

def source_file_path
  @source_file_path
end

Class Method Details

.default ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/rubyXL/objects/root.rb', line 27

def self.default
  obj = self.new
  obj.document_properties    = RubyXL::DocumentPropertiesFile.new
  obj.core_properties        = RubyXL::CorePropertiesFile.new
  obj.relationship_container = RubyXL::OOXMLRelationshipsFile.new
  obj.content_types          = RubyXL::ContentTypes.new
  obj
end

.parse_zip_file(zip_file) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rubyXL/objects/root.rb', line 71

def self.parse_zip_file(zip_file)
  root = self.new
  root.content_types = RubyXL::ContentTypes.parse_file(zip_file, ContentTypes::XLSX_PATH)
  root.load_relationships(zip_file, OOXMLTopLevelObject::ROOT)

  wb = root.workbook
  wb.root = root

  wb.sheets.each_with_index { |sheet, i|
    sheet_obj = wb.relationship_container.related_files[sheet.r_id]

    wb.worksheets[i] = sheet_obj # Must be done first so the sheet becomes aware of its number
    sheet_obj.workbook = wb

    sheet_obj.sheet_name = sheet.name
    sheet_obj.sheet_id = sheet.sheet_id
    sheet_obj.state = sheet.state
  }

  root
end

Instance Method Details



23
24
25
# File 'lib/rubyXL/objects/root.rb', line 23

def related_objects
  [ content_types, thumbnail, core_properties, document_properties, custom_properties, workbook ]
end

#stream ⇒ Object

Write .xlsx to a stream (useful for sending over HTTP)



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rubyXL/objects/root.rb', line 37

def stream
  stream = Zip::OutputStream.write_buffer(suppress_extra_fields: true) { |zipstream|
    self.rels_hash = {}
    self.relationship_container.owner = self
    collect_related_objects.compact.each { |obj|
      puts "<-- DEBUG: adding relationship to #{obj.class}" if @@debug
      obj.root = self if obj.respond_to?(:root=)
      self.rels_hash[obj.class] ||= []
      self.rels_hash[obj.class] << obj
    }

    self.rels_hash.keys.sort_by{ |c| c::SAVE_ORDER }.each { |klass|
      puts "<-- DEBUG: saving related #{klass} files" if @@debug
      # This is a hack.
      # Technically, MS Excel allows one file to be shared amongst multiple sheets in the XLS document.
      # That file exists in the archive structure only once, and all sheets refer to the same file by path.
      # RubyXL uses a tree structure, where each sheet has its own "branches" (related objects),
      # and loops in the relationship graph are not allowed (a child node can have onle one parent).
      # This needs to be fixed in the future. Thankfully, files like that seem to be rare, and this
      # situation can be avoided simply by not saving a node if it already exists in the zipfile.
      self.rels_hash[klass].uniq{ |item| item.xlsx_path }.select! { |obj|
        puts "<-- DEBUG:   > #{obj.xlsx_path}" if @@debug
        obj.add_to_zip(zipstream)
      }
    }
  }
  stream.rewind
  stream
end

#xlsx_path ⇒ Object



67
68
69
# File 'lib/rubyXL/objects/root.rb', line 67

def xlsx_path
  OOXMLTopLevelObject::ROOT
end