removeRow method

void removeRow(
  1. int rowIndex
)

If sheet exists and rowIndex < maxRows then it removes row at index = rowIndex

Implementation

void removeRow(int rowIndex) {
  if (rowIndex < 0 || rowIndex >= _maxRows) {
    return;
  }
  _checkMaxRow(rowIndex);

  bool updateSpanCell = false;

  for (int i = 0; i < _spanList.length; i++) {
    final _Span? spanObj = _spanList[i];
    if (spanObj == null) {
      continue;
    }
    int startColumn = spanObj.columnSpanStart,
        startRow = spanObj.rowSpanStart,
        endColumn = spanObj.columnSpanEnd,
        endRow = spanObj.rowSpanEnd;

    if (rowIndex <= endRow) {
      if (rowIndex < startRow) {
        startRow -= 1;
      }
      endRow -= 1;
      if (/* startRow >= endRow */
          (rowIndex == (endRow + 1)) &&
              (rowIndex == (rowIndex < startRow ? startRow + 1 : startRow))) {
        _spanList[i] = null;
      } else {
        final _Span newSpanObj = _Span(
          rowSpanStart: startRow,
          columnSpanStart: startColumn,
          rowSpanEnd: endRow,
          columnSpanEnd: endColumn,
        );
        _spanList[i] = newSpanObj;
      }
      updateSpanCell = true;
      _excel._mergeChanges = true;
    }
    if (_spanList[i] != null) {
      final String rc =
          getSpanCellId(startColumn, startRow, endColumn, endRow);
      if (!_spannedItems.contains(rc)) {
        _spannedItems.add(rc);
      }
    }
  }
  _cleanUpSpanMap();

  if (updateSpanCell) {
    _excel._mergeChangeLookup = sheetName;
  }

  if (_sheetData.isNotEmpty) {
    final Map<int, Map<int, Data>> _data = Map<int, Map<int, Data>>();
    if (rowIndex <= maxRows - 1) {
      /// do the shifting task
      final List<int> sortedKeys = _sheetData.keys.toList()..sort();
      sortedKeys.forEach((rowKey) {
        if (rowKey < rowIndex && _sheetData[rowKey] != null) {
          _data[rowKey] = Map<int, Data>.from(_sheetData[rowKey]!);
        }
        if (rowIndex == rowKey && _sheetData[rowKey] != null) {
          _sheetData.remove(rowKey);
        }
        if (rowIndex < rowKey && _sheetData[rowKey] != null) {
          _data[rowKey - 1] = Map<int, Data>.from(_sheetData[rowKey]!);
          _sheetData.remove(rowKey);
        }
      });
      _sheetData = Map<int, Map<int, Data>>.from(_data);
    }
    //_countRowsAndColumns();
  } else {
    _maxRows = 0;
    _maxColumns = 0;
  }

  if (_maxRows - 1 <= rowIndex) {
    _maxRows -= 1;
  }
}