From 171ffca724ba0bbf845af1f4c7712e663500d4e8 Mon Sep 17 00:00:00 2001 From: Adam Mueller Date: Fri, 23 Nov 2012 09:19:56 -0800 Subject: Adds an example of header/footers. Also, header and footer content is now escaped. So that the user doesn't have to worry about that. Change-Id: Ib35b2eca6755d9b2d9974be25887734873d6e64a --- examples/example.rb | 21 ++++++++++++++++----- lib/axlsx/workbook/worksheet/header_footer.rb | 17 +++++++++++------ 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/examples/example.rb b/examples/example.rb index 87678a2e..1e93f4f8 100755 --- a/examples/example.rb +++ b/examples/example.rb @@ -35,6 +35,7 @@ examples = [] #examples << :repeated_header #examples << :defined_name #examples << :printing +#examples << :header_footer #examples << :comments #examples << :panes examples << :conditional_formatting @@ -149,7 +150,7 @@ if examples.include? :surrounding_border # LEARN IT! LIVE IT! LOVE IT! defaults = { :style => :thick, :color => "000000" } borders = Hash.new do |hash, key| - hash[key] = wb.styles.add_style :border => defaults.merge( { :edges => key.to_s.split('_').map(&:to_sym) } ) + hash[key] = wb.styles.add_style :border => defaults.merge( { :edges => key.to_s.split('_').map(&:to_sym) } ) end top_row = [0, borders[:top_left], borders[:top], borders[:top], borders[:top_right]] middle_row = [0, borders[:left], nil, nil, borders[:right]] @@ -167,11 +168,11 @@ if examples.include? :surrounding_border end end -#```ruby +#```ruby # Hacking border styles if examples.include? :deep_custom_borders wb.styles do |s| - top_bottom = s.add_style :border => { :style => :thick, :color =>"FFFF0000", :edges => [:top, :bottom] } + top_bottom = s.add_style :border => { :style => :thick, :color =>"FFFF0000", :edges => [:top, :bottom] } border = s.borders[s.cellXfs[top_bottom].borderId] # edit existing border parts border.prs.each do |part| @@ -511,7 +512,7 @@ if examples.include? :repeated_header wb.add_worksheet(:name => "repeated header") do |sheet| sheet.add_row %w(These Column Header Will Render On Every Printed Sheet) 200.times { sheet.add_row %w(1 2 3 4 5 6 7 8) } - wb.add_defined_name("'repeated header'!$1:$1", :local_sheet_id => sheet.index, :name => '_xlnm.Print_Titles') + wb.add_defined_name("'repeated header'!$1:$1", :local_sheet_id => sheet.index, :name => '_xlnm.Print_Titles') end end @@ -546,7 +547,17 @@ if examples.include? :printing end #``` -## Add Comments to your spreadsheet +## Add headers and footers to a worksheet +#``` ruby +if examples.include? :header_footer + header_footer = {:different_first => false, :odd_header => '&L&F : &A&R&D &T', :odd_footer => '&C&Pof&N'} + wb.add_worksheet(:name => "header footer", :header_footer => header_footer) do |sheet| + sheet.add_row ["this sheet has a header and a footer"] + end +end +#``` + +## Add Comments to your spreadsheet #``` ruby if examples.include? :comments wb.add_worksheet(:name => 'comments') do |sheet| diff --git a/lib/axlsx/workbook/worksheet/header_footer.rb b/lib/axlsx/workbook/worksheet/header_footer.rb index 58e89d9e..20d7b0eb 100644 --- a/lib/axlsx/workbook/worksheet/header_footer.rb +++ b/lib/axlsx/workbook/worksheet/header_footer.rb @@ -1,6 +1,11 @@ module Axlsx # Header/Footer options for printing a worksheet. All settings are optional. # + # Headers and footers are generated using a string which is a combination + # of plain text and control characters. A fairly comprehensive list of control + # characters can be found here: + # https://github.com/randym/axlsx/blob/master/notes_on_header_footer.md + #      # @note The recommended way of managing header/footers is via Worksheet#header_footer # @see Worksheet#initialize class HeaderFooter @@ -41,14 +46,14 @@ module Axlsx serialized_attributes str str << ">" - str << "#{odd_header}" unless odd_header.nil? - str << "#{odd_footer}" unless odd_footer.nil? + str << "#{::CGI.escapeHTML(odd_header)}" unless odd_header.nil? + str << "#{::CGI.escapeHTML(odd_footer)}" unless odd_footer.nil? - str << "#{even_header}" unless even_header.nil? - str << "#{even_footer}" unless even_footer.nil? + str << "#{::CGI.escapeHTML(even_header)}" unless even_header.nil? + str << "#{::CGI.escapeHTML(even_footer)}" unless even_footer.nil? - str << "#{first_header}" unless first_header.nil? - str << "#{first_footer}" unless first_footer.nil? + str << "#{::CGI.escapeHTML(first_header)}" unless first_header.nil? + str << "#{::CGI.escapeHTML(first_footer)}" unless first_footer.nil? str << "" end -- cgit v1.2.3