pub struct SheetProtection {
pub password: Option<String>,
}Expand description
A sheet’s protection settings (<sheetProtection>, CT_SheetProtection). Only the legacy
16-bit password hash (password="XXXX", still fully understood by every version of Excel even
though the UI has written the newer SHA-512 hashValue/saltValue/spinCount form since Excel
2013 — see writer.rs::legacy_password_hash’s doc comment) is modeled. Every other
CT_SheetProtection flag (formatCells/insertRows/sort/ autoFilter/objects/..) is left
at Excel’s own schema default — this crate only ever writes sheet="1" and, if set, password,
matching what checking Excel’s own “Protect Sheet” dialog with no extra options touched
produces.
Fields§
§password: Option<String>The password, in plain text — hashed into the legacy 16-bit form on write; never stored or
written in plain text anywhere in the output file. None protects the sheet with no
password at all (Excel’s “Protect Sheet” with a blank password field — still blocks
accidental edits through the UI, but anyone can immediately remove the protection since
there’s nothing to check a password against).
Implementations§
Source§impl SheetProtection
impl SheetProtection
Sourcepub fn locked() -> SheetProtection
pub fn locked() -> SheetProtection
Protects the sheet with no password.
Examples found in repository?
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 // `Workbook.protection` (structure protection: locking sheet
41 // add/rename/hide/reorder/delete) has no `with_*` builder — it's set
42 // directly, like any other public field.
43 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}Sourcepub fn with_password(password: impl Into<String>) -> SheetProtection
pub fn with_password(password: impl Into<String>) -> SheetProtection
Protects the sheet with a password.
Examples found in repository?
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 // `Workbook.protection` (structure protection: locking sheet
41 // add/rename/hide/reorder/delete) has no `with_*` builder — it's set
42 // directly, like any other public field.
43 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}Trait Implementations§
Source§impl Clone for SheetProtection
impl Clone for SheetProtection
Source§fn clone(&self) -> SheetProtection
fn clone(&self) -> SheetProtection
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more