1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// protection - A module for representing worksheet protection options.
//
// SPDX-License-Identifier: MIT OR Apache-2.0
//
// Copyright 2022-2026, John McNamara, jmcnamara@cpan.org
#![warn(missing_docs)]
/// The `ProtectionOptions` struct is used to set protected elements in a worksheet.
///
/// You can specify which worksheet elements should be protected or unprotected via
/// the `ProtectionOptions` members. The corresponding Excel options with
/// their default states are shown below:
///
/// <img
/// src="https://rustxlsxwriter.github.io/images/worksheet_protect_with_options1.png">
///
/// # Examples
///
/// The following example demonstrates setting the worksheet properties to be
/// protected in a worksheet. In this case, we protect the overall
/// worksheet but allow columns and rows to be inserted.
///
/// ```
/// # // This code is available in examples/doc_worksheet_protect_with_options.rs
/// #
/// use rust_xlsxwriter::{ProtectionOptions, Workbook, XlsxError};
///
/// fn main() -> Result<(), XlsxError> {
/// let mut workbook = Workbook::new();
///
/// // Add a worksheet to the workbook.
/// let worksheet = workbook.add_worksheet();
///
/// // Set some of the options and use the defaults for everything else.
/// let options = ProtectionOptions {
/// insert_columns: true,
/// insert_rows: true,
/// ..ProtectionOptions::default()
/// };
///
/// // Set the protection options.
/// worksheet.protect_with_options(&options);
///
/// worksheet.write_string(0, 0, "Unlock the worksheet to edit the cell")?;
///
/// workbook.save("worksheet.xlsx")?;
///
/// Ok(())
/// }
/// ```
///
/// Excel dialog for the output file. Compare this with the default image above:
///
/// <img
/// src="https://rustxlsxwriter.github.io/images/worksheet_protect_with_options2.png">
///
///
///
#[derive(Clone)]
pub struct ProtectionOptions {
/// When `true` (the default), the user can select locked cells in a
/// protected worksheet.
pub select_locked_cells: bool,
/// When `true` (the default), the user can select unlocked cells in a
/// protected worksheet.
pub select_unlocked_cells: bool,
/// When `false` (the default), the user cannot format cells in a protected
/// worksheet.
pub format_cells: bool,
/// When `false` (the default), the user cannot format columns in a protected
/// worksheet.
pub format_columns: bool,
/// When `false` (the default), the user cannot format rows in a protected
/// worksheet.
pub format_rows: bool,
/// When `false` (the default), the user cannot insert new columns in a
/// protected worksheet.
pub insert_columns: bool,
/// When `false` (the default), the user cannot insert new rows in a
/// protected worksheet.
pub insert_rows: bool,
/// When `false` (the default), the user cannot insert hyperlinks/URLs in a
/// protected worksheet.
pub insert_links: bool,
/// When `false` (the default), the user cannot delete columns in a protected
/// worksheet.
pub delete_columns: bool,
/// When `false` (the default), the user cannot delete rows in a protected
/// worksheet.
pub delete_rows: bool,
/// When `false` (the default), the user cannot sort data in a protected
/// worksheet.
pub sort: bool,
/// When `false` (the default), the user cannot use autofilters in a
/// protected worksheet.
pub use_autofilter: bool,
/// When `false` (the default), the user cannot use pivot tables or pivot
/// charts in a protected worksheet.
pub use_pivot_tables: bool,
/// When `false` (the default), the user cannot edit scenarios in a protected
/// worksheet.
pub edit_scenarios: bool,
/// When `false` (the default), the user cannot edit objects such as images,
/// charts, or textboxes in a protected worksheet.
pub edit_objects: bool,
/// When `true` (the default), the user can edit the contents of a protected
/// chartsheet. Applies to chartsheets only.
pub contents: bool,
}
impl Default for ProtectionOptions {
fn default() -> Self {
Self::new()
}
}
impl ProtectionOptions {
/// Create a new [`ProtectionOptions`] object to use with the
/// [`Worksheet::protect_with_options()`](crate::Worksheet::protect_with_options) method.
///
pub fn new() -> ProtectionOptions {
ProtectionOptions {
select_locked_cells: true,
select_unlocked_cells: true,
format_cells: false,
format_columns: false,
format_rows: false,
insert_columns: false,
insert_rows: false,
insert_links: false,
delete_columns: false,
delete_rows: false,
sort: false,
use_autofilter: false,
use_pivot_tables: false,
edit_scenarios: false,
edit_objects: false,
contents: true,
}
}
}