Sha256: 781560ecacd9d3ee878d6d5c3211418a073217661e57c177e5a2ef10d348fc2a

Contents?: true

Size: 1.91 KB

Versions: 1

Compression:

Stored size: 1.91 KB

Contents

require 'rubyXL/objects/ooxml_object'

module RubyXL

  class Relationship < OOXMLObject
    define_attribute(:Id,     :string)
    define_attribute(:Type,   :string)
    define_attribute(:Target, :string)
    define_element_name 'Relationship'
  end

  # http://www.schemacentral.com/sc/ooxml/e-ssml_calcChain.html
  class WorkbookRelationships < OOXMLObject
    define_child_node(RubyXL::Relationship, :collection => true, :accessor => :relationships)
    define_element_name 'Relationships'
    set_namespaces('xmlns' => 'http://schemas.openxmlformats.org/package/2006/relationships')

    attr_accessor :workbook

    def create_relationship(target, type)
      RubyXL::Relationship.new(:id => "rId#{relationships.size + 1}", 
                               :type => "http://schemas.openxmlformats.org/officeDocument/2006/relationships/#{type}",
                               :target => target)
    end

    def before_write_xml
      self.relationships = []

      @workbook.worksheets.each_index { |i|
        relationships << create_relationship("worksheets/sheet#{i + 1}.xml", 'worksheet')
      }

      @workbook.external_links.each_key { |k| 
        relationships << create_relationship("externalLinks/#{k}", 'externalLink')
      }

      relationships << create_relationship('theme/theme1.xml', 'theme')
      relationships << create_relationship('styles.xml', 'styles')

      if @workbook.shared_strings_container && !@workbook.shared_strings_container.strings.empty? then
        relationships << create_relationship('sharedStrings.xml', 'sharedStrings') 
      end

      if @workbook.calculation_chain && !@workbook.calculation_chain.cells.empty? then
        relationships << create_relationship('calcChain.xml', 'calcChain') 
      end

      true
    end

    def find_by_rid(r_id)
      relationships.find { |r| r.id == r_id }
    end

    def self.filepath
      File.join('xl', '_rels', 'workbook.xml.rels')
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rubyXL-2.1.1 lib/rubyXL/objects/relationships.rb