Sha256: d30c977eef99ed66abc1cc7af7a2c869f0217119d58f43ef112d68c00343b459

Contents?: true

Size: 1.87 KB

Versions: 5

Compression:

Stored size: 1.87 KB

Contents

module Axlsx
  
  # This is a utility class for serialing the drawing node in a
  # worksheet. Drawing objects have their own serialization that exports
  # a drawing document. This is only for the single node in the
  # worksheet
  class WorksheetDrawing

    # Creates a new WorksheetDrawing
    # @param [Worksheet] worksheet
    def initialize(worksheet)
      raise ArgumentError, 'you must provide a worksheet' unless worksheet.is_a?(Worksheet)
      @worksheet = worksheet
      @drawing = nil
    end

    attr_reader :worksheet

    attr_reader :drawing
    
    # adds a chart to the drawing object
    # @param [Class] chart_type The type of chart to add
    # @param [Hash] options Options to pass on to the drawing and chart
    # @see Worksheet#add_chart
    def add_chart(chart_type, options)
      @drawing ||= Drawing.new worksheet
      drawing.add_chart(chart_type, options)
    end
    
    # adds an image to the drawing object
    # @param [Hash] options Options to pass on to the drawing and image 
    # @see Worksheet#add_image
    def add_image(options)
      @drawing ||= Drawing.new worksheet
      drawing.add_image(options)
    end 
  
    # helper method to tell us if the drawing has something in it or not
    # @return [Boolean] 
    def has_drawing?
      @drawing.is_a? Drawing
    end

    # The relationship required by this object
    # @return [Relationship]
    def relationship
      return unless has_drawing?
      Relationship.new(DRAWING_R, "../#{drawing.pn}") 
    end

    # returns the index of the worksheet releationship that defines this drawing.
    # @return [Integer]
    def index
       worksheet.relationships.index{ |r| r.Type == DRAWING_R } +1 
    end

    # Serialize the drawing for the worksheet
    # @param [String] str
    def to_xml_string(str = '')
      return unless has_drawing? 
      str << "<drawing r:id='rId#{index}'/>"
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
axlsx-1.3.6 lib/axlsx/workbook/worksheet/worksheet_drawing.rb
axlsx-1.3.5 lib/axlsx/workbook/worksheet/worksheet_drawing.rb
axlsx-1.3.4 lib/axlsx/workbook/worksheet/worksheet_drawing.rb
axlsx-1.3.3 lib/axlsx/workbook/worksheet/worksheet_drawing.rb
axlsx-1.3.2 lib/axlsx/workbook/worksheet/worksheet_drawing.rb