diff options
| -rw-r--r-- | examples/two_cell_anchor_image.rb | 11 | ||||
| -rw-r--r-- | lib/axlsx/drawing/drawing.rb | 9 | ||||
| -rw-r--r-- | lib/axlsx/drawing/pic.rb | 18 | ||||
| -rw-r--r-- | lib/axlsx/drawing/two_cell_anchor.rb | 7 | ||||
| -rw-r--r-- | test/drawing/tc_drawing.rb | 7 |
5 files changed, 48 insertions, 4 deletions
diff --git a/examples/two_cell_anchor_image.rb b/examples/two_cell_anchor_image.rb new file mode 100644 index 00000000..4fe4b566 --- /dev/null +++ b/examples/two_cell_anchor_image.rb @@ -0,0 +1,11 @@ +#!/usr/bin/env ruby -w -s +# -*- coding: utf-8 -*- +$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib" +require 'axlsx' + +p = Axlsx::Package.new +src = "#{File.dirname(__FILE__)}/image1.png" +p.workbook.add_worksheet(:name => 'double_anchor') do |ws| + ws.add_image(:image_src => src, :start_at => [0,0], :end_at => [2,4]) +end +p.serialize('two_cell_anchor_image.xlsx') diff --git a/lib/axlsx/drawing/drawing.rb b/lib/axlsx/drawing/drawing.rb index 943c0f2d..b06130f1 100644 --- a/lib/axlsx/drawing/drawing.rb +++ b/lib/axlsx/drawing/drawing.rb @@ -68,11 +68,16 @@ module Axlsx @anchors = SimpleTypedList.new [TwoCellAnchor, OneCellAnchor] end - # Adds an image to the chart + # Adds an image to the chart If th end_at option is specified we create a two cell anchor. By default we use a one cell anchor. # @note The recommended way to manage images is to use Worksheet.add_image. Please refer to that method for documentation. # @see Worksheet#add_image + # @return [Pic] def add_image(options={}) - OneCellAnchor.new(self, options) + if options[:end_at] + TwoCellAnchor.new(self, options).add_pic(options) + else + OneCellAnchor.new(self, options) + end @anchors.last.object end diff --git a/lib/axlsx/drawing/pic.rb b/lib/axlsx/drawing/pic.rb index e5e005fa..e7b5bbbb 100644 --- a/lib/axlsx/drawing/pic.rb +++ b/lib/axlsx/drawing/pic.rb @@ -113,23 +113,29 @@ module Axlsx # @param [Integer] v # @see OneCellAnchor.width def width + return unless @anchor.is_a?(OneCellAnchor) @anchor.width end # @see width def width=(v) + return unless @anchor.is_a?(OneCellAnchor) @anchor.width = v end # 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 def height + return unless @anchor.is_a?(OneCellAnchor) @anchor.height end # @see height + # @note This is a noop if you are using a TwoCellAnchor def height=(v) + return unless @anchor.is_a?(OneCellAnchor) @anchor.height = v end @@ -142,6 +148,18 @@ module Axlsx def start_at(x, y) @anchor.from.col = x @anchor.from.row = y + @anchor.from + end + + # noop if not using a two cell anchor + # @param [Integer] x The column + # @param [Integer] y The row + # @return [Marker] + def end_at(x, y) + return unless @anchor.is_a?(TwoCellAnchor) + @anchor.to.col = x + @anchor.to.row = y + @anchor.to end # Serializes the object diff --git a/lib/axlsx/drawing/two_cell_anchor.rb b/lib/axlsx/drawing/two_cell_anchor.rb index b496bd8a..8071515c 100644 --- a/lib/axlsx/drawing/two_cell_anchor.rb +++ b/lib/axlsx/drawing/two_cell_anchor.rb @@ -34,7 +34,7 @@ module Axlsx # @param [Drawing] drawing # @param [Class] chart_type This is passed to the graphic frame for instantiation. must be Chart or a subclass of Chart # @param object The object this anchor holds. - # @option options [Array] start_at the col, row to start at + # @option options [Array] start_at the col, row to start at THIS IS DOCUMENTED BUT NOT IMPLEMENTED HERE! # @option options [Array] end_at the col, row to end at def initialize(drawing, options={}) @drawing = drawing @@ -49,6 +49,11 @@ module Axlsx @object.chart end + # Creates an image associated with this anchor. + def add_pic(options={}) + @object = Pic.new(self, options) + end + # The index of this anchor in the drawing # @return [Integer] def index diff --git a/test/drawing/tc_drawing.rb b/test/drawing/tc_drawing.rb index ce77ef9b..4d213d56 100644 --- a/test/drawing/tc_drawing.rb +++ b/test/drawing/tc_drawing.rb @@ -32,7 +32,12 @@ class TestDrawing < Test::Unit::TestCase assert_equal(600, image.width) assert_equal(400, image.height) end - + def test_add_two_cell_anchor_image + src = File.dirname(__FILE__) + "/../../examples/image1.jpeg" + image = @ws.add_image(:image_src => src, :start_at=>[0,0], :end_at => [15,0]) + assert(@ws.drawing.anchors.last.is_a?(Axlsx::TwoCellAnchor)) + assert(image.is_a?(Axlsx::Pic)) + end def test_charts assert(@ws.drawing.charts.empty?) chart = @ws.add_chart(Axlsx::Pie3DChart, :title=>"bob", :start_at=>[0,0], :end_at=>[1,1]) |
