Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
JanKallman committed Dec 15, 2017
1 parent 98528af commit 8975e60
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 43 deletions.
2 changes: 1 addition & 1 deletion EPPlus/ExcelCommentCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ internal void Delete(int fromRow, int fromCol, int rows, int columns)
{
address = address.DeleteRow(fromRow, rows);
}
if(address.Address=="#REF!")
if(address==null || address.Address=="#REF!")
{
deletedComments.Add(comment);
}
Expand Down
86 changes: 45 additions & 41 deletions EPPlus/ExcelWorksheet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3324,58 +3324,62 @@ private void SavePivotTables()

//Rewrite the pivottable address again if any rows or columns have been inserted or deleted
pt.SetXmlNodeString("d:location/@ref", pt.Address.Address);
var ws = Workbook.Worksheets[pt.CacheDefinition.SourceRange.WorkSheet];
var t = ws.Tables.GetFromRange(pt.CacheDefinition.SourceRange);
if (pt.CacheDefinition.SourceRange!=null && !pt.CacheDefinition.SourceRange.IsName && t==null)
var r = pt.CacheDefinition.SourceRange;
if (r != null) //Source does not exist
{
pt.CacheDefinition.SetXmlNodeString(ExcelPivotCacheDefinition._sourceAddressPath, pt.CacheDefinition.SourceRange.Address);
}

var fields =
pt.CacheDefinition.CacheDefinitionXml.SelectNodes(
"d:pivotCacheDefinition/d:cacheFields/d:cacheField", NameSpaceManager);
int ix = 0;
if (fields != null)
{
var flds = new HashSet<string>();
foreach (XmlElement node in fields)
var ws = Workbook.Worksheets[pt.CacheDefinition.SourceRange.WorkSheet];
var t = ws.Tables.GetFromRange(pt.CacheDefinition.SourceRange);
if (pt.CacheDefinition.SourceRange != null && !pt.CacheDefinition.SourceRange.IsName && t == null)
{
if (ix >= pt.CacheDefinition.SourceRange.Columns) break;
var fldName = node.GetAttribute("name"); //Fixes issue 15295 dup name error
if (string.IsNullOrEmpty(fldName))
{
fldName = (t == null
? pt.CacheDefinition.SourceRange.Offset(0, ix++, 1, 1).Value.ToString()
: t.Columns[ix++].Name);
}
if (flds.Contains(fldName))
{
fldName = GetNewName(flds, fldName);
}
flds.Add(fldName);
node.SetAttribute("name", fldName);
pt.CacheDefinition.SetXmlNodeString(ExcelPivotCacheDefinition._sourceAddressPath, pt.CacheDefinition.SourceRange.Address);
}
foreach (var df in pt.DataFields)

var fields =
pt.CacheDefinition.CacheDefinitionXml.SelectNodes(
"d:pivotCacheDefinition/d:cacheFields/d:cacheField", NameSpaceManager);
int ix = 0;
if (fields != null)
{
if (string.IsNullOrEmpty(df.Name))
var flds = new HashSet<string>();
foreach (XmlElement node in fields)
{
string name;
if (df.Function == DataFieldFunctions.None)
if (ix >= pt.CacheDefinition.SourceRange.Columns) break;
var fldName = node.GetAttribute("name"); //Fixes issue 15295 dup name error
if (string.IsNullOrEmpty(fldName))
{
name = df.Field.Name; //Name must be set or Excel will crash on rename.
fldName = (t == null
? pt.CacheDefinition.SourceRange.Offset(0, ix++, 1, 1).Value.ToString()
: t.Columns[ix++].Name);
}
else
if (flds.Contains(fldName))
{
name = df.Function.ToString() + " of " + df.Field.Name; //Name must be set or Excel will crash on rename.
fldName = GetNewName(flds, fldName);
}
//Make sure name is unique
var newName = name;
var i = 2;
while (pt.DataFields.ExistsDfName(newName, df))
flds.Add(fldName);
node.SetAttribute("name", fldName);
}
foreach (var df in pt.DataFields)
{
if (string.IsNullOrEmpty(df.Name))
{
newName = name + (i++).ToString(CultureInfo.InvariantCulture);
string name;
if (df.Function == DataFieldFunctions.None)
{
name = df.Field.Name; //Name must be set or Excel will crash on rename.
}
else
{
name = df.Function.ToString() + " of " + df.Field.Name; //Name must be set or Excel will crash on rename.
}
//Make sure name is unique
var newName = name;
var i = 2;
while (pt.DataFields.ExistsDfName(newName, df))
{
newName = name + (i++).ToString(CultureInfo.InvariantCulture);
}
df.Name = newName;
}
df.Name = newName;
}
}
}
Expand Down
1 change: 1 addition & 0 deletions EPPlus/Style/StyleBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public enum ExcelVerticalAlignment
public enum ExcelVerticalAlignmentFont
{
None,
Baseline,
Subscript,
Superscript
}
Expand Down
27 changes: 26 additions & 1 deletion EPPlusTest/Issues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1864,9 +1864,34 @@ public void Issue66()
using (var pck2 = new ExcelPackage(pck.Stream))
{
ws = pck2.Workbook.Worksheets["Test!"];

}
}
}
[TestMethod, Ignore]
public void Issue68()
{
using (var pck = new ExcelPackage(new FileInfo(@"c:\temp\bug68.xlsx")))
{
var ws = pck.Workbook.Worksheets["Sheet1"];
pck.Workbook.Worksheets.Delete(ws);
ws = pck.Workbook.Worksheets.Add("Sheet1");
pck.SaveAs(new FileInfo(@"c:\temp\bug68-2.xlsx"));
}
}
[TestMethod, Ignore]
public void Issue70()
{
var documentPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"c:\temp\workbook with comment.xlsx");
var outputPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"c:\temp\WorkbookWithCommentOutput.xlsx");
var fileInfo = new FileInfo(documentPath);
Assert.IsTrue(fileInfo.Exists);
using (var workbook = new ExcelPackage(fileInfo))
{
var ws = workbook.Workbook.Worksheets.First();
ws.DeleteRow(3); // NRE thrown here
workbook.SaveAs(new FileInfo(outputPath));
}
}
}
}

0 comments on commit 8975e60

Please sign in to comment.