summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorRandy Morgan (@morgan_randy) <[email protected]>2013-08-16 20:23:39 -0700
committerRandy Morgan (@morgan_randy) <[email protected]>2013-08-16 20:23:39 -0700
commitc65ece00fbeaaca5b3b1a87511aa410b56c40b93 (patch)
treeb1e9b3e6e3b9450bb2a1aa64036095a5a6d0cd85
parent293f9005f41288f483ac6c1cdfbe5c76a0052864 (diff)
parentb980d98fe610c988e50394c81ddba1c0c3ed73f5 (diff)
downloadcaxlsx-c65ece00fbeaaca5b3b1a87511aa410b56c40b93.tar.gz
caxlsx-c65ece00fbeaaca5b3b1a87511aa410b56c40b93.zip
Merge pull request #174 from IndependentIP/master
Fix hardcoded sheet name in PivotTableCacheDefinition and allow other sheet to be the data source for the pivot table
-rw-r--r--lib/axlsx/workbook/worksheet/pivot_table.rb12
-rw-r--r--lib/axlsx/workbook/worksheet/pivot_table_cache_definition.rb2
-rw-r--r--test/workbook/worksheet/tc_pivot_table.rb10
-rw-r--r--test/workbook/worksheet/tc_pivot_table_cache_definition.rb8
4 files changed, 30 insertions, 2 deletions
diff --git a/lib/axlsx/workbook/worksheet/pivot_table.rb b/lib/axlsx/workbook/worksheet/pivot_table.rb
index 632765f9..0de22f8f 100644
--- a/lib/axlsx/workbook/worksheet/pivot_table.rb
+++ b/lib/axlsx/workbook/worksheet/pivot_table.rb
@@ -19,6 +19,7 @@ module Axlsx
@sheet = sheet
@sheet.workbook.pivot_tables << self
@name = "PivotTable#{index+1}"
+ @data_sheet = nil
@rows = []
@columns = []
@data = []
@@ -39,6 +40,15 @@ module Axlsx
# @return [String]
attr_reader :sheet
+ # The sheet used as data source for the pivot table
+ # @return [Worksheet]
+ attr_writer :data_sheet
+
+ # (see #data_sheet)
+ def data_sheet
+ @data_sheet || @sheet
+ end
+
# The range where the data for this pivot table lives.
# @return [String]
attr_reader :range
@@ -207,7 +217,7 @@ module Axlsx
# The header cells for the pivot table
# @return [Array]
def header_cells
- @sheet[header_range]
+ data_sheet[header_range]
end
# The values in the header cells collection
diff --git a/lib/axlsx/workbook/worksheet/pivot_table_cache_definition.rb b/lib/axlsx/workbook/worksheet/pivot_table_cache_definition.rb
index 340b94ff..665384f4 100644
--- a/lib/axlsx/workbook/worksheet/pivot_table_cache_definition.rb
+++ b/lib/axlsx/workbook/worksheet/pivot_table_cache_definition.rb
@@ -49,7 +49,7 @@ module Axlsx
str << '<?xml version="1.0" encoding="UTF-8"?>'
str << '<pivotCacheDefinition xmlns="' << XML_NS << '" xmlns:r="' << XML_NS_R << '" invalid="1" refreshOnLoad="1" recordCount="0">'
str << '<cacheSource type="worksheet">'
- str << '<worksheetSource ref="' << pivot_table.range << '" sheet="Data Sheet"/>'
+ str << '<worksheetSource ref="' << pivot_table.range << '" sheet="' << pivot_table.data_sheet.name << '"/>'
str << '</cacheSource>'
str << '<cacheFields count="' << pivot_table.header_cells_count.to_s << '">'
pivot_table.header_cells.each do |cell|
diff --git a/test/workbook/worksheet/tc_pivot_table.rb b/test/workbook/worksheet/tc_pivot_table.rb
index ee90bec2..56ac4bd4 100644
--- a/test/workbook/worksheet/tc_pivot_table.rb
+++ b/test/workbook/worksheet/tc_pivot_table.rb
@@ -36,6 +36,16 @@ class TestPivotTable < Test::Unit::TestCase
assert_equal(@ws.pivot_tables.last, pivot_table, "must be added to worksheet pivot tables collection")
end
+ def test_set_pivot_table_data_sheet
+ pivot_table = @ws.add_pivot_table('G5:G6', 'A1:D5')
+ data_sheet = @ws.clone
+ data_sheet.name = "Pivot Table Data Source"
+
+ assert_equal(pivot_table.data_sheet.name, @ws.name, "must default to the same sheet the pivot table is added to")
+ pivot_table.data_sheet = data_sheet
+ assert_equal(pivot_table.data_sheet.name, data_sheet.name, "data sheet assigned to pivot table")
+ end
+
def test_add_pivot_table_with_config
pivot_table = @ws.add_pivot_table('G5:G6', 'A1:D5') do |pt|
pt.rows = ['Year', 'Month']
diff --git a/test/workbook/worksheet/tc_pivot_table_cache_definition.rb b/test/workbook/worksheet/tc_pivot_table_cache_definition.rb
index a38e808a..282078b0 100644
--- a/test/workbook/worksheet/tc_pivot_table_cache_definition.rb
+++ b/test/workbook/worksheet/tc_pivot_table_cache_definition.rb
@@ -32,6 +32,14 @@ class TestPivotTableCacheDefinition < Test::Unit::TestCase
assert_equal(1, @cache_definition.cache_id)
end
+ def test_data_sheet
+ data_sheet = @ws.clone
+ data_sheet.name = "Pivot Table Data Source"
+ @pivot_table.data_sheet = data_sheet
+
+ assert(@cache_definition.to_xml_string.include?(data_sheet.name), "must set the data source correctly")
+ end
+
def test_to_xml_string
schema = Nokogiri::XML::Schema(File.open(Axlsx::SML_XSD))
doc = Nokogiri::XML(@cache_definition.to_xml_string)