merge method

dynamic merge(
  1. CellIndex start,
  2. CellIndex end, {
  3. dynamic customValue,
})

Merges the cells starting from start to end.

If custom value is not defined then it will look for the very first available value in range start to end by searching row-wise from left to right.

Implementation

merge(CellIndex start, CellIndex end, {dynamic customValue}) {
  int startColumn = start._columnIndex,
      startRow = start._rowIndex,
      endColumn = end._columnIndex,
      endRow = end._rowIndex;

  _checkMaxCol(startColumn);
  _checkMaxCol(endColumn);
  _checkMaxRow(startRow);
  _checkMaxRow(endRow);

  if ((startColumn == endColumn && startRow == endRow) ||
      (startColumn < 0 || startRow < 0 || endColumn < 0 || endRow < 0) ||
      (_spannedItems.contains(
          getSpanCellId(startColumn, startRow, endColumn, endRow)))) {
    return;
  }

  List<int> gotPosition = _getSpanPosition(start, end);

  _excel._mergeChanges = true;

  startColumn = gotPosition[0];
  startRow = gotPosition[1];
  endColumn = gotPosition[2];
  endRow = gotPosition[3];

  bool getValue = true;

  Data value = Data.newData(this, startRow, startColumn);
  if (customValue != null) {
    value._value = customValue;
    getValue = false;
  }

  for (int j = startRow; j <= endRow; j++) {
    for (int k = startColumn; k <= endColumn; k++) {
      if (_sheetData[j] != null && _sheetData[j]![k] != null) {
        if (getValue &&
            _sheetData[j]![k]!.value != null &&
            _sheetData[j]![k]!.cellStyle != null) {
          value = _sheetData[j]![k]!;
          getValue = false;
        }
        _sheetData[j]!.remove(k);
      }
    }
  }

  if (_sheetData[startRow] != null) {
    _sheetData[startRow]![startColumn] = value;
  } else {
    _sheetData[startRow] = {startColumn: value};
  }

  String sp = getSpanCellId(startColumn, startRow, endColumn, endRow);

  if (!_spannedItems.contains(sp)) {
    _spannedItems.add(sp);
  }

  _Span s = _Span();
  s._start = [startRow, startColumn];
  s._end = [endRow, endColumn];

  _spanList.add(s);
  _excel._mergeChangeLookup = sheetName;
}