removeColumn method

void removeColumn(
  1. int columnIndex
)

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

Implementation

void removeColumn(int columnIndex) {
  _checkMaxColumn(columnIndex);
  if (columnIndex < 0 || columnIndex >= maxColumns) {
    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 (columnIndex <= endColumn) {
      if (columnIndex < startColumn) {
        startColumn -= 1;
      }
      endColumn -= 1;
      if (/* startColumn >= endColumn */
          (columnIndex == (endColumn + 1)) &&
              (columnIndex ==
                  (columnIndex < startColumn
                      ? startColumn + 1
                      : startColumn))) {
        _spanList[i] = null;
      } else {
        _Span newSpanObj = _Span(
          rowSpanStart: startRow,
          columnSpanStart: startColumn,
          rowSpanEnd: endRow,
          columnSpanEnd: 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 (columnIndex <= maxColumns - 1) {
    /// do the shifting task
    List<int> sortedKeys = _sheetData.keys.toList()..sort();
    sortedKeys.forEach((rowKey) {
      Map<int, Data> columnMap = Map<int, Data>();
      List<int> sortedColumnKeys = _sheetData[rowKey]!.keys.toList()..sort();
      sortedColumnKeys.forEach((columnKey) {
        if (_sheetData[rowKey] != null &&
            _sheetData[rowKey]![columnKey] != null) {
          if (columnKey < columnIndex) {
            columnMap[columnKey] = _sheetData[rowKey]![columnKey]!;
          }
          if (columnIndex == columnKey) {
            _sheetData[rowKey]!.remove(columnKey);
          }
          if (columnIndex < columnKey) {
            columnMap[columnKey - 1] = _sheetData[rowKey]![columnKey]!;
            _sheetData[rowKey]!.remove(columnKey);
          }
        }
      });
      _data[rowKey] = Map<int, Data>.from(columnMap);
    });
    _sheetData = Map<int, Map<int, Data>>.from(_data);
  }

  if (_maxColumns - 1 <= columnIndex) {
    _maxColumns -= 1;
  }
}