xlsx_protection/
xlsx_protection.rs1use std::path::{Path, PathBuf};
9
10use office_toolkit::SaveToFile;
11use office_toolkit::excel::{
12 ProtectedRange, Sheet, SheetProtection, WorkbookProtection, WriteProtection,
13};
14use office_toolkit::prelude::*;
15
16fn main() -> office_toolkit::Result<()> {
17 let path = output_path("xlsx_protection.xlsx");
18
19 let sheet_protection_sheet = Sheet::new("Sheet protection")
20 .with_row(Row::new().with_text("Locked: this sheet is protected with a password."))
21 .with_protection(SheetProtection::with_password("secret"));
22
23 let protected_range_sheet = Sheet::new("Protected range")
24 .with_row(
25 Row::new()
26 .with_text("A1 is protected; B1 stays editable via a named unprotected range."),
27 )
28 .with_protection(SheetProtection::locked())
29 .with_protected_range(ProtectedRange::new("EditableArea", "B1:B1").with_password("secret"));
30
31 let workbook = Workbook::new()
32 .with_write_protection(
33 WriteProtection::recommended()
34 .with_user_name("Author")
35 .with_password("secret"),
36 )
37 .with_sheet(sheet_protection_sheet)
38 .with_sheet(protected_range_sheet);
39
40 let workbook = Workbook {
44 protection: Some(WorkbookProtection::locked().with_lock_windows(true)),
45 ..workbook
46 };
47
48 workbook.save_to_file(&path)?;
49 println!("Wrote {}", path.display());
50 Ok(())
51}
52
53fn output_path(filename: &str) -> PathBuf {
57 Path::new(env!("CARGO_MANIFEST_DIR"))
58 .join("../../tests-data/output")
59 .join(filename)
60}