Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 67 additions & 62 deletions main/SS/Util/SheetUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
32 changes: 30 additions & 2 deletions testcases/ooxml/XSSF/UserModel/TestXSSFBugs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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"));
}
}
}
}