Skip to main content

ProtectedRange

Struct ProtectedRange 

Source
pub struct ProtectedRange {
    pub name: String,
    pub range: String,
    pub password: Option<String>,
}
Expand description

One named, independently-passworded protected range (<protectedRange>, CT_ProtectedRange) — point 51, see Sheet::protected_ranges’s doc comment for how this differs from SheetProtection. Confirmed against sml.xsd: name/sqref are the only required attributes; password uses the same legacy 16-bit hash as SheetProtection/WorkbookProtection (see writer.rs::legacy_password_hash’s doc comment) — the newer SHA-512 algorithmName/hashValue/saltValue/spinCount form and the optional securityDescriptor child/attribute (a Windows ACL-based alternative to a password) are not modeled, same “one well-defined mechanism, not every historical Excel option” posture as SheetProtection.

Fields§

§name: String

This range’s name, shown in Excel’s “Allow Users to Edit Ranges” dialog (must be unique within the sheet, not validated here).

§range: String

The range this entry covers (sqref, e.g. "B2:D10") — like IgnoredError::range, several disjoint cells can be packed into one sqref (space-separated) if the caller wants a single named range to cover them.

§password: Option<String>

The password unlocking this range while the sheet is otherwise protected, in plain text — hashed into the legacy 16-bit form on write, never stored or written in plain text (same one-way posture as SheetProtection::password). None leaves the range editable by anyone once the sheet itself is protected (no password prompt), same as SheetProtection::password’s None case.

Implementations§

Source§

impl ProtectedRange

Source

pub fn new(name: impl Into<String>, range: impl Into<String>) -> ProtectedRange

Creates a named protected range with no password.

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

Sets a password and returns the range for chaining.

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

Source§

fn clone(&self) -> ProtectedRange

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 ProtectedRange

Source§

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

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

impl PartialEq for ProtectedRange

Source§

fn eq(&self, other: &ProtectedRange) -> 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 ProtectedRange

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.