summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorStefan Daschek <[email protected]>2012-02-27 21:11:36 +0100
committerSean Duckett <[email protected]>2012-03-07 14:45:02 -0600
commit0257768200922b0dd33eb6e666824d53d61c035c (patch)
tree71806a9adde8ef8aa518ae56c77c2c38f2fbfbc2
parent4fa61b0242704f3406737a6cbb6a39c46690a489 (diff)
downloadcaxlsx-0257768200922b0dd33eb6e666824d53d61c035c.tar.gz
caxlsx-0257768200922b0dd33eb6e666824d53d61c035c.zip
Add support for rows with custom height.
-rw-r--r--README.md4
-rw-r--r--lib/axlsx/workbook/worksheet/row.rb22
-rw-r--r--lib/axlsx/workbook/worksheet/worksheet.rb4
-rw-r--r--test/workbook/worksheet/tc_row.rb36
-rw-r--r--test/workbook/worksheet/tc_worksheet.rb4
5 files changed, 65 insertions, 5 deletions
diff --git a/README.md b/README.md
index 24c5bf58..bf32b8de 100644
--- a/README.md
+++ b/README.md
@@ -84,14 +84,14 @@ To install Axlsx, use the following command:
sheet.add_row [1, 2, 3]
end
-##Using Custom Styles
+##Using Custom Styles and Row Heights
wb.styles do |s|
black_cell = s.add_style :bg_color => "00", :fg_color => "FF", :sz => 14, :alignment => { :horizontal=> :center }
blue_cell = s.add_style :bg_color => "0000FF", :fg_color => "FF", :sz => 20, :alignment => { :horizontal=> :center }
wb.add_worksheet(:name => "Custom Styles") do |sheet|
sheet.add_row ["Text Autowidth", "Second", "Third"], :style => [black_cell, blue_cell, black_cell]
- sheet.add_row [1, 2, 3], :style => Axlsx::STYLE_THIN_BORDER
+ sheet.add_row [1, 2, 3], :style => Axlsx::STYLE_THIN_BORDER, :height => 20
end
end
diff --git a/lib/axlsx/workbook/worksheet/row.rb b/lib/axlsx/workbook/worksheet/row.rb
index 9c35a302..e24514bc 100644
--- a/lib/axlsx/workbook/worksheet/row.rb
+++ b/lib/axlsx/workbook/worksheet/row.rb
@@ -13,12 +13,14 @@ module Axlsx
# @return [SimpleTypedList]
attr_reader :cells
+ # The height of this row in points, if set explicitly.
+ # @return [Float]
+ attr_reader :height
+
# TODO 18.3.1.73
# collapsed
# customFormat
- # customHeight
# hidden
- # ht (height)
# outlineLevel
# ph
# s (style)
@@ -39,12 +41,14 @@ module Axlsx
# @option options [Array] values
# @option options [Array, Symbol] types
# @option options [Array, Integer] style
+ # @option options [Float] height the row's height (in points)
# @see Row#array_to_cells
# @see Cell
def initialize(worksheet, values=[], options={})
self.worksheet = worksheet
@cells = SimpleTypedList.new Cell
@worksheet.rows << self
+ self.height = options.delete(:height) if options[:height]
array_to_cells(values, options)
end
@@ -58,7 +62,9 @@ module Axlsx
# @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to.
# @return [String]
def to_xml(xml)
- xml.row(:r => index+1) { @cells.each { |cell| cell.to_xml(xml) } }
+ attrs = {:r => index+1}
+ attrs.merge!(:customHeight => 1, :ht => height) if custom_height?
+ xml.row(attrs) { |xml| @cells.each { |cell| cell.to_xml(xml) } }
end
# Adds a singel sell to the row based on the data provided and updates the worksheet's autofit data.
@@ -84,6 +90,16 @@ module Axlsx
@cells.to_ary
end
+ # @see height
+ def height=(v); Axlsx::validate_unsigned_numeric(v) unless v.nil?; @height = v end
+
+ # true if the row height has been manually set
+ # @return [Boolean]
+ # @see #height
+ def custom_height?
+ @height != nil
+ end
+
private
# assigns the owning worksheet for this row
diff --git a/lib/axlsx/workbook/worksheet/worksheet.rb b/lib/axlsx/workbook/worksheet/worksheet.rb
index 7622aba0..63ff1310 100644
--- a/lib/axlsx/workbook/worksheet/worksheet.rb
+++ b/lib/axlsx/workbook/worksheet/worksheet.rb
@@ -205,6 +205,9 @@ module Axlsx
#
# ws.add_row ['I wish', 'for a fish', 'on my fish wish dish'], :widths=>[:ignore, :auto, 80]
#
+ # @example - specify a fixed height for a row
+ # ws.add_row ['I wish', 'for a fish', 'on my fish wish dish'], :height => 40
+ #
# @example - create and use a style for all cells in the row
# blue = ws.styles.add_style :color => "#00FF00"
# ws.add_row [1, 2, 3], :style=>blue
@@ -225,6 +228,7 @@ module Axlsx
# @option options [Array, Symbol] types
# @option options [Array, Integer] style
# @option options [Array] widths each member of the widths array will affect how auto_fit behavies.
+ # @option options [Float] height the row's height (in points)
def add_row(values=[], options={})
Row.new(self, values, options)
update_auto_fit_data @rows.last.cells, options.delete(:widths) || []
diff --git a/test/workbook/worksheet/tc_row.rb b/test/workbook/worksheet/tc_row.rb
index f9b589e3..38c910c5 100644
--- a/test/workbook/worksheet/tc_row.rb
+++ b/test/workbook/worksheet/tc_row.rb
@@ -12,6 +12,14 @@ class TestRow < Test::Unit::TestCase
def test_initialize
assert(@row.cells.empty?, "no cells by default")
assert_equal(@row.worksheet, @ws, "has a reference to the worksheet")
+ assert_nil(@row.height, "height defaults to nil")
+ assert([email protected]_height?, "no custom height by default")
+ end
+
+ def test_initialize_with_fixed_height
+ row = @ws.add_row([1,2,3,4,5], :height=>40)
+ assert_equal(40, row.height)
+ assert(row.custom_height?)
end
def test_style
@@ -33,4 +41,32 @@ class TestRow < Test::Unit::TestCase
r = @ws.add_row [1,2,3], :style=>0, :types=>:integer
assert_equal(r.cells.size, 3)
end
+
+ def test_custom_height
+ @row.height = 20
+ assert(@row.custom_height?)
+ end
+
+ def test_height
+ assert_raise(ArgumentError) { @row.height = -3 }
+ assert_nothing_raised { @row.height = 15 }
+ assert_equal(15, @row.height)
+ end
+
+ def test_to_xml_without_custom_height
+ xml = Nokogiri::XML::Builder.new
+ @row.to_xml(xml)
+ doc = Nokogiri::XML.parse(xml.to_xml)
+ assert_equal(0, doc.xpath(".//row[@ht]").size)
+ assert_equal(0, doc.xpath(".//row[@customHeight]").size)
+ end
+
+ def test_to_xml_with_custom_height
+ @row.height = 20
+ xml = Nokogiri::XML::Builder.new
+ @row.to_xml(xml)
+ doc = Nokogiri::XML.parse(xml.to_xml)
+ assert_equal(1, doc.xpath(".//row[@ht=20][@customHeight=1]").size)
+ end
+
end
diff --git a/test/workbook/worksheet/tc_worksheet.rb b/test/workbook/worksheet/tc_worksheet.rb
index b290f06b..ac71fce3 100644
--- a/test/workbook/worksheet/tc_worksheet.rb
+++ b/test/workbook/worksheet/tc_worksheet.rb
@@ -218,6 +218,10 @@ class TestWorksheet < Test::Unit::TestCase
assert_equal(@ws.send(:auto_width, {:sz=>11, :longest => "This is a really long string", :fixed=>0.2}), 0.2, "fixed rules!")
end
+ def test_fixed_height
+ @ws.add_row [1, 2, 3], :height => 40
+ assert_equal(40, @ws.rows[-1].height)
+ end
def test_set_column_width