umya-spreadsheet icon indicating copy to clipboard operation
umya-spreadsheet copied to clipboard

remove_sheet_by_name corrupts final file

Open MarcoBelluDev opened this issue 7 months ago • 2 comments

Hi all,

I have a code looking like this to delete original default sheet1.

match book.remove_sheet_by_name("Sheet1") {
        Ok(_) => (),
        Err(e) => {
            output.push_str("Error while removing sheet called 'Sheeet1':\n");
            output.push_str(e);
            return output;
        }
    }

When I open the final file, I get the following warning from excel: Image "There was a problem with some content in 'Q4PA-2025-06-10.xlsx'. Do you want to recover the contents? If you trust the source of this workbook, click Yes."

Then, I choose to restore the file and: Image "It was possible to open the file by removing or restoring unreadable content. Restored part: Part /xl/worksheets/sheet1.xml with XML error. Loading error. Row 2, column 0."

Looking at the error log xml file I see:

<recoveryLog xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<logFileName>error293680_01.xml</logFileName>
<summary>Rilevati errori nel file 'C:\Users\AXIGO0V\Downloads\Q4PA-2025-06-10.xlsx'</summary>
<repairedParts>
<repairedPart>Parte ripristinata: Parte /xl/worksheets/sheet1.xml con errore XML. Errore di caricamento. Riga 2, colonna 0.</repairedPart>
</repairedParts>
</recoveryLog>

My guess is that the function remove_sheet_by_name() is deleting "Sheet1" from the workbook However, the reference to the associated XML file (sheet1.xml) is still present in the .xlsx package.

MarcoBelluDev avatar Jun 10 '25 13:06 MarcoBelluDev

Ok I found the error. Ticket can be closed but I will write the solution in case anyone needs. Issue was caused by the way I was creating a new sheet.

Old scripts generating the error:

fn main() {
	let mut book = new_file();
	let mut nuovo_sheet: Worksheet = Worksheet::default();
    nuovo_sheet.set_name("Sheet2");

	match book.add_sheet(nuovo_sheet) {
        Ok(_) => (),
        Err(e) => {
            println!("Error while adding new Sheet:\n{}",e);
            return;
        }
    }
	
	let path = std::path::Path::new("C:\\Users\\AXIGO0V\\Downloads\\ccc.xlsx");
	let _ = writer::xlsx::write(&book, path);
}

Correct script with no error:

fn main() {
	let mut book = new_file();
	match book.new_sheet("Sheet2") {
        Ok(_) => (),
        Err(e) => {
            println!("Error while adding new Sheet with user name:{}\n",e);
            return;
        }
    }

	let mut worksheet = book.get_sheet_by_name_mut("Sheet2").unwrap();
	worksheet.get_column_dimension_mut("A").set_width(60f64);
	
	let path = std::path::Path::new("C:\\Users\\AXIGO0V\\Downloads\\ccc.xlsx");
	let _ = writer::xlsx::write(&book, path);
}

MarcoBelluDev avatar Jun 10 '25 16:06 MarcoBelluDev

@ROMA96x Thank you for contacting us. We will review the contents and modify the program if necessary.

MathNya avatar Jun 17 '25 07:06 MathNya