insertRow method

void insertRow(
  1. int rowIndex
)

Inserts an empty row in sheet at position = rowIndex.

If rowIndex == null or rowIndex < 0 if will not execute

If the sheet does not exists then it will be created automatically.

Implementation

void insertRow(int rowIndex) {
  if (rowIndex < 0) {
    return;
  }

  _checkMaxRow(rowIndex);

  bool updateSpanCell = false;

  _spannedItems = FastList<String>();
  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;
      final _Span newSpanObj = _Span(
        rowSpanStart: startRow,
        columnSpanStart: startColumn,
        rowSpanEnd: endRow,
        columnSpanEnd: endColumn,
      );
      _spanList[i] = newSpanObj;
      updateSpanCell = true;
      _excel._mergeChanges = true;
    }
    String rc = getSpanCellId(startColumn, startRow, endColumn, endRow);
    if (!_spannedItems.contains(rc)) {
      _spannedItems.add(rc);
    }
  }

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

  Map<int, Map<int, Data>> _data = Map<int, Map<int, Data>>();
  if (_sheetData.isNotEmpty) {
    List<int> sortedKeys = _sheetData.keys.toList()
      ..sort((a, b) {
        return b.compareTo(a);
      });
    if (rowIndex <= maxRows - 1) {
      /// do the shifting task
      sortedKeys.forEach((rowKey) {
        if (rowKey < rowIndex) {
          _data[rowKey] = _sheetData[rowKey]!;
        }
        if (rowIndex <= rowKey) {
          _data[rowKey + 1] = _sheetData[rowKey]!;
        }
      });
    }
  }
  _data[rowIndex] = {0: Data.newData(this, rowIndex, 0)};
  _sheetData = Map<int, Map<int, Data>>.from(_data);

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

  //_countRowsAndColumns();
}