diff options
| author | Stefan Daschek <[email protected]> | 2012-05-03 19:46:50 +0200 |
|---|---|---|
| committer | Stefan Daschek <[email protected]> | 2012-05-03 19:46:50 +0200 |
| commit | 58effe424218aa8ba6aa8157b49233df05646308 (patch) | |
| tree | b61306ee59d2d2d52b7a25faa5b1b5105b7e8556 /lib | |
| parent | c87151a4bd740391b13d3fcf564e9fa7b0f21b29 (diff) | |
| download | caxlsx-58effe424218aa8ba6aa8157b49233df05646308.tar.gz caxlsx-58effe424218aa8ba6aa8157b49233df05646308.zip | |
Add support for pageSetup.
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/axlsx/util/validators.rb | 16 | ||||
| -rw-r--r-- | lib/axlsx/workbook/workbook.rb | 1 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/page_setup.rb | 90 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/worksheet.rb | 25 |
4 files changed, 132 insertions, 0 deletions
diff --git a/lib/axlsx/util/validators.rb b/lib/axlsx/util/validators.rb index 48f99877..dd86ff56 100644 --- a/lib/axlsx/util/validators.rb +++ b/lib/axlsx/util/validators.rb @@ -90,6 +90,22 @@ module Axlsx DataTypeValidator.validate :float, Float, v end + # Requires that the value is a string containing a positive decimal number followed by one of the following units: + # "mm", "cm", "in", "pt", "pc", "pi" + def self.validate_number_with_unit(v) + RegexValidator.validate "number_with_unit", /\A[0-9]+(\.[0-9]+)?(mm|cm|in|pt|pc|pi)\Z/, v + end + + # Requires that the value is an integer ranging from 10 to 400. + def self.validate_page_scale(v) + DataTypeValidator.validate "page_scale", [Fixnum, Integer], v, lambda { |arg| arg >= 10 && arg <= 400 } + end + + # Requires that the value is one of :default, :landscape, or :portrait. + def self.validate_page_orientation(v) + RestrictionValidator.validate "page_orientation", [:default, :landscape, :portrait], v + end + # Requires that the value is valid pattern type. # valid pattern types must be one of :none, :solid, :mediumGray, :darkGray, :lightGray, :darkHorizontal, :darkVertical, :darkDown, # :darkUp, :darkGrid, :darkTrellis, :lightHorizontal, :lightVertical, :lightDown, :lightUp, :lightGrid, :lightTrellis, :gray125, or :gray0625. diff --git a/lib/axlsx/workbook/workbook.rb b/lib/axlsx/workbook/workbook.rb index b9667648..aeedec61 100644 --- a/lib/axlsx/workbook/workbook.rb +++ b/lib/axlsx/workbook/workbook.rb @@ -4,6 +4,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/page_setup.rb' require 'axlsx/workbook/worksheet/print_options.rb' require 'axlsx/workbook/worksheet/cfvo.rb' require 'axlsx/workbook/worksheet/color_scale.rb' diff --git a/lib/axlsx/workbook/worksheet/page_setup.rb b/lib/axlsx/workbook/worksheet/page_setup.rb new file mode 100644 index 00000000..93ba0dd4 --- /dev/null +++ b/lib/axlsx/workbook/worksheet/page_setup.rb @@ -0,0 +1,90 @@ +module Axlsx + # Page setup settings for printing a worksheet. All settings are optional. + # + # @note The recommended way to manage print options is via Worksheet#page_setup + # @see Worksheet#print_options + # @see Worksheet#initialize + class PageSetup + + # TODO: Attributes defined by Open XML spec that are not implemented yet: + # + # * blackAndWhite + # * cellComments + # * copies + # * draft + # * errors + # * firstPageNumber + # * horizontalDpi + # * pageOrder + # * paperSize + # * useFirstPageNumber + # * usePrinterDefaults + # * verticalDpi + + # Number of vertical pages to fit on. + # @return [Integer] + attr_reader :fit_to_height + + # Number of horizontal pages to fit on. + # @return [Integer] + attr_reader :fit_to_width + + # Orientation of the page (:default, :landscape, :portrait) + # @return [Symbol] + attr_reader :orientation + + # Height of paper (string containing a number followed by a unit identifier: "297mm", "11in") + # @return [String] + attr_reader :paper_height + + # Width of paper (string containing a number followed by a unit identifier: "210mm", "8.5in") + # @return [String] + attr_reader :paper_width + + # Print scaling (percent value, given as integer ranging from 10 to 400) + # @return [Integer] + attr_reader :scale + + + # Creates a new PageSetup object + # @option options [Integer] fit_to_height Number of vertical pages to fit on + # @option options [Integer] fit_to_width Number of horizontal pages to fit on + # @option options [Symbol] orientation Orientation of the page (:default, :landscape, :portrait) + # @option options [String] paper_height Height of paper (number followed by unit identifier: "297mm", "11in") + # @option options [String] paper_width Width of paper (number followed by unit identifier: "210mm", "8.5in") + # @option options [Integer] scale Print scaling (percent value, integer ranging from 10 to 400) + def initialize(options = {}) + set(options) + end + + # Set some or all page settings at once. + # @param [Hash] options The page settings to set (possible keys are :fit_to_height, :fit_to_width, :orientation, :paper_height, :paper_width, and :scale). + def set(options) + options.each do |k, v| + send("#{k}=", v) if respond_to? "#{k}=" + end + end + + # @see fit_to_height + def fit_to_height=(v); Axlsx::validate_unsigned_int(v); @fit_to_height = v; end + # @see fit_to_width + def fit_to_width=(v); Axlsx::validate_unsigned_int(v); @fit_to_width = v; end + # @see orientation + def orientation=(v); Axlsx::validate_page_orientation(v); @orientation = v; end + # @see paper_height + def paper_height=(v); Axlsx::validate_number_with_unit(v); @paper_height = v; end + # @see paper_width + def paper_width=(v); Axlsx::validate_number_with_unit(v); @paper_width = v; end + # @see scale + def scale=(v); Axlsx::validate_page_scale(v); @scale = v; end + + # Serializes the page settings element. + # @param [String] str + # @return [String] + def to_xml_string(str = '') + str << '<pageSetup ' + str << instance_values.map{ |k,v| k.gsub(/_(.)/){ $1.upcase } << %{="#{v}"} }.join(' ') + str << '/>' + end + end +end diff --git a/lib/axlsx/workbook/worksheet/worksheet.rb b/lib/axlsx/workbook/worksheet/worksheet.rb index de5183aa..8f91d629 100644 --- a/lib/axlsx/workbook/worksheet/worksheet.rb +++ b/lib/axlsx/workbook/worksheet/worksheet.rb @@ -81,6 +81,29 @@ module Axlsx end + # Page setup settings for printing the worksheet. + # @example + # wb = Axlsx::Package.new.workbook + # + # # using options when creating the worksheet. + # ws = wb.add_worksheet :page_setup => {:fit_to_width => 1, :orientation => :landscape} + # + # # use the set method of the page_setup object + # ws.page_setup.set(:paper_width => "297mm", :paper_height => "210mm") + # + # # setup page in a block + # ws.page_setup do |page| + # page.scale = 80 + # page.orientation = :portrait + # end + # @see PageSetup#initialize + # @return [PageSetup] + def page_setup + @page_setup ||= PageSetup.new + yield @page_setup if block_given? + @page_setup + end + # Options for printing the worksheet. # @example # wb = Axlsx::Package.new.workbook @@ -130,6 +153,7 @@ module Axlsx @show_gridlines = true self.name = "Sheet" + (index+1).to_s @page_margins = PageMargins.new options[:page_margins] if options[:page_margins] + @page_setup = PageSetup.new options[:page_setup] if options[:page_setup] @print_options = PrintOptions.new options[:print_options] if options[:print_options] @rows = SimpleTypedList.new Row @@ -449,6 +473,7 @@ module Axlsx str.concat "<mergeCells count='%s'>%s</mergeCells>" % [@merged_cells.size, @merged_cells.reduce('') { |memo, obj| memo += "<mergeCell ref='%s'></mergeCell>" % obj } ] unless @merged_cells.empty? print_options.to_xml_string(str) if @print_options page_margins.to_xml_string(str) if @page_margins + page_setup.to_xml_string(str) if @page_setup str.concat "<drawing r:id='rId1'></drawing>" if @drawing unless @tables.empty? str.concat "<tableParts count='%s'>%s</tableParts>" % [@tables.size, @tables.reduce('') { |memo, obj| memo += "<tablePart r:id='%s'/>" % obj.rId }] |
