Skip to main content

WriteProtection

Struct WriteProtection 

Source
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: bool

Recommends 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

Source

pub fn recommended() -> WriteProtection

Recommends opening the file as read-only, no password/user name set.

Examples found in repository?
examples/xlsx_protection.rs (line 33)
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}
Source

pub fn with_password(self, password: impl Into<String>) -> WriteProtection

Sets the write-reservation password and returns it for chaining.

Examples found in repository?
examples/xlsx_protection.rs (line 35)
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}
Source

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?
examples/xlsx_protection.rs (line 34)
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

Source§

fn clone(&self) -> WriteProtection

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for WriteProtection

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Default for WriteProtection

Source§

fn default() -> WriteProtection

Returns the “default value” for a type. Read more
Source§

impl PartialEq for WriteProtection

Source§

fn eq(&self, other: &WriteProtection) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for WriteProtection

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.