summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJurriaan Pruis <[email protected]>2014-03-04 20:09:15 +0100
committerJurriaan Pruis <[email protected]>2014-03-04 20:09:15 +0100
commitb0c524ed2fb071ecb61f2b5b790462427e9b560d (patch)
tree349604bcaa26fa10584d6d34e09372ea57eb4a2d
parent39b700add2fd0920a61f6951a6466f15da355edb (diff)
parentb9d13226393223612be81a677e05939bc9db61cd (diff)
downloadcaxlsx-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.rb5
-rw-r--r--test/drawing/tc_axis.rb27
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 }