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

Sheet state not persisted when saving file

Open Aeradriel opened this issue 1 year ago • 3 comments

When opening an XLSX file with hidden sheet and saving it, it does not keep the state of hidden sheet. After investigating, the state field is not even present anymore on the workbook.xml file for any sheet. I tried to manually call this code but it does not work.

sheet.set_sheet_state("hidden".to_string());

Here is basically what I am doing:

pub struct ExcelModifier {
    book: Spreadsheet,
}

impl ExcelModifier {
    pub fn from_file(path: &str) -> Result<Self> {
        let path = Path::new(path);
        let book = xlsx::read(path)?;

        Ok(ExcelModifier { book })
    }

    pub fn save_to_file(&self, path: &str) -> Result<()> {
        let path = Path::new(path);

        write(&self.book, path)?;
        Ok(())
    }
}

fn main() {
    let mut modifier = ExcelModifier::from_file("path/to/file").unwrap();

    modifier.save_to_file("./result.xlsx").unwrap();
}

Aeradriel avatar Aug 14 '24 14:08 Aeradriel

@Aeradriel Thank you for contacting us. We will investigate the cause of the problem.

MathNya avatar Aug 16 '24 02:08 MathNya

@Aeradriel Fixed issue. Please get the latest version and check it.

To hide the sheet programmatically, do the following

    let path = std::path::Path::new("./tests/test_files/issue_217.xlsx");
    let mut book = reader::xlsx::read(path).unwrap();
    book.get_sheet_mut(&2)
        .unwrap()
        .set_state(SheetStateValues::Hidden);
    let path = std::path::Path::new("./tests/result_files/issue_217.xlsx");
    let _ = writer::xlsx::write(&book, path);

MathNya avatar Aug 23 '24 05:08 MathNya

Thanks for your quick intervention. It appears to be working on my side!

Aeradriel avatar Aug 23 '24 15:08 Aeradriel