Class: Axlsx::Pic

Inherits:
Object
  • Object
show all
Defined in:
lib/axlsx/drawing/pic.rb

Overview

a Pic object represents an image in your worksheet Worksheet#add_image is the recommended way to manage images in your sheets

Constant Summary collapse

ALLOWED_EXTENSIONS =

allowed file extenstions

['gif', 'jpeg', 'png', 'jpg']

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(anchor, options = {}) {|_self| ... } ⇒ Pic

Creates a new Pic(ture) object

Parameters:

  • anchor (Anchor)

    the anchor that holds this image

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • name (String)
  • descr (String)
  • image_src (String)
  • start_at (Array)
  • width (Intger)
  • height (Intger)

Yields:

  • (_self)

Yield Parameters:

  • _self (Axlsx::Pic)

    the object that the method was called on



66
67
68
69
70
71
72
73
74
# File 'lib/axlsx/drawing/pic.rb', line 66

def initialize(anchor, options={})
  @anchor = anchor
  @anchor.drawing.worksheet.workbook.images << self
  options.each do |o|
    self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
  end
  start_at *options[:start_at] if options[:start_at]
  yield self if block_given?
end

Instance Attribute Details

#anchorOneCellAnchor (readonly)

The anchor for this image

Returns:



36
37
38
# File 'lib/axlsx/drawing/pic.rb', line 36

def anchor
  @anchor
end

#descrString

A description of the picture

Returns:

  • (String)


27
28
29
# File 'lib/axlsx/drawing/pic.rb', line 27

def descr
  @descr
end

#extnameString (readonly)

returns the extension of image_src without the preceeding ‘.’

Returns:

  • (String)


40
41
42
# File 'lib/axlsx/drawing/pic.rb', line 40

def extname
  @extname
end

#file_nameString (readonly)

The name of the image file, sans directory info

Returns:

  • (String)


44
45
46
# File 'lib/axlsx/drawing/pic.rb', line 44

def file_name
  @file_name
end

#heightObject

providing access to update the anchor’s height attribute

Parameters:

  • v (Integer)

See Also:



23
24
25
# File 'lib/axlsx/drawing/pic.rb', line 23

def height
  @height
end

#image_srcString

The path to the image you want to include Only local images are supported at this time and only jpg support

Returns:

  • (String)


32
33
34
# File 'lib/axlsx/drawing/pic.rb', line 32

def image_src
  @image_src
end

#indexIndex (readonly)

The index of this image in the workbooks images collections

Returns:

  • (Index)


52
53
54
# File 'lib/axlsx/drawing/pic.rb', line 52

def index
  @index
end

#nameString

The name to use for this picture

Returns:

  • (String)


13
14
15
# File 'lib/axlsx/drawing/pic.rb', line 13

def name
  @name
end

#pnString (readonly)

The part name for this image used in serialization and relationship building

Returns:

  • (String)


48
49
50
# File 'lib/axlsx/drawing/pic.rb', line 48

def pn
  @pn
end

#widthObject

providing access to the anchor’s width attribute

Parameters:

  • v (Integer)

See Also:



18
19
20
# File 'lib/axlsx/drawing/pic.rb', line 18

def width
  @width
end

Instance Method Details

#start_at(x, y) ⇒ Marker

This is a short cut method to set the start anchor position If you need finer granularity in positioning use graphic_frame.anchor.from.colOff / rowOff

Parameters:

  • x (Integer)

    The column

  • y (Integer)

    The row

Returns:



124
125
126
127
# File 'lib/axlsx/drawing/pic.rb', line 124

def start_at(x, y)
  @anchor.from.col = x
  @anchor.from.row = y
end

#to_xml(xml) ⇒ String

Serializes the picture

Parameters:

  • xml (Nokogiri::XML::Builder)

    The document builder instance this objects xml will be added to.

Returns:

  • (String)


132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/axlsx/drawing/pic.rb', line 132

def to_xml(xml)
  xml.send('xdr:pic') {
    xml.send('xdr:nvPicPr') {
      xml.send('xdr:cNvPr', :id=>"2", :name=>name, :descr=>descr)
      xml.send('xdr:cNvPicPr') {
        xml.send('a:picLocks', :noChangeAspect=>1)
      }
    }
    xml.send('xdr:blipFill') {
      xml.send('a:blip', :'xmlns:r' => XML_NS_R, :'r:embed'=>"rId1")
      xml.send('a:stretch') {
        xml.send('a:fillRect')
      }
    }
    xml.send('xdr:spPr') {
      xml.send('a:xfrm') {
        xml.send('a:off', :x=>0, :y=>0)
        xml.send('a:ext', :cx=>2336800, :cy=>2161540)
      }
      xml.send('a:prstGeom', :prst=>:rect) {
        xml.send('a:avLst')
      }
    }
  }
end