A spreadsheet, also known as a workbook — is a family of the document formats, designed to work with tabular data. XLS, XLSX, ODS, CSV formats are the most common examples of such document formats, while Microsoft Excel, LibreOffice Calc, Apache OpenOffice Calc are examples of table processors — programs, which allow the creation and editing of such documents. GroupDocs.Editor has an ability to edit existing spreadsheet documents, to create new spreadsheets from scratch, and also to delete worksheets from the edited spreadsheet during saving.
Need to keep in mind that not every spreadsheet may have the worksheets. For example, text-based separator-delimited formats like CSV and TSV are basically the text files, they have no worksheets, so the worksheet cannot be removed from them. But almost all of the binary spreadsheet formats like XLS, XLSX and ODS do have them.
In GroupDocs.Editor the user edits one worksheet at a time — the spreadsheet is loaded to the constructor of the Editor class, then worksheet is specified by its number in the SpreadsheetEditOptions, and then document is converted to the editable form, represented by the EditableDocument class. Straight and simple. However, when the content of the worksheet was edited by the user and passed back to the EditableDocument class, there are two options:
Users can save the edited content of the worksheet as a new spreadsheet with this single worksheet inside. This is the default behaviour that was present in the GroupDocs.Editor from its beginning.
Users can save the edited worksheet by inserting it to the original spreadsheet. This is described in a separate article. This second option also has two sub-options:
The edited worksheet can replace the original worksheet in the input spreadsheet. For instance, in the loaded spreadsheet with 3 worksheets the user has chosen the 2nd one for edit; this worksheet was edited and then inserted back to the input spreadsheet, replacing the original 2nd worksheet onto the edited one.
The edited worksheet can be inserted into the input spreadsheet to stay together with the original one. For instance, in the loaded spreadsheet with 3 worksheets the user has chosen the 2nd one for edit; this worksheet was edited and then inserted back to the input spreadsheet, so now the spreadsheet contains 4 worksheets: the first, fourth, and two versions of the second (original and edited).
When the 2nd option (inserting edited worksheet into the original spreadsheet) is used, users also can delete particular worksheet(s) from it. The SpreadsheetSaveOptions class has a worksheet_numbers_to_delete property of list[int] type. By default it has a None value, which means that no worksheets should be removed. However, when it has one or more valid worksheet numbers, the worksheets with these numbers are removed while calling the editor.save() method.
Need to mention that the worksheet numbers in a worksheet_numbers_to_delete property are 1-based. For instance, for removing the first and fourth worksheets the worksheet_numbers_to_delete property should have a [1, 4] value.
With the worksheet removal feature the GroupDocs.Editor performs the next algorithm during saving the spreadsheet:
If the value of the worksheet_number property in the SpreadsheetSaveOptions instance is set to the default value 0, the GroupDocs.Editor generates a new spreadsheet with one worksheet. The worksheet_numbers_to_delete property is ignored regardless of its value.
If the value of the worksheet_number property in the SpreadsheetSaveOptions instance has some non-zero value, the GroupDocs.Editor treats it as a command to insert the edited worksheet into the original spreadsheet.
Depending on the insert_as_new_worksheet property, the GroupDocs.Editor replaces old worksheet, specified by the value of the worksheet_number property, on the new one, taken from the EditableDocument instance, or puts the new worksheet to be placed among existing worksheets without replacing any of them.
Finally, when all worksheet insertions and rearrangements are finished, the GroupDocs.Editor reads the value of the worksheet_numbers_to_delete property, iterates over all numbers and removes specified worksheets from the spreadsheet.
The final spreadsheet, with updated and deleted worksheets, is written to the output stream or file.
The example below shows a full roundtrip of an input XLSX file: a spreadsheet is loaded, its first worksheet is converted to the EditableDocument, then HTML-markup is emitted from the EditableDocument instance, modified, and the edited HTML-markup is converted back to another EditableDocument instance. This edited worksheet is then inserted into the original spreadsheet, while the worksheet_numbers_to_delete property is set to remove the original second worksheet during saving.
importosfromgroupdocs.editorimportEditor,EditableDocument,Licensefromgroupdocs.editor.optionsimportSpreadsheetLoadOptions,SpreadsheetEditOptions,SpreadsheetSaveOptionsfromgroupdocs.editor.formatsimportSpreadsheetFormatsdefdeleting_worksheets_from_spreadsheet():# Optionally set a licenselicense_path=os.path.abspath("./GroupDocs.Editor.lic")ifos.path.exists(license_path):License().set_license(license_path)# Load input spreadsheet to the Editor and specify loading optionswithEditor("./sample-spreadsheet.xlsx",SpreadsheetLoadOptions())aseditor:# Prepare edit options and set the 1st worksheet to editedit_options=SpreadsheetEditOptions()edit_options.worksheet_index=0# index is 0-based, so this is the 1st worksheet# Generate EditableDocument with the original content of the 1st worksheetworksheet_opened_for_edit=editor.edit(edit_options)# Get the HTML-markup from the EditableDocument with the original contentoriginal_html=worksheet_opened_for_edit.get_embedded_html()# Emulate HTML content editing in a WYSIWYG-editor in the browser or somewhere elseedited_html=original_html.replace("</body>","<p>Edited content</p></body>")# Generate EditableDocument with the edited contentworksheet_after_edit=EditableDocument.from_markup(edited_html)# Prepare save options that delete a worksheet during savingsave_options=SpreadsheetSaveOptions(SpreadsheetFormats.XLSX)# A non-zero worksheet_number is required so the spreadsheet is rebuilt and deletions are appliedsave_options.worksheet_number=1# 1-based; insert the edited worksheet at the 1st positionsave_options.insert_as_new_worksheet=True# keep the edited worksheet alongside the original onessave_options.worksheet_numbers_to_delete=[1]# delete the 1st original worksheet (1-based)# Save the spreadsheet with the deleted worksheeteditor.save(worksheet_after_edit,"./edited-spreadsheet.xlsx",save_options)worksheet_opened_for_edit.dispose()worksheet_after_edit.dispose()print("Saved spreadsheet with a deleted worksheet to edited-spreadsheet.xlsx")if__name__=="__main__":deleting_worksheets_from_spreadsheet()
sample-spreadsheet.xlsx is the sample file used in this example. Click here to download it.