Class: Axlsx::PageMargins

Inherits:
Object
  • Object
show all
Defined in:
lib/axlsx/workbook/worksheet/page_margins.rb

Overview

Note:

The recommended way to manage page margins is via Worksheet#page_margins

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).

Constant Summary collapse

DEFAULT_LEFT_RIGHT =

Default left and right margin (in inches)

0.75
DEFAULT_TOP_BOTTOM =

Default top and bottom margins (in inches)

1.00
0.50

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ PageMargins

Creates a new PageMargins object

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • left (Numeric)

    The left margin in inches

  • right (Numeric)

    The right margin in inches

  • bottom (Numeric)

    The bottom margin in inches

  • top (Numeric)

    The top margin in inches

  • header (Numeric)

    The header margin in inches

  • footer (Numeric)

    The footer margin in inches



53
54
55
56
57
58
59
60
61
62
# File 'lib/axlsx/workbook/worksheet/page_margins.rb', line 53

def initialize(options={})
  # Default values taken from MS Excel for Mac 2011
  @left = @right = DEFAULT_LEFT_RIGHT
  @top = @bottom = DEFAULT_TOP_BOTTOM
  @header = @footer = DEFAULT_HEADER_FOOTER

  options.each do |o|
    self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
  end
end

Instance Attribute Details

#bottomFloat

Bottom margin (in inches)

Returns:

  • (Float)


36
37
38
# File 'lib/axlsx/workbook/worksheet/page_margins.rb', line 36

def bottom
  @bottom
end

Footer margin (in inches)

Returns:

  • (Float)


44
45
46
# File 'lib/axlsx/workbook/worksheet/page_margins.rb', line 44

def footer
  @footer
end

#headerFloat

Header margin (in inches)

Returns:

  • (Float)


40
41
42
# File 'lib/axlsx/workbook/worksheet/page_margins.rb', line 40

def header
  @header
end

#leftFloat

Left margin (in inches)

Returns:

  • (Float)


24
25
26
# File 'lib/axlsx/workbook/worksheet/page_margins.rb', line 24

def left
  @left
end

#rightFloat

Right margin (in inches)

Returns:

  • (Float)


28
29
30
# File 'lib/axlsx/workbook/worksheet/page_margins.rb', line 28

def right
  @right
end

#topFloat

Top margin (in inches)

Returns:

  • (Float)


32
33
34
# File 'lib/axlsx/workbook/worksheet/page_margins.rb', line 32

def top
  @top
end

Instance Method Details

#set(margins) ⇒ Object

Set some or all margins at once.

Parameters:

  • margins (Hash)

    the margins to set (possible keys are :left, :right, :top, :bottom, :header and :footer).



66
67
68
69
70
71
# File 'lib/axlsx/workbook/worksheet/page_margins.rb', line 66

def set(margins)
  margins.select do |k, v|
    next unless [:left, :right, :top, :bottom, :header, :footer].include? k
    send("#{k}=", v)
  end
end

#to_xml_string(str = '') ⇒ String

Note:

For compatibility, this is a noop unless custom margins have been specified.

Serializes the page margins element

Parameters:

  • str (String) (defaults to: '')

Returns:

  • (String)

See Also:

  • #custom_margins_specified?


91
92
93
94
95
# File 'lib/axlsx/workbook/worksheet/page_margins.rb', line 91

def to_xml_string(str = '')
  str << '<pageMargins '
  str << instance_values.map { |key, value| '' << key << '="' << value.to_s << '"' }.join(' ')
  str << '/>'
end