npoi icon indicating copy to clipboard operation
npoi copied to clipboard

ShiftMergedRegions throws ArgumentException("lastRow < firstRow || lastCol < firstCol")

Open TimLee88 opened this issue 1 year ago • 9 comments

Path:https://github.com/nissl-lab/npoi/blob/master/main/SS/UserModel/Helpers/RowShifter.cs#L61 NPOI.SS.UserModel.Helpers.RowShifter / ShiftMergedRegions (line 61)

public List<CellRangeAddress> ShiftMergedRegions(int startRow, int endRow, int n)
{

...

var lastCol=sheet.GetRow(startRow) != null ? sheet.GetRow(startRow).LastCellNum : sheet.GetRow(endRow) != null ? sheet.GetRow(endRow).LastCellNum : 0;

 if (removalNeeded(merged, startRow, endRow, n, lastCol ))
 {
    removedIndices.Add(i);
    continue;
 }

...

加粗部分的代码,当sheet.GetRow(startRow)得到的IRow中没有Cell时,LastCellNum值为-1, 到 removalNeeded中new CellRangeAddress时,由于lastCol=-1,会抛出 ArgumentException("lastRow < firstRow || lastCol < firstCol") 错误。

建议将此处改为:var lastCol = ushort.MaxValue; 运行正常

TimLee88 avatar Nov 18 '22 08:11 TimLee88