Skip to content

Commit

Permalink
Copy RepeatingRows and RepeatingColumns
Browse files Browse the repository at this point in the history
This PR fixes nissl-lab#1182

Because RepeatingRows and RepeatingColumns are part of the Workbook, they're not serialized during creating a copy of the sheet. Just copy them manually
  • Loading branch information
Bykiev committed Sep 15, 2023
1 parent ece703e commit 42e0a36
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
6 changes: 6 additions & 0 deletions ooxml/XSSF/UserModel/XSSFWorkbook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,12 @@ public ISheet CloneSheet(int sheetNum, String newName)
ct.UnsetPageSetup();
}

if (srcSheet.RepeatingRows != null)
clonedSheet.RepeatingRows = srcSheet.RepeatingRows;

if (srcSheet.RepeatingColumns != null)
clonedSheet.RepeatingColumns = srcSheet.RepeatingColumns;

clonedSheet.IsSelected = (false);

// clone the sheet drawing alongs with its relationships
Expand Down
31 changes: 31 additions & 0 deletions testcases/ooxml/XSSF/UserModel/TestXSSFSheet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2078,6 +2078,7 @@ private void AddComments(ICreationHelper helper, ISheet sheet)
cell.CellComment = comment;
}
}

[Test]
public void TestCoordinate()
{
Expand Down Expand Up @@ -2118,5 +2119,35 @@ public void TestCoordinate()
Assert.IsTrue(sa.To.rowOff == anchor.To.rowOff, /**/"To.rowOff [{0}]({1}={2})", new object[] { shape.Name, sa.To.rowOff, anchor.To.rowOff });
}
}


[Test]
public void TestCopyRepeatingRowsAndColumns()
{
using (var book = new XSSFWorkbook())
{
var sheet = book.CreateSheet("Sheet1");

var row1 = sheet.CreateRow(0);
row1.CreateCell(0);

var row2 = sheet.CreateRow(1);
row2.CreateCell(0);

sheet.RepeatingRows = CellRangeAddress.ValueOf("1:1");
sheet.RepeatingColumns = CellRangeAddress.ValueOf("A1:B1");

var clonedSheet = book.CloneSheet(0);

Assert.IsNotNull(clonedSheet.RepeatingRows, "RepeatingRows is null");
Assert.AreEqual(clonedSheet.RepeatingRows.FirstRow, sheet.RepeatingRows.FirstRow, "RepeatingRows.FirstRow are not equal");
Assert.AreEqual(clonedSheet.RepeatingRows.LastRow, sheet.RepeatingRows.LastRow, "RepeatingRows.LastRow are not equal");

Assert.IsNotNull(clonedSheet.RepeatingColumns, "RepeatingColumns is null");
Assert.AreEqual(clonedSheet.RepeatingColumns.FirstColumn, sheet.RepeatingColumns.FirstColumn, "RepeatingColumns.FirstColumn are not equal");
Assert.AreEqual(clonedSheet.RepeatingColumns.LastColumn, sheet.RepeatingColumns.LastColumn, "RepeatingColumns.LastColumn are not equal");

}
}
}
}

0 comments on commit 42e0a36

Please sign in to comment.