summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJurriaan Pruis <[email protected]>2014-03-04 18:39:19 +0100
committerJurriaan Pruis <[email protected]>2014-03-04 18:39:19 +0100
commit39b700add2fd0920a61f6951a6466f15da355edb (patch)
tree4c7271e21de1bd079c7ae61f748c914282405251
parente8ec96f28c69443ce1133ccb7d60d0019669623e (diff)
parent277f491501084673da580ce750712ed5fb81970a (diff)
downloadcaxlsx-39b700add2fd0920a61f6951a6466f15da355edb.tar.gz
caxlsx-39b700add2fd0920a61f6951a6466f15da355edb.zip
Merge pull request #295 from tafryn/arrayformula
Added array formula support
-rw-r--r--lib/axlsx/workbook/worksheet/cell.rb4
-rw-r--r--lib/axlsx/workbook/worksheet/cell_serializer.rb13
-rw-r--r--test/workbook/worksheet/tc_cell.rb12
3 files changed, 28 insertions, 1 deletions
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 << ('<v>' << cell.formula_value.to_s << '</v>') 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">' << '<f t="array" ref="' << cell.r << '">' << cell.value.to_s.sub('{=', '').sub(/}$/, '') << '</f>')
+ str << ('<v>' << cell.formula_value.to_s << '</v>') 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
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)