ignore_errors/
ignore_errors.rs

1use std::collections::HashMap;
2use edit_xlsx::{Workbook, WorkbookResult, WorkSheetCol, Write};
3
4fn main() -> WorkbookResult<()> {
5    let mut workbook = Workbook::new();
6    let worksheet = workbook.get_worksheet_mut(1)?;
7
8    // Write strings that looks like numbers. This will cause an Excel warning.
9    worksheet.write_string("C2", "123".to_string())?;
10    worksheet.write_string("C3", "123".to_string())?;
11
12    // Write a divide by zero formula. This will also cause an Excel warning.
13    worksheet.write_formula("C5", "=1/0")?;
14    worksheet.write_formula("C6", "=1/0")?;
15    // In older versions of Excel, you could use the write_old_formula method:
16    // worksheet.write_old_formula("C5", "=1/0")?;
17    // worksheet.write_old_formula("C6", "=1/0")?;
18
19    // Turn off some of the warnings:
20    let mut error_map = HashMap::new();
21    error_map.insert("number_stored_as_text", "C3");
22    error_map.insert("eval_error", "C6");
23    worksheet.ignore_errors(error_map);
24
25    // Write some descriptions for the cells and make the column wider for clarity.
26    worksheet.set_columns_width("B:B", 16.0)?;
27    worksheet.write("B2", "Warning:")?;
28    worksheet.write("B3", "Warning turned off:")?;
29    worksheet.write("B5", "Warning:")?;
30    worksheet.write("B6", "Warning turned off:")?;
31
32    workbook.save_as("examples/ignore_errors.xlsx")?;
33    Ok(())
34}