insertColumn method
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 colIndex) {
if (colIndex < 0) {
return;
}
_checkMaxCol(colIndex);
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 (colIndex <= endColumn) {
_Span newSpanObj = _Span();
if (colIndex <= startColumn) {
startColumn += 1;
}
endColumn += 1;
newSpanObj._start = [startRow, startColumn];
newSpanObj._end = [endRow, 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) {
Map<int, Map<int, Data>> _data = Map<int, Map<int, Data>>();
List<int> sortedKeys = _sheetData.keys.toList()..sort();
if (colIndex <= maxCols - 1) {
/// do the shifting task
sortedKeys.forEach((rowKey) {
Map<int, Data> colMap = Map<int, Data>();
/// getting the cols keys in descending order so as to shifting becomes easy
List<int> sortedColKeys = _sheetData[rowKey]!.keys.toList()
..sort((a, b) {
return b.compareTo(a);
});
sortedColKeys.forEach((colKey) {
if (_sheetData[rowKey] != null &&
_sheetData[rowKey]![colKey] != null) {
if (colKey < colIndex) {
colMap[colKey] = _sheetData[rowKey]![colKey]!;
}
if (colIndex <= colKey) {
colMap[colKey + 1] = _sheetData[rowKey]![colKey]!;
}
}
});
colMap[colIndex] = Data.newData(this, rowKey, colIndex);
_data[rowKey] = Map<int, Data>.from(colMap);
});
_sheetData = Map<int, Map<int, Data>>.from(_data);
} else {
/// just put the data in the very first available row and
/// in the desired Column index only one time as we will be using less space on internal implementatoin
/// and mock the user as if the 2-D list is being saved
///
/// As when user calls DataObject.cells then we will output 2-D list - pretending.
_sheetData[sortedKeys.first]![colIndex] =
Data.newData(this, sortedKeys.first, colIndex);
}
} else {
/// here simply just take the first row and put the columnIndex as the _sheetData was previously null
_sheetData = Map<int, Map<int, Data>>();
_sheetData[0] = {colIndex: Data.newData(this, 0, colIndex)};
}
if (_maxCols - 1 <= colIndex) {
_maxCols += 1;
} else {
_maxCols = colIndex + 1;
}
//_countRowAndCol();
}