pub struct WriteProtection {
pub read_only_recommended: bool,
pub user_name: Option<String>,
pub password: Option<String>,
}Expand description
Write-protection / “read-only recommended” (<fileSharing>, CT_FileSharing) — point 54, see
Workbook::write_protection’s doc comment for how this differs from WorkbookProtection.
Confirmed against sml.xsd: readOnlyRecommended defaults to false; userName is a plain
display string (shown in Excel’s own “reserved by..” prompt); reservationPassword uses the
same legacy 16-bit hash as WorkbookProtection/SheetProtection (see
writer.rs::legacy_password_hash’s doc comment) — the newer SHA-512
algorithmName/hashValue/saltValue/spinCount form is not modeled, same “one well-defined
mechanism” posture as ProtectedRange.
Fields§
§read_only_recommended: boolRecommends opening the file as read-only (readOnlyRecommended) — the checkbox behind
Excel’s Save As > Tools > General Options > “Read-only recommended”.
user_name: Option<String>The name shown in Excel’s write-reservation prompt (userName), e.g. the name of the user
who last saved with a reservation password set.
password: Option<String>The write-reservation password, in plain text — hashed into the legacy 16-bit form on write,
never recoverable on read (same one-way posture as WorkbookProtection::password). Excel
prompts for this password (or “Read Only”) when opening the file.
Implementations§
Source§impl WriteProtection
impl WriteProtection
Sourcepub fn recommended() -> WriteProtection
pub fn recommended() -> WriteProtection
Recommends opening the file as read-only, no password/user name set.
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(self, password: impl Into<String>) -> WriteProtection
pub fn with_password(self, password: impl Into<String>) -> WriteProtection
Sets the write-reservation password and returns it for chaining.
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_user_name(self, user_name: impl Into<String>) -> WriteProtection
pub fn with_user_name(self, user_name: impl Into<String>) -> WriteProtection
Sets the user name shown in the write-reservation prompt and returns it for chaining.
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 WriteProtection
impl Clone for WriteProtection
Source§fn clone(&self) -> WriteProtection
fn clone(&self) -> WriteProtection
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more