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



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/axlsx/drawing/pic.rb', line 40

def initialize(anchor, options={})
  @anchor = anchor
  @hyperlink = nil
  @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?
  @picture_locking = PictureLocking.new(options)
end

Instance Attribute Details

#anchorOneCellAnchor (readonly)

The anchor for this image

Returns:



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

def anchor
  @anchor
end

#descrString

A description of the picture

Returns:

  • (String)


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

def descr
  @descr
end

Returns the value of attribute hyperlink.



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

def hyperlink
  @hyperlink
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)


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

def image_src
  @image_src
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

#picture_lockingObject (readonly)

The picture locking attributes for this picture



30
31
32
# File 'lib/axlsx/drawing/pic.rb', line 30

def picture_locking
  @picture_locking
end

Instance Method Details

#extnameString

returns the extension of image_src without the preceeding ‘.’

Returns:

  • (String)


91
92
93
# File 'lib/axlsx/drawing/pic.rb', line 91

def extname
  File.extname(image_src).delete('.') unless image_src.nil?
end

#file_nameString

The file name of image_src without any path information

Returns:

  • (String)


85
86
87
# File 'lib/axlsx/drawing/pic.rb', line 85

def file_name
  File.basename(image_src) unless image_src.nil?
end

#heightObject

providing access to update the anchor’s height attribute

Parameters:

  • v (Integer)

See Also:



127
128
129
# File 'lib/axlsx/drawing/pic.rb', line 127

def height
  @anchor.height
end

#height=(v) ⇒ Object

See Also:



132
133
134
# File 'lib/axlsx/drawing/pic.rb', line 132

def height=(v)
  @anchor.height = v
end

#idObject

The relational id withing the drawing’s relationships



108
109
110
# File 'lib/axlsx/drawing/pic.rb', line 108

def id
  @anchor.drawing.charts.size + @anchor.drawing.images.index(self) + 1
end

#indexIndex

The index of this image in the workbooks images collections

Returns:

  • (Index)


97
98
99
# File 'lib/axlsx/drawing/pic.rb', line 97

def index
  @anchor.drawing.worksheet.workbook.images.index(self)
end

#pnString

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

Returns:

  • (String)


103
104
105
# File 'lib/axlsx/drawing/pic.rb', line 103

def pn
  "#{IMAGE_PN % [(index+1), extname]}"
end

#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:



142
143
144
145
# File 'lib/axlsx/drawing/pic.rb', line 142

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

#to_xml_string(str = '') ⇒ String

Serializes the object

Parameters:

  • str (String) (defaults to: '')

Returns:

  • (String)


150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/axlsx/drawing/pic.rb', line 150

def to_xml_string(str = '')
  str << '<xdr:pic>'
  str << '<xdr:nvPicPr>'
  str << '<xdr:cNvPr id="2" name="' << name.to_s << '" descr="' << descr.to_s << '">'
  @hyperlink.to_xml_string(str) if @hyperlink.is_a?(Hyperlink)
  str << '</xdr:cNvPr><xdr:cNvPicPr>'
  picture_locking.to_xml_string(str)
  str << '</xdr:cNvPicPr></xdr:nvPicPr>'
  str << '<xdr:blipFill>'
  str << '<a:blip xmlns:r ="' << XML_NS_R << '" r:embed="rId' << id.to_s <<  '"/>'
  str << '<a:stretch><a:fillRect/></a:stretch></xdr:blipFill><xdr:spPr>'
  str << '<a:xfrm><a:off x="0" y="0"/><a:ext cx="2336800" cy="2161540"/></a:xfrm>'
  str << '<a:prstGeom prst="rect"><a:avLst/></a:prstGeom></xdr:spPr></xdr:pic>'

end

#widthObject

providing access to the anchor’s width attribute

Parameters:

  • v (Integer)

See Also:



115
116
117
# File 'lib/axlsx/drawing/pic.rb', line 115

def width
  @anchor.width
end

#width=(v) ⇒ Object

See Also:



120
121
122
# File 'lib/axlsx/drawing/pic.rb', line 120

def width=(v)
  @anchor.width = v
end