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 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 = Map<int, Map<int, Data>>();
final List<int> sortedKeys = _sheetData.keys.toList()..sort();
if (columnIndex <= maxColumns - 1) {
/// do the shifting task
sortedKeys.forEach((rowKey) {
final Map<int, Data> columnMap = Map<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);
});
sortedColumnKeys.forEach((columnKey) {
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, rowKey, columnIndex);
_data[rowKey] = Map<int, Data>.from(columnMap);
});
_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]![columnIndex] =
Data.newData(this, sortedKeys.first, columnIndex);
}
} 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] = {columnIndex: Data.newData(this, 0, columnIndex)};
}
if (_maxColumns - 1 <= columnIndex) {
_maxColumns += 1;
} else {
_maxColumns = columnIndex + 1;
}
//_countRowsAndColumns();
}