summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorNoel Peden <[email protected]>2022-02-05 15:17:29 -0800
committerGitHub <[email protected]>2022-02-05 15:17:29 -0800
commite10ba950c6f09a5f2b1779ae103a2c044ce0615e (patch)
tree986b233b1a54990d007aeceb76ecfb6a4f2d346b
parent34a287b33364657bc340e98be846b65fa8a77922 (diff)
parent540cf9bf59d333228b7e69f0a5c2e0cff06b5fca (diff)
downloadcaxlsx-e10ba950c6f09a5f2b1779ae103a2c044ce0615e.tar.gz
caxlsx-e10ba950c6f09a5f2b1779ae103a2c044ce0615e.zip
Merge branch 'master' into improve_range_error
-rw-r--r--CHANGELOG.md3
-rw-r--r--README.md4
-rw-r--r--lib/axlsx/package.rb2
-rw-r--r--lib/axlsx/workbook/worksheet/pivot_table.rb6
-rw-r--r--test/tc_package.rb3
-rw-r--r--test/workbook/worksheet/tc_pivot_table.rb37
6 files changed, 47 insertions, 8 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 700a6b31..6e5604fa 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,7 +2,8 @@ CHANGELOG
---------
- **Unreleased**
- - Improve error messages when incorrect ranges are provided to `Worksheet#[]`
+ - [PR #122](https://github.com/caxlsx/caxlsx/pull/122) - Improve error messages when incorrect ranges are provided to `Worksheet#[]`
+ - [PR #123](https://github.com/caxlsx/caxlsx/pull/123) - Fix invalid xml when pivot table created with more than one column in data field. Solves [Issue #110](https://github.com/caxlsx/caxlsx/issues/110)
- **September.22.21**: 3.1.1
- [PR #107](https://github.com/caxlsx/caxlsx/pull/107) - Add overlap to bar charts
diff --git a/README.md b/README.md
index 76079ada..34929c91 100644
--- a/README.md
+++ b/README.md
@@ -155,10 +155,6 @@ p.use_shared_strings = true
p.serialize('simple.xlsx')
```
-## Known Bugs
-
-There’s a [list of known bugs](https://github.com/caxlsx/caxlsx/issues?q=label%3A%22known+bug%22). (If you want to contribute to caxlsx, this is a good place to start!)
-
## Contributing
See [CONTRIBUTING.md](https://github.com/caxlsx/caxlsx/blob/master/CONTRIBUTING.md)
diff --git a/lib/axlsx/package.rb b/lib/axlsx/package.rb
index d9865a48..1b30c815 100644
--- a/lib/axlsx/package.rb
+++ b/lib/axlsx/package.rb
@@ -124,7 +124,7 @@ module Axlsx
def to_stream(confirm_valid=false)
return false unless !confirm_valid || self.validate.empty?
Relationship.initialize_ids_cache
- zip = write_parts(Zip::OutputStream.new(StringIO.new, true))
+ zip = write_parts(Zip::OutputStream.new(StringIO.new.binmode, true))
stream = zip.close_buffer
stream.rewind
stream
diff --git a/lib/axlsx/workbook/worksheet/pivot_table.rb b/lib/axlsx/workbook/worksheet/pivot_table.rb
index d8c630ec..bc500a8a 100644
--- a/lib/axlsx/workbook/worksheet/pivot_table.rb
+++ b/lib/axlsx/workbook/worksheet/pivot_table.rb
@@ -173,7 +173,9 @@ module Axlsx
# @return [String]
def to_xml_string(str = '')
str << '<?xml version="1.0" encoding="UTF-8"?>'
- str << ('<pivotTableDefinition xmlns="' << XML_NS << '" name="' << name << '" cacheId="' << cache_definition.cache_id.to_s << '" dataOnRows="1" applyNumberFormats="0" applyBorderFormats="0" applyFontFormats="0" applyPatternFormats="0" applyAlignmentFormats="0" applyWidthHeightFormats="1" dataCaption="Data" showMultipleLabel="0" showMemberPropertyTips="0" useAutoFormatting="1" indent="0" compact="0" compactData="0" gridDropZones="1" multipleFieldFilters="0">')
+
+ str << ('<pivotTableDefinition xmlns="' << XML_NS << '" name="' << name << '" cacheId="' << cache_definition.cache_id.to_s << '"' << (data.size <= 1 ? ' dataOnRows="1"' : '') << ' applyNumberFormats="0" applyBorderFormats="0" applyFontFormats="0" applyPatternFormats="0" applyAlignmentFormats="0" applyWidthHeightFormats="1" dataCaption="Data" showMultipleLabel="0" showMemberPropertyTips="0" useAutoFormatting="1" indent="0" compact="0" compactData="0" gridDropZones="1" multipleFieldFilters="0">')
+
str << ('<location firstDataCol="1" firstDataRow="1" firstHeaderRow="1" ref="' << ref << '"/>')
str << ('<pivotFields count="' << header_cells_count.to_s << '">')
header_cell_values.each do |cell_value|
@@ -195,7 +197,7 @@ module Axlsx
end
str << '</rowItems>'
end
- if columns.empty?
+ if columns.empty? && data.size <= 1
str << '<colItems count="1"><i/></colItems>'
else
str << ('<colFields count="' << columns.size.to_s << '">')
diff --git a/test/tc_package.rb b/test/tc_package.rb
index f1ce0421..b98fa2f0 100644
--- a/test/tc_package.rb
+++ b/test/tc_package.rb
@@ -303,6 +303,9 @@ class TestPackage < Test::Unit::TestCase
# this is just a roundabout guess for a package as it is build now
# in testing.
assert(stream.size > 80000)
+ # Stream (of zipped contents) should have appropriate default encoding
+ assert stream.string.valid_encoding?
+ assert_equal(stream.external_encoding, Encoding::ASCII_8BIT)
# Cached ids should be cleared
assert(Axlsx::Relationship.ids_cache.empty?)
end
diff --git a/test/workbook/worksheet/tc_pivot_table.rb b/test/workbook/worksheet/tc_pivot_table.rb
index a494dcfd..591f0ba9 100644
--- a/test/workbook/worksheet/tc_pivot_table.rb
+++ b/test/workbook/worksheet/tc_pivot_table.rb
@@ -140,4 +140,41 @@ class TestPivotTable < Test::Unit::TestCase
doc = Nokogiri::XML(pivot_table.to_xml_string)
assert_equal('4', doc.at_css('dataFields dataField')['numFmtId'], 'adding format options to pivot_table')
end
+
+ def test_pivot_table_with_more_than_one_data_row
+ ### https://github.com/caxlsx/caxlsx/issues/110
+
+ pivot_table = @ws.add_pivot_table('G5:G6', 'A1:E5') do |pt|
+ pt.rows = ["Date", "Name"]
+ pt.data = [
+ {ref: "Gross amount", num_fmt: 2},
+ {ref: "Net amount", num_fmt: 2},
+ ]
+ end
+
+ xml = pivot_table.to_xml_string
+
+ assert(xml.include?('colFields'))
+
+ assert(!xml.include?('dataOnRows'))
+ assert(!xml.include?('colItems'))
+ end
+
+ def test_pivot_table_with_only_one_data_row
+ ### https://github.com/caxlsx/caxlsx/issues/110
+
+ pivot_table = @ws.add_pivot_table('G5:G6', 'A1:E5') do |pt|
+ pt.rows = ["Date", "Name"]
+ pt.data = [
+ {ref: "Gross amount", num_fmt: 2},
+ ]
+ end
+
+ xml = pivot_table.to_xml_string
+
+ assert(xml.include?('dataOnRows'))
+ assert(xml.include?('colItems'))
+
+ assert(!xml.include?('colFields'))
+ end
end