updateCell method
Updates the contents of sheet
of the cellIndex: CellIndex.indexByColumnRow(0, 0);
where indexing starts from 0
----or---- by cellIndex: CellIndex.indexByString("A3");
.
Styling of cell can be done by passing the CellStyle object to cellStyle
.
If sheet
does not exist then it will be automatically created.
Implementation
void updateCell(CellIndex cellIndex, CellValue? value,
{CellStyle? cellStyle}) {
int columnIndex = cellIndex.columnIndex;
int rowIndex = cellIndex.rowIndex;
if (columnIndex < 0 || rowIndex < 0) {
return;
}
_checkMaxColumn(columnIndex);
_checkMaxRow(rowIndex);
int newRowIndex = rowIndex, newColumnIndex = columnIndex;
/// Check if this is lying in merged-cell cross-section
/// If yes then get the starting position of merged cells
if (_spanList.isNotEmpty) {
(newRowIndex, newColumnIndex) = _isInsideSpanning(rowIndex, columnIndex);
}
/// Puts Data
_putData(newRowIndex, newColumnIndex, value);
// check if the numberFormat works with the value provided
// otherwise fall back to the default for this value type
if (cellStyle != null) {
final numberFormat = cellStyle.numberFormat;
if (!numberFormat.accepts(value)) {
cellStyle =
cellStyle.copyWith(numberFormat: NumFormat.defaultFor(value));
}
} else {
final cellStyleBefore =
_sheetData[cellIndex.rowIndex]?[cellIndex.columnIndex]?.cellStyle;
if (cellStyleBefore != null &&
!cellStyleBefore.numberFormat.accepts(value)) {
cellStyle =
cellStyleBefore.copyWith(numberFormat: NumFormat.defaultFor(value));
}
}
/// Puts the cellStyle
if (cellStyle != null) {
_sheetData[newRowIndex]![newColumnIndex]!._cellStyle = cellStyle;
_excel._styleChanges = true;
}
}