Skip to content
This repository
Browse code

implement paper_size as at least some version of office ignore the pa…

…per width and paper height page setup attributes.

See PageSetup#paper_size for a list of sizes supported in the
specification.
  • Loading branch information...
commit ccbb0925fc4b4bae863e5be6a37d4c51f280836a 1 parent 8f237fd
Randy Morgan (@morgan_randy) authored
9 examples/page_setup.rb
... ... @@ -0,0 +1,9 @@
  1 +$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib"
  2 +require 'axlsx'
  3 +xls = Axlsx::Package.new
  4 +wb = xls.workbook
  5 +wb.add_worksheet do |ws|
  6 + ws.page_setup.set :paper_width => "210mm", :paper_size => 10, :paper_height => "297mm", :orientation => :landscape
  7 + ws.add_row %w(AXLSX is cool)
  8 +end
  9 +xls.serialize "page_setup.xlsx"
138 lib/axlsx/workbook/worksheet/page_setup.rb
@@ -20,11 +20,11 @@ class PageSetup
20 20 # * useFirstPageNumber
21 21 # * usePrinterDefaults
22 22 # * verticalDpi
23   -
  23 +
24 24 # Number of vertical pages to fit on.
25 25 # @note PageSetup#fit_to is the recomended way to manage page fitting as only specifying one of fit_to_width/fit_to_height will result in the counterpart
26 26 # being set to 1.
27   - # @return [Integer]
  27 + # @return [Integer]
28 28 attr_reader :fit_to_height
29 29
30 30 # Number of horizontal pages to fit on.
@@ -36,7 +36,7 @@ class PageSetup
36 36 # Orientation of the page (:default, :landscape, :portrait)
37 37 # @return [Symbol]
38 38 attr_reader :orientation
39   -
  39 +
40 40 # Height of paper (string containing a number followed by a unit identifier: "297mm", "11in")
41 41 # @return [String]
42 42 attr_reader :paper_height
@@ -44,11 +44,134 @@ class PageSetup
44 44 # Width of paper (string containing a number followed by a unit identifier: "210mm", "8.5in")
45 45 # @return [String]
46 46 attr_reader :paper_width
47   -
  47 +
