From 228772ee37e75f0206a635178d991b2399870353 Mon Sep 17 00:00:00 2001 From: Stefan Date: Fri, 13 May 2022 12:09:23 +0200 Subject: Fix missing cell reference for chart title when cell empty Before this change, creating a chart title referencing an empty cell would lose the cell reference. If the cell was later set to a non-blank value (either during generating the file, or while editing it in a spreadsheet app), the chart title would stay blank. --- lib/axlsx/drawing/title.rb | 2 +- test/drawing/tc_title.rb | 12 +++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/axlsx/drawing/title.rb b/lib/axlsx/drawing/title.rb index 002ae94f..d3100d43 100644 --- a/lib/axlsx/drawing/title.rb +++ b/lib/axlsx/drawing/title.rb @@ -61,7 +61,7 @@ module Axlsx # @return [String] def to_xml_string(str = '') str << '' - unless @text.empty? + unless @text.empty? && @cell.nil? clean_value = Axlsx::trust_input ? @text.to_s : ::CGI.escapeHTML(Axlsx::sanitize(@text.to_s)) str << '' if @cell.is_a?(Cell) diff --git a/test/drawing/tc_title.rb b/test/drawing/tc_title.rb index cdbd87e1..eb549b35 100644 --- a/test/drawing/tc_title.rb +++ b/test/drawing/tc_title.rb @@ -47,8 +47,18 @@ class TestTitle < Test::Unit::TestCase def test_to_xml_string_cell @chart.title.cell = @row.cells.first doc = Nokogiri::XML(@chart.to_xml_string) + assert_equal("'Sheet1'!$A$1:$A$1", doc.xpath('//c:strRef/c:f').text) assert_equal(1, doc.xpath('//c:strCache').size) - assert_equal(1, doc.xpath('//c:v[text()="one"]').size) + assert_equal('one', doc.xpath('//c:strCache/c:pt//c:v').text) + end + + def test_to_xml_string_empty_cell + @row.cells.first.value = "" + @chart.title.cell = @row.cells.first + doc = Nokogiri::XML(@chart.to_xml_string) + assert_equal("'Sheet1'!$A$1:$A$1", doc.xpath('//c:strRef/c:f').text) + assert_equal(1, doc.xpath('//c:strCache').size) + assert_equal('', doc.xpath('//c:strCache/c:pt//c:v').text) end def test_to_xml_string_for_special_characters -- cgit v1.2.3