summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorStefan Daschek <[email protected]>2012-05-03 17:44:29 +0200
committerStefan Daschek <[email protected]>2012-05-03 17:44:48 +0200
commitc87151a4bd740391b13d3fcf564e9fa7b0f21b29 (patch)
tree998f5fa0090a72dd7f522a10d0cb24bda38b180f
parent03103965277963e3c62bb92ce8bb84a177b9d9bd (diff)
downloadcaxlsx-c87151a4bd740391b13d3fcf564e9fa7b0f21b29.tar.gz
caxlsx-c87151a4bd740391b13d3fcf564e9fa7b0f21b29.zip
Add support for printOptions
-rwxr-xr-x[-rw-r--r--]examples/example.rb7
-rw-r--r--lib/axlsx/workbook/workbook.rb1
-rw-r--r--lib/axlsx/workbook/worksheet/print_options.rb63
-rw-r--r--lib/axlsx/workbook/worksheet/worksheet.rb25
-rw-r--r--test/workbook/worksheet/tc_print_options.rb72
-rw-r--r--test/workbook/worksheet/tc_worksheet.rb28
6 files changed, 192 insertions, 4 deletions
diff --git a/examples/example.rb b/examples/example.rb
index e1fc945e..c74f7455 100644..100755
--- a/examples/example.rb
+++ b/examples/example.rb
@@ -333,12 +333,13 @@ end
#```
-##Specify Page Margins for printing
+##Specify page margins and other options for printing
#```ruby
margins = {:left => 3, :right => 3, :top => 1.2, :bottom => 1.2, :header => 0.7, :footer => 0.7}
-wb.add_worksheet(:name => "print margins", :page_margins => margins) do |sheet|
- sheet.add_row ["this sheet uses customized page margins for printing"]
+options = {:grid_lines => true, :headings => true, :horizontal_centered => true}
+wb.add_worksheet(:name => "print margins", :page_margins => margins, :print_options => options) do |sheet|
+ sheet.add_row ["this sheet uses customized print settings"]
end
#```
diff --git a/lib/axlsx/workbook/workbook.rb b/lib/axlsx/workbook/workbook.rb
index 5f849b2b..b9667648 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/print_options.rb'
require 'axlsx/workbook/worksheet/cfvo.rb'
require 'axlsx/workbook/worksheet/color_scale.rb'
require 'axlsx/workbook/worksheet/data_bar.rb'
diff --git a/lib/axlsx/workbook/worksheet/print_options.rb b/lib/axlsx/workbook/worksheet/print_options.rb
new file mode 100644
index 00000000..bc177980
--- /dev/null
+++ b/lib/axlsx/workbook/worksheet/print_options.rb
@@ -0,0 +1,63 @@
+module Axlsx
+ # Options for printing a worksheet. All options are boolean and false by default.
+ #
+ # @note The recommended way to manage print options is via Worksheet#print_options
+ # @see Worksheet#print_options
+ # @see Worksheet#initialize
+ class PrintOptions
+
+ # Whether grid lines should be printed.
+ # @return [Boolean]
+ attr_reader :grid_lines
+
+ # Whether row and column headings should be printed.
+ # @return [Boolean]
+ attr_reader :headings
+
+ # Whether the content should be centered horizontally on the page.
+ # @return [Boolean]
+ attr_reader :horizontal_centered
+
+ # Whether the content should be centered vertically on the page.
+ # @return [Boolean]
+ attr_reader :vertical_centered
+
+ # Creates a new PrintOptions object
+ # @option options [Boolean] grid_lines Whether grid lines should be printed
+ # @option options [Boolean] headings Whether row and column headings should be printed
+ # @option options [Boolean] horizontal_centered Whether the content should be centered horizontally
+ # @option options [Boolean] vertical_centered Whether the content should be centered vertically
+ def initialize(options = {})
+ @grid_lines = @headings = @horizontal_centered = @vertical_centered = false
+ set(options)
+ end
+
+ # Set some or all options at once.
+ # @param [Hash] options The options to set (possible keys are :grid_lines, :headings, :horizontal_centered, and :vertical_centered).
+ def set(options)
+ options.each do |k, v|
+ send("#{k}=", v) if respond_to? "#{k}="
+ end
+ end
+
+ # @see grid_lines
+ def grid_lines=(v); Axlsx::validate_boolean(v); @grid_lines = v; end
+ # @see headings
+ def headings=(v); Axlsx::validate_boolean(v); @headings = v; end
+ # @see horizontal_centered
+ def horizontal_centered=(v); Axlsx::validate_boolean(v); @horizontal_centered = v; end
+ # @see vertical_centered
+ def vertical_centered=(v); Axlsx::validate_boolean(v); @vertical_centered = v; end
+
+ # Serializes the page options element.
+ # @note As all attributes default to "false" according to the xml schema definition, the generated xml includes only those attributes that are set to true.
+ # @param [String] str
+ # @return [String]
+ def to_xml_string(str = '')
+ str << '<printOptions '
+ #
+ str << instance_values.select{ |k,v| v == true }.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 862ceada..de5183aa 100644
--- a/lib/axlsx/workbook/worksheet/worksheet.rb
+++ b/lib/axlsx/workbook/worksheet/worksheet.rb
@@ -81,6 +81,28 @@ module Axlsx
end
+ # Options for printing the worksheet.
+ # @example
+ # wb = Axlsx::Package.new.workbook
+ # # using options when creating the worksheet.
+ # ws = wb.add_worksheet :print_options => {:gridLines => true, :horizontalCentered => true}
+ #
+ # # use the set method of the page_margins object
+ # ws.print_options.set(:headings => true)
+ #
+ # # set page margins in a block
+ # ws.print_options do |options|
+ # options.horizontalCentered = true
+ # options.verticalCentered = true
+ # end
+ # @see PrintOptions#initialize
+ # @return [PrintOptions]
+ def print_options
+ @print_options ||= PrintOptions.new
+ yield @print_options if block_given?
+ @print_options
+ end
+
# definition of characters which are less than the maximum width of 0-9 in the default font for use in String#count.
# This is used for autowidth calculations
# @return [String]
@@ -93,6 +115,7 @@ module Axlsx
# @see Workbook#add_worksheet
# @option options [String] name The name of this worksheet.
# @option options [Hash] page_margins A hash containing page margins for this worksheet. @see PageMargins
+ # @option options [Hash] print_options A hash containing print options for this worksheet. @see PrintOptions
# @option options [Boolean] show_gridlines indicates if gridlines should be shown for this sheet.
def initialize(wb, options={})
self.workbook = wb
@@ -107,6 +130,7 @@ module Axlsx
@show_gridlines = true
self.name = "Sheet" + (index+1).to_s
@page_margins = PageMargins.new options[:page_margins] if options[:page_margins]
+ @print_options = PrintOptions.new options[:print_options] if options[:print_options]
@rows = SimpleTypedList.new Row
@column_info = SimpleTypedList.new Col
@@ -423,6 +447,7 @@ module Axlsx
str.concat '</sheetData>'
str.concat "<autoFilter ref='%s'></autoFilter>" % @auto_filter if @auto_filter
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
str.concat "<drawing r:id='rId1'></drawing>" if @drawing
unless @tables.empty?
diff --git a/test/workbook/worksheet/tc_print_options.rb b/test/workbook/worksheet/tc_print_options.rb
new file mode 100644
index 00000000..37ff7fe5
--- /dev/null
+++ b/test/workbook/worksheet/tc_print_options.rb
@@ -0,0 +1,72 @@
+require 'tc_helper.rb'
+
+class TestPrintOptions < Test::Unit::TestCase
+
+ def setup
+ p = Axlsx::Package.new
+ ws = p.workbook.add_worksheet :name => "hmmm"
+ @po = ws.print_options
+ end
+
+ def test_initialize
+ assert_equal(false, @po.grid_lines)
+ assert_equal(false, @po.headings)
+ assert_equal(false, @po.horizontal_centered)
+ assert_equal(false, @po.vertical_centered)
+ end
+
+ def test_initialize_with_options
+ optioned = Axlsx::PrintOptions.new(:grid_lines => true, :headings => true, :horizontal_centered => true, :vertical_centered => true)
+ assert_equal(true, optioned.grid_lines)
+ assert_equal(true, optioned.headings)
+ assert_equal(true, optioned.horizontal_centered)
+ assert_equal(true, optioned.vertical_centered)
+ end
+
+ def test_set_all_values
+ @po.set(:grid_lines => true, :headings => true, :horizontal_centered => true, :vertical_centered => true)
+ assert_equal(true, @po.grid_lines)
+ assert_equal(true, @po.headings)
+ assert_equal(true, @po.horizontal_centered)
+ assert_equal(true, @po.vertical_centered)
+ end
+
+ def test_set_some_values
+ @po.set(:grid_lines => true, :headings => true)
+ assert_equal(true, @po.grid_lines)
+ assert_equal(true, @po.headings)
+ assert_equal(false, @po.horizontal_centered)
+ assert_equal(false, @po.vertical_centered)
+ end
+
+ def test_to_xml
+ @po.set(:grid_lines => true, :headings => true, :horizontal_centered => true, :vertical_centered => true)
+ doc = Nokogiri::XML.parse(@po.to_xml_string)
+ assert_equal(1, doc.xpath(".//printOptions[@gridLines='true'][@headings='true'][@horizontalCentered='true'][@verticalCentered='true']").size)
+ end
+
+ def test_grid_lines
+ assert_raise(ArgumentError) { @po.grid_lines = 99 }
+ assert_nothing_raised { @po.grid_lines = true }
+ assert_equal(@po.grid_lines, true)
+ end
+
+ def test_headings
+ assert_raise(ArgumentError) { @po.headings = 99 }
+ assert_nothing_raised { @po.headings = true }
+ assert_equal(@po.headings, true)
+ end
+
+ def test_horizontal_centered
+ assert_raise(ArgumentError) { @po.horizontal_centered = 99 }
+ assert_nothing_raised { @po.horizontal_centered = true }
+ assert_equal(@po.horizontal_centered, true)
+ end
+
+ def test_vertical_centered
+ assert_raise(ArgumentError) { @po.vertical_centered = 99 }
+ assert_nothing_raised { @po.vertical_centered = true }
+ assert_equal(@po.vertical_centered, true)
+ end
+
+end
diff --git a/test/workbook/worksheet/tc_worksheet.rb b/test/workbook/worksheet/tc_worksheet.rb
index 0d5fbb1a..a4a62d09 100644
--- a/test/workbook/worksheet/tc_worksheet.rb
+++ b/test/workbook/worksheet/tc_worksheet.rb
@@ -25,6 +25,17 @@ class TestWorksheet < Test::Unit::TestCase
end
end
+ def test_print_options
+ assert(@ws.print_options.is_a? Axlsx::PrintOptions)
+ end
+
+ def test_print_options_yield
+ @ws.print_options do |po|
+ assert(po.is_a? Axlsx::PrintOptions)
+ assert(@ws.print_options == po)
+ end
+ end
+
def test_no_autowidth
@ws.workbook.use_autowidth = false
@ws.add_row [1,2,3,4]
@@ -33,13 +44,18 @@ class TestWorksheet < Test::Unit::TestCase
def test_initialization_options
page_margins = {:left => 2, :right => 2, :bottom => 2, :top => 2, :header => 2, :footer => 2}
- optioned = @ws.workbook.add_worksheet(:name => 'bob', :page_margins => page_margins, :selected => true, :show_gridlines => false)
+ print_options = {:grid_lines => true, :headings => true, :horizontal_centered => true, :vertical_centered => true}
+ optioned = @ws.workbook.add_worksheet(:name => 'bob', :page_margins => page_margins, :print_options => print_options, :selected => true, :show_gridlines => false)
assert_equal(optioned.page_margins.left, page_margins[:left])
assert_equal(optioned.page_margins.right, page_margins[:right])
assert_equal(optioned.page_margins.top, page_margins[:top])
assert_equal(optioned.page_margins.bottom, page_margins[:bottom])
assert_equal(optioned.page_margins.header, page_margins[:header])
assert_equal(optioned.page_margins.footer, page_margins[:footer])
+ assert_equal(optioned.print_options.grid_lines, print_options[:grid_lines])
+ assert_equal(optioned.print_options.headings, print_options[:headings])
+ assert_equal(optioned.print_options.horizontal_centered, print_options[:horizontal_centered])
+ assert_equal(optioned.print_options.vertical_centered, print_options[:vertical_centered])
assert_equal(optioned.name, 'bob')
assert_equal(optioned.selected, true)
assert_equal(optioned.show_gridlines, false)
@@ -222,6 +238,15 @@ class TestWorksheet < Test::Unit::TestCase
assert_equal(doc.xpath('//xmlns:worksheet/xmlns:pageMargins[@left="9"][@right="7"]').size, 1)
end
+ def test_to_xml_string_print_options
+ @ws.print_options do |po|
+ po.grid_lines = true
+ po.horizontal_centered = true
+ end
+ doc = Nokogiri::XML(@ws.to_xml_string)
+ assert_equal(doc.xpath('//xmlns:worksheet/xmlns:printOptions[@gridLines="true"][@horizontalCentered="true"]').size, 1)
+ end
+
def test_to_xml_string_drawing
c = @ws.add_chart Axlsx::Pie3DChart
doc = Nokogiri::XML(@ws.to_xml_string)
@@ -258,6 +283,7 @@ class TestWorksheet < Test::Unit::TestCase
# is generated in correct order.
def test_valid_with_optional_elements
@ws.page_margins.set :left => 9
+ @ws.print_options.set :headings => true
@ws.auto_filter = "A1:C3"
@ws.merge_cells "A4:A5"
@ws.add_chart Axlsx::Pie3DChart