Class: RubyXL::Writer::ContentTypesWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyXL/writer/content_types_writer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dirpath, wb) ⇒ ContentTypesWriter

Returns a new instance of ContentTypesWriter.



13
14
15
16
17
# File 'lib/rubyXL/writer/content_types_writer.rb', line 13

def initialize(dirpath, wb)
  @dirpath = dirpath
  @workbook = wb
  @filepath = dirpath + '/[Content_Types].xml'
end

Instance Attribute Details

#dirpathObject

Returns the value of attribute dirpath.



12
13
14
# File 'lib/rubyXL/writer/content_types_writer.rb', line 12

def dirpath
  @dirpath
end

#filepathObject

Returns the value of attribute filepath.



12
13
14
# File 'lib/rubyXL/writer/content_types_writer.rb', line 12

def filepath
  @filepath
end

#workbookObject

Returns the value of attribute workbook.



12
13
14
# File 'lib/rubyXL/writer/content_types_writer.rb', line 12

def workbook
  @workbook
end

Instance Method Details

#writeObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
# File 'lib/rubyXL/writer/content_types_writer.rb', line 19

def write()
  builder = Nokogiri::XML::Builder.new do |xml|
    xml.Types('xmlns'=>"http://schemas.openxmlformats.org/package/2006/content-types") {
      xml.Default('Extension'=>'xml', 'ContentType'=>'application/xml')
      unless @workbook.shared_strings.nil?
        xml.Override('PartName'=>'/xl/sharedStrings.xml',
          'ContentType'=>"application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml")
      end
      if @workbook.macros.nil? && @workbook.drawings.nil?
        xml.Override('PartName'=>'/xl/workbook.xml',
          'ContentType'=>"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml")
      else
        xml.Override('PartName'=>'/xl/workbook.xml',
          'ContentType'=>"application/vnd.ms-excel.sheet.macroEnabled.main+xml")
      end
      xml.Override('PartName'=>"/xl/styles.xml",
        'ContentType'=>"application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml")
      xml.Default('Extension'=>'rels','ContentType'=>'application/vnd.openxmlformats-package.relationships+xml')
      unless @workbook.external_links.nil?
        1.upto(@workbook.external_links.size-1) do |i|
          xml.Override('PartName'=>"/xl/externalLinks/externalLink#{i}.xml",
          'ContentType'=>"application/vnd.openxmlformats-officedocument.spreadsheetml.externalLink+xml")
        end
      end
      unless @workbook.macros.nil?
        xml.Override('PartName'=>'/xl/vbaProject.bin',
          'ContentType'=>'application/vnd.ms-office.vbaProject')
      end
      unless @workbook.printer_settings.nil?
        xml.Default('Extension'=>'bin',
          'ContentType'=>'application/vnd.openxmlformats-officedocument.spreadsheetml.printerSettings')
      end
      unless @workbook.drawings.nil?
        xml.Default('Extension'=>'vml',
          'ContentType'=>'application/vnd.openxmlformats-officedocument.vmlDrawing')
      end
      xml.Override('PartName'=>'/xl/theme/theme1.xml',
        'ContentType'=>"application/vnd.openxmlformats-officedocument.theme+xml")
      @workbook.worksheets.each_with_index do |sheet,i|
        xml.Override('PartName'=>"/xl/worksheets/sheet#{i+1}.xml",
          'ContentType'=>"application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml")
      end
      xml.Override('PartName'=>'/docProps/core.xml',
        'ContentType'=>"application/vnd.openxmlformats-package.core-properties+xml")
      xml.Override('PartName'=>'/docProps/app.xml',
        'ContentType'=>"application/vnd.openxmlformats-officedocument.extended-properties+xml")
    }
  end

  contents = builder.to_xml
  contents = contents.gsub(/\n/,'')
  contents = contents.gsub(/>(\s)+</,'><')
  contents = contents.sub(/<\?xml version=\"1.0\"\?>/,'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'+"\n")

  contents
end