diff options
| author | Jurriaan Pruis <[email protected]> | 2014-03-04 20:09:15 +0100 |
|---|---|---|
| committer | Jurriaan Pruis <[email protected]> | 2014-03-04 20:09:15 +0100 |
| commit | b0c524ed2fb071ecb61f2b5b790462427e9b560d (patch) | |
| tree | 349604bcaa26fa10584d6d34e09372ea57eb4a2d | |
| parent | 39b700add2fd0920a61f6951a6466f15da355edb (diff) | |
| parent | b9d13226393223612be81a677e05939bc9db61cd (diff) | |
| download | caxlsx-b0c524ed2fb071ecb61f2b5b790462427e9b560d.tar.gz caxlsx-b0c524ed2fb071ecb61f2b5b790462427e9b560d.zip | |
Merge pull request #162 from croby/format_code
sourceLinked should be false (0) when specifying a format_code for an axis
| -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 } |
