diff options
| author | Randy Morgan <[email protected]> | 2013-09-10 10:08:00 +0900 |
|---|---|---|
| committer | Randy Morgan <[email protected]> | 2013-09-13 00:08:39 +0900 |
| commit | 2067c6940fc3a7ef1e8398474ac2c862130504ce (patch) | |
| tree | 0740f40483727e8bdba858831bb550e8b8b9fe50 | |
| parent | 877a38dc528b0c579ca753af6354796d0d572cdd (diff) | |
| download | caxlsx-2067c6940fc3a7ef1e8398474ac2c862130504ce.tar.gz caxlsx-2067c6940fc3a7ef1e8398474ac2c862130504ce.zip | |
Page Breaks - round one
This sets up the basic col and row break creation and serialization.
You can specify either a string reference like "C7" or provide an
Axlsx::Cell instance to specify the break point.
Serialization is working cleanly but excel does not automatically
render those breaks yet. You need to switch to page preview mode.
| -rwxr-xr-x | examples/example.rb | 18 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/col_breaks.rb | 7 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/row_breaks.rb | 8 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/worksheet.rb | 19 | ||||
| -rw-r--r-- | test/workbook/worksheet/tc_worksheet.rb | 6 |
5 files changed, 43 insertions, 15 deletions
diff --git a/examples/example.rb b/examples/example.rb index 218237a6..17e67411 100755 --- a/examples/example.rb +++ b/examples/example.rb @@ -46,6 +46,8 @@ examples << :streaming examples << :shared_strings examples << :no_autowidth examples << :cached_formula +examples << :page_breaks + p = Axlsx::Package.new wb = p.workbook #``` @@ -278,12 +280,11 @@ if examples.include? :images img = File.expand_path('../image1.jpeg', __FILE__) # specifying the :hyperlink option will add a hyper link to your image. # @note - Numbers does not support this part of the specification. - sheet.add_image(:image_src => img, :noSelect => true, end_at: true, :noMove => true, :hyperlink=>"http://axlsx.blogspot.com") do |image| + sheet.add_image(:image_src => img, :noSelect => true, :noMove => true, :hyperlink=>"http://axlsx.blogspot.com") do |image| image.width=720 image.height=666 image.hyperlink.tooltip = "Labeled Link" image.start_at 2, 2 - image.end_at 200, 200 end end end @@ -643,6 +644,7 @@ if examples.include? :sheet_view end end + # conditional formatting # if examples.include? :conditional_formatting @@ -713,7 +715,17 @@ if examples.include? :conditional_formatting end end -##Validate and Serialize +# Page Breaks +if examples.include? :page_breaks + ws = wb.add_worksheet(:name => "page breaks") do |sheet| + sheet.add_row ["A"] * 10 + sheet.add_row ["A"] * 10 + sheet.add_page_break("A2") + sheet.add_page_break(sheet.rows.last.cells[5]) + end +end + +#Validate and Serialize #```ruby # Serialize directly to file diff --git a/lib/axlsx/workbook/worksheet/col_breaks.rb b/lib/axlsx/workbook/worksheet/col_breaks.rb index fe869ff5..0e274b93 100644 --- a/lib/axlsx/workbook/worksheet/col_breaks.rb +++ b/lib/axlsx/workbook/worksheet/col_breaks.rb @@ -22,11 +22,14 @@ module Axlsx # Serialize the collection to xml # @param [String] str The string to append this lists xml to. + # <colBreaks count="1" manualBreakCount="1"> + # <brk id="3" max="1048575" man="1"/> + # </colBreaks> def to_xml_string(str='') return if empty? - str << '<colBreaks count="' << @list.size << '" manualBreakCount="' << @list.size << '">' + str << '<colBreaks count="' << @list.count.to_s << '" manualBreakCount="' << @list.size.to_s << '">' each { |brk| brk.to_xml_string(str) } - str << '</rowBreaks>' + str << '</colBreaks>' end end end diff --git a/lib/axlsx/workbook/worksheet/row_breaks.rb b/lib/axlsx/workbook/worksheet/row_breaks.rb index 8a826ed1..2af7216d 100644 --- a/lib/axlsx/workbook/worksheet/row_breaks.rb +++ b/lib/axlsx/workbook/worksheet/row_breaks.rb @@ -14,10 +14,14 @@ module Axlsx @list << Break.new(options) last end - + # <rowBreaks count="3" manualBreakCount="3"> + # <brk id="1" max="16383" man="1"/> + # <brk id="7" max="16383" man="1"/> + # <brk id="13" max="16383" man="1"/> + # </rowBreaks> def to_xml_string(str='') return if empty? - str << '<rowBreaks count="' << @list.size << '" manualBreakCount="' << @list.size << '">' + str << '<rowBreaks count="' << @list.size.to_s << '" manualBreakCount="' << @list.size.to_s << '">' each { |brk| brk.to_xml_string(str) } str << '</rowBreaks>' end diff --git a/lib/axlsx/workbook/worksheet/worksheet.rb b/lib/axlsx/workbook/worksheet/worksheet.rb index 473d5ebb..1498677c 100644 --- a/lib/axlsx/workbook/worksheet/worksheet.rb +++ b/lib/axlsx/workbook/worksheet/worksheet.rb @@ -509,12 +509,21 @@ module Axlsx end # Adds a page break (row break) to the worksheet - # @param row_index Zero-based row index of the page break. - def add_page_break(row_index, col_index=0) - row_breaks.add_break(:id => row_index) - if col_index > 0 - col_breaks.add_break(:id => col_index) + # @param cell A Cell object or excel style string reference indicating where the break + # should be added to the sheet. + # @example + # ws.add_page_break("A4") + def add_page_break(cell) + DataTypeValidator.validate "Worksheet#add_page_break cell", [String, Cell], cell + column_index, row_index = if cell.is_a?(String) + Axlsx.name_to_indices(cell) + else + cell.pos + end + if column_index > 0 + col_breaks.add_break(:id => column_index) end + row_breaks.add_break(:id => row_index) end # This is a helper method that Lets you specify a fixed width for multiple columns in a worksheet in one go. diff --git a/test/workbook/worksheet/tc_worksheet.rb b/test/workbook/worksheet/tc_worksheet.rb index 77229eb4..6d03f6fc 100644 --- a/test/workbook/worksheet/tc_worksheet.rb +++ b/test/workbook/worksheet/tc_worksheet.rb @@ -170,7 +170,7 @@ class TestWorksheet < Test::Unit::TestCase def test_add_page_break assert(@ws.row_breaks.empty?) assert(@ws.col_breaks.empty?) - @ws.add_page_break(10) + @ws.add_page_break("A1") assert_equal(1, @ws.row_breaks.size) assert_equal(0, @ws.col_breaks.size) end @@ -302,9 +302,9 @@ class TestWorksheet < Test::Unit::TestCase end def test_to_xml_string_row_breaks - @ws.add_page_break(1) + @ws.add_page_break("A1") doc = Nokogiri::XML(@ws.to_xml_string) - assert_equal(doc.xpath('//xmlns:worksheet/xmlns:rowBreaks/xmlns:brk[@id="1"]').size, 1) + assert_equal(doc.xpath('//xmlns:worksheet/xmlns:rowBreaks/xmlns:brk[@id="0"]').size, 1) end def test_to_xml_string_sheet_protection |
