Class: RubyXL::ContentTypes

Inherits:
OOXMLTopLevelObject show all
Defined in:
lib/rubyXL/objects/content_types.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from OOXMLTopLevelObject

#add_to_zip, parse_file, set_namespaces, #xlsx_path

Methods included from OOXMLObjectClassMethods

#define_attribute, #define_child_node, #define_element_name, #obtain_class_variable, #parse, #set_countable

Methods included from OOXMLObjectInstanceMethods

#dup, #index_in_collection, #initialize, #write_xml

Instance Attribute Details

#workbookObject

Returns the value of attribute workbook.



18
19
20
# File 'lib/rubyXL/objects/content_types.rb', line 18

def workbook
  @workbook
end

Class Method Details

.xlsx_pathObject



26
27
28
# File 'lib/rubyXL/objects/content_types.rb', line 26

def self.xlsx_path
  '[Content_Types].xml'
end

Instance Method Details

#before_write_xmlObject



34
35
36
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rubyXL/objects/content_types.rb', line 34

def before_write_xml
  self.defaults = []
  unless @workbook.printer_settings.empty?
    defaults << RubyXL::ContentTypeDefault.new(:extension => 'bin',
                  :content_type => 'application/vnd.openxmlformats-officedocument.spreadsheetml.printerSettings')
  end

  defaults << RubyXL::ContentTypeDefault.new(:extension => 'rels',
                  :content_type => 'application/vnd.openxmlformats-package.relationships+xml' )

  defaults << RubyXL::ContentTypeDefault.new(:extension => 'xml',
                  :content_type => 'application/xml' )

  # TODO: Need to only write these types when respective content is actually present.
  defaults << RubyXL::ContentTypeDefault.new(:extension => 'jpeg', :content_type => 'image/jpeg' )
  defaults << RubyXL::ContentTypeDefault.new(:extension => 'png', :content_type => 'image/png' )

  self.overrides = []
  overrides << generate_override(workbook)
  workbook.worksheets.each { |sheet| overrides << generate_override(sheet) }
  overrides << generate_override(workbook.stylesheet)
  overrides << generate_override(workbook.document_properties)
  overrides << generate_override(workbook.core_properties)
  overrides << generate_override(workbook.shared_strings_container) unless workbook.shared_strings_container.empty?
  overrides << generate_override(workbook.calculation_chain) unless workbook.calculation_chain.nil?
  overrides << generate_override(workbook.theme)

  workbook.charts.each_pair { |k, v|
    case k
    when /^chart\d*.xml$/ then
      overrides << RubyXL::ContentTypeOverride.new(:part_name => "/#{@workbook.charts.local_dir_path}/#{k}",
                     :content_type => 'application/vnd.openxmlformats-officedocument.drawingml.chart+xml')
    when /^style\d*.xml$/ then
      overrides << RubyXL::ContentTypeOverride.new(:part_name => "/#{@workbook.charts.local_dir_path}/#{k}",
                     :content_type => 'application/vnd.ms-office.chartstyle+xml')
    when /^colors\d*.xml$/ then
      overrides << RubyXL::ContentTypeOverride.new(:part_name => "/#{@workbook.charts.local_dir_path}/#{k}",
                     :content_type => 'application/vnd.ms-office.chartcolorstyle+xml')
    end
  }

  workbook.drawings.each_pair { |k, v|
    overrides << RubyXL::ContentTypeOverride.new(:part_name => "/#{@workbook.drawings.local_dir_path}/#{k}",
                   :content_type => 'application/vnd.openxmlformats-officedocument.drawing+xml')
  }

  unless workbook.external_links.nil?
    1.upto(workbook.external_links.size - 1) { |i|
      overrides << RubyXL::ContentTypeOverride.new(:part_name => "/xl/externalLinks/externalLink#{i}.xml",
                   :content_type => 'application/vnd.openxmlformats-officedocument.spreadsheetml.externalLink+xml')
    }
  end

  unless @workbook.macros.empty?
    overrides << RubyXL::ContentTypeOverride.new(:part_name => '/xl/vbaProject.bin',
                   :content_type => 'application/vnd.ms-office.vbaProject')
  end

  true
end

#generate_override(obj) ⇒ Object



30
31
32
# File 'lib/rubyXL/objects/content_types.rb', line 30

def generate_override(obj)
  RubyXL::ContentTypeOverride.new(:part_name => "/#{obj.xlsx_path}", :content_type => obj.class.content_type)
end