diff options
| author | Randy Morgan <[email protected]> | 2013-08-19 23:15:05 +0900 |
|---|---|---|
| committer | Randy Morgan <[email protected]> | 2013-08-19 23:15:05 +0900 |
| commit | dd1f24a10f6842e8c4e6254eef586c383791cd03 (patch) | |
| tree | ff2f90f0638263dacb91ad2e8fc992b10f3e8bc8 | |
| parent | bcaeecc9aee01c6e9f8b801074f18d35c2cc803c (diff) | |
| download | caxlsx-dd1f24a10f6842e8c4e6254eef586c383791cd03.tar.gz caxlsx-dd1f24a10f6842e8c4e6254eef586c383791cd03.zip | |
More work on page breaks
| -rw-r--r-- | lib/axlsx/workbook/workbook.rb | 5 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/break.rb | 11 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/col_breaks.rb | 21 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/row_breaks.rb | 25 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/worksheet.rb | 21 | ||||
| -rw-r--r-- | test/profile.rb | 10 | ||||
| -rw-r--r-- | test/workbook/worksheet/tc_break.rb | 20 | ||||
| -rw-r--r-- | test/workbook/worksheet/tc_worksheet.rb | 13 |
8 files changed, 114 insertions, 12 deletions
diff --git a/lib/axlsx/workbook/workbook.rb b/lib/axlsx/workbook/workbook.rb index 3356b9de..0f70f37f 100644 --- a/lib/axlsx/workbook/workbook.rb +++ b/lib/axlsx/workbook/workbook.rb @@ -35,6 +35,11 @@ require 'axlsx/workbook/worksheet/worksheet_comments.rb' require 'axlsx/workbook/worksheet/worksheet_hyperlink' require 'axlsx/workbook/worksheet/worksheet_hyperlinks' require 'axlsx/workbook/worksheet/break' +require 'axlsx/workbook/worksheet/row_breaks' +require 'axlsx/workbook/worksheet/col_breaks' + + + require 'axlsx/workbook/worksheet/worksheet.rb' require 'axlsx/workbook/shared_strings_table.rb' require 'axlsx/workbook/defined_name.rb' diff --git a/lib/axlsx/workbook/worksheet/break.rb b/lib/axlsx/workbook/worksheet/break.rb index 9de2c84c..493aeafd 100644 --- a/lib/axlsx/workbook/worksheet/break.rb +++ b/lib/axlsx/workbook/worksheet/break.rb @@ -16,26 +16,23 @@ module Axlsx # @option options [Integer] max Zero-based index of end row or column of the break. For row breaks, specifies column index; for column breaks, specifies row index. # @option options [Boolean] man Manual Break flag. 1 means the break is a manually inserted break. # @option option [Boolean] pt Flag indicating that a PivotTable created this break. - def initialize(worksheet, options={}) - @worksheet = worksheet + def initialize(options={}) parse_options options yield self if block_given? end unsigned_int_attr_accessor :id, :min, :max + boolean_attr_accessor :man, :pt - serializable_attributes :id, :min, :max, :man, :pt - attr_reader :worksheet + serializable_attributes :id, :min, :max, :man, :pt # serializes the break to xml def to_xml_string(str='') - str << '<brk' + str << '<brk ' serialized_attributes str str << '></brk>' end - end - end diff --git a/lib/axlsx/workbook/worksheet/col_breaks.rb b/lib/axlsx/workbook/worksheet/col_breaks.rb new file mode 100644 index 00000000..2bf927e1 --- /dev/null +++ b/lib/axlsx/workbook/worksheet/col_breaks.rb @@ -0,0 +1,21 @@ +module Axlsx + + class ColBreaks < SimpleTypedList + + def initialize + super Break + end + + def add_break(options) + options.merge max: 1048575, man: true + @list << Break.new(options) + end + + def to_xml_string(str='') + return if empty? + str << '<colBreaks count="' << @list.size << '" manualBreakCount="' << @list.size << '">' + each { |brk| brk.to_xml_string(str) } + str << '</rowBreaks>' + end + end +end diff --git a/lib/axlsx/workbook/worksheet/row_breaks.rb b/lib/axlsx/workbook/worksheet/row_breaks.rb new file mode 100644 index 00000000..b8158a89 --- /dev/null +++ b/lib/axlsx/workbook/worksheet/row_breaks.rb @@ -0,0 +1,25 @@ +module Axlsx + + # A collection of break objects that define row breaks (page breaks) for printing and preview + + class RowBreaks < SimpleTypedList + + def initialize + super Break + end + + def add_break(options) + # force feed the excel default + options.merge max: 16383, man: true + @list << Break.new(options) + last + end + + def to_xml_string(str='') + return if empty? + str << '<rowBreaks count="' << @list.size << '" manualBreakCount="' << @list.size << '">' + each { |brk| brk.to_xml_string(str) } + str << '</rowBreaks>' + end + end +end diff --git a/lib/axlsx/workbook/worksheet/worksheet.rb b/lib/axlsx/workbook/worksheet/worksheet.rb index df755abf..0e0f751a 100644 --- a/lib/axlsx/workbook/worksheet/worksheet.rb +++ b/lib/axlsx/workbook/worksheet/worksheet.rb @@ -37,6 +37,8 @@ module Axlsx @page_setup = PageSetup.new options[:page_setup] if options[:page_setup] @print_options = PrintOptions.new options[:print_options] if options[:print_options] @header_footer = HeaderFooter.new options[:header_footer] if options[:header_footer] + @row_breaks = RowBreaks.new + @col_breaks = ColBreaks.new end # The name of the worksheet @@ -94,6 +96,14 @@ module Axlsx @pivot_tables ||= PivotTables.new self end + def col_breaks + @col_breaks ||= ColBreaks.new + end + + def row_breaks + @row_breaks ||= RowBreaks.new + end + # A typed collection of hyperlinks associated with this worksheet # @return [WorksheetHyperlinks] def hyperlinks @@ -498,6 +508,15 @@ module Axlsx image 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) + end + end + # This is a helper method that Lets you specify a fixed width for multiple columns in a worksheet in one go. # Axlsx is sparse, so if you have not set data for a column, you cannot set the width. # Setting a fixed column width to nil will revert the behaviour back to calculating the width for you on the next call to add_row. @@ -652,7 +671,7 @@ module Axlsx sheet_data, sheet_calc_pr, @sheet_protection, protected_ranges, auto_filter, merged_cells, conditional_formattings, data_validations, hyperlinks, print_options, page_margins, - page_setup, header_footer, worksheet_drawing, worksheet_comments, + page_setup, header_footer, row_breaks, col_breaks, worksheet_drawing, worksheet_comments, tables] end diff --git a/test/profile.rb b/test/profile.rb index ffe057e4..de00b32b 100644 --- a/test/profile.rb +++ b/test/profile.rb @@ -3,6 +3,8 @@ $:.unshift "#{File.dirname(__FILE__)}/../lib" require 'axlsx' require 'ruby-prof' +#RubyProf.measure_mode = RubyProf::MEMORY +# row = [] # Taking worst case scenario of all string data input = (32..126).to_a.pack('U*').chars.to_a @@ -11,10 +13,12 @@ input = (32..126).to_a.pack('U*').chars.to_a profile = RubyProf.profile do p = Axlsx::Package.new p.workbook.add_worksheet do |sheet| - 30.times do + 10000.times do sheet << row end end + p.to_stream end -printer = RubyProf::CallTreePrinter.new(profile) -printer.print(File.new('axlsx.qcachegrind', 'w')) + +printer = RubyProf::FlatPrinter.new(profile) +printer.print(STDOUT, {}) diff --git a/test/workbook/worksheet/tc_break.rb b/test/workbook/worksheet/tc_break.rb index 9f235dfe..c0ad69ca 100644 --- a/test/workbook/worksheet/tc_break.rb +++ b/test/workbook/worksheet/tc_break.rb @@ -5,7 +5,7 @@ class TestBreak < Test::Unit::TestCase def setup p = Axlsx::Package.new ws = p.workbook.add_worksheet - @break = Axlsx::Break.new(ws, {id: 1, min: 1, max: 10, man: true, pt: false}) + @break = Axlsx::Break.new(id: 1, min: 1, max: 10, man: true, pt: false) end def test_id @@ -16,17 +16,35 @@ class TestBreak < Test::Unit::TestCase end def test_min + assert_equal(1, @break.min) + assert_raises ArgumentError do + Axlsx::Break.new(:hoge, min: -1) + end end def test_max + assert_equal(10, @break.max) + assert_raises ArgumentError do + Axlsx::Break.new(:hoge, max: -1) + end end + def test_man + assert_equal(true, @break.man) + assert_raises ArgumentError do + Axlsx::Break.new(man: -1) + end end def test_pt + assert_equal(false, @break.pt) + assert_raises ArgumentError do + Axlsx::Break.new(pt: -1) + end end def test_to_xml_string + assert_equal("<brk id=\"1\" min=\"1\" max=\"10\" man=\"true\" pt=\"false\" ></brk>", @break.to_xml_string) end end diff --git a/test/workbook/worksheet/tc_worksheet.rb b/test/workbook/worksheet/tc_worksheet.rb index 00983fec..505d314c 100644 --- a/test/workbook/worksheet/tc_worksheet.rb +++ b/test/workbook/worksheet/tc_worksheet.rb @@ -167,6 +167,13 @@ class TestWorksheet < Test::Unit::TestCase assert_equal(@ws.workbook.charts.size, 1, "add_chart adds a chart to the workbook") end + def test_add_page_break + assert(@ws.row_breaks.empty?) + assert(@ws.col_breaks.empty?) + @ws.add_page_break(10) + assert_equal(@ws.row_breaks.size, 1) + end + def test_drawing assert @ws.drawing == nil @ws.add_chart(Axlsx::Pie3DChart) @@ -293,6 +300,12 @@ class TestWorksheet < Test::Unit::TestCase assert_equal(doc.xpath('//xmlns:worksheet/xmlns:mergeCells/xmlns:mergeCell[@ref="E1:F1"]').size, 1) end + def test_to_xml_string_row_breaks + @ws.add_page_break(1) + doc = Nokogiri::XML(@ws.to_xml_string) + assert_equal(doc.xpath('//xmlns:worksheet/xmlns:rowBreaks/xmlns:brk[@id="1"]').size, 1) + end + def test_to_xml_string_sheet_protection @ws.sheet_protection.password = 'fish' doc = Nokogiri::XML(@ws.to_xml_string) |
