diff options
| author | Randy Morgan <[email protected]> | 2013-09-11 11:01:15 +0900 |
|---|---|---|
| committer | Randy Morgan <[email protected]> | 2013-09-13 00:08:39 +0900 |
| commit | 923e7b7ff9157bdfe95584b8d311e2f6099ab71d (patch) | |
| tree | 0af22b454d592a5e4e4287660ac469cbe456fbcb /lib | |
| parent | 2067c6940fc3a7ef1e8398474ac2c862130504ce (diff) | |
| download | caxlsx-923e7b7ff9157bdfe95584b8d311e2f6099ab71d.tar.gz caxlsx-923e7b7ff9157bdfe95584b8d311e2f6099ab71d.zip | |
Fixes for anchor swapping when adding images. - Two -> One swap still pending.
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/axlsx/drawing/drawing.rb | 3 | ||||
| -rw-r--r-- | lib/axlsx/drawing/marker.rb | 30 | ||||
| -rw-r--r-- | lib/axlsx/drawing/one_cell_anchor.rb | 9 | ||||
| -rw-r--r-- | lib/axlsx/drawing/pic.rb | 20 | ||||
| -rw-r--r-- | lib/axlsx/drawing/two_cell_anchor.rb | 34 | ||||
| -rw-r--r-- | lib/axlsx/util/serialized_attributes.rb | 3 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/col_breaks.rb | 6 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/row_breaks.rb | 3 |
8 files changed, 58 insertions, 50 deletions
diff --git a/lib/axlsx/drawing/drawing.rb b/lib/axlsx/drawing/drawing.rb index 1748e9f4..b5856909 100644 --- a/lib/axlsx/drawing/drawing.rb +++ b/lib/axlsx/drawing/drawing.rb @@ -82,7 +82,7 @@ module Axlsx TwoCellAnchor.new(self, options).add_pic(options) else OneCellAnchor.new(self, options) - end + end @anchors.last.object end @@ -154,7 +154,6 @@ module Axlsx def to_xml_string(str = '') str << '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' str << '<xdr:wsDr xmlns:xdr="' << XML_NS_XDR << '" xmlns:a="' << XML_NS_A << '">' - anchors.each { |anchor| anchor.to_xml_string(str) } str << '</xdr:wsDr>' end diff --git a/lib/axlsx/drawing/marker.rb b/lib/axlsx/drawing/marker.rb index 2a46e27f..6180a0f4 100644 --- a/lib/axlsx/drawing/marker.rb +++ b/lib/axlsx/drawing/marker.rb @@ -43,11 +43,14 @@ module Axlsx def rowOff=(v) Axlsx::validate_int v; @rowOff = v end # shortcut to set the column, row position for this marker - # @param col the column for the marker - # @param row the row of the marker - def coord(col, row) - self.col = col - self.row = row + # @param col the column for the marker, a Cell object or a string reference like "B7" + # or an Array. + # @param row the row of the marker. This is ignored if the col parameter is a Cell or + # String or Array. + def coord(col, row=0) + coordinates = parse_coord_args(col, row) + self.col = coordinates[0] + self.row = coordinates[1] end # Serializes the object @@ -58,6 +61,23 @@ module Axlsx str << '<xdr:' << k.to_s << '>' << self.send(k).to_s << '</xdr:' << k.to_s << '>' end end + private + + # handles multiple inputs for setting the position of a marker + # @see Chart#start_at + def parse_coord_args(x, y=0) + if x.is_a?(String) + x, y = *Axlsx::name_to_indices(x) + end + if x.is_a?(Cell) + x, y = *x.pos + end + if x.is_a?(Array) + x, y = *x + end + [x, y] + end + end diff --git a/lib/axlsx/drawing/one_cell_anchor.rb b/lib/axlsx/drawing/one_cell_anchor.rb index b16a939b..faf78278 100644 --- a/lib/axlsx/drawing/one_cell_anchor.rb +++ b/lib/axlsx/drawing/one_cell_anchor.rb @@ -48,6 +48,15 @@ module Axlsx # @return [Integer] attr_reader :height + # sets the starting position for the anchor. + # You can provide a String like "A1", an array like [0,0] or a cell object for the x parameter. + # We just 'figure it out' for you. + # @param [Array, String, Cell, Integer] x Accepts many inputs for defining the starting position of the cell. + # @param [Integer] y When x is an integer, this value is used for the row index at which the anchor starts. + def start_at(x, y=0) + from.coord x, y + end + # # @see height def height=(v) Axlsx::validate_unsigned_int(v); @height = v; end diff --git a/lib/axlsx/drawing/pic.rb b/lib/axlsx/drawing/pic.rb index a6c85705..d001ad8f 100644 --- a/lib/axlsx/drawing/pic.rb +++ b/lib/axlsx/drawing/pic.rb @@ -144,9 +144,8 @@ module Axlsx # @param [Integer] x The column # @param [Integer] y The row # @return [Marker] - def start_at(x, y) - @anchor.from.col = x - @anchor.from.row = y + def start_at(x, y=nil) + @anchor.start_at x, y @anchor.from end @@ -154,10 +153,9 @@ module Axlsx # @param [Integer] x The column # @param [Integer] y The row # @return [Marker] - def end_at(x, y) + def end_at(x, y=nil) use_two_cell_anchor unless @anchor.is_a?(TwoCellAnchor) - @anchor.to.col = x - @anchor.to.row = y + @anchor.end_at x, y @anchor.to end @@ -185,22 +183,22 @@ module Axlsx # Changes the anchor to a one cell anchor. def use_one_cell_anchor return if @anchor.is_a?(OneCellAnchor) - swap_anchor(OneCellAnchor.new(@anchor.drawing, :from => @anchor.from)) + new_anchor = OneCellAnchor.new(@anchor.drawing, :start_at => [@anchor.from.col, @anchor.from.row]) + swap_anchor(new_anchor) end #changes the anchor type to a two cell anchor def use_two_cell_anchor return if @anchor.is_a?(TwoCellAnchor) - swap_anchor(TwoCellAnchor.new(@anchor.drawing)).tap do |new_anchor| - new_anchor.from.col = @anchor.from.col - new_anchor.from.row = @anchor.from.row - end + new_anchor = TwoCellAnchor.new(@anchor.drawing, :start_at => [@anchor.from.col, @anchor.from.row]) + swap_anchor(new_anchor) end # refactoring of swapping code, law of demeter be damned! 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 end diff --git a/lib/axlsx/drawing/two_cell_anchor.rb b/lib/axlsx/drawing/two_cell_anchor.rb index e7b27408..b18268f7 100644 --- a/lib/axlsx/drawing/two_cell_anchor.rb +++ b/lib/axlsx/drawing/two_cell_anchor.rb @@ -5,6 +5,8 @@ module Axlsx # @see Worksheet#add_chart class TwoCellAnchor + include Axlsx::OptionsParser + # A marker that defines the from cell anchor. The default from column and row are 0 and 0 respectively # @return [Marker] attr_reader :from @@ -34,22 +36,23 @@ module Axlsx @drawing = drawing drawing.anchors << self @from, @to = Marker.new, Marker.new(:col => 5, :row=>10) + parse_options options end # sets the col, row attributes for the from marker. # @note The recommended way to set the start position for graphical # objects is directly thru the object. # @see Chart#start_at - def start_at(x, y) - set_marker_coords(x, y, from) + def start_at(x, y=nil) + from.coord x, y end # sets the col, row attributes for the to marker # @note the recommended way to set the to position for graphical # objects is directly thru the object # @see Char#end_at - def end_at(x, y) - set_marker_coords(x, y, to) + def end_at(x, y=nil) + to.coord x, y end # Creates a graphic frame and chart object associated with this anchor @@ -85,28 +88,5 @@ module Axlsx str << '<xdr:clientData/>' str << '</xdr:twoCellAnchor>' end - private - - # parses coordinates and sets up a marker's row/col propery - def set_marker_coords(x, y, marker) - marker.col, marker.row = *parse_coord_args(x, y) - end - - # handles multiple inputs for setting the position of a marker - # @see Chart#start_at - def parse_coord_args(x, y=0) - if x.is_a?(String) - x, y = *Axlsx::name_to_indices(x) - end - if x.is_a?(Cell) - x, y = *x.pos - end - if x.is_a?(Array) - x, y = *x - end - [x, y] - end - - end end diff --git a/lib/axlsx/util/serialized_attributes.rb b/lib/axlsx/util/serialized_attributes.rb index d38afe78..2a651ede 100644 --- a/lib/axlsx/util/serialized_attributes.rb +++ b/lib/axlsx/util/serialized_attributes.rb @@ -47,6 +47,9 @@ module Axlsx str end + # A hash of instance variables that have been declared with + # seraialized_attributes and are not nil. + # This requires ruby 1.9.3 or higher def declared_attributes instance_values.select do |key, value| value != nil && self.class.xml_attributes.include?(key.to_sym) diff --git a/lib/axlsx/workbook/worksheet/col_breaks.rb b/lib/axlsx/workbook/worksheet/col_breaks.rb index 0e274b93..63ba8292 100644 --- a/lib/axlsx/workbook/worksheet/col_breaks.rb +++ b/lib/axlsx/workbook/worksheet/col_breaks.rb @@ -16,8 +16,8 @@ module Axlsx # Break will be passed to the created break object. # @see Break def add_break(options) - options.merge :max => 1048575, :man => true - @list << Break.new(options) + @list << Break.new(options.merge(:max => 1048575, :man => true)) + last end # Serialize the collection to xml @@ -27,7 +27,7 @@ module Axlsx # </colBreaks> def to_xml_string(str='') return if empty? - str << '<colBreaks count="' << @list.count.to_s << '" manualBreakCount="' << @list.size.to_s << '">' + str << '<colBreaks count="' << @list.size.to_s << '" manualBreakCount="' << @list.size.to_s << '">' each { |brk| brk.to_xml_string(str) } str << '</colBreaks>' end diff --git a/lib/axlsx/workbook/worksheet/row_breaks.rb b/lib/axlsx/workbook/worksheet/row_breaks.rb index 2af7216d..968cf2f7 100644 --- a/lib/axlsx/workbook/worksheet/row_breaks.rb +++ b/lib/axlsx/workbook/worksheet/row_breaks.rb @@ -10,8 +10,7 @@ module Axlsx def add_break(options) # force feed the excel default - options.merge :max => 16383, :man => true - @list << Break.new(options) + @list << Break.new(options.merge(:max => 16383, :man => true)) last end # <rowBreaks count="3" manualBreakCount="3"> |
