class Axlsx::Pic

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

Constants

ALLOWED_EXTENSIONS

allowed file extenstions

Attributes

anchor[R]

The anchor for this image @return [OneCellAnchor]

descr[R]

A description of the picture @return [String]

image_src[R]

The path to the image you want to include Only local images are supported at this time. @return [String]

name[R]

The name to use for this picture @return [String]

picture_locking[R]

The picture locking attributes for this picture

Public Class Methods

new(anchor, options={}) { |self| ... } click to toggle source

Creates a new Pic(ture) object @param [Anchor] anchor the anchor that holds this image @option options [String] name @option options [String] descr @option options [String] image_src @option options [Array] start_at @option options [Intger] width @option options [Intger] height

# File lib/axlsx/drawing/pic.rb, line 18
def initialize(anchor, options={})
  @anchor = anchor
  @hyperlink = nil
  @anchor.drawing.worksheet.workbook.images << self
  parse_options options
  start_at(*options[:start_at]) if options[:start_at]
  yield self if block_given?
  @picture_locking = PictureLocking.new(options)
end

Public Instance Methods

descr=(v) click to toggle source

@see descr

# File lib/axlsx/drawing/pic.rb, line 79
def descr=(v) Axlsx::validate_string(v); @descr = v; end
end_at(x, y=nil) click to toggle source

noop if not using a two cell anchor @param [Integer] x The column @param [Integer] y The row @return [Marker]

# File lib/axlsx/drawing/pic.rb, line 156
def end_at(x, y=nil)
  use_two_cell_anchor unless @anchor.is_a?(TwoCellAnchor)
  @anchor.end_at x, y
  @anchor.to
end
extname() click to toggle source

returns the extension of image_src without the preceeding ‘.’ @return [String]

# File lib/axlsx/drawing/pic.rb, line 90
def extname
  File.extname(image_src).delete('.') unless image_src.nil?
end
file_name() click to toggle source

The file name of image_src without any path information @return [String]

# File lib/axlsx/drawing/pic.rb, line 84
def file_name
  File.basename(image_src) unless image_src.nil?
end
height() click to toggle source

providing access to update the anchor’s height attribute @param [Integer] v @see OneCellAnchor.width @note this is a noop if you are using a TwoCellAnchor

# File lib/axlsx/drawing/pic.rb, line 130
def height
  @anchor.height
end
height=(v) click to toggle source

@see height @note This is a noop if you are using a TwoCellAnchor

# File lib/axlsx/drawing/pic.rb, line 136
def height=(v)
  use_one_cell_anchor unless @anchor.is_a?(OneCellAnchor)
  @anchor.height = v
end
image_src=(v) click to toggle source
# File lib/axlsx/drawing/pic.rb, line 68
def image_src=(v)
  Axlsx::validate_string(v)
  RestrictionValidator.validate 'Pic.image_src', ALLOWED_EXTENSIONS, File.extname(v.downcase).delete('.')
  raise ArgumentError, "File does not exist" unless File.exist?(v)
  @image_src = v
end
index() click to toggle source

The index of this image in the workbooks images collections @return [Index]

# File lib/axlsx/drawing/pic.rb, line 96
def index
  @anchor.drawing.worksheet.workbook.images.index(self)
end
name=(v) click to toggle source

@see name

# File lib/axlsx/drawing/pic.rb, line 76
def name=(v) Axlsx::validate_string(v); @name = v; end
pn() click to toggle source

The part name for this image used in serialization and relationship building @return [String]

# File lib/axlsx/drawing/pic.rb, line 102
def pn
  "#{IMAGE_PN % [(index+1), extname]}"
end
relationship() click to toggle source

The relationship object for this pic. @return [Relationship]

# File lib/axlsx/drawing/pic.rb, line 108
def relationship
  Relationship.new(self, IMAGE_R, "../#{pn}")
end
start_at(x, y=nil) click to toggle source

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 @param [Integer] x The column @param [Integer] y The row @return [Marker]

# File lib/axlsx/drawing/pic.rb, line 147
def start_at(x, y=nil)
  @anchor.start_at x, y
  @anchor.from
end
to_xml_string(str = '') click to toggle source

Serializes the object @param [String] str @return [String]

# File lib/axlsx/drawing/pic.rb, line 165
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="' << relationship.Id <<  '"/>'
  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
width() click to toggle source

providing access to the anchor’s width attribute @param [Integer] v @see OneCellAnchor.width

# File lib/axlsx/drawing/pic.rb, line 115
def width
  return unless @anchor.is_a?(OneCellAnchor)
  @anchor.width
end
width=(v) click to toggle source

@see width

# File lib/axlsx/drawing/pic.rb, line 121
def width=(v)
  use_one_cell_anchor unless @anchor.is_a?(OneCellAnchor)
  @anchor.width = v
end

Private Instance Methods

swap_anchor(new_anchor) click to toggle source

refactoring of swapping code, law of demeter be damned!

# File lib/axlsx/drawing/pic.rb, line 198
def swap_anchor(new_anchor)
  new_anchor.drawing.anchors.delete(new_anchor)
  @anchor.drawing.anchors[@anchor.drawing.anchors.index(@anchor)] = new_anchor
  new_anchor.instance_variable_set "@object", @anchor.object
  @anchor = new_anchor
end
use_one_cell_anchor() click to toggle source

Changes the anchor to a one cell anchor.

# File lib/axlsx/drawing/pic.rb, line 184
def use_one_cell_anchor
  return if @anchor.is_a?(OneCellAnchor)
  new_anchor = OneCellAnchor.new(@anchor.drawing, :start_at => [@anchor.from.col, @anchor.from.row])
  swap_anchor(new_anchor)
end
use_two_cell_anchor() click to toggle source

changes the anchor type to a two cell anchor

# File lib/axlsx/drawing/pic.rb, line 191
def use_two_cell_anchor
  return if @anchor.is_a?(TwoCellAnchor)
  new_anchor = TwoCellAnchor.new(@anchor.drawing, :start_at => [@anchor.from.col, @anchor.from.row])
  swap_anchor(new_anchor)
end