Class: RubyXL::Writer::WorkbookRelsWriter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dirpath, wb) ⇒ WorkbookRelsWriter

Returns a new instance of WorkbookRelsWriter.



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

def initialize(dirpath, wb)
  @dirpath = dirpath
  @workbook = wb
end

Instance Attribute Details

#dirpathObject

Returns the value of attribute dirpath.



11
12
13
# File 'lib/rubyXL/writer/workbook_rels_writer.rb', line 11

def dirpath
  @dirpath
end

#filepathObject

Returns the value of attribute filepath.



11
12
13
# File 'lib/rubyXL/writer/workbook_rels_writer.rb', line 11

def filepath
  @filepath
end

#workbookObject

Returns the value of attribute workbook.



11
12
13
# File 'lib/rubyXL/writer/workbook_rels_writer.rb', line 11

def workbook
  @workbook
end

Instance Method Details

#writeObject

all attributes out of order



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
# File 'lib/rubyXL/writer/workbook_rels_writer.rb', line 19

def write()
  builder = Nokogiri::XML::Builder.new do |xml|
    xml.Relationships('xmlns'=>'http://schemas.openxmlformats.org/package/2006/relationships') {
      i = 1
      @workbook.worksheets.each do |sheet|
        xml.Relationship('Id'=>'rId'+i.to_s,
          'Type'=>"http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet",
          'Target'=>'worksheets/sheet'+i.to_s+'.xml')
        i += 1
      end
      unless @workbook.external_links.nil?
        1.upto(@workbook.external_links.size-1) do |link|
          xml.Relationship('Id'=>'rId'+i.to_s,
            'Type'=>"http://schemas.openxmlformats.org/officeDocument/2006/relationships/externalLink",
            'Target'=>"externalLinks/externalLink#{link}.xml"
          )
          i+=1
        end
      end
      xml.Relationship('Id'=>'rId'+i.to_s,
        'Type'=>"http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme",
        'Target'=>'theme/theme1.xml')
      i += 1
      xml.Relationship('Id'=>'rId'+i.to_s,
        'Type'=>"http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles",
        'Target'=>'styles.xml')
      i+=1
      xml.Relationship('Id'=>'rId'+i.to_s,
        'Type'=>"http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings",
          'Target'=>'sharedStrings.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