diff options
| -rw-r--r-- | lib/axlsx/drawing/axis.rb | 5 | ||||
| -rw-r--r-- | test/drawing/tc_axis.rb | 27 |
2 files changed, 31 insertions, 1 deletions
diff --git a/lib/axlsx/drawing/axis.rb b/lib/axlsx/drawing/axis.rb index 7e677a2e..b2bb8fe7 100644 --- a/lib/axlsx/drawing/axis.rb +++ b/lib/axlsx/drawing/axis.rb @@ -165,7 +165,10 @@ module Axlsx end str << '</c:majorGridlines>' @title.to_xml_string(str) unless @title == nil - str << ('<c:numFmt formatCode="' << @format_code << '" sourceLinked="1"/>') + # Need to set sourceLinked to 0 if we're setting a format code on this row + # otherwise it will never take, as it will always prefer the 'General' formatting + # of the cells themselves + str << ('<c:numFmt formatCode="' << @format_code << '" sourceLinked="' << (@format_code.eql?('General') ? '1' : '0') << '"/>') str << '<c:majorTickMark val="none"/>' str << '<c:minorTickMark val="none"/>' str << ('<c:tickLblPos val="' << @tick_lbl_pos.to_s << '"/>') diff --git a/test/drawing/tc_axis.rb b/test/drawing/tc_axis.rb index 3546f786..5a9fa5a3 100644 --- a/test/drawing/tc_axis.rb +++ b/test/drawing/tc_axis.rb @@ -61,6 +61,33 @@ class TestAxis < Test::Unit::TestCase assert_nothing_raised("accepts valid format code") { @axis.format_code = "00.##" } end + def create_chart_with_formatting(format_string=nil) + p = Axlsx::Package.new + p.workbook.add_worksheet(:name => "Formatting Test") do |sheet| + sheet.add_row(['test', 20]) + sheet.add_chart(Axlsx::Bar3DChart, :start_at => [0,5], :end_at => [10, 20], :title => "Test Formatting") do |chart| + chart.add_series :data => sheet["B1:B1"], :labels => sheet["A1:A1"] + chart.val_axis.format_code = format_string if format_string + doc = Nokogiri::XML(chart.to_xml_string) + yield doc + end + end + end + + def test_format_code_resets_source_linked + create_chart_with_formatting("#,##0.00") do |doc| + assert_equal(doc.xpath("//c:valAx/c:numFmt[@formatCode='#,##0.00']").size, 1) + assert_equal(doc.xpath("//c:valAx/c:numFmt[@sourceLinked='0']").size, 1) + end + end + + def test_no_format_code_keeps_source_linked + create_chart_with_formatting do |doc| + assert_equal(doc.xpath("//c:valAx/c:numFmt[@formatCode='General']").size, 1) + assert_equal(doc.xpath("//c:valAx/c:numFmt[@sourceLinked='1']").size, 1) + end + end + def test_crosses assert_raise(ArgumentError, "requires valid crosses") { @axis.crosses = 1 } assert_nothing_raised("accepts valid crosses") { @axis.crosses = :min } |
