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: StringThis range’s name, shown in Excel’s “Allow Users to Edit Ranges” dialog (must be unique within the sheet, not validated here).
range: StringThe 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
impl ProtectedRange
Sourcepub fn new(name: impl Into<String>, range: impl Into<String>) -> ProtectedRange
pub fn new(name: impl Into<String>, range: impl Into<String>) -> ProtectedRange
Creates a named protected range 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(self, password: impl Into<String>) -> ProtectedRange
pub fn with_password(self, password: impl Into<String>) -> ProtectedRange
Sets a password and returns the range 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 ProtectedRange
impl Clone for ProtectedRange
Source§fn clone(&self) -> ProtectedRange
fn clone(&self) -> ProtectedRange
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more