Skip to content

Commit

Permalink
Fixes issue #1646
Browse files Browse the repository at this point in the history
  • Loading branch information
JanKallman committed Oct 22, 2024
1 parent 997b157 commit 7fb4962
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/EPPlus/Drawing/Slicer/ExcelPivotTableSlicerCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ internal override void Init(ExcelWorkbook wb)
{
if (_field == null)
{
_field = pt.Fields.Where(x => x.Cache.Name == SourceName).FirstOrDefault();
_field = pt.Fields.Where(x => x.Cache.Name.Equals(SourceName, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
}

PivotTables._list.Add(pt);
Expand Down
20 changes: 15 additions & 5 deletions src/EPPlus/Drawing/Slicer/ExcelPivotTableSlicerCacheTabularData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Date Author Change
using OfficeOpenXml.Table.PivotTable;
using OfficeOpenXml.Utils.Extensions;
using System;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Xml;
Expand Down Expand Up @@ -131,14 +132,23 @@ internal void UpdateItemsXml()
int x = 0;
if (_cache._field == null) return;

foreach (var item in _cache._field.Items)
foreach (var item in _cache._field.Items.OrderBy(x=>x.X))
{
if (item.Type == eItemType.Data)
{
if (IsHidden(item))
sb.Append($"<i x=\"{x++}\" />");
else
sb.Append($"<i x=\"{x++}\" s=\"1\"/>");
sb.Append($"<i x=\"{item.X}\" ");

if (item.Value is PivotNull)
{
sb.Append("nd=\"1\" ");
}

if (IsHidden(item)==false)
{
sb.Append($"s=\"1\" ");
}

sb.Append($"/>");
}
}

Expand Down
7 changes: 5 additions & 2 deletions src/EPPlus/Drawing/Slicer/ExcelPivotTableSlicerItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using OfficeOpenXml.Table.PivotTable;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OfficeOpenXml.Drawing.Slicer
Expand Down Expand Up @@ -44,7 +45,8 @@ public bool Hidden
{
throw(new IndexOutOfRangeException());
}
return _cache._field.Items[_index].Hidden;
var ix = _cache._field.Items.CacheLookup[_index].First();
return _cache._field.Items[ix].Hidden;
}
set
{
Expand All @@ -56,7 +58,8 @@ public bool Hidden
{
var fld = pt.Fields[_cache._field.Index];
if (_index >= fld.Items.Count || fld.Items[_index].Type != Table.PivotTable.eItemType.Data) continue;
fld.Items[_index].Hidden = value;
var ix = fld.Items.CacheLookup[_index].First();
fld.Items[ix].Hidden = value;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ internal void RefreshMe()
{
_items.RemoveAt(_items.Count - 1);
}
}
}
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/EPPlus/Table/PivotTable/ExcelPivotTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1704,7 +1704,7 @@ internal void Save()

SetXmlNodeString("d:location/@ref", Address.Address);

foreach (var field in Fields)
foreach(var field in Fields)
{
field.SaveToXml();
}
Expand Down
4 changes: 2 additions & 2 deletions src/EPPlus/Table/PivotTable/ExcelPivotTableCacheField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -959,7 +959,7 @@ private void UpdatePivotItemsFromSharedItems(HashSet<object> siHs)
if (ptField.ShouldHaveItems == false) continue;
var existingItems = new HashSet<object>(new InvariantObjectComparer());
var list = ptField.Items._list;

var hasFilter = list.Any(x => x.Hidden);
for (var ix = 0; ix < list.Count; ix++)
{
var v = list[ix].Value ?? ExcelPivotTable.PivotNullValue;
Expand All @@ -978,7 +978,7 @@ private void UpdatePivotItemsFromSharedItems(HashSet<object> siHs)
{
if (!existingItems.Contains(c))
{
list.Insert(list.Count - hasSubTotalSubt, new ExcelPivotTableFieldItem() { Value = c });
list.Insert(list.Count - hasSubTotalSubt, new ExcelPivotTableFieldItem() { Value = c, Hidden=hasFilter });
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/EPPlus/Table/PivotTable/ExcelPivotTableField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ public ExcelPivotTableSlicer Slicer
{
foreach (var d in ws.Drawings)
{
if (d is ExcelPivotTableSlicer s && s.Cache != null && s.Cache.PivotTables.Contains(PivotTable) && Index == s.Cache._field.Index)
if (d is ExcelPivotTableSlicer s && s.Cache != null && s.Cache.PivotTables.Contains(PivotTable) && Index == s.Cache._field?.Index)
{
_slicer = s;
return _slicer;
Expand Down
25 changes: 18 additions & 7 deletions src/EPPlus/Table/PivotTable/ExcelPivotTableFieldCollectionBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace OfficeOpenXml.Table.PivotTable
public class ExcelPivotTableFieldItemsCollection : ExcelPivotTableFieldCollectionBase<ExcelPivotTableFieldItem>
{
ExcelPivotTableField _field;
internal Lookup<int, int> _cacheLookup = null;
private Lookup<int, int> _cacheLookup = null;

List<int> _hiddenItemIndex=null;
internal ExcelPivotTableFieldItemsCollection(ExcelPivotTableField field) : base()
Expand Down Expand Up @@ -78,9 +78,9 @@ public ExcelPivotTableFieldItem GetByValue(object value)
var cl = _field.Cache.GetCacheLookup();
if (cl.TryGetValue(value, out int ix))
{
if (_cacheLookup.Contains(ix))
if (CacheLookup.Contains(ix))
{
return _list[_cacheLookup[ix].First()];
return _list[CacheLookup[ix].First()];
}
}
return null;
Expand All @@ -96,7 +96,7 @@ public int GetIndexByValue(object value)
var cl = _field.Cache.GetCacheLookup();
if (cl.TryGetValue(value, out int ix))
{
if (_cacheLookup.Contains(ix))
if (CacheLookup.Contains(ix))
{
return _cacheLookup[ix].First();
}
Expand All @@ -120,7 +120,18 @@ internal void MatchValueToIndex()
item.X = -1;
}
}
_cacheLookup = (Lookup<int,int>)_list.Where(x=> x.X >= 0).ToLookup(x => x.X, y => _list.IndexOf(y));
_cacheLookup = null;
}
internal Lookup<int, int> CacheLookup
{
get
{
if (_cacheLookup == null)
{
_cacheLookup = (Lookup<int, int>)_list.Where(x => x.X >= 0).ToLookup(x => x.X, y => _list.IndexOf(y));
}
return _cacheLookup;
}
}
/// <summary>
/// Set Hidden to false for all items in the collection
Expand Down Expand Up @@ -186,12 +197,12 @@ internal void Sort(eSortType sort)
{
var comparer = new PivotItemComparer(sort, _field);
_list.Sort(comparer);
_cacheLookup = (Lookup<int,int>)_list.Where(x=>x.X > -1).ToLookup(x => x.X, y=>_list.IndexOf(y));
_cacheLookup = null;
}

internal ExcelPivotTableFieldItem GetByCacheIndex(int index)
{
if (_cacheLookup.Contains(index))
if (CacheLookup.Contains(index))
{
return _list[_cacheLookup[index].First()];
}
Expand Down
50 changes: 50 additions & 0 deletions src/EPPlusTest/Issues/PivotTableIssues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,56 @@ public void i1603()
f2.Items.ShowDetails(false);
Assert.AreEqual(6, f2.Items.Count);

SaveAndCleanup(package);
}
}
[TestMethod]
public void s747()
{
using (var package = OpenTemplatePackage("s747.xlsx"))
{
var workbook = package.Workbook;
var worksheet = workbook.Worksheets["Sheet2"];
worksheet.Cells["A20"].Value = "C";
worksheet.Cells["A21"].Value = "C";
worksheet.Cells["A22"].Value = "C";
worksheet.Cells["A23"].Value = "H";
worksheet.Cells["A24"].Value = "H";
worksheet.Cells["A25"].Value = "H";
worksheet.Cells["B20"].Value = "Test";
worksheet.Cells["B21"].Value = "Test";
worksheet.Cells["B22"].Value = "Test";
worksheet.Cells["B23"].Value = "Test";
worksheet.Cells["B24"].Value = "Test";
worksheet.Cells["B25"].Value = "Test";
worksheet.Cells["C20"].Value = 1;
worksheet.Cells["C21"].Value = 1;
worksheet.Cells["C22"].Value = 1;
worksheet.Cells["C23"].Value = 1;
worksheet.Cells["C24"].Value = 1;
worksheet.Cells["C25"].Value = 1;

var ws2 = workbook.Worksheets["High Level Summary"];
var pt = ws2.PivotTables[0];
var slicer1 =ws2.Drawings[0].As.Slicer.PivotTableSlicer;

Assert.AreEqual(pt.Fields[0].Items.Count, 5);
Assert.AreEqual(4, slicer1.Cache.Data.Items.Count);
Assert.AreEqual(false, slicer1.Cache.Data.Items[0].Hidden);
Assert.AreEqual(false, slicer1.Cache.Data.Items[1].Hidden);
Assert.AreEqual(true, slicer1.Cache.Data.Items[2].Hidden);
Assert.AreEqual(true, slicer1.Cache.Data.Items[3].Hidden);

workbook.CalculateAllPivotTables(true); //This causes different but still unexpected changes in the selected values. Happends for true or false

Assert.AreEqual(6, slicer1.Cache.Data.Items.Count);
Assert.AreEqual(false, slicer1.Cache.Data.Items[0].Hidden);
Assert.AreEqual(false, slicer1.Cache.Data.Items[1].Hidden);
Assert.AreEqual(true, slicer1.Cache.Data.Items[2].Hidden);
Assert.AreEqual(true, slicer1.Cache.Data.Items[3].Hidden);
Assert.AreEqual(true, slicer1.Cache.Data.Items[4].Hidden);
Assert.AreEqual(true, slicer1.Cache.Data.Items[5].Hidden);

SaveAndCleanup(package);
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/EPPlusTest/Issues/WorksheetIssues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -477,5 +477,17 @@ public void I1596()
worksheet.DeleteRow(256);
}
}
[TestMethod]
public void s746()
{
using (var p = OpenTemplatePackage("s746.xlsm"))
{
var workbook = p.Workbook;
var worksheet = workbook.Worksheets["Sheet1"];
workbook.Worksheets["Sheet1"].Columns[2].Width = 100; //Commenting this line out stops the error.
SaveAndCleanup(p);

}
}
}
}

0 comments on commit 7fb4962

Please sign in to comment.