Skip to content

Commit f927721

Browse files
authored
Fixes issue #1746 (#1747)
1 parent 5465231 commit f927721

File tree

5 files changed

+93
-43
lines changed

5 files changed

+93
-43
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*************************************************************************************************
2+
Required Notice: Copyright (C) EPPlus Software AB.
3+
This software is licensed under PolyForm Noncommercial License 1.0.0
4+
and may only be used for noncommercial purposes
5+
https://polyformproject.org/licenses/noncommercial/1.0.0/
6+
7+
A commercial license to use this software can be purchased at https://epplussoftware.com
8+
*************************************************************************************************
9+
Date Author Change
10+
*************************************************************************************************
11+
01/27/2020 EPPlus Software AB Initial release EPPlus 5
12+
*************************************************************************************************/
13+
using OfficeOpenXml.Utils;
14+
using System;
15+
using System.Collections.Generic;
16+
17+
namespace OfficeOpenXml.Table.PivotTable
18+
{
19+
internal class CacheComparer : IEqualityComparer<object>
20+
{
21+
public new bool Equals(object x, object y)
22+
{
23+
x = GetCaseInsensitiveValue(x);
24+
y = GetCaseInsensitiveValue(y);
25+
return x.Equals(y);
26+
}
27+
28+
private static object GetCaseInsensitiveValue(object x)
29+
{
30+
if (x == null || x.Equals(ExcelPivotTable.PivotNullValue)) return ExcelPivotTable.PivotNullValue;
31+
32+
if (x is string sx)
33+
{
34+
return sx.ToLower();
35+
}
36+
else if (x is char cx)
37+
{
38+
return char.ToLower(cx).ToString();
39+
}
40+
else if(x is DateTime)
41+
{
42+
return x;
43+
}
44+
else if(x is TimeSpan ts)
45+
{
46+
return DateTime.FromOADate(0).Add(ts);
47+
}
48+
if(ConvertUtil.IsExcelNumeric(x))
49+
{
50+
return ConvertUtil.GetValueDouble(x);
51+
}
52+
return x.ToString().ToLower();
53+
}
54+
55+
public int GetHashCode(object obj)
56+
{
57+
return GetCaseInsensitiveValue(obj).GetHashCode();
58+
}
59+
}
60+
}

src/EPPlus/Table/PivotTable/ExcelPivotTableCacheField.cs

+12-42
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,7 @@ private void UpdateSharedItems()
937937
{
938938
UpdatePivotItemsFromSharedItems(siHs);
939939
}
940+
940941
SharedItems._list = siHs.ToList();
941942
UpdateCacheLookupFromItems(SharedItems._list, ref _cacheLookup);
942943
if (HasSlicer)
@@ -962,15 +963,18 @@ private void UpdatePivotItemsFromSharedItems(HashSet<object> siHs)
962963
var hasFilter = list.Any(x => x.Hidden);
963964
for (var ix = 0; ix < list.Count; ix++)
964965
{
965-
var v = list[ix].Value ?? ExcelPivotTable.PivotNullValue;
966-
if (!siHs.Contains(v) || existingItems.Contains(v))
967-
{
968-
list.RemoveAt(ix);
969-
ix--;
970-
}
971-
else
966+
if (list[ix].Type == eItemType.Data)
972967
{
973-
existingItems.Add(v);
968+
var v = list[ix].Value ?? ExcelPivotTable.PivotNullValue;
969+
if (!siHs.Contains(v) || existingItems.Contains(v))
970+
{
971+
list.RemoveAt(ix);
972+
ix--;
973+
}
974+
else
975+
{
976+
existingItems.Add(v);
977+
}
974978
}
975979
}
976980
var hasSubTotalSubt = list.Count > 0 && list[list.Count - 1].Type == eItemType.Default ? 1 : 0;
@@ -1106,38 +1110,4 @@ internal Dictionary<object, int> GetCacheLookup()
11061110
}
11071111

11081112
}
1109-
1110-
internal class CacheComparer : IEqualityComparer<object>
1111-
{
1112-
public new bool Equals(object x, object y)
1113-
{
1114-
x = GetCaseInsensitiveValue(x);
1115-
y = GetCaseInsensitiveValue(y);
1116-
return x.Equals(y);
1117-
}
1118-
1119-
private static object GetCaseInsensitiveValue(object x)
1120-
{
1121-
if (x == null || x.Equals(ExcelPivotTable.PivotNullValue)) return ExcelPivotTable.PivotNullValue;
1122-
1123-
if (x is string sx)
1124-
{
1125-
return sx.ToLower();
1126-
}
1127-
else if (x is char cx)
1128-
{
1129-
return char.ToLower(cx).ToString();
1130-
}
1131-
if(ConvertUtil.IsExcelNumeric(x))
1132-
{
1133-
return ConvertUtil.GetValueDouble(x);
1134-
}
1135-
return x.ToString().ToLower();
1136-
}
1137-
1138-
public int GetHashCode(object obj)
1139-
{
1140-
return GetCaseInsensitiveValue(obj).GetHashCode();
1141-
}
1142-
}
11431113
}

src/EPPlus/Table/PivotTable/ExcelPivotTableField.cs

-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ internal ExcelPivotTableField(XmlNamespaceManager ns, XmlNode topNode, ExcelPivo
5151
}
5252

5353
internal ExcelPivotTable PivotTable { get; set; }
54-
//internal ExcelPivotTableCacheField CacheField { get; set; } = null;
5554

5655
/// <summary>
5756
/// The index of the pivot table field

src/EPPlusTest/Issues/PivotTableIssues.cs

+19
Original file line numberDiff line numberDiff line change
@@ -258,5 +258,24 @@ public void s744()
258258
SaveAndCleanup(p);
259259
}
260260
}
261+
[TestMethod]
262+
public void s744_2()
263+
{
264+
using (var p = OpenTemplatePackage("s744-2.xlsx"))
265+
{
266+
ExcelWorkbook workbook = p.Workbook;
267+
SaveAndCleanup(p);
268+
}
269+
}
270+
[TestMethod]
271+
public void s744_3()
272+
{
273+
using (var p = OpenTemplatePackage("FilterClearingExample.xlsx"))
274+
{
275+
ExcelWorkbook workbook = p.Workbook;
276+
p.Workbook.Worksheets[0].PivotTables[0].Calculate();
277+
SaveAndCleanup(p);
278+
}
279+
}
261280
}
262281
}

src/EPPlusTest/Table/PivotTable/PivotTableTests.cs

+2
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,8 @@ public void Pivot_Blank()
355355
var rf = pt.RowFields.Add(pt.Fields[0]);
356356
rf.SubTotalFunctions = eSubTotalFunctions.None;
357357
pt.DataOnRows = true;
358+
pt.Fields[0].Items.Refresh();
359+
pt.Fields[1].Items.Refresh();
358360
}
359361
[TestMethod]
360362
public void Pivot_SaveDataFalse()

0 commit comments

Comments
 (0)