diff options
| author | Stefan Daschek <[email protected]> | 2012-02-24 22:30:39 +0100 |
|---|---|---|
| committer | Stefan Daschek <[email protected]> | 2012-02-24 22:55:31 +0100 |
| commit | a3f287c6bc810584ff279ae62b955cbe635c5310 (patch) | |
| tree | daee57023f31b6c4cf75db3f580cef75fcc5973e /lib | |
| parent | 6bd48b06be03d4b85b4d2cc2db4f540fb1e5e4a9 (diff) | |
| download | caxlsx-a3f287c6bc810584ff279ae62b955cbe635c5310.tar.gz caxlsx-a3f287c6bc810584ff279ae62b955cbe635c5310.zip | |
Add support for page margins to worksheet.
Diffstat (limited to 'lib')
| -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 |
3 files changed, 96 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 |
