From 87cd49d0b8ab08c251368e89cdd9cb1f1eedbd4e Mon Sep 17 00:00:00 2001 From: tafryn Date: Mon, 3 Mar 2014 16:29:06 -0800 Subject: Add support for array formulas. --- lib/axlsx/workbook/worksheet/cell.rb | 4 ++++ lib/axlsx/workbook/worksheet/cell_serializer.rb | 13 ++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/axlsx/workbook/worksheet/cell.rb b/lib/axlsx/workbook/worksheet/cell.rb index a0607e26..8dec8e92 100644 --- a/lib/axlsx/workbook/worksheet/cell.rb +++ b/lib/axlsx/workbook/worksheet/cell.rb @@ -328,6 +328,10 @@ module Axlsx type == :string && @value.to_s.start_with?(?=) end + def is_array_formula? + type == :string && @value.to_s.start_with?('{=') && @value.to_s.end_with?('}') + end + # returns the absolute or relative string style reference for # this cell. # @param [Boolean] absolute -when false a relative reference will be diff --git a/lib/axlsx/workbook/worksheet/cell_serializer.rb b/lib/axlsx/workbook/worksheet/cell_serializer.rb index 0f66c53e..81ae11de 100644 --- a/lib/axlsx/workbook/worksheet/cell_serializer.rb +++ b/lib/axlsx/workbook/worksheet/cell_serializer.rb @@ -95,6 +95,15 @@ module Axlsx str << ('' << cell.formula_value.to_s << '') unless cell.formula_value.nil? end + # Serializes cells that are type array formula + # @param [Cell] cell The cell that is being serialized + # @param [String] str The string the serialized content will be appended to. + # @return [String] + def array_formula_serialization(cell, str='') + str << ('t="str">' << '' << cell.value.to_s.sub('{=', '').sub(/}$/, '') << '') + str << ('' << cell.formula_value.to_s << '') unless cell.formula_value.nil? + end + # Serializes cells that are type inline_string # @param [Cell] cell The cell that is being serialized # @param [String] str The string the serialized content will be appended to. @@ -110,7 +119,9 @@ module Axlsx # @param [String] str The string the serialized content will be appended to. # @return [String] def string(cell, str='') - if cell.is_formula? + if cell.is_array_formula? + array_formula_serialization cell, str + elsif cell.is_formula? formula_serialization cell, str elsif !cell.ssti.nil? value_serialization 's', cell.ssti, str -- cgit v1.2.3 From 277f491501084673da580ce750712ed5fb81970a Mon Sep 17 00:00:00 2001 From: tafryn Date: Tue, 4 Mar 2014 08:33:09 -0800 Subject: Array forumla tests. --- test/workbook/worksheet/tc_cell.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/workbook/worksheet/tc_cell.rb b/test/workbook/worksheet/tc_cell.rb index ec39bba0..f07cdd58 100644 --- a/test/workbook/worksheet/tc_cell.rb +++ b/test/workbook/worksheet/tc_cell.rb @@ -287,6 +287,18 @@ class TestCell < Test::Unit::TestCase end + def test_to_xml_string_array_formula + p = Axlsx::Package.new + ws = p.workbook.add_worksheet do |sheet| + sheet.add_row ["{=SUM(C2:C11*D2:D11)}"] + end + doc = Nokogiri::XML(ws.to_xml_string) + doc.remove_namespaces! + assert(doc.xpath("//f[text()='SUM(C2:C11*D2:D11)']")) + assert(doc.xpath("//f[@t='array']")) + assert(doc.xpath("//f[@ref='A1']")) + end + def test_font_size_with_custom_style_and_no_sz @c.style = @c.row.worksheet.workbook.styles.add_style :bg_color => 'FF00FF' sz = @c.send(:font_size) -- cgit v1.2.3