From c69d3b93794b2a407d662d97f2322b4ed09fc29c Mon Sep 17 00:00:00 2001 From: ABykiev Date: Tue, 8 Aug 2023 19:53:59 +0300 Subject: [PATCH 1/2] Fix copying empty row This PR closes #1051 --- main/SS/Util/SheetUtil.cs | 129 +++++++++--------- .../ooxml/XSSF/UserModel/TestXSSFBugs.cs | 31 ++++- 2 files changed, 97 insertions(+), 63 deletions(-) diff --git a/main/SS/Util/SheetUtil.cs b/main/SS/Util/SheetUtil.cs index 4d12ad5a8..aa66f0449 100644 --- a/main/SS/Util/SheetUtil.cs +++ b/main/SS/Util/SheetUtil.cs @@ -187,82 +187,87 @@ public static IRow CopyRow(ISheet sheet, int sourceRowIndex, int targetRowIndex) { sheet.ShiftRows(targetRowIndex, sheet.LastRowNum, 1); } - newRow = sheet.CreateRow(targetRowIndex); - newRow.Height = sourceRow.Height; //copy row height - // Loop through source columns to add to new row - for (int i = sourceRow.FirstCellNum; i < sourceRow.LastCellNum; i++) + if (sourceRow != null) { - // Grab a copy of the old/new cell - ICell oldCell = sourceRow.GetCell(i); + newRow = sheet.CreateRow(targetRowIndex); + newRow.Height = sourceRow.Height; //copy row height - // If the old cell is null jump to next cell - if (oldCell == null) + // Loop through source columns to add to new row + for (int i = sourceRow.FirstCellNum; i < sourceRow.LastCellNum; i++) { - continue; - } - ICell newCell = newRow.CreateCell(i); + // Grab a copy of the old/new cell + ICell oldCell = sourceRow.GetCell(i); - if (oldCell.CellStyle != null) - { - // apply style from old cell to new cell - newCell.CellStyle = oldCell.CellStyle; - } + // If the old cell is null jump to next cell + if (oldCell == null) + { + continue; + } + ICell newCell = newRow.CreateCell(i); - // If there is a cell comment, copy - if (oldCell.CellComment != null) - { - sheet.CopyComment(oldCell, newCell); - } + if (oldCell.CellStyle != null) + { + // apply style from old cell to new cell + newCell.CellStyle = oldCell.CellStyle; + } - // If there is a cell hyperlink, copy - if (oldCell.Hyperlink != null) - { - newCell.Hyperlink = oldCell.Hyperlink; - } + // If there is a cell comment, copy + if (oldCell.CellComment != null) + { + sheet.CopyComment(oldCell, newCell); + } - // Set the cell data type - newCell.SetCellType(oldCell.CellType); + // If there is a cell hyperlink, copy + if (oldCell.Hyperlink != null) + { + newCell.Hyperlink = oldCell.Hyperlink; + } - // Set the cell data value - switch (oldCell.CellType) - { - case CellType.Blank: - newCell.SetCellValue(oldCell.StringCellValue); - break; - case CellType.Boolean: - newCell.SetCellValue(oldCell.BooleanCellValue); - break; - case CellType.Error: - newCell.SetCellErrorValue(oldCell.ErrorCellValue); - break; - case CellType.Formula: - newCell.SetCellFormula(oldCell.CellFormula); - break; - case CellType.Numeric: - newCell.SetCellValue(oldCell.NumericCellValue); - break; - case CellType.String: - newCell.SetCellValue(oldCell.RichStringCellValue); - break; + // Set the cell data type + newCell.SetCellType(oldCell.CellType); + + // Set the cell data value + switch (oldCell.CellType) + { + case CellType.Blank: + newCell.SetCellValue(oldCell.StringCellValue); + break; + case CellType.Boolean: + newCell.SetCellValue(oldCell.BooleanCellValue); + break; + case CellType.Error: + newCell.SetCellErrorValue(oldCell.ErrorCellValue); + break; + case CellType.Formula: + newCell.SetCellFormula(oldCell.CellFormula); + break; + case CellType.Numeric: + newCell.SetCellValue(oldCell.NumericCellValue); + break; + case CellType.String: + newCell.SetCellValue(oldCell.RichStringCellValue); + break; + } } - } - // If there are are any merged regions in the source row, copy to new row - for (int i = 0; i < sheet.NumMergedRegions; i++) - { - CellRangeAddress cellRangeAddress = sheet.GetMergedRegion(i); - if (cellRangeAddress != null && cellRangeAddress.FirstRow == sourceRow.RowNum) + // If there are are any merged regions in the source row, copy to new row + for (int i = 0; i < sheet.NumMergedRegions; i++) { - CellRangeAddress newCellRangeAddress = new CellRangeAddress(newRow.RowNum, - (newRow.RowNum + - (cellRangeAddress.LastRow - cellRangeAddress.FirstRow - )), - cellRangeAddress.FirstColumn, - cellRangeAddress.LastColumn); - sheet.AddMergedRegion(newCellRangeAddress); + CellRangeAddress cellRangeAddress = sheet.GetMergedRegion(i); + if (cellRangeAddress != null && cellRangeAddress.FirstRow == sourceRow.RowNum) + { + CellRangeAddress newCellRangeAddress = new CellRangeAddress(newRow.RowNum, + (newRow.RowNum + + (cellRangeAddress.LastRow - cellRangeAddress.FirstRow + )), + cellRangeAddress.FirstColumn, + cellRangeAddress.LastColumn); + sheet.AddMergedRegion(newCellRangeAddress); + } } } + return newRow; } diff --git a/testcases/ooxml/XSSF/UserModel/TestXSSFBugs.cs b/testcases/ooxml/XSSF/UserModel/TestXSSFBugs.cs index 2a9f61eea..a35683bb7 100644 --- a/testcases/ooxml/XSSF/UserModel/TestXSSFBugs.cs +++ b/testcases/ooxml/XSSF/UserModel/TestXSSFBugs.cs @@ -1628,7 +1628,7 @@ public void Test55692() WorkbookFactory.Create(inpA); Assert.Fail("Should've raised a EncryptedDocumentException error"); } - catch (EncryptedDocumentException ) { } + catch (EncryptedDocumentException) { } // Via a POIFSFileSystem POIFSFileSystem fsP = new POIFSFileSystem(inpB); @@ -3383,6 +3383,35 @@ public void Bug61063() Assert.AreEqual(2.0, cv.NumberValue, 0.00001); wb.Close(); } + + [Test] + public void TestCopyEmptyRow() + { + using (var wb = new XSSFWorkbook()) + { + var sheet = wb.CreateSheet(); + var row = sheet.CreateRow(1); + + row.CreateCell(1).SetCellValue("B2"); + row.CreateCell(2).SetCellValue("C2"); + row.CreateCell(3).SetCellValue("D2"); + + Assert.DoesNotThrow(() => + { + sheet.CopyRow(0, 1); + }); + + var movedRow = sheet.GetRow(1); + Assert.IsNull(movedRow); + + var shiftedRow = sheet.GetRow(2); + Assert.IsNotNull(shiftedRow); + + Assert.IsTrue(shiftedRow.GetCell(1).StringCellValue.Equals("B2")); + Assert.IsTrue(shiftedRow.GetCell(2).StringCellValue.Equals("C2")); + Assert.IsTrue(shiftedRow.GetCell(3).StringCellValue.Equals("D2")); + } + } } } From 650003a4a96c310f76649f3ed9d93781f12854ab Mon Sep 17 00:00:00 2001 From: ABykiev Date: Sun, 13 Aug 2023 19:51:27 +0300 Subject: [PATCH 2/2] Update from upstream --- .../ooxml/XSSF/UserModel/TestXSSFBugs.cs | 32 +++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/testcases/ooxml/XSSF/UserModel/TestXSSFBugs.cs b/testcases/ooxml/XSSF/UserModel/TestXSSFBugs.cs index 75cb129e0..6ba011d3f 100644 --- a/testcases/ooxml/XSSF/UserModel/TestXSSFBugs.cs +++ b/testcases/ooxml/XSSF/UserModel/TestXSSFBugs.cs @@ -1628,7 +1628,7 @@ public void Test55692() WorkbookFactory.Create(inpA); Assert.Fail("Should've raised a EncryptedDocumentException error"); } - catch (EncryptedDocumentException ) { } + catch (EncryptedDocumentException) { } // Via a POIFSFileSystem POIFSFileSystem fsP = new POIFSFileSystem(inpB); @@ -3424,6 +3424,34 @@ public void TestBug690() } } } - } + [Test] + public void TestCopyEmptyRow() + { + using (var wb = new XSSFWorkbook()) + { + var sheet = wb.CreateSheet(); + var row = sheet.CreateRow(1); + + row.CreateCell(1).SetCellValue("B2"); + row.CreateCell(2).SetCellValue("C2"); + row.CreateCell(3).SetCellValue("D2"); + + Assert.DoesNotThrow(() => + { + sheet.CopyRow(0, 1); + }); + + var movedRow = sheet.GetRow(1); + Assert.IsNull(movedRow); + + var shiftedRow = sheet.GetRow(2); + Assert.IsNotNull(shiftedRow); + + Assert.IsTrue(shiftedRow.GetCell(1).StringCellValue.Equals("B2")); + Assert.IsTrue(shiftedRow.GetCell(2).StringCellValue.Equals("C2")); + Assert.IsTrue(shiftedRow.GetCell(3).StringCellValue.Equals("D2")); + } + } + } }