delete method
If sheet
exist in excel.tables.keys
and excel.tables.keys.length >= 2
then it will be deleted
.
Implementation
void delete(String sheet) {
///
/// remove the sheet `name` or `key` from the below locations if they exist.
///
/// If it is not the last sheet then `delete` otherwise `return`;
if (_sheetMap.length <= 1) {
return;
}
///
///remove from _defaultSheet var also
if (_defaultSheet == sheet) {
_defaultSheet = null;
}
///
/// remove the `Sheet Object` from `_sheetMap`.
if (_sheetMap[sheet] != null) {
_sheetMap.remove(sheet);
}
///
/// remove from `_mergeChangeLook`.
if (_mergeChangeLook.contains(sheet)) {
_mergeChangeLook.remove(sheet);
}
///
/// remove from `_rtlChangeLook`.
if (_rtlChangeLook.contains(sheet)) {
_rtlChangeLook.remove(sheet);
}
///
/// remove from `_xmlSheetId`.
if (_xmlSheetId[sheet] != null) {
String sheetId1 =
"worksheets" + _xmlSheetId[sheet]!.split('worksheets')[1],
sheetId2 = _xmlSheetId[sheet]!;
_xmlFiles['xl/_rels/workbook.xml.rels']
?.rootElement
.children
.removeWhere((_sheetName) {
return _sheetName.getAttribute('Target') != null &&
_sheetName.getAttribute('Target') == sheetId1;
});
_xmlFiles['[Content_Types].xml']
?.rootElement
.children
.removeWhere((_sheetName) {
return _sheetName.getAttribute('PartName') != null &&
_sheetName.getAttribute('PartName') == '/' + sheetId2;
});
///
/// Also remove from the _xmlFiles list as we might want to create this sheet again from new starting.
if (_xmlFiles[_xmlSheetId[sheet]] != null) {
_xmlFiles.remove(_xmlSheetId[sheet]);
}
///
/// Maybe overkill and unsafe to do this, but works for now especially
/// delete or renaming default sheet name (`Sheet1`),
/// another safer method preferred
_archive = _cloneArchive(
_archive,
_xmlFiles.map((k, v) {
final encode = utf8.encode(v.toString());
final value = ArchiveFile(k, encode.length, encode);
return MapEntry(k, value);
}),
excludedFile: _xmlSheetId[sheet],
);
_xmlSheetId.remove(sheet);
}
///
/// remove from key = `sheet` from `_sheets`
if (_sheets[sheet] != null) {
///
/// Remove from `xl/workbook.xml`
///
_xmlFiles['xl/workbook.xml']
?.findAllElements('sheets')
.first
.children
.removeWhere((element) {
return element.getAttribute('name') != null &&
element.getAttribute('name').toString() == sheet;
});
_sheets.remove(sheet);
}
///
/// remove the cellStlye Referencing as it would be useless to have cellStyleReferenced saved
if (_cellStyleReferenced[sheet] != null) {
_cellStyleReferenced.remove(sheet);
}
}