Skip to content

Commit

Permalink
If worksheet.fit_to_page must be true, lets set it to true. I am far …
Browse files Browse the repository at this point in the history
…too lazy and forgetful to remember to set that every time I want to fit_to_width or fit_to_height.
  • Loading branch information
Randy Morgan committed May 15, 2012
1 parent 80252d1 commit 5dddaaf
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
15 changes: 7 additions & 8 deletions lib/axlsx/workbook/worksheet/page_setup.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
module Axlsx
# Page setup settings for printing a worksheet. All settings are optional.
#
# When using {#fit_to_width} and/or {#fit_to_height}, make sure to also set {Worksheet#fit_to_page} to true –
# otherwise this setting will not have any effect.
#
# @note The recommended way to manage print options is via Worksheet#page_setup
# @see Worksheet#print_options
# @see Worksheet#initialize
Expand All @@ -26,12 +23,10 @@ class PageSetup

# Number of vertical pages to fit on.
# @return [Integer]
# @note Make sure to also set {Worksheet#fit_to_page} to true – otherwise this setting will not have any effect.
attr_reader :fit_to_height

# Number of horizontal pages to fit on.
# @return [Integer]
# @note Make sure to also set {Worksheet#fit_to_page} to true – otherwise this setting will not have any effect.
attr_reader :fit_to_width

# Orientation of the page (:default, :landscape, :portrait)
Expand All @@ -50,6 +45,8 @@ class PageSetup
# @return [Integer]
attr_reader :scale

# The worksheet that owns this page_setup.
attr_reader :worksheet

# Creates a new PageSetup object
# @option options [Integer] fit_to_height Number of vertical pages to fit on
Expand All @@ -59,6 +56,8 @@ class PageSetup
# @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 = {})
raise ArgumentError, "Worksheet option is required" unless options[:worksheet].is_a?(Worksheet)
@worksheet = options[:worksheet]
set(options)
end

Expand All @@ -71,9 +70,9 @@ def set(options)
end

# @see fit_to_height
def fit_to_height=(v); Axlsx::validate_unsigned_int(v); @fit_to_height = v; end
def fit_to_height=(v); Axlsx::validate_unsigned_int(v); @fit_to_height = v; @worksheet.fit_to_page = true; end
# @see fit_to_width
def fit_to_width=(v); Axlsx::validate_unsigned_int(v); @fit_to_width = v; end
def fit_to_width=(v); Axlsx::validate_unsigned_int(v); @fit_to_width = v; @worksheet.fit_to_page = true; end
# @see orientation
def orientation=(v); Axlsx::validate_page_orientation(v); @orientation = v; end
# @see paper_height
Expand All @@ -88,7 +87,7 @@ def scale=(v); Axlsx::validate_page_scale(v); @scale = v; end
# @return [String]
def to_xml_string(str = '')
str << '<pageSetup '
str << instance_values.map{ |k,v| k.gsub(/_(.)/){ $1.upcase } << %{="#{v}"} }.join(' ')
str << instance_values.reject{ |k, v| k == 'worksheet' }.map{ |k,v| k.gsub(/_(.)/){ $1.upcase } << %{="#{v}"} }.join(' ')
str << '/>'
end
end
Expand Down
5 changes: 2 additions & 3 deletions lib/axlsx/workbook/worksheet/worksheet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ def page_margins
#
# # using options when creating the worksheet.
# ws = wb.add_worksheet :page_setup => {:fit_to_width => 2, :orientation => :landscape}
# ws.fit_to_page = true # otherwise fit_to_width will be ignored
#
# # use the set method of the page_setup object
# ws.page_setup.set(:paper_width => "297mm", :paper_height => "210mm")
Expand All @@ -103,7 +102,7 @@ def page_margins
# @see PageSetup#initialize
# @return [PageSetup]
def page_setup
@page_setup ||= PageSetup.new
@page_setup ||= PageSetup.new(:worksheet => self)
yield @page_setup if block_given?
@page_setup
end
Expand Down Expand Up @@ -157,7 +156,7 @@ def initialize(wb, options={})
@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]
@page_setup = PageSetup.new options[:page_setup].merge(:worksheet=>self) if options[:page_setup]
@print_options = PrintOptions.new options[:print_options] if options[:print_options]
@rows = SimpleTypedList.new Row
@column_info = SimpleTypedList.new Col
Expand Down
17 changes: 14 additions & 3 deletions test/workbook/worksheet/tc_page_setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
class TestPageSetup < Test::Unit::TestCase

def setup
p = Axlsx::Package.new
ws = p.workbook.add_worksheet :name => "hmmm"
@p = Axlsx::Package.new
ws = @p.workbook.add_worksheet :name => "hmmm"
@ps = ws.page_setup
end

Expand All @@ -18,9 +18,17 @@ def test_initialize
end

def test_initialize_with_options
optioned = Axlsx::PageSetup.new(:fit_to_height => 1, :fit_to_width => 2, :orientation => :landscape, :paper_height => "297mm", :paper_width => "210mm", :scale => 50)
page_setup = { :fit_to_height => 1,
:fit_to_width => 2,
:orientation => :landscape,
:paper_height => "297mm",
:paper_width => "210mm",
:scale => 50 }

optioned = @p.workbook.add_worksheet(:name => 'optioned', :page_setup => page_setup).page_setup
assert_equal(1, optioned.fit_to_height)
assert_equal(2, optioned.fit_to_width)
assert_equal(true, optioned.worksheet.fit_to_page)
assert_equal(:landscape, optioned.orientation)
assert_equal("297mm", optioned.paper_height)
assert_equal("210mm", optioned.paper_width)
Expand All @@ -31,6 +39,7 @@ def test_set_all_values
@ps.set(:fit_to_height => 1, :fit_to_width => 2, :orientation => :landscape, :paper_height => "297mm", :paper_width => "210mm", :scale => 50)
assert_equal(1, @ps.fit_to_height)
assert_equal(2, @ps.fit_to_width)
assert_equal(true, @ps.worksheet.fit_to_page)
assert_equal(:landscape, @ps.orientation)
assert_equal("297mm", @ps.paper_height)
assert_equal("210mm", @ps.paper_width)
Expand Down Expand Up @@ -68,12 +77,14 @@ def test_fit_to_height
assert_raise(ArgumentError) { @ps.fit_to_height = 1.5 }
assert_nothing_raised { @ps.fit_to_height = 2 }
assert_equal(2, @ps.fit_to_height)
assert_equal(true, @ps.worksheet.fit_to_page)
end

def test_fit_to_width
assert_raise(ArgumentError) { @ps.fit_to_width = false }
assert_nothing_raised { @ps.fit_to_width = 1 }
assert_equal(1, @ps.fit_to_width)
assert_equal(true, @ps.worksheet.fit_to_page)
end

def test_orientation
Expand Down

0 comments on commit 5dddaaf

Please sign in to comment.