Skip to content

Commit

Permalink
XSSFSheet.CopySheet - fix null reference error
Browse files Browse the repository at this point in the history
  • Loading branch information
Bykiev committed May 4, 2024
1 parent 1ff4c6e commit b7102a7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
5 changes: 1 addition & 4 deletions ooxml/XSSF/UserModel/XSSFSheet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3635,9 +3635,7 @@ public ISheet CopySheet(string name, bool copyStyle)

PackageRelationship rel = r.GetPackageRelationship();
clonedSheet.GetPackagePart().AddRelationship(
rel.TargetUri, (TargetMode)rel.TargetMode, rel.RelationshipType);
clonedSheet.AddRelation(rel.Id, r);
}
rel.TargetUri, (TargetMode)rel.TargetMode, rel.RelationshipType, rel.Id); }

// copy hyperlinks
clonedSheet.hyperlinks = new List<XSSFHyperlink>(hyperlinks);
Expand All @@ -3664,7 +3662,6 @@ public ISheet CopySheet(string name, bool copyStyle)
foreach (POIXMLDocumentPart rel in srcRels)
{
PackageRelationship relation = rel.GetPackageRelationship();
clonedDg.AddRelation(relation.Id, rel);
clonedDg
.GetPackagePart()
.AddRelationship(relation.TargetUri, relation.TargetMode.Value,
Expand Down
18 changes: 17 additions & 1 deletion testcases/ooxml/XSSF/UserModel/TestXSSFSheetCopyTo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ limitations under the License.

using NPOI.SS.UserModel;
using NPOI.SS.Util;
using NPOI.XSSF;
using NPOI.XSSF.UserModel;
using NUnit.Framework;
using System;
using System.IO;
using System.Linq;
using TestCases.SS.UserModel;

namespace TestCases.XSSF.UserModel
{
Expand Down Expand Up @@ -88,5 +89,20 @@ public void CopySheetToWorkbookShouldCopyMergedRegionsOver()
destSheet.MergedRegions
.Select(r => r.FormatAsString())));
}


[Test]
public void TestBug346()
{
using(var wb = XSSFTestDataSamples.OpenSampleWorkbook("MSA_Templete.xlsx"))
{
var sheet = wb.GetSheetAt(0);

Assert.DoesNotThrow(() =>
{
sheet.CopySheet("Copysheet");
});
}
}
}
}
Binary file not shown.

0 comments on commit b7102a7

Please sign in to comment.