48 48 # Print scaling (percent value, given as integer ranging from 10 to 400)
49 49 # @return [Integer]
50 50 attr_reader :scale
51 51
  52 + # The paper size to use in printing
  53 + #1 = Letter paper (8.5 in. by 11 in.)
  54 + #2 = Letter small paper (8.5 in. by 11 in.)
  55 + #3 = Tabloid paper (11 in. by 17 in.)
  56 + #4 = Ledger paper (17 in. by 11 in.)
  57 + #5 = Legal paper (8.5 in. by 14 in.)
  58 + #6 = Statement paper (5.5 in. by 8.5 in.)
  59 + #7 = Executive paper (7.25 in. by 10.5 in.)
  60 + #8 = A3 paper (297 mm by 420 mm)
  61 + #9 = A4 paper (210 mm by 297 mm)
  62 + #10 = A4 small paper (210 mm by 297 mm)
  63 + #11 = A5 paper (148 mm by 210 mm)
  64 + #12 = B4 paper (250 mm by 353 mm)
  65 + #13 = B5 paper (176 mm by 250 mm)
  66 + #14 = Folio paper (8.5 in. by 13 in.)
  67 + #15 = Quarto paper (215 mm by 275 mm)
  68 + #16 = Standard paper (10 in. by 14 in.)
  69 + #17 = Standard paper (11 in. by 17 in.)
  70 + #18 = Note paper (8.5 in. by 11 in.)
  71 + #19 = #9 envelope (3.875 in. by 8.875 in.)
  72 + #20 = #10 envelope (4.125 in. by 9.5 in.)
  73 + #21 = #11 envelope (4.5 in. by 10.375 in.)
  74 + #22 = #12 envelope (4.75 in. by 11 in.)
  75 + #23 = #14 envelope (5 in. by 11.5 in.) 24 = C paper (17 in. by 22 in.)
  76 + #25 = D paper (22 in. by 34 in.)
  77 + #26 = E paper (34 in. by 44 in.)
  78 + #27 = DL envelope (110 mm by 220 mm)
  79 + #28 = C5 envelope (162 mm by 229 mm)
  80 + #29 = C3 envelope (324 mm by 458 mm)
  81 + #30 = C4 envelope (229 mm by 324 mm)
  82 + #31 = C6 envelope (114 mm by 162 mm)
  83 + #32 = C65 envelope (114 mm by 229 mm)
  84 + #33 = B4 envelope (250 mm by 353 mm)
  85 + #34 = B5 envelope (176 mm by 250 mm)
  86 + #35 = B6 envelope (176 mm by 125 mm)
  87 + #36 = Italy envelope (110 mm by 230 mm)
  88 + #37 = Monarch envelope (3.875 in. by 7.5 in.). 38 = 6 3/4 envelope (3.625 in. by 6.5 in.)
  89 + #39 = US standard fanfold (14.875 in. by 11 in.)
  90 + #40 = German standard fanfold (8.5 in. by 12 in.)
  91 + #41 = German legal fanfold (8.5 in. by 13 in.)
  92 + #42 = ISO B4 (250 mm by 353 mm)
  93 + #43 = Japanese double postcard (200 mm by 148 mm)
  94 + #44 = Standard paper (9 in. by 11 in.)
  95 + #45 = Standard paper (10 in. by 11 in.)
  96 + #46 = Standard paper (15 in. by 11 in.)
  97 + #47 = Invite envelope (220 mm by 220 mm)
  98 + #50 = Letter extra paper (9.275 in. by 12 in.)
  99 + #51 = Legal extra paper (9.275 in. by 15 in.)
  100 + #52 = Tabloid extra paper (11.69 in. by 18 in.)
  101 + #53 = A4 extra paper (236 mm by 322 mm)
  102 + #54 = Letter transverse paper (8.275 in. by 11 in.)
  103 + #55 = A4 transverse paper (210 mm by 297 mm)
  104 + #56 = Letter extra transverse paper (9.275 in. by 12 in.)
  105 + #57 = SuperA/SuperA/A4 paper (227 mm by 356 mm)
  106 + #58 = SuperB/SuperB/A3 paper (305 mm by 487 mm)
  107 + #59 = Letter plus paper (8.5 in. by 12.69 in.)
  108 + #60 = A4 plus paper (210 mm by 330 mm)
  109 + #61 = A5 transverse paper (148 mm by 210 mm)
  110 + #62 = JIS B5 transverse paper (182 mm by 257 mm)
  111 + #63 = A3 extra paper (322 mm by 445 mm)
  112 + #64 = A5 extra paper (174 mm by 235 mm)
  113 + #65 = ISO B5 extra paper (201 mm by 276 mm)
  114 + #66 = A2 paper (420 mm by 594 mm)
  115 + #67 = A3 transverse paper (297 mm by 420 mm)
  116 + #68 = A3 extra transverse paper (322 mm by 445 mm)
  117 + #69 = Japanese Double Postcard (200 mm x 148 mm)
  118 + #70 = A6 (105 mm x 148 mm
  119 + #71 = Japanese Envelope Kaku #2
  120 + #72 = Japanese Envelope Kaku #3
  121 + #73 = Japanese Envelope Chou #3
  122 + #74 = Japanese Envelope Chou #4
  123 + #75 = Letter Rotated (11in x 8 1/2 11 in)
  124 + #76 = A3 Rotated (420 mm x 297 mm)
  125 + #77 = A4 Rotated (297 mm x 210 mm)
  126 + #78 = A5 Rotated (210 mm x 148 mm)
  127 + #79 = B4 (JIS) Rotated (364 mm x 257 mm)
  128 + #80 = B5 (JIS) Rotated (257 mm x 182 mm)
  129 + #81 = Japanese Postcard Rotated (148 mm x 100 mm)
  130 + #82 = Double Japanese Postcard Rotated (148 mm x 200 mm)
  131 + #83 = A6 Rotated (148 mm x 105 mm)
  132 + #84 = Japanese Envelope Kaku #2 Rotated
  133 + #85 = Japanese Envelope Kaku #3 Rotated
  134 + #86 = Japanese Envelope Chou #3 Rotated
  135 + #87 = Japanese Envelope Chou #4 Rotated
  136 + #88 = B6 (JIS) (128 mm x 182 mm)
  137 + #89 = B6 (JIS) Rotated (182 mm x 128 mm)
  138 + #90 = (12 in x 11 in)
  139 + #91 = Japanese Envelope You #4
  140 + #92 = Japanese Envelope You #4 Rotated
  141 + #93 = PRC 16K (146 mm x 215 mm)
  142 + #94 = PRC 32K (97 mm x 151 mm)
  143 + #95 = PRC 32K(Big) (97 mm x 151 mm)
  144 + #96 = PRC Envelope #1 (102 mm x 165 mm)
  145 + #97 = PRC Envelope #2 (102 mm x 176 mm)
  146 + #98 = PRC Envelope #3 (125 mm x 176 mm)
  147 + #99 = PRC Envelope #4 (110 mm x 208 mm)
  148 + #100 = PRC Envelope #5 (110 mm x 220 mm)
  149 + #101 = PRC Envelope #6 (120 mm x 230 mm)
  150 + #102 = PRC Envelope #7 (160 mm x 230 mm)
  151 + #103 = PRC Envelope #8 (120 mm x 309 mm)
  152 + #104 = PRC Envelope #9 (229 mm x 324 mm)
  153 + #105 = PRC Envelope #10 (324 mm x 458 mm)
  154 + #106 = PRC 16K Rotated
  155 + #107 = PRC 32K Rotated
  156 + #108 = PRC 32K(Big) Rotated
  157 + #109 = PRC Envelope #1 Rotated (165 mm x 102 mm)
  158 + #110 = PRC Envelope #2 Rotated (176 mm x 102 mm)
  159 + #111 = PRC Envelope #3 Rotated (176 mm x 125 mm)
  160 + #112 = PRC Envelope #4 Rotated (208 mm x 110 mm)
  161 + #113 = PRC Envelope #5 Rotated (220 mm x 110 mm)
  162 + #114 = PRC Envelope #6 Rotated (230 mm x 120 mm)
  163 + #115 = PRC Envelope #7 Rotated (230 mm x 160 mm)
  164 + #116 = PRC Envelope #8 Rotated (309 mm x 120 mm)
  165 + #117 = PRC Envelope #9 Rotated (324 mm x 229 mm)
  166 + #118 = PRC Envelope #10 Rotated (458 mm x 324 mm)
  167 + # @return [Integer]
  168 + attr_reader :paper_size
  169 +
  170 + def paper_size=(size)
  171 + RestrictionValidator.validate 'paper_size', (1..118), size
  172 + @paper_size = size
  173 + end
  174 +
