Skip to content

Commit

Permalink
Fixes issue #1089 (#1090)
Browse files Browse the repository at this point in the history
  • Loading branch information
JanKallman authored Oct 9, 2023
1 parent 4145b76 commit 6aceb2a
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 13 deletions.
6 changes: 3 additions & 3 deletions src/EPPlus/Table/PivotTable/ExcelPivotTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ internal ExcelPivotTable(ExcelWorksheet sheet, ExcelAddressBase address, PivotTa
CreatePivotTable(sheet, address, pivotTableCache.Fields.Count, name, tblId);

CacheDefinition = new ExcelPivotCacheDefinition(sheet.NameSpaceManager, this, pivotTableCache);
CacheId = pivotTableCache.CacheId;
CacheId = pivotTableCache.ExtLstCacheId;

LoadFields();
Styles = new ExcelPivotTableAreaStyleCollection(this);
Expand All @@ -160,7 +160,7 @@ internal ExcelPivotTable(ExcelWorksheet sheet, ExcelAddressBase address, ExcelRa
CreatePivotTable(sheet, address, sourceAddress._toCol - sourceAddress._fromCol + 1, name, tblId);

CacheDefinition = new ExcelPivotCacheDefinition(sheet.NameSpaceManager, this, sourceAddress);
CacheId = CacheDefinition._cacheReference.CacheId;
CacheId = CacheDefinition._cacheReference.ExtLstCacheId;

LoadFields();
Styles = new ExcelPivotTableAreaStyleCollection(this);
Expand Down Expand Up @@ -206,7 +206,6 @@ private void LoadFields()
fld.LoadItems();
Fields.AddInternal(fld);
}

}
private string GetStartXml(string name, ExcelAddressBase address, int fields)
{
Expand Down Expand Up @@ -1145,6 +1144,7 @@ internal int ChangeCacheId(int oldCacheId)
var newCacheId = WorkSheet.Workbook.GetNewPivotCacheId();
CacheId = newCacheId;
CacheDefinition._cacheReference.CacheId = newCacheId;
CacheDefinition._cacheReference.ExtLstCacheId = newCacheId;
WorkSheet.Workbook.SetXmlNodeInt($"d:pivotCaches/d:pivotCache[@cacheId={oldCacheId}]/@cacheId", newCacheId);

return newCacheId;
Expand Down
43 changes: 33 additions & 10 deletions src/EPPlus/Table/PivotTable/PivotTableCacheInternal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ public PivotTableCacheInternal(ExcelWorkbook wb, Uri uri, int cacheId) : base (w
CacheDefinitionXml = new XmlDocument();
LoadXmlSafe(CacheDefinitionXml, Part.GetStream());
TopNode = CacheDefinitionXml.DocumentElement;
if (CacheId <= 0) //Check if the is set via exLst (used by for example slicers), otherwise set it to the cacheId
CacheId = cacheId;
if (ExtLstCacheId <= 0) //Check if the is set via exLst (used by for example slicers), otherwise set it to the cacheId
{
CacheId = cacheId;
ExtLstCacheId = cacheId;
}

ZipPackageRelationship rel = Part.GetRelationshipsByType(ExcelPackage.schemaRelationships + "/pivotCacheRecords").FirstOrDefault();
Expand Down Expand Up @@ -489,7 +490,7 @@ internal void InitNew(ExcelPivotTable pivotTable, ExcelRangeBase sourceAddress,
}
}

CacheId = _wb.GetNewPivotCacheId();
CacheId = ExtLstCacheId = _wb.GetNewPivotCacheId();

var c = CacheId;
CacheDefinitionUri = GetNewUri(pck, "/xl/pivotCache/pivotCacheDefinition{0}.xml", ref c);
Expand Down Expand Up @@ -565,28 +566,50 @@ internal void SetSourceAddress(string address)
SetXmlNodeString(_sourceAddressPath, address);
}
int _cacheId = int.MinValue;
/// <summary>
/// This is the cache id from the workbook
/// </summary>
internal int CacheId
{
get
{
if (_cacheId < 0)
if(_cacheId<0)
{
_cacheId = _wb.GetPivotCacheId(CacheDefinitionUri);
}
return _cacheId;
}
set
{
_cacheId = value;
}
}
int _extLstCacheId = int.MinValue;
/// <summary>
/// This a second cache id used for newer items like slicers. EPPlus will set this id to the same as the cache id by default.
/// </summary>
internal int ExtLstCacheId
{
get
{
if (_extLstCacheId < 0)
{
_cacheId = GetXmlNodeInt("d:extLst/d:ext/x14:pivotCacheDefinition/@pivotCacheId");
if (_cacheId < 0)
_extLstCacheId = GetXmlNodeInt("d:extLst/d:ext/x14:pivotCacheDefinition/@pivotCacheId");
if (_extLstCacheId < 0)
{
_cacheId = _wb.GetPivotCacheId(CacheDefinitionUri);
_extLstCacheId = CacheId;
var node = GetOrCreateExtLstSubNode(ExtLstUris.PivotCacheDefinitionUri, "x14");
node.InnerXml = $"<x14:pivotCacheDefinition pivotCacheId=\"{_cacheId}\"/>";
node.InnerXml = $"<x14:pivotCacheDefinition pivotCacheId=\"{_extLstCacheId}\"/>";
}
}
return _cacheId;
return _extLstCacheId;
}
set
{
var node = GetOrCreateExtLstSubNode(ExtLstUris.PivotCacheDefinitionUri, "x14");
if(node.InnerXml=="")
{
node.InnerXml = $"<x14:pivotCacheDefinition pivotCacheId=\"{_cacheId}\"/>";
node.InnerXml = $"<x14:pivotCacheDefinition pivotCacheId=\"{_extLstCacheId}\"/>";
}
else
{
Expand Down
39 changes: 39 additions & 0 deletions src/EPPlusTest/Issues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5274,5 +5274,44 @@ public void i1079()
Assert.AreEqual(1d, ws.Cells["A1"].Value);
}
}
[TestMethod]
public void s532()
{
var pivotTableWorksheetName = "Sheet3";
var NewSourceDataSheetName = "Sheet2";
var pivotTableName = "PivotTable1";
var exc = "";
var pivotTableCount = 0;
try
{

using (var package = OpenTemplatePackage("Pivot_Test_Orig.xlsx"))
{
var pivotTableWorksheet = package.Workbook.Worksheets[pivotTableWorksheetName];

ExcelWorksheet ws = package.Workbook.Worksheets[NewSourceDataSheetName];

pivotTableCount = pivotTableWorksheet.PivotTables.Count;

//var foundCache = package.Workbook.GetPivotCacheFromAddress(ws.Cells["M6:S16"].FullAddress, out PivotTableCacheInternal cache);

pivotTableWorksheet.PivotTables[pivotTableName].CacheDefinition.SourceRange = ws.Cells["M6:S16"];

SaveAndCleanup(package);
}
}
catch (Exception e)
{
exc = "Failed. " + e.ToString();
}

finally

{

System.GC.Collect();

}
}
}
}

0 comments on commit 6aceb2a

Please sign in to comment.