diff options
| author | Stefan Daschek <[email protected]> | 2012-02-24 22:30:39 +0100 |
|---|---|---|
| committer | Sean Duckett <[email protected]> | 2012-03-07 14:45:01 -0600 |
| commit | 228ffa672ad993489529bff2128ecc65d42046e8 (patch) | |
| tree | 3f19b0f51d414ad9524aab1b624fbad23ec28432 | |
| parent | 6a93c109acf064ad81882e910593b8bfce79d412 (diff) | |
| download | caxlsx-228ffa672ad993489529bff2128ecc65d42046e8.tar.gz caxlsx-228ffa672ad993489529bff2128ecc65d42046e8.zip | |
Add support for page margins to worksheet.
| -rw-r--r-- | lib/axlsx/workbook/workbook.rb | 1 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/page_margins.rb | 89 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/worksheet.rb | 6 | ||||
| -rw-r--r-- | test/workbook/worksheet/tc_page_margins.rb | 105 |
4 files changed, 201 insertions, 0 deletions
diff --git a/lib/axlsx/workbook/workbook.rb b/lib/axlsx/workbook/workbook.rb index 8a6eb629..47af692b 100644 --- a/lib/axlsx/workbook/workbook.rb +++ b/lib/axlsx/workbook/workbook.rb @@ -3,6 +3,7 @@ module Axlsx require 'axlsx/workbook/worksheet/date_time_converter.rb' require 'axlsx/workbook/worksheet/cell.rb' +require 'axlsx/workbook/worksheet/page_margins.rb' require 'axlsx/workbook/worksheet/row.rb' require 'axlsx/workbook/worksheet/worksheet.rb' require 'axlsx/workbook/shared_strings_table.rb' diff --git a/lib/axlsx/workbook/worksheet/page_margins.rb b/lib/axlsx/workbook/worksheet/page_margins.rb new file mode 100644 index 00000000..86c686c5 --- /dev/null +++ b/lib/axlsx/workbook/worksheet/page_margins.rb @@ -0,0 +1,89 @@ +module Axlsx + # PageMargins specify the margins when printing a worksheet. + # + # For compatibility, PageMargins serialize to an empty string, unless at least one custom margin value + # has been specified. Otherwise, it serializes to a PageMargin element specifying all 6 margin values + # (using default values for margins that have not been specified explicitly). + # + # @see Worksheet#page_margins + class PageMargins + + # Default left and right margin (in inches) + DEFAULT_LEFT_RIGHT = 0.5 + + # Default top and bottom margins (in inches) + DEFAULT_TOP_BOTTOM = 1.00 + + # Default header and footer margins (in inches) + DEFAULT_HEADER_FOOTER = 0.50 + + # Left margin (in inches) + # @return [Float] + attr_reader :left + + # Right margin (in inches) + # @return [Float] + attr_reader :right + + # Top margin (in inches) + # @return [Float] + attr_reader :top + + # Bottom margin (in inches) + # @return [Float] + attr_reader :bottom + + # Header margin (in inches) + # @return [Float] + attr_reader :header + + # Footer margin (in inches) + # @return [Float] + attr_reader :footer + + def initialize + # Default values taken from MS Excel for Mac 2011 + @left = @right = DEFAULT_LEFT_RIGHT + @top = @bottom = DEFAULT_TOP_BOTTOM + @header = @footer = DEFAULT_HEADER_FOOTER + + @custom_margins_specified = false + end + + # True if custom page margins have been specified. + def custom_margins_specified? + @custom_margins_specified + end + + # Set some or all margins at once. + # @param [Hash] margins the margins to set (possible keys are :left, :right, :top, :bottom, :header and :footer). + def set(margins) + margins.select do |k, v| + next unless [:left, :right, :top, :bottom, :header, :footer].include? k + send("#{k}=", v) + end + end + + # @see left + def left=(v); Axlsx::validate_unsigned_numeric(v); @custom_margins_specified = true; @left = v end + # @see right + def right=(v); Axlsx::validate_unsigned_numeric(v); @custom_margins_specified = true; @right = v end + # @see top + def top=(v); Axlsx::validate_unsigned_numeric(v); @custom_margins_specified = true; @top = v end + # @see bottom + def bottom=(v); Axlsx::validate_unsigned_numeric(v); @custom_margins_specified = true; @bottom = v end + # @see header + def header=(v); Axlsx::validate_unsigned_numeric(v); @custom_margins_specified = true; @header = v end + # @see footer + def footer=(v); Axlsx::validate_unsigned_numeric(v); @custom_margins_specified = true; @footer = v end + + # Serializes the page margins element + # @note For compatibility, this is a noop unless custom margins have been specified. + # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to. + # @see #custom_margins_specified? + def to_xml(xml) + return unless custom_margins_specified? + xml.pageMargins :left => left, :right => right, :top => top, :bottom => bottom, :header => header, :footer => footer + end + end +end
\ No newline at end of file diff --git a/lib/axlsx/workbook/worksheet/worksheet.rb b/lib/axlsx/workbook/worksheet/worksheet.rb index a8846913..ca2c51d6 100644 --- a/lib/axlsx/workbook/worksheet/worksheet.rb +++ b/lib/axlsx/workbook/worksheet/worksheet.rb @@ -35,6 +35,10 @@ module Axlsx # @return Array attr_reader :auto_filter + # Page margins for printing the worksheet. + # @return [PageMargins] + attr_reader :page_margins + # Creates a new worksheet. # @note the recommended way to manage worksheets is Workbook#add_worksheet # @see Workbook#add_worksheet @@ -50,6 +54,7 @@ module Axlsx @magick_draw = Magick::Draw.new @cols = SimpleTypedList.new Cell @merged_cells = [] + @page_margins = PageMargins.new end # convinience method to access all cells in this worksheet @@ -321,6 +326,7 @@ module Axlsx } xml.autoFilter :ref=>@auto_filter if @auto_filter xml.mergeCells(:count=>@merged_cells.size) { @merged_cells.each { | mc | xml.mergeCell(:ref=>mc) } } unless @merged_cells.empty? + @page_margins.to_xml(xml) xml.drawing :"r:id"=>"rId1" if @drawing } end diff --git a/test/workbook/worksheet/tc_page_margins.rb b/test/workbook/worksheet/tc_page_margins.rb new file mode 100644 index 00000000..6f2e6fdb --- /dev/null +++ b/test/workbook/worksheet/tc_page_margins.rb @@ -0,0 +1,105 @@ +require 'test/unit' +require 'axlsx.rb' + +class TestPageMargins < Test::Unit::TestCase + + def setup + p = Axlsx::Package.new + ws = p.workbook.add_worksheet :name=>"hmmm" + @pm = ws.page_margins + end + + def test_initialize + assert_equal(false, @pm.custom_margins_specified?) + assert_equal(Axlsx::PageMargins::DEFAULT_LEFT_RIGHT, @pm.left) + assert_equal(Axlsx::PageMargins::DEFAULT_LEFT_RIGHT, @pm.right) + assert_equal(Axlsx::PageMargins::DEFAULT_TOP_BOTTOM, @pm.top) + assert_equal(Axlsx::PageMargins::DEFAULT_TOP_BOTTOM, @pm.bottom) + assert_equal(Axlsx::PageMargins::DEFAULT_HEADER_FOOTER, @pm.header) + assert_equal(Axlsx::PageMargins::DEFAULT_HEADER_FOOTER, @pm.footer) + end + + def test_custom_margins_specified + @pm.left = 0.5 + assert(@pm.custom_margins_specified?) + end + + def test_set_all_values + @pm.set(:left => 1.1, :right => 1.2, :top => 1.3, :bottom => 1.4, :header => 0.8, :footer => 0.9) + assert(@pm.custom_margins_specified?) + assert_equal(1.1, @pm.left) + assert_equal(1.2, @pm.right) + assert_equal(1.3, @pm.top) + assert_equal(1.4, @pm.bottom) + assert_equal(0.8, @pm.header) + assert_equal(0.9, @pm.footer) + end + + def test_set_some_values + @pm.set(:left => 1.1, :right => 1.2) + assert(@pm.custom_margins_specified?) + assert_equal(1.1, @pm.left) + assert_equal(1.2, @pm.right) + assert_equal(Axlsx::PageMargins::DEFAULT_TOP_BOTTOM, @pm.top) + assert_equal(Axlsx::PageMargins::DEFAULT_TOP_BOTTOM, @pm.bottom) + assert_equal(Axlsx::PageMargins::DEFAULT_HEADER_FOOTER, @pm.header) + assert_equal(Axlsx::PageMargins::DEFAULT_HEADER_FOOTER, @pm.footer) + end + + def test_to_xml + @pm.left = 1.1 + @pm.right = 1.2 + @pm.top = 1.3 + @pm.bottom = 1.4 + @pm.header = 0.8 + @pm.footer = 0.9 + xml = Nokogiri::XML::Builder.new + @pm.to_xml(xml) + doc = Nokogiri::XML.parse(xml.to_xml) + assert_equal(1, doc.xpath(".//pageMargins[@left=1.1][@right=1.2][@top=1.3][@bottom=1.4][@header=0.8][@footer=0.9]").size) + end + + def test_to_xml_is_noop_unless_custom_margins_specified + assert_equal(false, @pm.custom_margins_specified?) + xml = Nokogiri::XML::Builder.new + @pm.to_xml(xml) + doc = Nokogiri::XML.parse(xml.to_xml) + assert_equal(0, doc.children.size) + end + + def test_left + assert_raise(ArgumentError) { @pm.left = -1.2 } + assert_nothing_raised { @pm.left = 1.5 } + assert_equal(@pm.left, 1.5) + end + + def test_right + assert_raise(ArgumentError) { @pm.right = -1.2 } + assert_nothing_raised { @pm.right = 1.5 } + assert_equal(@pm.right, 1.5) + end + + def test_top + assert_raise(ArgumentError) { @pm.top = -1.2 } + assert_nothing_raised { @pm.top = 1.5 } + assert_equal(@pm.top, 1.5) + end + + def test_bottom + assert_raise(ArgumentError) { @pm.bottom = -1.2 } + assert_nothing_raised { @pm.bottom = 1.5 } + assert_equal(@pm.bottom, 1.5) + end + + def test_header + assert_raise(ArgumentError) { @pm.header = -1.2 } + assert_nothing_raised { @pm.header = 1.5 } + assert_equal(@pm.header, 1.5) + end + + def test_footer + assert_raise(ArgumentError) { @pm.footer = -1.2 } + assert_nothing_raised { @pm.footer = 1.5 } + assert_equal(@pm.footer, 1.5) + end +end |
