diff options
| -rw-r--r-- | CHANGELOG.md | 3 | ||||
| -rw-r--r-- | lib/axlsx/stylesheet/styles.rb | 4 | ||||
| -rw-r--r-- | lib/axlsx/workbook/workbook.rb | 5 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/border_creator.rb | 50 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/worksheet.rb | 6 | ||||
| -rw-r--r-- | test/tc_axlsx | 0 | ||||
| -rw-r--r-- | test/tc_axlsx_styler.rb | 284 | ||||
| -rw-r--r-- | test/tc_package.rb | 35 | ||||
| -rw-r--r-- | test/workbook/worksheet/tc_border_creator.rb | 43 | ||||
| -rw-r--r-- | test/workbook/worksheet/tc_worksheet.rb | 244 |
10 files changed, 351 insertions, 323 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index fac4015a..95bb0781 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,7 +11,8 @@ CHANGELOG - `worksheet.add_border "A1", {style: :thin}` - `worksheet.add_border "A1:B2", {style: :thin}` - `worksheet.add_border ["A1", "B2:C7", "D8:E9"], {style: :thin}` - - Add `BorderCreator` - TODO: Do we mention this at all? + - Add `Axlsx::BorderCreator` the class used under the hood for `worksheet.add_border` + - Allow specifying `:all` in `border: {edges: :all}` which is a shortcut for `border: {edges: [:left, :right, :top, :bottom]}` - **Unreleased** - [PR #155](https://github.com/caxlsx/caxlsx/pull/155) - Add `hideDropDown` alias for `showDropDown` setting, as the latter is confusing to use (because its logic seems inverted). diff --git a/lib/axlsx/stylesheet/styles.rb b/lib/axlsx/stylesheet/styles.rb index a84e6df7..1ddbb788 100644 --- a/lib/axlsx/stylesheet/styles.rb +++ b/lib/axlsx/stylesheet/styles.rb @@ -231,6 +231,10 @@ module Axlsx raise ArgumentError, "Type must be one of [:xf, :dxf]" unless [:xf, :dxf].include?(options[:type] ) + if options[:border].is_a?(Hash) && options[:border][:edges] == :all + options[:border][:edges] = Axlsx::Border::EDGES + end + if options[:type] == :xf # Check to see if style in cache already diff --git a/lib/axlsx/workbook/workbook.rb b/lib/axlsx/workbook/workbook.rb index ef56debd..938d4aee 100644 --- a/lib/axlsx/workbook/workbook.rb +++ b/lib/axlsx/workbook/workbook.rb @@ -188,12 +188,13 @@ require 'axlsx/workbook/worksheet/selection.rb' end # An array that holds all cells with styles + # @return Set def styled_cells @styled_cells ||= Set.new end - # Checks if styles are indexed to make it work for pre 0.1.5 version - # users that still explicitly call @workbook.apply_styles + # Are the styles added with workbook.add_styles applied yet + # @return Boolean attr_accessor :styles_applied # A helper to apply styles that were added using `worksheet.add_style` diff --git a/lib/axlsx/workbook/worksheet/border_creator.rb b/lib/axlsx/workbook/worksheet/border_creator.rb index a38e78b3..a4146ee1 100644 --- a/lib/axlsx/workbook/worksheet/border_creator.rb +++ b/lib/axlsx/workbook/worksheet/border_creator.rb @@ -8,49 +8,43 @@ module Axlsx @worksheet = worksheet @cells = cells if args.is_a?(Hash) - @edges = args[:edges] || :all + @edges = args[:edges] || Axlsx::Border::EDGES @width = args[:style] || :thin @color = args[:color] || '000000' else - @edges = args || :all + @edges = args || Axlsx::Border::Edges @width = :thin @color = '000000' end - end - - def draw - selected_edges(edges).each { |edge| add_border(edge, width, color) } - end - - private - def selected_edges(edges) - all_edges = [:top, :right, :bottom, :left] - if edges == :all - all_edges - elsif edges.is_a?(Array) && edges - all_edges == [] - edges.uniq + if @edges == :all + @edges = Axlsx::Border::EDGES + elsif @edges.is_a?(Array) + @edges = (@edges.map(&:to_sym).uniq & Axlsx::Border::EDGES) else - [] + @edges = [] end end - def add_border(position, width, color) - style = { - border: { - style: width, color: color, edges: [position.to_sym] - } - } - worksheet.add_style border_cells[position.to_sym], style + def draw + @edges.each do |edge| + worksheet.add_style( + border_cells[edge], + { + border: {style: @width, color: @color, edges: [edge]} + } + ) + end end + private + def border_cells - # example range "B2:D5" { - top: "#{first_cell}:#{last_col}#{first_row}", # "B2:D2" - right: "#{last_col}#{first_row}:#{last_cell}", # "D2:D5" - bottom: "#{first_col}#{last_row}:#{last_cell}", # "B5:D5" - left: "#{first_cell}:#{first_col}#{last_row}" # "B2:B5" + top: "#{first_cell}:#{last_col}#{first_row}", + right: "#{last_col}#{first_row}:#{last_cell}", + bottom: "#{first_col}#{last_row}:#{last_cell}", + left: "#{first_cell}:#{first_col}#{last_row}", } end diff --git a/lib/axlsx/workbook/worksheet/worksheet.rb b/lib/axlsx/workbook/worksheet/worksheet.rb index 527d2e71..a240e8e9 100644 --- a/lib/axlsx/workbook/worksheet/worksheet.rb +++ b/lib/axlsx/workbook/worksheet/worksheet.rb @@ -565,7 +565,7 @@ module Axlsx # Set the style for cells in a specific column # @param [String|Array] cell references - # @param styles TODO: how to specify this + # @param [Hash] styles def add_style(cell_refs, *styles) if !cell_refs.is_a?(Array) cell_refs = [cell_refs] @@ -586,8 +586,8 @@ module Axlsx # Set the style for cells in a specific column # @param [String|Array] cell references - # @param [Hash|Symbol] options TODO: describe this - def add_border(cell_refs, options = :all) ### TODO: will we support the :all argument + # @param [Hash|Array|Symbol] border options + def add_border(cell_refs, options = Axlsx::Border::EDGES) if !cell_refs.is_a?(Array) cell_refs = [cell_refs] end diff --git a/test/tc_axlsx b/test/tc_axlsx deleted file mode 100644 index e69de29b..00000000 --- a/test/tc_axlsx +++ /dev/null diff --git a/test/tc_axlsx_styler.rb b/test/tc_axlsx_styler.rb deleted file mode 100644 index e790a458..00000000 --- a/test/tc_axlsx_styler.rb +++ /dev/null @@ -1,284 +0,0 @@ -# encoding: UTF-8 -require 'tc_helper.rb' - -class TestAxlsxStyler < Test::Unit::TestCase - - def test_to_stream_automatically_performs_apply_styles - p = Axlsx::Package.new - wb = p.workbook - - assert_nil wb.styles_applied - wb.add_worksheet do |sheet| - sheet.add_row ['A1', 'B1'] - sheet.add_style 'A1:B1', b: true - end - - p.to_stream - - assert_equal 1, wb.styles.style_index.count - end - - def test_serialize_automatically_performs_apply_styles - p = Axlsx::Package.new - wb = p.workbook - - assert_nil wb.styles_applied - wb.add_worksheet do |sheet| - sheet.add_row ['A1', 'B1'] - sheet.add_style 'A1:B1', b: true - end - - @fname = 'axlsx_test_serialization.xlsx' - - p.serialize(@fname) - - assert_equal true, wb.styles_applied - assert_equal 1, wb.styles.style_index.count - - File.delete(@fname) - end - - def test_merge_styles_1 - p = Axlsx::Package.new - wb = p.workbook - - bold = wb.styles.add_style b: true - - wb.add_worksheet do |sheet| - sheet.add_row - sheet.add_row ['', '1', '2', '3'], style: [nil, bold] - sheet.add_row ['', '4', '5', '6'], style: bold - sheet.add_row ['', '7', '8', '9'] - sheet.add_style 'B2:D4', b: true - sheet.add_border 'B2:D4', { style: :thin, color: '000000' } - end - - wb.apply_styles - - assert_equal 9, wb.styles.style_index.count - end - - def test_merge_styles_2 - p = Axlsx::Package.new - wb = p.workbook - - bold = wb.styles.add_style b: true - - wb.add_worksheet do |sheet| - sheet.add_row ['A1', 'B1'], style: [nil, bold] - sheet.add_row ['A2', 'B2'], style: bold - sheet.add_row ['A3', 'B3'] - sheet.add_style 'A1:A2', i: true - end - - wb.apply_styles - - assert_equal 3, wb.styles.style_index.count - end - - def test_merge_styles_3 - p = Axlsx::Package.new - wb = p.workbook - - bold = wb.styles.add_style b: true - - wb.add_worksheet do |sheet| - sheet.add_row ['A1', 'B1'], style: [nil, bold] - sheet.add_row ['A2', 'B2'] - sheet.add_style 'B1:B2', bg_color: 'FF0000' - end - - wb.apply_styles - - assert_equal 3, wb.styles.style_index.count - end - - def test_table_with_borders - p = Axlsx::Package.new - wb = p.workbook - - wb.add_worksheet do |sheet| - sheet.add_row - sheet.add_row ['', 'Product', 'Category', 'Price'] - sheet.add_row ['', 'Butter', 'Dairy', 4.99] - sheet.add_row ['', 'Bread', 'Baked Goods', 3.45] - sheet.add_row ['', 'Broccoli', 'Produce', 2.99] - sheet.add_row ['', 'Pizza', 'Frozen Foods', 4.99] - sheet.column_widths 5, 20, 20, 20 - - sheet.add_style 'B2:D2', b: true - sheet.add_style 'B2:B6', b: true - sheet.add_style 'B2:D2', bg_color: '95AFBA' - sheet.add_style 'B3:D6', bg_color: 'E2F89C' - sheet.add_style 'D3:D6', alignment: { horizontal: :left } - sheet.add_border 'B2:D6' - sheet.add_border 'B3:D3', [:top] - sheet.add_border 'B3:D3', edges: [:bottom], style: :medium - sheet.add_border 'B3:D3', edges: [:bottom], style: :medium, color: '32f332' - end - - wb.apply_styles - - assert_equal true, wb.styles_applied - assert_equal 12, wb.styles.style_index.count - assert_equal 12 + 2, wb.styles.style_index.keys.max - end - - def test_duplicate_borders - p = Axlsx::Package.new - wb = p.workbook - - wb.add_worksheet do |sheet| - sheet.add_row - sheet.add_row ['', 'B2', 'C2', 'D2'] - sheet.add_row ['', 'B3', 'C3', 'D3'] - sheet.add_row ['', 'B4', 'C4', 'D4'] - - sheet.add_border 'B2:D4' - sheet.add_border 'B2:D4' - end - - wb.apply_styles - - assert_equal true, wb.styles_applied - assert_equal 8, wb.styles.style_index.count - assert_equal 8, wb.styled_cells.count - end - - def test_multiple_style_borders_on_same_cells - p = Axlsx::Package.new - wb = p.workbook - - wb.add_worksheet do |sheet| - sheet.add_row - sheet.add_row ['', 'B2', 'C2', 'D2'] - sheet.add_row ['', 'B3', 'C3', 'D3'] - - sheet.add_border 'B2:D3', :all - sheet.add_border 'B2:D2', edges: [:bottom], style: :thick, color: 'ff0000' - end - - wb.apply_styles - - assert_equal true, wb.styles_applied - assert_equal 6, wb.styles.style_index.count - assert_equal 6, wb.styled_cells.count - - b2_cell_style = { - border: { - style: :thick, - color: 'ff0000', - edges: [:bottom, :left, :top] - }, - type: :xf, - name: 'Arial', - sz: 11, - family: 1 - } - assert_equal b2_cell_style, wb.styles.style_index.values.find{|x| x == b2_cell_style} - - d3_cell_style = { - border: { - style: :thin, - color: '000000', - edges: [:bottom, :right] - }, - type: :xf, - name: 'Arial', - sz: 11, - family: 1 - } - assert_equal d3_cell_style, wb.styles.style_index.values.find{|x| x == d3_cell_style} - end - - def test_mixed_borders_1 - p = Axlsx::Package.new - wb = p.workbook - - wb.add_worksheet do |sheet| - sheet.add_row - sheet.add_row ['', '1', '2', '3'] - sheet.add_row ['', '4', '5', '6'] - sheet.add_row ['', '7', '8', '9'] - sheet.add_style 'B2:D4', border: { style: :thin, color: '000000' } - sheet.add_border 'C3:D4', style: :medium - end - - wb.apply_styles - - assert_equal 9, wb.styled_cells.count - assert_equal 2, wb.styles.style_index.count - end - - def test_mixed_borders_2 - p = Axlsx::Package.new - wb = p.workbook - - wb.add_worksheet do |sheet| - sheet.add_row - sheet.add_row ['', '1', '2', '3'] - sheet.add_row ['', '4', '5', '6'] - sheet.add_row ['', '7', '8', '9'] - sheet.add_border 'B2:D4', style: :medium - sheet.add_style 'D2:D4', border: { style: :thin, color: '000000' } - end - - wb.apply_styles - - assert_equal 8, wb.styled_cells.count - assert_equal 6, wb.styles.style_index.count - end - - def test_dxf_cell - p = Axlsx::Package.new - wb = p.workbook - - wb.add_worksheet do |sheet| - sheet.add_row (1..2).to_a - sheet.add_style "A1:A1", { bg_color: "AA0000" } - - sheet.add_row (1..2).to_a - sheet.add_style "B1:B1", { bg_color: "CC0000" } - - sheet.add_row (1..2).to_a - sheet.add_style "A3:B3", { bg_color: "00FF00" } - - wb.styles.add_style(bg_color: "0000FF", type: :dxf) - end - - wb.apply_styles - - assert_equal 1, wb.styles.dxfs.count - - assert_equal 6, wb.styles.cellXfs.count - end - - def test_default_font_with_style_index - p = Axlsx::Package.new - wb = p.workbook - - wb.styles.fonts[0].name = 'Pontiac' ### TODO, is this a valid font name in all environments - wb.styles.fonts[0].sz = 12 - - wb.add_worksheet do |sheet| - sheet.add_row [1,2,3] - sheet.add_style "A1:C1", { color: "FFFFFF" } - end - - wb.apply_styles - - assert_equal 1, wb.styles.style_index.size - - assert_equal( - { - type: :xf, - name: "Pontiac", - sz: 12, - family: 1, - color: "FFFFFF", - }, - wb.styles.style_index.values.first - ) - end - -end diff --git a/test/tc_package.rb b/test/tc_package.rb index 79fe20f5..0788d029 100644 --- a/test/tc_package.rb +++ b/test/tc_package.rb @@ -157,6 +157,26 @@ class TestPackage < Test::Unit::TestCase end end + def test_serialize_automatically_performs_apply_styles + p = Axlsx::Package.new + wb = p.workbook + + assert_nil wb.styles_applied + wb.add_worksheet do |sheet| + sheet.add_row ['A1', 'B1'] + sheet.add_style 'A1:B1', b: true + end + + @fname = 'axlsx_test_serialization.xlsx' + + p.serialize(@fname) + + assert_equal true, wb.styles_applied + assert_equal 1, wb.styles.style_index.count + + File.delete(@fname) + end + def assert_zip_file_matches_package(fname, package) zf = Zip::File.open(fname) package.send(:parts).each{ |part| zf.get_entry(part[:entry]) } @@ -305,6 +325,21 @@ class TestPackage < Test::Unit::TestCase assert(Axlsx::Relationship.ids_cache.empty?) end + def test_to_stream_automatically_performs_apply_styles + p = Axlsx::Package.new + wb = p.workbook + + assert_nil wb.styles_applied + wb.add_worksheet do |sheet| + sheet.add_row ['A1', 'B1'] + sheet.add_style 'A1:B1', b: true + end + + p.to_stream + + assert_equal 1, wb.styles.style_index.count + end + def test_encrypt # this is no where near close to ready yet assert(@package.encrypt('your_mom.xlsxl', 'has a password') == false) diff --git a/test/workbook/worksheet/tc_border_creator.rb b/test/workbook/worksheet/tc_border_creator.rb index 95a85b18..86b47fc1 100644 --- a/test/workbook/worksheet/tc_border_creator.rb +++ b/test/workbook/worksheet/tc_border_creator.rb @@ -13,16 +13,26 @@ class TestBorderCreator < Test::Unit::TestCase end bc = Axlsx::BorderCreator.new(@ws, @ws["A1:B2"], {}) - assert_equal bc.instance_variable_get(:@edges), :all + assert_equal bc.instance_variable_get(:@edges), Axlsx::Border::EDGES assert_equal bc.instance_variable_get(:@width), :thin assert_equal bc.instance_variable_get(:@color), "000000" - bc = Axlsx::BorderCreator.new(@ws, @ws["A1:B2"], [:top]) + bc = Axlsx::BorderCreator.new(@ws, @ws["A1:B2"], ["top"]) assert_equal bc.instance_variable_get(:@edges), [:top] assert_equal bc.instance_variable_get(:@width), :thin assert_equal bc.instance_variable_get(:@color), "000000" - bc = Axlsx::BorderCreator.new(@ws, @ws["A1:B2"], {edges: [:top], style: :thick, color: "ffffff"}) + bc = Axlsx::BorderCreator.new(@ws, @ws["A1:B2"], :all) + assert_equal bc.instance_variable_get(:@edges), Axlsx::Border::EDGES + assert_equal bc.instance_variable_get(:@width), :thin + assert_equal bc.instance_variable_get(:@color), "000000" + + bc = Axlsx::BorderCreator.new(@ws, @ws["A1:B2"], [:foo]) + assert_equal bc.instance_variable_get(:@edges), [] + assert_equal bc.instance_variable_get(:@width), :thin + assert_equal bc.instance_variable_get(:@color), "000000" + + bc = Axlsx::BorderCreator.new(@ws, @ws["A1:B2"], {edges: ["top"], style: :thick, color: "ffffff"}) assert_equal bc.instance_variable_get(:@edges), [:top] assert_equal bc.instance_variable_get(:@width), :thick assert_equal bc.instance_variable_get(:@color), "ffffff" @@ -33,8 +43,31 @@ class TestBorderCreator < Test::Unit::TestCase @ws.add_row [1,2,3,4,5] end - bc = Axlsx::BorderCreator.new(@ws, @ws["A1:C3"], {}) + bc = Axlsx::BorderCreator.new(@ws, @ws["A1:C3"], {edges: ["top", :left], style: :thick, color: "ffffff"}) + bc.draw - # TODO add more expectations + + assert_equal 2, @ws.styles.borders.size + + @wb.apply_styles + + assert_equal 5, @ws.styles.borders.size + + assert_equal 2, @ws.styles.borders[2].prs.size + assert_equal ["FFFFFFFF"], @ws.styles.borders[2].prs.map(&:color).map(&:rgb).uniq + assert_equal [:thick], @ws.styles.borders[2].prs.map(&:style).uniq + assert_equal [:left, :top], @ws.styles.borders[2].prs.map(&:name) + + + assert_equal 1, @ws.styles.borders[3].prs.size + assert_equal ["FFFFFFFF"], @ws.styles.borders[3].prs.map(&:color).map(&:rgb).uniq + assert_equal [:thick], @ws.styles.borders[3].prs.map(&:style).uniq + assert_equal [:top], @ws.styles.borders[3].prs.map(&:name) + + assert_equal 1, @ws.styles.borders[4].prs.size + assert_equal ["FFFFFFFF"], @ws.styles.borders[4].prs.map(&:color).map(&:rgb).uniq + assert_equal [:thick], @ws.styles.borders[4].prs.map(&:style).uniq + assert_equal [:left], @ws.styles.borders[4].prs.map(&:name) end + end diff --git a/test/workbook/worksheet/tc_worksheet.rb b/test/workbook/worksheet/tc_worksheet.rb index c679e18c..6dbea530 100644 --- a/test/workbook/worksheet/tc_worksheet.rb +++ b/test/workbook/worksheet/tc_worksheet.rb @@ -629,4 +629,248 @@ class TestWorksheet < Test::Unit::TestCase assert_equal(doc.xpath('//xmlns:worksheet/xmlns:sheetPr/xmlns:outlinePr').size, 1) assert_equal(doc.xpath('//xmlns:worksheet/xmlns:sheetPr/xmlns:outlinePr[@summaryBelow=0][@summaryRight=1]').size, 1) end + + def test_merge_styles_1 + p = Axlsx::Package.new + wb = p.workbook + + bold = wb.styles.add_style b: true + + wb.add_worksheet do |sheet| + sheet.add_row + sheet.add_row ['', '1', '2', '3'], style: [nil, bold] + sheet.add_row ['', '4', '5', '6'], style: bold + sheet.add_row ['', '7', '8', '9'] + sheet.add_style 'B2:D4', b: true + sheet.add_border 'B2:D4', { style: :thin, color: '000000' } + end + + wb.apply_styles + + assert_equal 9, wb.styles.style_index.count + end + + def test_merge_styles_2 + p = Axlsx::Package.new + wb = p.workbook + + bold = wb.styles.add_style b: true + + wb.add_worksheet do |sheet| + sheet.add_row ['A1', 'B1'], style: [nil, bold] + sheet.add_row ['A2', 'B2'], style: bold + sheet.add_row ['A3', 'B3'] + sheet.add_style 'A1:A2', i: true + end + + wb.apply_styles + + assert_equal 3, wb.styles.style_index.count + end + + def test_merge_styles_3 + p = Axlsx::Package.new + wb = p.workbook + + bold = wb.styles.add_style b: true + + wb.add_worksheet do |sheet| + sheet.add_row ['A1', 'B1'], style: [nil, bold] + sheet.add_row ['A2', 'B2'] + sheet.add_style 'B1:B2', bg_color: 'FF0000' + end + + wb.apply_styles + + assert_equal 3, wb.styles.style_index.count + end + + def test_table_with_borders + p = Axlsx::Package.new + wb = p.workbook + + wb.add_worksheet do |sheet| + sheet.add_row + sheet.add_row ['', 'Product', 'Category', 'Price'] + sheet.add_row ['', 'Butter', 'Dairy', 4.99] + sheet.add_row ['', 'Bread', 'Baked Goods', 3.45] + sheet.add_row ['', 'Broccoli', 'Produce', 2.99] + sheet.add_row ['', 'Pizza', 'Frozen Foods', 4.99] + sheet.column_widths 5, 20, 20, 20 + + sheet.add_style 'B2:D2', b: true + sheet.add_style 'B2:B6', b: true + sheet.add_style 'B2:D2', bg_color: '95AFBA' + sheet.add_style 'B3:D6', bg_color: 'E2F89C' + sheet.add_style 'D3:D6', alignment: { horizontal: :left } + sheet.add_border 'B2:D6' + sheet.add_border 'B3:D3', [:top] + sheet.add_border 'B3:D3', edges: [:bottom], style: :medium + sheet.add_border 'B3:D3', edges: [:bottom], style: :medium, color: '32f332' + end + + wb.apply_styles + + assert_equal true, wb.styles_applied + assert_equal 12, wb.styles.style_index.count + assert_equal 12 + 2, wb.styles.style_index.keys.max + end + + def test_duplicate_borders + p = Axlsx::Package.new + wb = p.workbook + + wb.add_worksheet do |sheet| + sheet.add_row + sheet.add_row ['', 'B2', 'C2', 'D2'] + sheet.add_row ['', 'B3', 'C3', 'D3'] + sheet.add_row ['', 'B4', 'C4', 'D4'] + + sheet.add_border 'B2:D4' + sheet.add_border 'B2:D4' + end + + wb.apply_styles + + assert_equal true, wb.styles_applied + assert_equal 8, wb.styles.style_index.count + assert_equal 8, wb.styled_cells.count + end + + def test_multiple_style_borders_on_same_cells + p = Axlsx::Package.new + wb = p.workbook + + wb.add_worksheet do |sheet| + sheet.add_row + sheet.add_row ['', 'B2', 'C2', 'D2'] + sheet.add_row ['', 'B3', 'C3', 'D3'] + + sheet.add_border 'B2:D3', :all + sheet.add_border 'B2:D2', edges: [:bottom], style: :thick, color: 'ff0000' + end + + wb.apply_styles + + assert_equal true, wb.styles_applied + assert_equal 6, wb.styles.style_index.count + assert_equal 6, wb.styled_cells.count + + b2_cell_style = { + border: { + style: :thick, + color: 'ff0000', + edges: [:bottom, :left, :top] + }, + type: :xf, + name: 'Arial', + sz: 11, + family: 1 + } + assert_equal b2_cell_style, wb.styles.style_index.values.find{|x| x == b2_cell_style} + + d3_cell_style = { + border: { + style: :thin, + color: '000000', + edges: [:bottom, :right] + }, + type: :xf, + name: 'Arial', + sz: 11, + family: 1 + } + assert_equal d3_cell_style, wb.styles.style_index.values.find{|x| x == d3_cell_style} + end + + def test_mixed_borders_1 + p = Axlsx::Package.new + wb = p.workbook + + wb.add_worksheet do |sheet| + sheet.add_row + sheet.add_row ['', '1', '2', '3'] + sheet.add_row ['', '4', '5', '6'] + sheet.add_row ['', '7', '8', '9'] + sheet.add_style 'B2:D4', border: { style: :thin, color: '000000' } + sheet.add_border 'C3:D4', style: :medium + end + + wb.apply_styles + + assert_equal 9, wb.styled_cells.count + assert_equal 2, wb.styles.style_index.count + end + + def test_mixed_borders_2 + p = Axlsx::Package.new + wb = p.workbook + + wb.add_worksheet do |sheet| + sheet.add_row + sheet.add_row ['', '1', '2', '3'] + sheet.add_row ['', '4', '5', '6'] + sheet.add_row ['', '7', '8', '9'] + sheet.add_border 'B2:D4', style: :medium + sheet.add_style 'D2:D4', border: { style: :thin, color: '000000' } + end + + wb.apply_styles + + assert_equal 8, wb.styled_cells.count + assert_equal 6, wb.styles.style_index.count + end + + def test_dxf_cell + p = Axlsx::Package.new + wb = p.workbook + + wb.add_worksheet do |sheet| + sheet.add_row (1..2).to_a + sheet.add_style "A1:A1", { bg_color: "AA0000" } + + sheet.add_row (1..2).to_a + sheet.add_style "B1:B1", { bg_color: "CC0000" } + + sheet.add_row (1..2).to_a + sheet.add_style "A3:B3", { bg_color: "00FF00" } + + wb.styles.add_style(bg_color: "0000FF", type: :dxf) + end + + wb.apply_styles + + assert_equal 1, wb.styles.dxfs.count + + assert_equal 6, wb.styles.cellXfs.count + end + + def test_default_font_with_style_index + p = Axlsx::Package.new + wb = p.workbook + + wb.styles.fonts[0].name = 'Times New Roman' + wb.styles.fonts[0].sz = 12 + + wb.add_worksheet do |sheet| + sheet.add_row [1,2,3] + sheet.add_style "A1:C1", { color: "FFFFFF" } + end + + wb.apply_styles + + assert_equal 1, wb.styles.style_index.size + + assert_equal( + { + type: :xf, + name: "Times New Roman", + sz: 12, + family: 1, + color: "FFFFFF", + }, + wb.styles.style_index.values.first + ) + end + end |
