delete method

void delete(
  1. String sheet
)

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].toString().split('worksheets')[1].toString(),
        sheetId2 = _xmlSheetId[sheet]!;

    _xmlFiles['xl/_rels/workbook.xml.rels']
        ?.rootElement
        .children
        .removeWhere((_sheetName) {
      return _sheetName.getAttribute('Target') != null &&
          _sheetName.getAttribute('Target').toString() == sheetId1;
    });

    _xmlFiles['[Content_Types].xml']
        ?.rootElement
        .children
        .removeWhere((_sheetName) {
      return _sheetName.getAttribute('PartName') != null &&
          _sheetName.getAttribute('PartName').toString() == '/' + sheetId2;
    });

    ///
    /// Remove from the `_archive` also
    _archive.files.removeWhere((file) {
      return file.name.toLowerCase() ==
          _xmlSheetId[sheet].toString().toLowerCase();
    });

    ///
    /// 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]);
    }

    _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);
  }
}