insertColumn method

void insertColumn(
  1. int columnIndex
)
inherited

Inserts an empty column in sheet at position = columnIndex.

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

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

Implementation

void insertColumn(int columnIndex) {
  if (columnIndex < 0) {
    return;
  }
  _checkMaxColumn(columnIndex);

  bool updateSpanCell = false;

  _spannedItems = FastList<String>();
  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;
      _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;
  }

  if (_sheetData.isNotEmpty) {
    final Map<int, Map<int, Data>> data = <int, Map<int, Data>>{};
    final List<int> sortedKeys = _sheetData.keys.toList()..sort();
    if (columnIndex <= maxColumns - 1) {
      /// do the shifting task
      for (var rowKey in sortedKeys) {
        final Map<int, Data> columnMap = <int, Data>{};

        /// getting the column keys in descending order so as to shifting becomes easy
        final List<int> sortedColumnKeys = _sheetData[rowKey]!.keys.toList()
          ..sort((a, b) {
            return b.compareTo(a);
          });
        for (var columnKey in sortedColumnKeys) {
          if (_sheetData[rowKey] != null &&
              _sheetData[rowKey]![columnKey] != null) {
            if (columnKey < columnIndex) {
              columnMap[columnKey] = _sheetData[rowKey]![columnKey]!;
            }
            if (columnIndex <= columnKey) {
              columnMap[columnKey + 1] = _sheetData[rowKey]![columnKey]!;
            }
          }
        }
        columnMap[columnIndex] = Data.newData(
          this as Sheet,
          rowKey,
          columnIndex,
        );
        data[rowKey] = Map<int, Data>.from(columnMap);
      }
      _sheetData = Map<int, Map<int, Data>>.from(data);
    } else {
      _sheetData[sortedKeys.first]![columnIndex] = Data.newData(
        this as Sheet,
        sortedKeys.first,
        columnIndex,
      );
    }
  } else {
    _sheetData = <int, Map<int, Data>>{};
    _sheetData[0] = {
      columnIndex: Data.newData(this as Sheet, 0, columnIndex),
    };
  }
  if (_maxColumns - 1 <= columnIndex) {
    _maxColumns += 1;
  } else {
    _maxColumns = columnIndex + 1;
  }
}