nissl-lab / npoi

a .NET library that can read/write Office formats without Microsoft Office installed. No COM+, no interop.
Apache License 2.0
5.72k stars 1.43k forks source link

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

Open TimLee88 opened 1 year ago

TimLee88 commented 1 year ago

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 commented 1 year ago

此函数影响 ISheet.ShiftRows

tonyqus commented 1 year ago

Which version of NPOI are you using?

TimLee88 commented 1 year ago

2.6

bieshaoxiong commented 1 year ago

请问这个问题是在 2.6.1解决了吗? image 我这里怎么只有2.6.0

tonyqus commented 1 year ago

2.6.1 只是规划版本,还没发布呢,2.6.0才刚刚发正式版。半年以后吧。

bieshaoxiong commented 1 year ago

2.6.1 只是规划版本,还没发布呢,2.6.0才刚刚发正式版。半年以后吧。

那现在这个有什么解决方案吗

18663450635 commented 1 year ago

我也遇到了这个问题 但在 ShiftRows 之前 执行sheet.GetRow(startRow) != null ? sheet.GetRow(startRow).LastCellNum : sheet.GetRow(endRow) != null ? sheet.GetRow(endRow).LastCellNum : 0; 值为11,但依然抛出了此异常

18663450635 commented 1 year ago

升级2.6.1 问题依然存在

18663450635 commented 1 year ago

源码中并没有变化啊