summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorRandy Morgan <[email protected]>2013-09-11 11:01:15 +0900
committerRandy Morgan <[email protected]>2013-09-13 00:08:39 +0900
commit923e7b7ff9157bdfe95584b8d311e2f6099ab71d (patch)
tree0af22b454d592a5e4e4287660ac469cbe456fbcb
parent2067c6940fc3a7ef1e8398474ac2c862130504ce (diff)
downloadcaxlsx-923e7b7ff9157bdfe95584b8d311e2f6099ab71d.tar.gz
caxlsx-923e7b7ff9157bdfe95584b8d311e2f6099ab71d.zip
Fixes for anchor swapping when adding images. - Two -> One swap still pending.
-rw-r--r--Gemfile3
-rw-r--r--axlsx.gemspec2
-rw-r--r--examples/anchor_swapping.rb28
-rwxr-xr-xexamples/example.rb4
-rw-r--r--lib/axlsx/drawing/drawing.rb3
-rw-r--r--lib/axlsx/drawing/marker.rb30
-rw-r--r--lib/axlsx/drawing/one_cell_anchor.rb9
-rw-r--r--lib/axlsx/drawing/pic.rb20
-rw-r--r--lib/axlsx/drawing/two_cell_anchor.rb34
-rw-r--r--lib/axlsx/util/serialized_attributes.rb3
-rw-r--r--lib/axlsx/workbook/worksheet/col_breaks.rb6
-rw-r--r--lib/axlsx/workbook/worksheet/row_breaks.rb3
-rw-r--r--test/tc_package.rb2
-rw-r--r--test/workbook/worksheet/tc_worksheet.rb4
14 files changed, 94 insertions, 57 deletions
diff --git a/Gemfile b/Gemfile
index 2786dcba..a68ee61c 100644
--- a/Gemfile
+++ b/Gemfile
@@ -2,8 +2,7 @@ source "https://rubygems.org"
gemspec
group :test do
- gem "rake", "0.8.7" if RUBY_VERSION == "1.9.2"
- gem "rake", ">= 0.8.7" unless RUBY_VERSION == "1.9.2"
+ gem "rake", ">= 0.8.7"
gem "simplecov"
end
diff --git a/axlsx.gemspec b/axlsx.gemspec
index 6f171b6e..332d11c1 100644
--- a/axlsx.gemspec
+++ b/axlsx.gemspec
@@ -23,6 +23,6 @@ Gem::Specification.new do |s|
s.add_development_dependency 'yard'
s.add_development_dependency 'kramdown'
s.add_development_dependency 'timecop', "~> 0.6.1"
- s.required_ruby_version = '>= 1.8.7'
+ s.required_ruby_version = '>= 1.9.2'
s.require_path = 'lib'
end
diff --git a/examples/anchor_swapping.rb b/examples/anchor_swapping.rb
new file mode 100644
index 00000000..88302005
--- /dev/null
+++ b/examples/anchor_swapping.rb
@@ -0,0 +1,28 @@
+$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib"
+require 'axlsx'
+xls = Axlsx::Package.new
+wb = xls.workbook
+wb.add_worksheet do |ws|
+ img = File.expand_path('../image1.jpeg', __FILE__)
+ ws.add_image(:image_src => img) do |image|
+ image.start_at 2, 2
+ image.end_at 5, 5
+ end
+end
+wb.add_worksheet do |ws|
+ img = File.expand_path('../image1.jpeg', __FILE__)
+ ws.add_image(:image_src => img, :start_at => "B2") do |image|
+ image.width = 70
+ image.height = 50
+ end
+end
+wb.add_worksheet do |ws|
+ img = File.expand_path('../image1.jpeg', __FILE__)
+ ws.add_image(:image_src => img, :start_at => [1,1]) do |image|
+ image.end_at "E7"
+ end
+end
+
+
+
+xls.serialize 'anchor.xlsx'
diff --git a/examples/example.rb b/examples/example.rb
index 17e67411..6c8fbd0e 100755
--- a/examples/example.rb
+++ b/examples/example.rb
@@ -720,8 +720,8 @@ if examples.include? :page_breaks
ws = wb.add_worksheet(:name => "page breaks") do |sheet|
sheet.add_row ["A"] * 10
sheet.add_row ["A"] * 10
- sheet.add_page_break("A2")
- sheet.add_page_break(sheet.rows.last.cells[5])
+ sheet.add_page_break("B2")
+ sheet.sheet_view.view = :page_layout # so you can see the breaks!
end
end
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">
diff --git a/test/tc_package.rb b/test/tc_package.rb
index 2ca65132..4d77b8bd 100644
--- a/test/tc_package.rb
+++ b/test/tc_package.rb
@@ -74,6 +74,8 @@ class TestPackage < Test::Unit::TestCase
ws.add_pivot_table 'G5:G6', 'A1:B3'
+ ws.add_page_break "B2"
+
end
def test_use_autowidth
diff --git a/test/workbook/worksheet/tc_worksheet.rb b/test/workbook/worksheet/tc_worksheet.rb
index 6d03f6fc..f6037f57 100644
--- a/test/workbook/worksheet/tc_worksheet.rb
+++ b/test/workbook/worksheet/tc_worksheet.rb
@@ -170,9 +170,9 @@ class TestWorksheet < Test::Unit::TestCase
def test_add_page_break
assert(@ws.row_breaks.empty?)
assert(@ws.col_breaks.empty?)
- @ws.add_page_break("A1")
+ @ws.add_page_break("B1")
assert_equal(1, @ws.row_breaks.size)
- assert_equal(0, @ws.col_breaks.size)
+ assert_equal(1, @ws.col_breaks.size)
end
def test_drawing