From 1f2795ac1c840d2bbb5435abd22e31abcf7fbc4e Mon Sep 17 00:00:00 2001 From: Chris Roby Date: Wed, 16 Jan 2013 15:50:00 -0800 Subject: sourceLinked should be false (0) when specifying a format_code for an axis --- lib/axlsx/drawing/axis.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) 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 << '' @title.to_xml_string(str) unless @title == nil - str << ('') + # 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 << ('') str << '' str << '' str << ('') -- cgit v1.2.3 From b9d13226393223612be81a677e05939bc9db61cd Mon Sep 17 00:00:00 2001 From: Chris Roby Date: Sun, 9 Feb 2014 20:38:12 -0800 Subject: Add tests for checking formatting and sourceLinked properties when setting formatting on an axis --- test/drawing/tc_axis.rb | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) 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 } -- cgit v1.2.3