Skip to content

Commit

Permalink
Added support for workbooks that has duplicate defined names. The las…
Browse files Browse the repository at this point in the history
…t name in the load order will be used. Issue #37.
  • Loading branch information
JanKallman committed Feb 21, 2020
1 parent f6b1b1f commit 3548252
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/EPPlus/ExcelNamedRangeCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,16 @@ public ExcelNamedRange Add(string Name, ExcelRangeBase Range)

private void AddName(string Name, ExcelNamedRange item)
{
_dic.Add(Name, _list.Count);
_list.Add(item);
if(_dic.ContainsKey(Name)) //Excel allows duplicate names on load. Duplicates can be generated by for example Libre Office print areas. Always pick the last in the list.
{
_list.RemoveAt(_dic[Name]);
_list.Insert(_dic[Name], item);
}
else
{
_dic.Add(Name, _list.Count);
_list.Add(item);
}
}
/// <summary>
/// Add a defined name referencing value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public void ValidateFormulasAfterInsertRow()
ws.InsertRow(3, 1);

//Assert
Assert.AreEqual(1, ws._sharedFormulas.Count);
Assert.AreEqual("Sum(C6:C11)", ws.Cells["A1"].Formula);
Assert.AreEqual("Sum(C6:C11)", ws.Cells["B1"].Formula);
Assert.AreEqual("Sum(C7:C12)", ws.Cells["B2"].Formula);
Expand Down
11 changes: 11 additions & 0 deletions src/EPPlusTest/Issues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1075,5 +1075,16 @@ public void Issue464()
}
}
}
[TestMethod]
public void Issue625()
{
using (var p = OpenTemplatePackage("multiple_print_areas.xlsx"))
{

var workSheet = p.Workbook.Worksheets[0];

SaveWorkbook("Issue625.xlsx", p);
}
}
}
}

0 comments on commit 3548252

Please sign in to comment.