removeColumn method

void removeColumn(
  1. int colIndex
)

If sheet exists and columnIndex < maxColumns then it removes column at index = columnIndex

Implementation

void removeColumn(int colIndex) {
  _checkMaxCol(colIndex);
  if (colIndex < 0 || colIndex >= maxCols) {
    return;
  }

  bool updateSpanCell = false;

  /// Do the shifting of the cell Id of span Object

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

    if (colIndex <= endColumn) {
      _Span newSpanObj = _Span();
      if (colIndex < startColumn) {
        startColumn -= 1;
      }
      endColumn -= 1;
      if (/* startColumn >= endColumn */
          (colIndex == (endColumn + 1)) &&
              (colIndex ==
                  (colIndex < startColumn ? startColumn + 1 : startColumn))) {
        _spanList[i] = null;
      } else {
        newSpanObj._start = [startRow, startColumn];
        newSpanObj._end = [endRow, endColumn];
        _spanList[i] = newSpanObj;
      }
      updateSpanCell = true;
      _excel._mergeChanges = true;
    }

    if (_spanList[i] != null) {
      String rc = getSpanCellId(startColumn, startRow, endColumn, endRow);
      if (!_spannedItems.contains(rc)) {
        _spannedItems.add(rc);
      }
    }
  }
  _cleanUpSpanMap();

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

  Map<int, Map<int, Data>> _data = Map<int, Map<int, Data>>();
  if (colIndex <= maxCols - 1) {
    /// do the shifting task
    List<int> sortedKeys = _sheetData.keys.toList()..sort();
    sortedKeys.forEach((rowKey) {
      Map<int, Data> colMap = Map<int, Data>();
      List<int> sortedColKeys = _sheetData[rowKey]!.keys.toList()..sort();
      sortedColKeys.forEach((colKey) {
        if (_sheetData[rowKey] != null &&
            _sheetData[rowKey]![colKey] != null) {
          if (colKey < colIndex) {
            colMap[colKey] = _sheetData[rowKey]![colKey]!;
          }
          if (colIndex == colKey) {
            _sheetData[rowKey]!.remove(colKey);
          }
          if (colIndex < colKey) {
            colMap[colKey - 1] = _sheetData[rowKey]![colKey]!;
            _sheetData[rowKey]!.remove(colKey);
          }
        }
      });
      _data[rowKey] = Map<int, Data>.from(colMap);
    });
    _sheetData = Map<int, Map<int, Data>>.from(_data);
  }

  if (_maxCols - 1 <= colIndex) {
    _maxCols -= 1;
  }

  //_countRowAndCol();
}