diff options
| author | Randy Morgan <[email protected]> | 2011-12-08 10:33:38 +0900 |
|---|---|---|
| committer | Randy Morgan <[email protected]> | 2011-12-08 10:33:38 +0900 |
| commit | 4791367ceaee648f6c1e712bf0ab827271529b1b (patch) | |
| tree | 6614d339ce2c1977e0ca9efe14595b3c66279b18 | |
| parent | 4ba478ebba7dc54ef05579d021200561cecf2987 (diff) | |
| download | caxlsx-4791367ceaee648f6c1e712bf0ab827271529b1b.tar.gz caxlsx-4791367ceaee648f6c1e712bf0ab827271529b1b.zip | |
adding in support for merged cells
| -rw-r--r-- | README.md | 22 | ||||
| -rw-r--r-- | examples/example.rb | 17 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/cell.rb | 12 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/worksheet.rb | 27 | ||||
| -rw-r--r-- | test/workbook/worksheet/tc_cell.rb | 15 | ||||
| -rw-r--r-- | test/workbook/worksheet/tc_worksheet.rb | 13 |
6 files changed, 103 insertions, 3 deletions
@@ -228,6 +228,23 @@ Using cell specific styling and range / name based access p.workbook['My Worksheet!A1:D2'].each { |c| c.style = Axlsx::STYLE_THIN_BORDER } p.serialize("example13.xlsx") +#Merging Cells. + p = Axlsx::Package.new + p.workbook.add_worksheet(:name=>'My Worksheet') do |sheet| + # cell level style overides when adding cells + sheet.add_row ['col 1', 'col 2', 'col 3', 'col 4'], :sz => 16 + sheet.add_row [1, 2, 3, "=SUM(A2:C2)"] + sheet.add_row [2, 3, 4, "=SUM(A3:C3)"] + sheet.add_row ["total", "", "", "=SUM(D2:D3)"] + sheet.merge_cells('A4:C4') + # cell level style overrides via sheet range + sheet["A1:D1"].each { |c| c.color = "FF0000"} + end + p.workbook['My Worksheet!A1:D4'].each { |c| c.style = Axlsx::STYLE_THIN_BORDER } + p.serialize("example14.xlsx") + +end + ###Documentation @@ -244,8 +261,11 @@ This gem has 100% test coverage using test/unit. To execute tests for this gem, Changelog --------- +- **December.?.11**: 1.0.14 release + - Added support for mergin cells + - **December.8.11**: 1.0.13 release - Fixing .gemspec errors that caused gem to miss the lib directory. Sorry about that. + - Fixing .gemspec errors that caused gem to miss the lib directory. Sorry about that. - **December.7.11**: 1.0.12 release DO NOT USE THIS VERSION = THE GEM IS BROKEN diff --git a/examples/example.rb b/examples/example.rb index 4bc9bf23..b92fbb29 100644 --- a/examples/example.rb +++ b/examples/example.rb @@ -200,5 +200,22 @@ if ARGV.size==0 || ARGV.include?("13") end +#Merging Cells. +if ARGV.size==0 || ARGV.include?("14") + p = Axlsx::Package.new + p.workbook.add_worksheet(:name=>'My Worksheet') do |sheet| + # cell level style overides when adding cells + sheet.add_row ['col 1', 'col 2', 'col 3', 'col 4'], :sz => 16 + sheet.add_row [1, 2, 3, "=SUM(A2:C2)"] + sheet.add_row [2, 3, 4, "=SUM(A3:C3)"] + sheet.add_row ["total", "", "", "=SUM(D2:D3)"] + sheet.merge_cells('A1:B4') + sheet["A1:D1"].each { |c| c.color = "FF0000"} + end + p.workbook['My Worksheet!A1:D4'].each { |c| c.style = Axlsx::STYLE_THIN_BORDER } + p.serialize("example14.xlsx") + +end + diff --git a/lib/axlsx/workbook/worksheet/cell.rb b/lib/axlsx/workbook/worksheet/cell.rb index 0a260d7e..16608a8f 100644 --- a/lib/axlsx/workbook/worksheet/cell.rb +++ b/lib/axlsx/workbook/worksheet/cell.rb @@ -219,6 +219,18 @@ module Axlsx [index, row.index] end + # Merges all the cells in a range created between this cell and the cell or string name for a cell provided + # @see worksheet.merge_cells + # @param [Cell, String] target The last cell, or str ref for the cell in the merge range + def merge(target) + range_end = if target.is_a?(String) + target + elsif(target.is_a?(Cell)) + target.r + end + self.row.worksheet.merge_cells "#{self.r}:#{range_end}" unless range_end.nil? + end + # Serializes the cell # @param [Nokogiri::XML::Builder] xml The document builder instance this objects xml will be added to. # @return [String] xml text for the cell diff --git a/lib/axlsx/workbook/worksheet/worksheet.rb b/lib/axlsx/workbook/worksheet/worksheet.rb index 1500d515..b815bee2 100644 --- a/lib/axlsx/workbook/worksheet/worksheet.rb +++ b/lib/axlsx/workbook/worksheet/worksheet.rb @@ -24,8 +24,10 @@ module Axlsx # @return [Array] of Hash attr_reader :auto_fit_data - # TODO Merge Cells - # attr_reader :merge_cells + # An array of merged cell ranges e.d "A1:B3" + # Content and formatting is read from the first cell. + # @return Array + attr_reader :merged_cells # Creates a new worksheet. # @note the recommended way to manage worksheets is Workbook#add_worksheet @@ -40,8 +42,28 @@ module Axlsx self.name = options[:name] || "Sheet" + (index+1).to_s @magick_draw = Magick::Draw.new @cols = SimpleTypedList.new Cell + @merged_cells = [] end + # Creates merge information for this worksheet. + # Cells can be merged by calling the merge_cells method on a worksheet. + # @example This would merge the three cells C1..E1 # + # worksheet.merge_cells "C1:E1" + # # you can also provide an array of cells to be merged + # worksheet.merge_cells worksheet.rows.first.cells[(2..4)] + # #alternatively you can do it from a single cell + # worksheet["C1"].merge worksheet["E1"] + # @param [Array, string] + def merge_cells(cells) + @merged_cells << if cells.is_a?(String) + cells + elsif cells.is_a?(Array) + cells = cells.sort { |x, y| x.r <=> y.r } + "#{cells.first.r}:#{cells.last.r}" + end + end + + # Returns the cell or cells defined using excel style A1:B3 references. # @param [String] cell_def the string defining the cell or range of cells # @return [Cell, Array] @@ -214,6 +236,7 @@ module Axlsx row.to_xml(xml) end } + xml.mergeCells(:count=>@merged_cells.size) { @merged_cells.each { | mc | xml.mergeCell(:ref=>mc) } } unless @merged_cells.empty? xml.drawing :"r:id"=>"rId1" if @drawing } end diff --git a/test/workbook/worksheet/tc_cell.rb b/test/workbook/worksheet/tc_cell.rb index d4b1d29e..2859ffec 100644 --- a/test/workbook/worksheet/tc_cell.rb +++ b/test/workbook/worksheet/tc_cell.rb @@ -175,5 +175,20 @@ class TestCell < Test::Unit::TestCase assert_equal(@c.b, false) end + def test_merge_with_string + assert_equal(@c.row.worksheet.merged_cells.size, 0) + @c.row.add_cell 2 + @c.row.add_cell 3 + @c.merge "A2" + assert_equal(@c.row.worksheet.merged_cells.last, "A1:A2") + end + + def test_merge_with_cell + assert_equal(@c.row.worksheet.merged_cells.size, 0) + @c.row.add_cell 2 + @c.row.add_cell 3 + @c.merge @row.cells.last + assert_equal(@c.row.worksheet.merged_cells.last, "A1:C1") + end end diff --git a/test/workbook/worksheet/tc_worksheet.rb b/test/workbook/worksheet/tc_worksheet.rb index c86d410a..4e230a33 100644 --- a/test/workbook/worksheet/tc_worksheet.rb +++ b/test/workbook/worksheet/tc_worksheet.rb @@ -135,4 +135,17 @@ class TestWorksheet < Test::Unit::TestCase assert(@ws.send(:auto_width, {:sz=>12, :longest=>"fish"}) > @ws.send(:auto_width, {:sz=>11, :longest=>"fish"}), "larger font size gets a longer auto_width using the same text") end + def test_merge_cells + assert(@ws.merged_cells.is_a?(Array)) + assert_equal(@ws.merged_cells.size, 0) + @ws.add_row [1,2,3] + @ws.add_row [4,5,6] + @ws.add_row [7,8,9] + @ws.merge_cells "A1:A2" + @ws.merge_cells "B2:C3" + @ws.merge_cells @ws.rows.last.cells[(0..1)] + assert_equal(@ws.merged_cells.size, 3) + assert_equal(@ws.merged_cells.last, "A3:B3") + end + end |