52 175 # Creates a new PageSetup object
53 176 # @option options [Integer] fit_to_height Number of vertical pages to fit on
54 177 # @option options [Integer] fit_to_width Number of horizontal pages to fit on
@@ -56,6 +179,7 @@ class PageSetup
56 179 # @option options [String] paper_height Height of paper (number followed by unit identifier: "297mm", "11in")
57 180 # @option options [String] paper_width Width of paper (number followed by unit identifier: "210mm", "8.5in")
58 181 # @option options [Integer] scale Print scaling (percent value, integer ranging from 10 to 400)
  182 + # @option options [Integer] paper_size - the size of paper to use
59 183 def initialize(options = {})
60 184 set(options)
61 185 end
@@ -80,7 +204,7 @@ def paper_height=(v); Axlsx::validate_number_with_unit(v); @paper_height = v; en
80 204 def paper_width=(v); Axlsx::validate_number_with_unit(v); @paper_width = v; end
81 205 # @see scale
82 206 def scale=(v); Axlsx::validate_scale_10_400(v); @scale = v; end
83   -
  207 +
84 208 # convenience method to achieve sanity when setting fit_to_width and fit_to_height
85 209 # as they both default to 1 if only their counterpart is specified.
86 210 # @note This method will overwrite any value you explicitly set via the fit_to_height or fit_to_width methods.
@@ -97,8 +221,8 @@ def fit_to(options={})
97 221 # We treat any page set up that has a value set for fit_to_width or fit_to_height value as fit_to_page.
98 222 # @return [Boolean]
99 223 def fit_to_page?
100   - # is there some better what to express this?
101   - (fit_to_width != nil || fit_to_height != nil)
  224 + # is there some better what to express this?
  225 + (fit_to_width != nil || fit_to_height != nil)
102 226 end
103 227
104 228 # Serializes the page settings element.
7 test/workbook/worksheet/tc_page_setup.rb
@@ -44,6 +44,11 @@ def test_set_all_values
44 44 assert_equal(50, @ps.scale)
45 45 end
46 46
  47 + def test_paper_size
  48 + assert_raise(ArgumentError) { @ps.paper_size = 119 }
  49 + assert_nothing_raised { @ps.paper_size = 10 }
  50 + end
  51 +
47 52 def test_set_some_values
48 53 @ps.set(:fit_to_width => 2, :orientation => :portrait)
49 54 assert_equal(2, @ps.fit_to_width)
@@ -58,7 +63,7 @@ def test_default_fit_to_page?
58 63 assert(@ps.fit_to_width == nil && @ps.fit_to_height == nil)
59 64 assert(@ps.fit_to_page? == false)
60 65 end
61   -
  66 +
62 67 def test_with_height_fit_to_page?
63 68 assert(@ps.fit_to_width == nil && @ps.fit_to_height == nil)
64 69 @ps.set(:fit_to_height => 1)

0 comments on commit ccbb092

Please sign in to comment.
Something went wrong with that request. Please try again.