pub struct Worksheet { /* private fields */ }Implementations§
Source§impl Worksheet
impl Worksheet
Sourcepub fn write_number(
&mut self,
index: Index,
number: f64,
format: Option<&Format>,
) -> Result<()>
pub fn write_number( &mut self, index: Index, number: f64, format: Option<&Format>, ) -> Result<()>
Examples found in repository?
examples/hello.rs (line 9)
3fn main() -> karo::Result<()> {
4 let mut workbook = Workbook::new();
5
6 let worksheet = workbook.add_worksheet(None)?;
7
8 worksheet.write_string(index(0, 0)?, "Hello", None)?;
9 worksheet.write_number(index(1, 0)?, 123.0, None).unwrap();
10
11 workbook.write_file("hello_world.xlsx")?;
12
13 Ok(())
14}More examples
examples/data_validate.rs (line 20)
10fn write_data(w: &mut Worksheet, format: Option<&Format>) -> Result<()> {
11 w.write_string(
12 cell("A1")?,
13 "Some examples of data validation in karo",
14 format,
15 )?;
16 w.write_string(cell("B1")?, "Enter values in this column", format)?;
17 w.write_string(cell("D1")?, "Sample data", format)?;
18
19 w.write_string(cell("D3")?, "Integers", None)?;
20 w.write_number(cell("E3")?, 1f64, None)?;
21 w.write_number(cell("F3")?, 10f64, None)?;
22
23 w.write_string(cell("D4")?, "List data", None)?;
24 w.write_string(cell("E4")?, "open", None)?;
25 w.write_string(cell("F4")?, "high", None)?;
26 w.write_string(cell("G4")?, "close", None)?;
27
28 w.write_string(cell("D5")?, "Formula", None)?;
29 w.write_formula(cell("E5")?, "=AND(F5=50,G5=60)", None)?;
30 w.write_number(cell("F5")?, 50f64, None)?;
31 w.write_number(cell("G5")?, 60f64, None)?;
32
33 Ok(())
34}examples/tutorial1.rs (line 40)
10fn main() -> karo::Result<()> {
11 let expenses = [
12 Expense {
13 item: "Rent",
14 cost: 1000f64,
15 },
16 Expense {
17 item: "Gas",
18 cost: 100f64,
19 },
20 Expense {
21 item: "Food",
22 cost: 300f64,
23 },
24 Expense {
25 item: "Gym",
26 cost: 50f64,
27 },
28 ];
29
30 // Create a new workbook.
31 let mut workbook = Workbook::new();
32
33 {
34 // Add a worksheet with a user defined name.
35 let worksheet = workbook.add_worksheet(None)?;
36
37 let mut row = 0u32;
38 for Expense { item, cost } in expenses.iter() {
39 worksheet.write_string(index(row, 0)?, item, None)?;
40 worksheet.write_number(index(row, 1)?, *cost, None)?;
41 row += 1;
42 }
43
44 worksheet.write_string(index(row, 0)?, "Total", None)?;
45 worksheet.write_formula(index(row, 1)?, "=SUM(B1:B4)", None)?;
46 }
47
48 workbook.write_file("tutorial01.xlsx")?;
49
50 Ok(())
51}examples/tutorial2.rs (line 53)
10fn main() -> karo::Result<()> {
11 let expenses = [
12 Expense {
13 item: "Rent",
14 cost: 1000f64,
15 },
16 Expense {
17 item: "Gas",
18 cost: 100f64,
19 },
20 Expense {
21 item: "Food",
22 cost: 300f64,
23 },
24 Expense {
25 item: "Gym",
26 cost: 50f64,
27 },
28 ];
29
30 // Create a new workbook.
31 let mut workbook = Workbook::new();
32
33 {
34 // Add a worksheet with a user defined name.
35 let worksheet = workbook.add_worksheet(None)?;
36
37 // Add a bold format to use to highlight cells.
38 let mut bold = Format::default();
39 bold.font.bold = true;
40
41 // Add a number format for cells with money.
42 let mut money = Format::default();
43 money.num_format = NumFormat::from_format_string("$#,##0");
44
45 let mut row = 0u32;
46
47 worksheet.write_string(index(row, 0)?, "Item", Some(&bold))?;
48 worksheet.write_string(index(row, 1)?, "Cost", Some(&bold))?;
49 row += 1;
50
51 for Expense { item, cost } in expenses.iter() {
52 worksheet.write_string(index(row, 0)?, item, None)?;
53 worksheet.write_number(index(row, 1)?, *cost, Some(&money))?;
54 row += 1;
55 }
56
57 worksheet.write_string(index(row, 0)?, "Total", Some(&bold))?;
58 worksheet.write_formula(
59 index(row, 1)?,
60 "=SUM(B2:B5)",
61 Some(&money),
62 )?;
63 }
64
65 workbook.write_file("tutorial02.xlsx")?;
66
67 Ok(())
68}examples/anatomy.rs (line 41)
5fn main() -> karo::Result<()> {
6 // Create a new workbook.
7 let mut workbook = Workbook::new();
8
9 // Add some cell formats.
10 let mut myformat1 = Format::default();
11 let mut myformat2 = Format::default();
12
13 // Set the bold property for the first format.
14 myformat1.font.bold = true;
15
16 // Set a number format for the second format.
17 myformat2.num_format = NumFormat::Custom("$#,##0.00".to_string());
18
19 {
20 // Add a worksheet with a user defined name.
21 let worksheet1 = workbook.add_worksheet(Some("Demo"))?;
22
23 // Widen the first column to make the text clearer.
24 worksheet1.set_column(col_range(0, 0)?, 20.0, None)?;
25
26 // Write some unformatted data.
27 worksheet1.write_string(index(0, 0)?, "Peach", None)?;
28 worksheet1.write_string(index(1, 0)?, "Plum", None)?;
29
30 // Write formatted data.
31 worksheet1.write_string(index(2, 0)?, "Pear", Some(&myformat1))?;
32
33 // Formats can be reused.
34 worksheet1.write_string(
35 index(3, 0)?,
36 "Persimmon",
37 Some(&myformat1),
38 )?;
39
40 // Write some numbers.
41 worksheet1.write_number(index(5, 0)?, 123.0, None)?;
42 worksheet1.write_number(
43 index(6, 0)?,
44 4567.555,
45 Some(&myformat2),
46 )?;
47 }
48
49 {
50 let worksheet2 = workbook.add_worksheet(None)?;
51
52 worksheet2.write_string(
53 index(0, 0)?,
54 "Some text",
55 Some(&myformat1),
56 )?;
57 }
58
59 workbook.write_file("anatomy.xlsx")?;
60
61 Ok(())
62}examples/tutorial3.rs (line 73)
12fn main() -> karo::Result<()> {
13 let expenses = [
14 Expense {
15 item: "Rent",
16 cost: 1000f64,
17 datetime: Utc.ymd(2013, 1, 13).and_hms(0, 0, 0),
18 },
19 Expense {
20 item: "Gas",
21 cost: 100f64,
22 datetime: Utc.ymd(2013, 1, 14).and_hms(0, 0, 0),
23 },
24 Expense {
25 item: "Food",
26 cost: 300f64,
27 datetime: Utc.ymd(2013, 1, 16).and_hms(0, 0, 0),
28 },
29 Expense {
30 item: "Gym",
31 cost: 50f64,
32 datetime: Utc.ymd(2013, 1, 20).and_hms(0, 0, 0),
33 },
34 ];
35
36 // Create a new workbook.
37 let mut workbook = Workbook::new();
38
39 {
40 // Add a worksheet with a user defined name.
41 let worksheet = workbook.add_worksheet(None)?;
42
43 // Add a bold format to use to highlight cells.
44 let mut bold = Format::default();
45 bold.font.bold = true;
46
47 // Add a number format for cells with money.
48 let mut money = Format::default();
49 money.num_format = NumFormat::from_format_string("$#,##0");
50
51 // Add a number format for cells with money.
52 let mut date = Format::default();
53 date.num_format = NumFormat::from_format_string("mmmm d yyyy");
54
55 let mut row = 0u32;
56
57 worksheet.write_string(index(row, 0)?, "Item", Some(&bold))?;
58 worksheet.write_string(index(row, 1)?, "Cost", Some(&bold))?;
59 row += 1;
60
61 for Expense {
62 item,
63 cost,
64 datetime,
65 } in expenses.iter()
66 {
67 worksheet.write_string(index(row, 0)?, item, None)?;
68 worksheet.write_datetime(
69 index(row, 1)?,
70 *datetime,
71 Some(&date),
72 )?;
73 worksheet.write_number(index(row, 2)?, *cost, Some(&money))?;
74 row += 1;
75 }
76
77 worksheet.write_string(index(row, 0)?, "Total", Some(&bold))?;
78 worksheet.write_formula(
79 index(row, 2)?,
80 "=SUM(C2:C5)",
81 Some(&money),
82 )?;
83 }
84
85 workbook.write_file("tutorial03.xlsx")?;
86
87 Ok(())
88}Additional examples can be found in:
Sourcepub fn write_string<S: AsRef<str>>(
&mut self,
index: Index,
string: S,
format: Option<&Format>,
) -> Result<()>
pub fn write_string<S: AsRef<str>>( &mut self, index: Index, string: S, format: Option<&Format>, ) -> Result<()>
Examples found in repository?
examples/hello.rs (line 8)
3fn main() -> karo::Result<()> {
4 let mut workbook = Workbook::new();
5
6 let worksheet = workbook.add_worksheet(None)?;
7
8 worksheet.write_string(index(0, 0)?, "Hello", None)?;
9 worksheet.write_number(index(1, 0)?, 123.0, None).unwrap();
10
11 workbook.write_file("hello_world.xlsx")?;
12
13 Ok(())
14}More examples
examples/utf8.rs (lines 8-12)
3fn main() -> karo::Result<()> {
4 let mut workbook = Workbook::new();
5
6 {
7 let worksheet = workbook.add_worksheet(None)?;
8 worksheet.write_string(
9 index(2, 1)?,
10 "Это фраза на русском!",
11 None,
12 )?;
13 }
14
15 workbook.write_file("utf8.xlsx")?;
16
17 Ok(())
18}examples/tab_colors.rs (line 25)
4fn main() -> karo::Result<()> {
5 let mut workbook = Workbook::new();
6
7 {
8 let worksheet1 = workbook.add_worksheet(None)?;
9 worksheet1.set_tab_color(colors::RED);
10 }
11 {
12 let worksheet2 = workbook.add_worksheet(None)?;
13 worksheet2.set_tab_color(colors::GREEN);
14 }
15 {
16 let worksheet3 = workbook.add_worksheet(None)?;
17 worksheet3.set_tab_color(RGB8 {
18 r: 0xff,
19 g: 0x99,
20 b: 0x00,
21 });
22 }
23 {
24 let worksheet4 = workbook.add_worksheet(None)?;
25 worksheet4.write_string(index(0, 0)?, "Hello", None)?;
26 }
27
28 workbook.write_file("tab_colors.xlsx")?;
29
30 Ok(())
31}examples/data_validate.rs (lines 11-15)
10fn write_data(w: &mut Worksheet, format: Option<&Format>) -> Result<()> {
11 w.write_string(
12 cell("A1")?,
13 "Some examples of data validation in karo",
14 format,
15 )?;
16 w.write_string(cell("B1")?, "Enter values in this column", format)?;
17 w.write_string(cell("D1")?, "Sample data", format)?;
18
19 w.write_string(cell("D3")?, "Integers", None)?;
20 w.write_number(cell("E3")?, 1f64, None)?;
21 w.write_number(cell("F3")?, 10f64, None)?;
22
23 w.write_string(cell("D4")?, "List data", None)?;
24 w.write_string(cell("E4")?, "open", None)?;
25 w.write_string(cell("F4")?, "high", None)?;
26 w.write_string(cell("G4")?, "close", None)?;
27
28 w.write_string(cell("D5")?, "Formula", None)?;
29 w.write_formula(cell("E5")?, "=AND(F5=50,G5=60)", None)?;
30 w.write_number(cell("F5")?, 50f64, None)?;
31 w.write_number(cell("G5")?, 60f64, None)?;
32
33 Ok(())
34}
35
36fn main() -> karo::Result<()> {
37 let mut workbook = Workbook::new();
38
39 let mut format = Format::default();
40 format.borders.set_around(Some(Border {
41 style: BorderStyle::Thin,
42 color: None,
43 }));
44 format.fill.fg_color = Some(RGB8 {
45 r: 0xc6,
46 g: 0xef,
47 b: 0xce,
48 });
49 format.fill.pattern = Some(Pattern::Solid);
50 format.font.bold = true;
51 format.text_wrap = true;
52 format.vertical_alignment = Some(VerticalAlignment::Center);
53 format.indent = 1;
54
55 {
56 let worksheet = workbook.add_worksheet(None)?;
57 write_data(worksheet, Some(&format))?;
58
59 // Set up layout of the worksheet.
60 worksheet.set_column(col_range(0, 0)?, 55f64, None)?;
61 worksheet.set_column(col_range(1, 1)?, 15f64, None)?;
62 worksheet.set_column(col_range(3, 3)?, 15f64, None)?;
63 worksheet.set_row(row_range(0, 0)?, 36f64, None)?;
64
65 // Example 1. Limiting input to an integer in a fixed range.
66 worksheet.write_string(
67 cell("A3")?,
68 "Enter an integer between 1 and 10",
69 None,
70 )?;
71 let validation = Validation::new(
72 ValidationType::Integer(Criterion::Between(1, 10)),
73 cell("B3")?,
74 );
75 worksheet.data_validation(validation)?;
76
77 // Example 2. Limiting input to an integer outside a fixed range.
78 worksheet.write_string(
79 cell("A5")?,
80 "Enter an integer not between 1 and 10 \
81 (using cell references)",
82 None,
83 )?;
84 let validation = Validation::new(
85 ValidationType::IntegerFormula(Criterion::NotBetween(
86 "=E3".to_string(),
87 "=F3".to_string(),
88 )),
89 cell("B5")?,
90 );
91 worksheet.data_validation(validation)?;
92
93 // Example 3. Limiting input to an integer greater than a fixed value.
94 worksheet.write_string(
95 cell("A7")?,
96 "Enter an integer greater than 0",
97 None,
98 )?;
99 let validation = Validation::new(
100 ValidationType::Integer(Criterion::GreaterThan(0)),
101 cell("B7")?,
102 );
103 worksheet.data_validation(validation)?;
104
105 // Example 4. Limiting input to an integer less than a fixed value.
106 worksheet.write_string(
107 cell("A9")?,
108 "Enter an integer less than 10",
109 None,
110 )?;
111 let validation = Validation::new(
112 ValidationType::Integer(Criterion::LessThan(10)),
113 cell("B9")?,
114 );
115 worksheet.data_validation(validation)?;
116
117 // Example 5. Limiting input to a decimal in a fixed range.
118 worksheet.write_string(
119 cell("A11")?,
120 "Enter a decimal between 0.1 and 0.5",
121 None,
122 )?;
123 let validation = Validation::new(
124 ValidationType::Decimal(Criterion::Between(0.1, 0.5)),
125 cell("B11")?,
126 );
127 worksheet.data_validation(validation)?;
128
129 // Example 6. Limiting input to a value in dropdown list.
130 worksheet.write_string(
131 cell("A13")?,
132 "Select a value from a drop down list",
133 None,
134 )?;
135 let validation = Validation::new(
136 ValidationType::List {
137 values: indexset! {
138 "open".to_string(),
139 "high".to_string(),
140 "close".to_string(),
141 },
142 show_dropdown: true,
143 },
144 cell("B13")?,
145 );
146 worksheet.data_validation(validation)?;
147
148 // Example 7. Limiting input to a value in a dropdown list.
149 worksheet.write_string(
150 cell("A15")?,
151 "Select a value from a drop down list (using a cell range)",
152 None,
153 )?;
154 let validation = Validation::new(
155 ValidationType::ListFormula {
156 formula: "=$E$4:$G$4".to_string(),
157 show_dropdown: true,
158 },
159 cell("B15")?,
160 );
161 worksheet.data_validation(validation)?;
162
163 // Example 8. Limiting input to date in a fixed range.
164 worksheet.write_string(
165 cell("A17")?,
166 "Enter a date between 1/1/2008 and 12/12/2008",
167 None,
168 )?;
169 let date1 = Utc.ymd(2008, 1, 1).and_hms(0, 0, 0);
170 let date2 = Utc.ymd(2008, 12, 12).and_hms(0, 0, 0);
171 let validation = Validation::new(
172 ValidationType::Date(Criterion::Between(date1, date2)),
173 cell("B17")?,
174 );
175 worksheet.data_validation(validation)?;
176
177 // Example 9. Limiting input to time in a fixed range.
178 worksheet.write_string(
179 cell("A19")?,
180 "Enter a time between 6:00 and 12:00",
181 None,
182 )?;
183 let time1 = NaiveTime::from_hms(6, 0, 0);
184 let time2 = NaiveTime::from_hms(12, 0, 0);
185 let validation = Validation::new(
186 ValidationType::Time(Criterion::Between(time1, time2)),
187 cell("B19")?,
188 );
189 worksheet.data_validation(validation)?;
190
191 // Example 10. Limiting input to a string greater than a fixed length.
192 worksheet.write_string(
193 cell("A21")?,
194 "Enter a string longer than 3 characters",
195 None,
196 )?;
197 let validation = Validation::new(
198 ValidationType::Length(Criterion::GreaterThan(3)),
199 cell("B21")?,
200 );
201 worksheet.data_validation(validation)?;
202
203 // Example 11. Limiting input based on a formula.
204 worksheet.write_string(
205 cell("A23")?,
206 "Enter a value if the folowing is true \"=AND(F5=50,G5=60)\"",
207 None,
208 )?;
209 let validation = Validation::new(
210 ValidationType::CustomFormula("=AND(F5=50,G5=60)".to_string()),
211 cell("B23")?,
212 );
213 worksheet.data_validation(validation)?;
214
215 // Example 12. Displaying and modifying data validation messages.
216 worksheet.write_string(
217 cell("A25")?,
218 "Displays a message when you select the cell",
219 None,
220 )?;
221 let mut validation = Validation::new(
222 ValidationType::Integer(Criterion::Between(1, 100)),
223 cell("B25")?,
224 );
225 validation.input_title = Some("Enter an integer:".to_string());
226 validation.input_message = Some("between 1 and 100".to_string());
227 worksheet.data_validation(validation)?;
228
229 // Example 13. Displaying and modifying data validation messages.
230 worksheet.write_string(
231 cell("A27")?,
232 "Displays a custom error message when integer isn't \
233 between 1 and 100",
234 None,
235 )?;
236 let mut validation = Validation::new(
237 ValidationType::Integer(Criterion::Between(1, 100)),
238 cell("B27")?,
239 );
240 validation.input_title = Some("Enter an integer:".to_string());
241 validation.input_message = Some("between 1 and 100".to_string());
242 validation.error_title =
243 Some("Input value is not valid!".to_string());
244 validation.error_message = Some(
245 "It should be an integer \
246 between 1 and 100"
247 .to_string(),
248 );
249 worksheet.data_validation(validation)?;
250
251 // Example 14. Displaying and modifying data validation messages.
252 worksheet.write_string(
253 cell("A29")?,
254 "Displays a custom info message when integer isn't \
255 between 1 and 100",
256 None,
257 )?;
258 let mut validation = Validation::new(
259 ValidationType::Integer(Criterion::Between(1, 100)),
260 cell("B29")?,
261 );
262 validation.input_title = Some("Enter an integer:".to_string());
263 validation.input_message = Some("between 1 and 100".to_string());
264 validation.error_title =
265 Some("Input value is not valid!".to_string());
266 validation.error_message = Some(
267 "It should be an integer \
268 between 1 and 100"
269 .to_string(),
270 );
271 validation.error_type = ValidationErrorType::Information;
272 worksheet.data_validation(validation)?;
273 }
274
275 workbook.write_file("data_validate.xlsx")?;
276
277 Ok(())
278}examples/tutorial1.rs (line 39)
10fn main() -> karo::Result<()> {
11 let expenses = [
12 Expense {
13 item: "Rent",
14 cost: 1000f64,
15 },
16 Expense {
17 item: "Gas",
18 cost: 100f64,
19 },
20 Expense {
21 item: "Food",
22 cost: 300f64,
23 },
24 Expense {
25 item: "Gym",
26 cost: 50f64,
27 },
28 ];
29
30 // Create a new workbook.
31 let mut workbook = Workbook::new();
32
33 {
34 // Add a worksheet with a user defined name.
35 let worksheet = workbook.add_worksheet(None)?;
36
37 let mut row = 0u32;
38 for Expense { item, cost } in expenses.iter() {
39 worksheet.write_string(index(row, 0)?, item, None)?;
40 worksheet.write_number(index(row, 1)?, *cost, None)?;
41 row += 1;
42 }
43
44 worksheet.write_string(index(row, 0)?, "Total", None)?;
45 worksheet.write_formula(index(row, 1)?, "=SUM(B1:B4)", None)?;
46 }
47
48 workbook.write_file("tutorial01.xlsx")?;
49
50 Ok(())
51}examples/doc_custom_properties.rs (lines 25-29)
6fn main() -> karo::Result<()> {
7 // Create a new workbook.
8 let mut workbook = Workbook::new();
9
10 let datetime = Utc.ymd(2016, 12, 12).and_hms(0, 0, 0);
11 workbook.set_custom_property_str("Checked by", "Eve")?;
12 workbook.set_custom_property_datetime("Date completed", datetime)?;
13 workbook.set_custom_property_integer("Document number", 12345)?;
14 workbook.set_custom_property_number("Reference number", 1.2345)?;
15 workbook.set_custom_property_boolean("Has review", true)?;
16 workbook.set_custom_property_boolean("Signed off", false)?;
17
18 {
19 // Add a worksheet with a user defined name.
20 let worksheet = workbook.add_worksheet(None)?;
21
22 // Widen the first column to make the text clearer.
23 worksheet.set_column(col_range(0, 0)?, 50f64, None)?;
24
25 worksheet.write_string(
26 index(0, 0)?,
27 "Select 'Workbook Properties' to see properties.",
28 None,
29 )?;
30 }
31
32 workbook.write_file("doc_custom_properties.xlsx")?;
33
34 Ok(())
35}Additional examples can be found in:
Sourcepub fn write_formula<S: Into<String>>(
&mut self,
index: Index,
formula: S,
format: Option<&Format>,
) -> Result<()>
pub fn write_formula<S: Into<String>>( &mut self, index: Index, formula: S, format: Option<&Format>, ) -> Result<()>
Examples found in repository?
examples/data_validate.rs (line 29)
10fn write_data(w: &mut Worksheet, format: Option<&Format>) -> Result<()> {
11 w.write_string(
12 cell("A1")?,
13 "Some examples of data validation in karo",
14 format,
15 )?;
16 w.write_string(cell("B1")?, "Enter values in this column", format)?;
17 w.write_string(cell("D1")?, "Sample data", format)?;
18
19 w.write_string(cell("D3")?, "Integers", None)?;
20 w.write_number(cell("E3")?, 1f64, None)?;
21 w.write_number(cell("F3")?, 10f64, None)?;
22
23 w.write_string(cell("D4")?, "List data", None)?;
24 w.write_string(cell("E4")?, "open", None)?;
25 w.write_string(cell("F4")?, "high", None)?;
26 w.write_string(cell("G4")?, "close", None)?;
27
28 w.write_string(cell("D5")?, "Formula", None)?;
29 w.write_formula(cell("E5")?, "=AND(F5=50,G5=60)", None)?;
30 w.write_number(cell("F5")?, 50f64, None)?;
31 w.write_number(cell("G5")?, 60f64, None)?;
32
33 Ok(())
34}More examples
examples/tutorial1.rs (line 45)
10fn main() -> karo::Result<()> {
11 let expenses = [
12 Expense {
13 item: "Rent",
14 cost: 1000f64,
15 },
16 Expense {
17 item: "Gas",
18 cost: 100f64,
19 },
20 Expense {
21 item: "Food",
22 cost: 300f64,
23 },
24 Expense {
25 item: "Gym",
26 cost: 50f64,
27 },
28 ];
29
30 // Create a new workbook.
31 let mut workbook = Workbook::new();
32
33 {
34 // Add a worksheet with a user defined name.
35 let worksheet = workbook.add_worksheet(None)?;
36
37 let mut row = 0u32;
38 for Expense { item, cost } in expenses.iter() {
39 worksheet.write_string(index(row, 0)?, item, None)?;
40 worksheet.write_number(index(row, 1)?, *cost, None)?;
41 row += 1;
42 }
43
44 worksheet.write_string(index(row, 0)?, "Total", None)?;
45 worksheet.write_formula(index(row, 1)?, "=SUM(B1:B4)", None)?;
46 }
47
48 workbook.write_file("tutorial01.xlsx")?;
49
50 Ok(())
51}examples/tutorial2.rs (lines 58-62)
10fn main() -> karo::Result<()> {
11 let expenses = [
12 Expense {
13 item: "Rent",
14 cost: 1000f64,
15 },
16 Expense {
17 item: "Gas",
18 cost: 100f64,
19 },
20 Expense {
21 item: "Food",
22 cost: 300f64,
23 },
24 Expense {
25 item: "Gym",
26 cost: 50f64,
27 },
28 ];
29
30 // Create a new workbook.
31 let mut workbook = Workbook::new();
32
33 {
34 // Add a worksheet with a user defined name.
35 let worksheet = workbook.add_worksheet(None)?;
36
37 // Add a bold format to use to highlight cells.
38 let mut bold = Format::default();
39 bold.font.bold = true;
40
41 // Add a number format for cells with money.
42 let mut money = Format::default();
43 money.num_format = NumFormat::from_format_string("$#,##0");
44
45 let mut row = 0u32;
46
47 worksheet.write_string(index(row, 0)?, "Item", Some(&bold))?;
48 worksheet.write_string(index(row, 1)?, "Cost", Some(&bold))?;
49 row += 1;
50
51 for Expense { item, cost } in expenses.iter() {
52 worksheet.write_string(index(row, 0)?, item, None)?;
53 worksheet.write_number(index(row, 1)?, *cost, Some(&money))?;
54 row += 1;
55 }
56
57 worksheet.write_string(index(row, 0)?, "Total", Some(&bold))?;
58 worksheet.write_formula(
59 index(row, 1)?,
60 "=SUM(B2:B5)",
61 Some(&money),
62 )?;
63 }
64
65 workbook.write_file("tutorial02.xlsx")?;
66
67 Ok(())
68}examples/tutorial3.rs (lines 78-82)
12fn main() -> karo::Result<()> {
13 let expenses = [
14 Expense {
15 item: "Rent",
16 cost: 1000f64,
17 datetime: Utc.ymd(2013, 1, 13).and_hms(0, 0, 0),
18 },
19 Expense {
20 item: "Gas",
21 cost: 100f64,
22 datetime: Utc.ymd(2013, 1, 14).and_hms(0, 0, 0),
23 },
24 Expense {
25 item: "Food",
26 cost: 300f64,
27 datetime: Utc.ymd(2013, 1, 16).and_hms(0, 0, 0),
28 },
29 Expense {
30 item: "Gym",
31 cost: 50f64,
32 datetime: Utc.ymd(2013, 1, 20).and_hms(0, 0, 0),
33 },
34 ];
35
36 // Create a new workbook.
37 let mut workbook = Workbook::new();
38
39 {
40 // Add a worksheet with a user defined name.
41 let worksheet = workbook.add_worksheet(None)?;
42
43 // Add a bold format to use to highlight cells.
44 let mut bold = Format::default();
45 bold.font.bold = true;
46
47 // Add a number format for cells with money.
48 let mut money = Format::default();
49 money.num_format = NumFormat::from_format_string("$#,##0");
50
51 // Add a number format for cells with money.
52 let mut date = Format::default();
53 date.num_format = NumFormat::from_format_string("mmmm d yyyy");
54
55 let mut row = 0u32;
56
57 worksheet.write_string(index(row, 0)?, "Item", Some(&bold))?;
58 worksheet.write_string(index(row, 1)?, "Cost", Some(&bold))?;
59 row += 1;
60
61 for Expense {
62 item,
63 cost,
64 datetime,
65 } in expenses.iter()
66 {
67 worksheet.write_string(index(row, 0)?, item, None)?;
68 worksheet.write_datetime(
69 index(row, 1)?,
70 *datetime,
71 Some(&date),
72 )?;
73 worksheet.write_number(index(row, 2)?, *cost, Some(&money))?;
74 row += 1;
75 }
76
77 worksheet.write_string(index(row, 0)?, "Total", Some(&bold))?;
78 worksheet.write_formula(
79 index(row, 2)?,
80 "=SUM(C2:C5)",
81 Some(&money),
82 )?;
83 }
84
85 workbook.write_file("tutorial03.xlsx")?;
86
87 Ok(())
88}pub fn write_array_formula<S: AsRef<str>>( &mut self, start: Index, end: Index, formula: S, format: Option<&Format>, ) -> Result<()>
pub fn write_array_formula_num<S: AsRef<str>>( &mut self, start: Index, end: Index, formula: S, format: Option<&Format>, number: f64, ) -> Result<()>
Sourcepub fn write_datetime(
&mut self,
index: Index,
datetime: DateTime<Utc>,
format: Option<&Format>,
) -> Result<()>
pub fn write_datetime( &mut self, index: Index, datetime: DateTime<Utc>, format: Option<&Format>, ) -> Result<()>
Examples found in repository?
examples/tutorial3.rs (lines 68-72)
12fn main() -> karo::Result<()> {
13 let expenses = [
14 Expense {
15 item: "Rent",
16 cost: 1000f64,
17 datetime: Utc.ymd(2013, 1, 13).and_hms(0, 0, 0),
18 },
19 Expense {
20 item: "Gas",
21 cost: 100f64,
22 datetime: Utc.ymd(2013, 1, 14).and_hms(0, 0, 0),
23 },
24 Expense {
25 item: "Food",
26 cost: 300f64,
27 datetime: Utc.ymd(2013, 1, 16).and_hms(0, 0, 0),
28 },
29 Expense {
30 item: "Gym",
31 cost: 50f64,
32 datetime: Utc.ymd(2013, 1, 20).and_hms(0, 0, 0),
33 },
34 ];
35
36 // Create a new workbook.
37 let mut workbook = Workbook::new();
38
39 {
40 // Add a worksheet with a user defined name.
41 let worksheet = workbook.add_worksheet(None)?;
42
43 // Add a bold format to use to highlight cells.
44 let mut bold = Format::default();
45 bold.font.bold = true;
46
47 // Add a number format for cells with money.
48 let mut money = Format::default();
49 money.num_format = NumFormat::from_format_string("$#,##0");
50
51 // Add a number format for cells with money.
52 let mut date = Format::default();
53 date.num_format = NumFormat::from_format_string("mmmm d yyyy");
54
55 let mut row = 0u32;
56
57 worksheet.write_string(index(row, 0)?, "Item", Some(&bold))?;
58 worksheet.write_string(index(row, 1)?, "Cost", Some(&bold))?;
59 row += 1;
60
61 for Expense {
62 item,
63 cost,
64 datetime,
65 } in expenses.iter()
66 {
67 worksheet.write_string(index(row, 0)?, item, None)?;
68 worksheet.write_datetime(
69 index(row, 1)?,
70 *datetime,
71 Some(&date),
72 )?;
73 worksheet.write_number(index(row, 2)?, *cost, Some(&money))?;
74 row += 1;
75 }
76
77 worksheet.write_string(index(row, 0)?, "Total", Some(&bold))?;
78 worksheet.write_formula(
79 index(row, 2)?,
80 "=SUM(C2:C5)",
81 Some(&money),
82 )?;
83 }
84
85 workbook.write_file("tutorial03.xlsx")?;
86
87 Ok(())
88}pub fn write_url<S: AsRef<str>>( &mut self, index: Index, url: S, format: Option<&Format>, ) -> Result<()>
pub fn write_boolean( &mut self, index: Index, value: bool, format: Option<&Format>, ) -> Result<()>
pub fn write_blank( &mut self, index: Index, format: Option<&Format>, ) -> Result<()>
pub fn write_formula_num<S: Into<String>>( &mut self, index: Index, formula: S, format: Option<&Format>, result: f64, ) -> Result<()>
pub fn write_rich_string( &mut self, index: Index, rich_string: &[RichStringTuple], format: Option<&Format>, ) -> Result<()>
Sourcepub fn set_row(
&mut self,
range: RowRange,
height: f64,
format: Option<&Format>,
) -> Result<()>
pub fn set_row( &mut self, range: RowRange, height: f64, format: Option<&Format>, ) -> Result<()>
Examples found in repository?
examples/data_validate.rs (line 63)
36fn main() -> karo::Result<()> {
37 let mut workbook = Workbook::new();
38
39 let mut format = Format::default();
40 format.borders.set_around(Some(Border {
41 style: BorderStyle::Thin,
42 color: None,
43 }));
44 format.fill.fg_color = Some(RGB8 {
45 r: 0xc6,
46 g: 0xef,
47 b: 0xce,
48 });
49 format.fill.pattern = Some(Pattern::Solid);
50 format.font.bold = true;
51 format.text_wrap = true;
52 format.vertical_alignment = Some(VerticalAlignment::Center);
53 format.indent = 1;
54
55 {
56 let worksheet = workbook.add_worksheet(None)?;
57 write_data(worksheet, Some(&format))?;
58
59 // Set up layout of the worksheet.
60 worksheet.set_column(col_range(0, 0)?, 55f64, None)?;
61 worksheet.set_column(col_range(1, 1)?, 15f64, None)?;
62 worksheet.set_column(col_range(3, 3)?, 15f64, None)?;
63 worksheet.set_row(row_range(0, 0)?, 36f64, None)?;
64
65 // Example 1. Limiting input to an integer in a fixed range.
66 worksheet.write_string(
67 cell("A3")?,
68 "Enter an integer between 1 and 10",
69 None,
70 )?;
71 let validation = Validation::new(
72 ValidationType::Integer(Criterion::Between(1, 10)),
73 cell("B3")?,
74 );
75 worksheet.data_validation(validation)?;
76
77 // Example 2. Limiting input to an integer outside a fixed range.
78 worksheet.write_string(
79 cell("A5")?,
80 "Enter an integer not between 1 and 10 \
81 (using cell references)",
82 None,
83 )?;
84 let validation = Validation::new(
85 ValidationType::IntegerFormula(Criterion::NotBetween(
86 "=E3".to_string(),
87 "=F3".to_string(),
88 )),
89 cell("B5")?,
90 );
91 worksheet.data_validation(validation)?;
92
93 // Example 3. Limiting input to an integer greater than a fixed value.
94 worksheet.write_string(
95 cell("A7")?,
96 "Enter an integer greater than 0",
97 None,
98 )?;
99 let validation = Validation::new(
100 ValidationType::Integer(Criterion::GreaterThan(0)),
101 cell("B7")?,
102 );
103 worksheet.data_validation(validation)?;
104
105 // Example 4. Limiting input to an integer less than a fixed value.
106 worksheet.write_string(
107 cell("A9")?,
108 "Enter an integer less than 10",
109 None,
110 )?;
111 let validation = Validation::new(
112 ValidationType::Integer(Criterion::LessThan(10)),
113 cell("B9")?,
114 );
115 worksheet.data_validation(validation)?;
116
117 // Example 5. Limiting input to a decimal in a fixed range.
118 worksheet.write_string(
119 cell("A11")?,
120 "Enter a decimal between 0.1 and 0.5",
121 None,
122 )?;
123 let validation = Validation::new(
124 ValidationType::Decimal(Criterion::Between(0.1, 0.5)),
125 cell("B11")?,
126 );
127 worksheet.data_validation(validation)?;
128
129 // Example 6. Limiting input to a value in dropdown list.
130 worksheet.write_string(
131 cell("A13")?,
132 "Select a value from a drop down list",
133 None,
134 )?;
135 let validation = Validation::new(
136 ValidationType::List {
137 values: indexset! {
138 "open".to_string(),
139 "high".to_string(),
140 "close".to_string(),
141 },
142 show_dropdown: true,
143 },
144 cell("B13")?,
145 );
146 worksheet.data_validation(validation)?;
147
148 // Example 7. Limiting input to a value in a dropdown list.
149 worksheet.write_string(
150 cell("A15")?,
151 "Select a value from a drop down list (using a cell range)",
152 None,
153 )?;
154 let validation = Validation::new(
155 ValidationType::ListFormula {
156 formula: "=$E$4:$G$4".to_string(),
157 show_dropdown: true,
158 },
159 cell("B15")?,
160 );
161 worksheet.data_validation(validation)?;
162
163 // Example 8. Limiting input to date in a fixed range.
164 worksheet.write_string(
165 cell("A17")?,
166 "Enter a date between 1/1/2008 and 12/12/2008",
167 None,
168 )?;
169 let date1 = Utc.ymd(2008, 1, 1).and_hms(0, 0, 0);
170 let date2 = Utc.ymd(2008, 12, 12).and_hms(0, 0, 0);
171 let validation = Validation::new(
172 ValidationType::Date(Criterion::Between(date1, date2)),
173 cell("B17")?,
174 );
175 worksheet.data_validation(validation)?;
176
177 // Example 9. Limiting input to time in a fixed range.
178 worksheet.write_string(
179 cell("A19")?,
180 "Enter a time between 6:00 and 12:00",
181 None,
182 )?;
183 let time1 = NaiveTime::from_hms(6, 0, 0);
184 let time2 = NaiveTime::from_hms(12, 0, 0);
185 let validation = Validation::new(
186 ValidationType::Time(Criterion::Between(time1, time2)),
187 cell("B19")?,
188 );
189 worksheet.data_validation(validation)?;
190
191 // Example 10. Limiting input to a string greater than a fixed length.
192 worksheet.write_string(
193 cell("A21")?,
194 "Enter a string longer than 3 characters",
195 None,
196 )?;
197 let validation = Validation::new(
198 ValidationType::Length(Criterion::GreaterThan(3)),
199 cell("B21")?,
200 );
201 worksheet.data_validation(validation)?;
202
203 // Example 11. Limiting input based on a formula.
204 worksheet.write_string(
205 cell("A23")?,
206 "Enter a value if the folowing is true \"=AND(F5=50,G5=60)\"",
207 None,
208 )?;
209 let validation = Validation::new(
210 ValidationType::CustomFormula("=AND(F5=50,G5=60)".to_string()),
211 cell("B23")?,
212 );
213 worksheet.data_validation(validation)?;
214
215 // Example 12. Displaying and modifying data validation messages.
216 worksheet.write_string(
217 cell("A25")?,
218 "Displays a message when you select the cell",
219 None,
220 )?;
221 let mut validation = Validation::new(
222 ValidationType::Integer(Criterion::Between(1, 100)),
223 cell("B25")?,
224 );
225 validation.input_title = Some("Enter an integer:".to_string());
226 validation.input_message = Some("between 1 and 100".to_string());
227 worksheet.data_validation(validation)?;
228
229 // Example 13. Displaying and modifying data validation messages.
230 worksheet.write_string(
231 cell("A27")?,
232 "Displays a custom error message when integer isn't \
233 between 1 and 100",
234 None,
235 )?;
236 let mut validation = Validation::new(
237 ValidationType::Integer(Criterion::Between(1, 100)),
238 cell("B27")?,
239 );
240 validation.input_title = Some("Enter an integer:".to_string());
241 validation.input_message = Some("between 1 and 100".to_string());
242 validation.error_title =
243 Some("Input value is not valid!".to_string());
244 validation.error_message = Some(
245 "It should be an integer \
246 between 1 and 100"
247 .to_string(),
248 );
249 worksheet.data_validation(validation)?;
250
251 // Example 14. Displaying and modifying data validation messages.
252 worksheet.write_string(
253 cell("A29")?,
254 "Displays a custom info message when integer isn't \
255 between 1 and 100",
256 None,
257 )?;
258 let mut validation = Validation::new(
259 ValidationType::Integer(Criterion::Between(1, 100)),
260 cell("B29")?,
261 );
262 validation.input_title = Some("Enter an integer:".to_string());
263 validation.input_message = Some("between 1 and 100".to_string());
264 validation.error_title =
265 Some("Input value is not valid!".to_string());
266 validation.error_message = Some(
267 "It should be an integer \
268 between 1 and 100"
269 .to_string(),
270 );
271 validation.error_type = ValidationErrorType::Information;
272 worksheet.data_validation(validation)?;
273 }
274
275 workbook.write_file("data_validate.xlsx")?;
276
277 Ok(())
278}pub fn set_row_options( &mut self, range: RowRange, height: f64, format: Option<&Format>, options: RowColOptions, ) -> Result<()>
Sourcepub fn set_column(
&mut self,
range: ColRange,
width: f64,
format: Option<&Format>,
) -> Result<()>
pub fn set_column( &mut self, range: ColRange, width: f64, format: Option<&Format>, ) -> Result<()>
Examples found in repository?
examples/doc_custom_properties.rs (line 23)
6fn main() -> karo::Result<()> {
7 // Create a new workbook.
8 let mut workbook = Workbook::new();
9
10 let datetime = Utc.ymd(2016, 12, 12).and_hms(0, 0, 0);
11 workbook.set_custom_property_str("Checked by", "Eve")?;
12 workbook.set_custom_property_datetime("Date completed", datetime)?;
13 workbook.set_custom_property_integer("Document number", 12345)?;
14 workbook.set_custom_property_number("Reference number", 1.2345)?;
15 workbook.set_custom_property_boolean("Has review", true)?;
16 workbook.set_custom_property_boolean("Signed off", false)?;
17
18 {
19 // Add a worksheet with a user defined name.
20 let worksheet = workbook.add_worksheet(None)?;
21
22 // Widen the first column to make the text clearer.
23 worksheet.set_column(col_range(0, 0)?, 50f64, None)?;
24
25 worksheet.write_string(
26 index(0, 0)?,
27 "Select 'Workbook Properties' to see properties.",
28 None,
29 )?;
30 }
31
32 workbook.write_file("doc_custom_properties.xlsx")?;
33
34 Ok(())
35}More examples
examples/doc_properties.rs (line 26)
5fn main() -> karo::Result<()> {
6 // Create a new workbook.
7 let mut workbook = Workbook::new();
8
9 // This method returns an &mut DocProperties.
10 let p = workbook.properties();
11 p.title = "This is an example spreadsheet".to_string();
12 p.subject = "With document properties".to_string();
13 p.author = "Max Mustermann".to_string();
14 p.manager = "Dr. Heinz Doofenshmirtz".to_string();
15 p.company = "of Wolves".to_string();
16 p.category = "Example spreadsheets".to_string();
17 p.keywords = "Sample, Example, Properties".to_string();
18 p.comments = "Created with karo".to_string();
19 p.status = "Quo".to_string();
20
21 {
22 // Add a worksheet with a user defined name.
23 let worksheet = workbook.add_worksheet(None)?;
24
25 // Widen the first column to make the text clearer.
26 worksheet.set_column(col_range(0, 0)?, 50f64, None)?;
27
28 worksheet.write_string(
29 index(0, 0)?,
30 "Select 'Workbook Properties' to see properties.",
31 None,
32 )?;
33 }
34
35 workbook.write_file("doc_properties.xlsx")?;
36
37 Ok(())
38}examples/format_font.rs (line 28)
5fn main() -> karo::Result<()> {
6 // Create a new workbook.
7 let mut workbook = Workbook::new();
8
9 // Add some cell formats.
10 let mut myformat1 = Format::default();
11 let mut myformat2 = Format::default();
12 let mut myformat3 = Format::default();
13
14 // Set the bold property for format 1.
15 myformat1.font.bold = true;
16
17 // Set the italic property for format 2.
18 myformat2.font.italic = true;
19
20 // Set the bold and italic properties for format 3.
21 myformat3.font.bold = true;
22 myformat3.font.italic = true;
23
24 {
25 let worksheet = workbook.add_worksheet(None)?;
26
27 // Widen the first column to make the text clearer.
28 worksheet.set_column(col_range(0, 0)?, 20f64, None)?;
29
30 worksheet.write_string(
31 index(0, 0)?,
32 "This is bold",
33 Some(&myformat1),
34 )?;
35 worksheet.write_string(
36 index(1, 0)?,
37 "This is italic",
38 Some(&myformat2),
39 )?;
40 worksheet.write_string(
41 index(2, 0)?,
42 "Bold and italic",
43 Some(&myformat3),
44 )?;
45 }
46
47 workbook.write_file("format_font.xlsx")?;
48
49 Ok(())
50}examples/anatomy.rs (line 24)
5fn main() -> karo::Result<()> {
6 // Create a new workbook.
7 let mut workbook = Workbook::new();
8
9 // Add some cell formats.
10 let mut myformat1 = Format::default();
11 let mut myformat2 = Format::default();
12
13 // Set the bold property for the first format.
14 myformat1.font.bold = true;
15
16 // Set a number format for the second format.
17 myformat2.num_format = NumFormat::Custom("$#,##0.00".to_string());
18
19 {
20 // Add a worksheet with a user defined name.
21 let worksheet1 = workbook.add_worksheet(Some("Demo"))?;
22
23 // Widen the first column to make the text clearer.
24 worksheet1.set_column(col_range(0, 0)?, 20.0, None)?;
25
26 // Write some unformatted data.
27 worksheet1.write_string(index(0, 0)?, "Peach", None)?;
28 worksheet1.write_string(index(1, 0)?, "Plum", None)?;
29
30 // Write formatted data.
31 worksheet1.write_string(index(2, 0)?, "Pear", Some(&myformat1))?;
32
33 // Formats can be reused.
34 worksheet1.write_string(
35 index(3, 0)?,
36 "Persimmon",
37 Some(&myformat1),
38 )?;
39
40 // Write some numbers.
41 worksheet1.write_number(index(5, 0)?, 123.0, None)?;
42 worksheet1.write_number(
43 index(6, 0)?,
44 4567.555,
45 Some(&myformat2),
46 )?;
47 }
48
49 {
50 let worksheet2 = workbook.add_worksheet(None)?;
51
52 worksheet2.write_string(
53 index(0, 0)?,
54 "Some text",
55 Some(&myformat1),
56 )?;
57 }
58
59 workbook.write_file("anatomy.xlsx")?;
60
61 Ok(())
62}examples/format_num_format.rs (line 14)
6fn main() -> karo::Result<()> {
7 // Create a new workbook.
8 let mut workbook = Workbook::new();
9
10 {
11 let worksheet = workbook.add_worksheet(None)?;
12
13 // Widen the first column to make the text clearer.
14 worksheet.set_column(col_range(0, 0)?, 30f64, None)?;
15
16 let mut f = Format::default();
17
18 // 3.1415926
19 worksheet.write_number(index(0, 0)?, 3.1415926, None)?;
20
21 // 3.142
22 f.num_format = NumFormat::from_format_string("0.000");
23 worksheet.write_number(index(1, 0)?, 3.1415926, Some(&f))?;
24
25 // 1,235
26 f.num_format = NumFormat::from_format_string("#,##0");
27 worksheet.write_number(index(2, 0)?, 1234.56, Some(&f))?;
28
29 // 1,234.56
30 f.num_format = NumFormat::from_format_string("#,##0.00");
31 worksheet.write_number(index(3, 0)?, 1234.56, Some(&f))?;
32
33 // 49.99
34 f.num_format = NumFormat::from_format_string("0.00");
35 worksheet.write_number(index(4, 0)?, 49.99, Some(&f))?;
36
37 // 01/01/01
38 f.num_format = NumFormat::from_format_string("mm/dd/yy");
39 worksheet.write_number(index(5, 0)?, 36892.521, Some(&f))?;
40
41 // Jan 1 2001
42 f.num_format = NumFormat::from_format_string("mmm d yyyy");
43 worksheet.write_number(index(6, 0)?, 36892.521, Some(&f))?;
44
45 // 1 January 2001
46 f.num_format = NumFormat::from_format_string("d mmmm yyyy");
47 worksheet.write_number(index(7, 0)?, 36892.521, Some(&f))?;
48
49 // 01/01/2001 12:30 AM
50 f.num_format =
51 NumFormat::from_format_string("dd/mm/yyyy hh:mm AM/PM");
52 worksheet.write_number(index(8, 0)?, 36892.521, Some(&f))?;
53
54 // 1 dollar and .87 cents
55 f.num_format = NumFormat::from_format_string(
56 "0 \"dollar and\" .00 \"cents\"",
57 );
58 worksheet.write_number(index(9, 0)?, 1.87, Some(&f))?;
59
60 // Show limited conditional number formats.
61 f.num_format = NumFormat::from_format_string(
62 "[Green]General;[Red]-General;General",
63 );
64 worksheet.write_number(index(10, 0)?, 123.0, Some(&f))?; // > 0 Green
65 worksheet.write_number(index(11, 0)?, -45.0, Some(&f))?; // < 0 Red
66 worksheet.write_number(index(12, 0)?, 0.0, Some(&f))?; // = 0 Default color
67
68 // Format a Zip code
69 f.num_format = NumFormat::from_format_string("00000");
70 worksheet.write_number(index(13, 0)?, 1209.0, Some(&f))?;
71 }
72
73 workbook.write_file("format_num_format.xlsx")?;
74
75 Ok(())
76}examples/data_validate.rs (line 60)
36fn main() -> karo::Result<()> {
37 let mut workbook = Workbook::new();
38
39 let mut format = Format::default();
40 format.borders.set_around(Some(Border {
41 style: BorderStyle::Thin,
42 color: None,
43 }));
44 format.fill.fg_color = Some(RGB8 {
45 r: 0xc6,
46 g: 0xef,
47 b: 0xce,
48 });
49 format.fill.pattern = Some(Pattern::Solid);
50 format.font.bold = true;
51 format.text_wrap = true;
52 format.vertical_alignment = Some(VerticalAlignment::Center);
53 format.indent = 1;
54
55 {
56 let worksheet = workbook.add_worksheet(None)?;
57 write_data(worksheet, Some(&format))?;
58
59 // Set up layout of the worksheet.
60 worksheet.set_column(col_range(0, 0)?, 55f64, None)?;
61 worksheet.set_column(col_range(1, 1)?, 15f64, None)?;
62 worksheet.set_column(col_range(3, 3)?, 15f64, None)?;
63 worksheet.set_row(row_range(0, 0)?, 36f64, None)?;
64
65 // Example 1. Limiting input to an integer in a fixed range.
66 worksheet.write_string(
67 cell("A3")?,
68 "Enter an integer between 1 and 10",
69 None,
70 )?;
71 let validation = Validation::new(
72 ValidationType::Integer(Criterion::Between(1, 10)),
73 cell("B3")?,
74 );
75 worksheet.data_validation(validation)?;
76
77 // Example 2. Limiting input to an integer outside a fixed range.
78 worksheet.write_string(
79 cell("A5")?,
80 "Enter an integer not between 1 and 10 \
81 (using cell references)",
82 None,
83 )?;
84 let validation = Validation::new(
85 ValidationType::IntegerFormula(Criterion::NotBetween(
86 "=E3".to_string(),
87 "=F3".to_string(),
88 )),
89 cell("B5")?,
90 );
91 worksheet.data_validation(validation)?;
92
93 // Example 3. Limiting input to an integer greater than a fixed value.
94 worksheet.write_string(
95 cell("A7")?,
96 "Enter an integer greater than 0",
97 None,
98 )?;
99 let validation = Validation::new(
100 ValidationType::Integer(Criterion::GreaterThan(0)),
101 cell("B7")?,
102 );
103 worksheet.data_validation(validation)?;
104
105 // Example 4. Limiting input to an integer less than a fixed value.
106 worksheet.write_string(
107 cell("A9")?,
108 "Enter an integer less than 10",
109 None,
110 )?;
111 let validation = Validation::new(
112 ValidationType::Integer(Criterion::LessThan(10)),
113 cell("B9")?,
114 );
115 worksheet.data_validation(validation)?;
116
117 // Example 5. Limiting input to a decimal in a fixed range.
118 worksheet.write_string(
119 cell("A11")?,
120 "Enter a decimal between 0.1 and 0.5",
121 None,
122 )?;
123 let validation = Validation::new(
124 ValidationType::Decimal(Criterion::Between(0.1, 0.5)),
125 cell("B11")?,
126 );
127 worksheet.data_validation(validation)?;
128
129 // Example 6. Limiting input to a value in dropdown list.
130 worksheet.write_string(
131 cell("A13")?,
132 "Select a value from a drop down list",
133 None,
134 )?;
135 let validation = Validation::new(
136 ValidationType::List {
137 values: indexset! {
138 "open".to_string(),
139 "high".to_string(),
140 "close".to_string(),
141 },
142 show_dropdown: true,
143 },
144 cell("B13")?,
145 );
146 worksheet.data_validation(validation)?;
147
148 // Example 7. Limiting input to a value in a dropdown list.
149 worksheet.write_string(
150 cell("A15")?,
151 "Select a value from a drop down list (using a cell range)",
152 None,
153 )?;
154 let validation = Validation::new(
155 ValidationType::ListFormula {
156 formula: "=$E$4:$G$4".to_string(),
157 show_dropdown: true,
158 },
159 cell("B15")?,
160 );
161 worksheet.data_validation(validation)?;
162
163 // Example 8. Limiting input to date in a fixed range.
164 worksheet.write_string(
165 cell("A17")?,
166 "Enter a date between 1/1/2008 and 12/12/2008",
167 None,
168 )?;
169 let date1 = Utc.ymd(2008, 1, 1).and_hms(0, 0, 0);
170 let date2 = Utc.ymd(2008, 12, 12).and_hms(0, 0, 0);
171 let validation = Validation::new(
172 ValidationType::Date(Criterion::Between(date1, date2)),
173 cell("B17")?,
174 );
175 worksheet.data_validation(validation)?;
176
177 // Example 9. Limiting input to time in a fixed range.
178 worksheet.write_string(
179 cell("A19")?,
180 "Enter a time between 6:00 and 12:00",
181 None,
182 )?;
183 let time1 = NaiveTime::from_hms(6, 0, 0);
184 let time2 = NaiveTime::from_hms(12, 0, 0);
185 let validation = Validation::new(
186 ValidationType::Time(Criterion::Between(time1, time2)),
187 cell("B19")?,
188 );
189 worksheet.data_validation(validation)?;
190
191 // Example 10. Limiting input to a string greater than a fixed length.
192 worksheet.write_string(
193 cell("A21")?,
194 "Enter a string longer than 3 characters",
195 None,
196 )?;
197 let validation = Validation::new(
198 ValidationType::Length(Criterion::GreaterThan(3)),
199 cell("B21")?,
200 );
201 worksheet.data_validation(validation)?;
202
203 // Example 11. Limiting input based on a formula.
204 worksheet.write_string(
205 cell("A23")?,
206 "Enter a value if the folowing is true \"=AND(F5=50,G5=60)\"",
207 None,
208 )?;
209 let validation = Validation::new(
210 ValidationType::CustomFormula("=AND(F5=50,G5=60)".to_string()),
211 cell("B23")?,
212 );
213 worksheet.data_validation(validation)?;
214
215 // Example 12. Displaying and modifying data validation messages.
216 worksheet.write_string(
217 cell("A25")?,
218 "Displays a message when you select the cell",
219 None,
220 )?;
221 let mut validation = Validation::new(
222 ValidationType::Integer(Criterion::Between(1, 100)),
223 cell("B25")?,
224 );
225 validation.input_title = Some("Enter an integer:".to_string());
226 validation.input_message = Some("between 1 and 100".to_string());
227 worksheet.data_validation(validation)?;
228
229 // Example 13. Displaying and modifying data validation messages.
230 worksheet.write_string(
231 cell("A27")?,
232 "Displays a custom error message when integer isn't \
233 between 1 and 100",
234 None,
235 )?;
236 let mut validation = Validation::new(
237 ValidationType::Integer(Criterion::Between(1, 100)),
238 cell("B27")?,
239 );
240 validation.input_title = Some("Enter an integer:".to_string());
241 validation.input_message = Some("between 1 and 100".to_string());
242 validation.error_title =
243 Some("Input value is not valid!".to_string());
244 validation.error_message = Some(
245 "It should be an integer \
246 between 1 and 100"
247 .to_string(),
248 );
249 worksheet.data_validation(validation)?;
250
251 // Example 14. Displaying and modifying data validation messages.
252 worksheet.write_string(
253 cell("A29")?,
254 "Displays a custom info message when integer isn't \
255 between 1 and 100",
256 None,
257 )?;
258 let mut validation = Validation::new(
259 ValidationType::Integer(Criterion::Between(1, 100)),
260 cell("B29")?,
261 );
262 validation.input_title = Some("Enter an integer:".to_string());
263 validation.input_message = Some("between 1 and 100".to_string());
264 validation.error_title =
265 Some("Input value is not valid!".to_string());
266 validation.error_message = Some(
267 "It should be an integer \
268 between 1 and 100"
269 .to_string(),
270 );
271 validation.error_type = ValidationErrorType::Information;
272 worksheet.data_validation(validation)?;
273 }
274
275 workbook.write_file("data_validate.xlsx")?;
276
277 Ok(())
278}pub fn set_column_options( &mut self, range: ColRange, width: f64, format: Option<&Format>, options: RowColOptions, ) -> Result<()>
pub fn insert_image<P: AsRef<Path>>( &mut self, index: Index, filename: P, ) -> Result<()>
pub fn insert_image_options<P: AsRef<Path>>( &mut self, index: Index, filename: P, options: ImageOptions, ) -> Result<()>
pub fn insert_image_buffer( &mut self, index: Index, image_buffer: &[u8], ) -> Result<()>
pub fn insert_image_buffer_options( &mut self, index: Index, image_buffer: &[u8], options: ImageOptions, ) -> Result<()>
pub fn insert_chart(&mut self, index: Index, chart: Chart) -> Result<()>
pub fn insert_chart_options( &mut self, index: Index, chart: Chart, options: ImageOptions, ) -> Result<()>
pub fn merge_range( &mut self, range: IndexRange, format: Option<&Format>, ) -> Result<()>
pub fn merge_range_str( &mut self, range: IndexRange, string: &str, format: Option<&Format>, ) -> Result<()>
pub fn autofilter(&mut self, range: IndexRange) -> Result<()>
Sourcepub fn data_validation(&mut self, validation: Validation) -> Result<()>
pub fn data_validation(&mut self, validation: Validation) -> Result<()>
Examples found in repository?
examples/data_validate.rs (line 75)
36fn main() -> karo::Result<()> {
37 let mut workbook = Workbook::new();
38
39 let mut format = Format::default();
40 format.borders.set_around(Some(Border {
41 style: BorderStyle::Thin,
42 color: None,
43 }));
44 format.fill.fg_color = Some(RGB8 {
45 r: 0xc6,
46 g: 0xef,
47 b: 0xce,
48 });
49 format.fill.pattern = Some(Pattern::Solid);
50 format.font.bold = true;
51 format.text_wrap = true;
52 format.vertical_alignment = Some(VerticalAlignment::Center);
53 format.indent = 1;
54
55 {
56 let worksheet = workbook.add_worksheet(None)?;
57 write_data(worksheet, Some(&format))?;
58
59 // Set up layout of the worksheet.
60 worksheet.set_column(col_range(0, 0)?, 55f64, None)?;
61 worksheet.set_column(col_range(1, 1)?, 15f64, None)?;
62 worksheet.set_column(col_range(3, 3)?, 15f64, None)?;
63 worksheet.set_row(row_range(0, 0)?, 36f64, None)?;
64
65 // Example 1. Limiting input to an integer in a fixed range.
66 worksheet.write_string(
67 cell("A3")?,
68 "Enter an integer between 1 and 10",
69 None,
70 )?;
71 let validation = Validation::new(
72 ValidationType::Integer(Criterion::Between(1, 10)),
73 cell("B3")?,
74 );
75 worksheet.data_validation(validation)?;
76
77 // Example 2. Limiting input to an integer outside a fixed range.
78 worksheet.write_string(
79 cell("A5")?,
80 "Enter an integer not between 1 and 10 \
81 (using cell references)",
82 None,
83 )?;
84 let validation = Validation::new(
85 ValidationType::IntegerFormula(Criterion::NotBetween(
86 "=E3".to_string(),
87 "=F3".to_string(),
88 )),
89 cell("B5")?,
90 );
91 worksheet.data_validation(validation)?;
92
93 // Example 3. Limiting input to an integer greater than a fixed value.
94 worksheet.write_string(
95 cell("A7")?,
96 "Enter an integer greater than 0",
97 None,
98 )?;
99 let validation = Validation::new(
100 ValidationType::Integer(Criterion::GreaterThan(0)),
101 cell("B7")?,
102 );
103 worksheet.data_validation(validation)?;
104
105 // Example 4. Limiting input to an integer less than a fixed value.
106 worksheet.write_string(
107 cell("A9")?,
108 "Enter an integer less than 10",
109 None,
110 )?;
111 let validation = Validation::new(
112 ValidationType::Integer(Criterion::LessThan(10)),
113 cell("B9")?,
114 );
115 worksheet.data_validation(validation)?;
116
117 // Example 5. Limiting input to a decimal in a fixed range.
118 worksheet.write_string(
119 cell("A11")?,
120 "Enter a decimal between 0.1 and 0.5",
121 None,
122 )?;
123 let validation = Validation::new(
124 ValidationType::Decimal(Criterion::Between(0.1, 0.5)),
125 cell("B11")?,
126 );
127 worksheet.data_validation(validation)?;
128
129 // Example 6. Limiting input to a value in dropdown list.
130 worksheet.write_string(
131 cell("A13")?,
132 "Select a value from a drop down list",
133 None,
134 )?;
135 let validation = Validation::new(
136 ValidationType::List {
137 values: indexset! {
138 "open".to_string(),
139 "high".to_string(),
140 "close".to_string(),
141 },
142 show_dropdown: true,
143 },
144 cell("B13")?,
145 );
146 worksheet.data_validation(validation)?;
147
148 // Example 7. Limiting input to a value in a dropdown list.
149 worksheet.write_string(
150 cell("A15")?,
151 "Select a value from a drop down list (using a cell range)",
152 None,
153 )?;
154 let validation = Validation::new(
155 ValidationType::ListFormula {
156 formula: "=$E$4:$G$4".to_string(),
157 show_dropdown: true,
158 },
159 cell("B15")?,
160 );
161 worksheet.data_validation(validation)?;
162
163 // Example 8. Limiting input to date in a fixed range.
164 worksheet.write_string(
165 cell("A17")?,
166 "Enter a date between 1/1/2008 and 12/12/2008",
167 None,
168 )?;
169 let date1 = Utc.ymd(2008, 1, 1).and_hms(0, 0, 0);
170 let date2 = Utc.ymd(2008, 12, 12).and_hms(0, 0, 0);
171 let validation = Validation::new(
172 ValidationType::Date(Criterion::Between(date1, date2)),
173 cell("B17")?,
174 );
175 worksheet.data_validation(validation)?;
176
177 // Example 9. Limiting input to time in a fixed range.
178 worksheet.write_string(
179 cell("A19")?,
180 "Enter a time between 6:00 and 12:00",
181 None,
182 )?;
183 let time1 = NaiveTime::from_hms(6, 0, 0);
184 let time2 = NaiveTime::from_hms(12, 0, 0);
185 let validation = Validation::new(
186 ValidationType::Time(Criterion::Between(time1, time2)),
187 cell("B19")?,
188 );
189 worksheet.data_validation(validation)?;
190
191 // Example 10. Limiting input to a string greater than a fixed length.
192 worksheet.write_string(
193 cell("A21")?,
194 "Enter a string longer than 3 characters",
195 None,
196 )?;
197 let validation = Validation::new(
198 ValidationType::Length(Criterion::GreaterThan(3)),
199 cell("B21")?,
200 );
201 worksheet.data_validation(validation)?;
202
203 // Example 11. Limiting input based on a formula.
204 worksheet.write_string(
205 cell("A23")?,
206 "Enter a value if the folowing is true \"=AND(F5=50,G5=60)\"",
207 None,
208 )?;
209 let validation = Validation::new(
210 ValidationType::CustomFormula("=AND(F5=50,G5=60)".to_string()),
211 cell("B23")?,
212 );
213 worksheet.data_validation(validation)?;
214
215 // Example 12. Displaying and modifying data validation messages.
216 worksheet.write_string(
217 cell("A25")?,
218 "Displays a message when you select the cell",
219 None,
220 )?;
221 let mut validation = Validation::new(
222 ValidationType::Integer(Criterion::Between(1, 100)),
223 cell("B25")?,
224 );
225 validation.input_title = Some("Enter an integer:".to_string());
226 validation.input_message = Some("between 1 and 100".to_string());
227 worksheet.data_validation(validation)?;
228
229 // Example 13. Displaying and modifying data validation messages.
230 worksheet.write_string(
231 cell("A27")?,
232 "Displays a custom error message when integer isn't \
233 between 1 and 100",
234 None,
235 )?;
236 let mut validation = Validation::new(
237 ValidationType::Integer(Criterion::Between(1, 100)),
238 cell("B27")?,
239 );
240 validation.input_title = Some("Enter an integer:".to_string());
241 validation.input_message = Some("between 1 and 100".to_string());
242 validation.error_title =
243 Some("Input value is not valid!".to_string());
244 validation.error_message = Some(
245 "It should be an integer \
246 between 1 and 100"
247 .to_string(),
248 );
249 worksheet.data_validation(validation)?;
250
251 // Example 14. Displaying and modifying data validation messages.
252 worksheet.write_string(
253 cell("A29")?,
254 "Displays a custom info message when integer isn't \
255 between 1 and 100",
256 None,
257 )?;
258 let mut validation = Validation::new(
259 ValidationType::Integer(Criterion::Between(1, 100)),
260 cell("B29")?,
261 );
262 validation.input_title = Some("Enter an integer:".to_string());
263 validation.input_message = Some("between 1 and 100".to_string());
264 validation.error_title =
265 Some("Input value is not valid!".to_string());
266 validation.error_message = Some(
267 "It should be an integer \
268 between 1 and 100"
269 .to_string(),
270 );
271 validation.error_type = ValidationErrorType::Information;
272 worksheet.data_validation(validation)?;
273 }
274
275 workbook.write_file("data_validate.xlsx")?;
276
277 Ok(())
278}pub fn activate(&mut self)
pub fn select(&mut self)
pub fn hide(&mut self)
pub fn set_first_sheet(&mut self)
pub fn freeze_panes(&mut self, index: Index)
pub fn split_panes(&mut self, vertical: f64, horizontal: f64)
pub fn set_selection(&mut self, start: Index, end: Index)
pub fn set_landscape(&mut self)
pub fn set_portrait(&mut self)
pub fn set_page_view(&mut self)
pub fn set_paper(&mut self, paper_type: Paper)
pub fn set_margins(&mut self, left: f64, right: f64, top: f64, bottom: f64)
pub fn set_header<S: AsRef<str>>(&mut self, string: S) -> Result<()>
pub fn set_header_options<S: AsRef<str>>( &mut self, string: S, options: HeaderFooterOptions, ) -> Result<()>
pub fn set_h_pagebreaks(&mut self, breaks: &[Row]) -> Result<()>
pub fn set_v_pagebreaks(&mut self, breaks: &[Col]) -> Result<()>
pub fn print_across(&mut self)
pub fn set_zoom(&mut self, scale: u16)
pub fn set_gridlines(&mut self, option: Gridlines)
pub fn center_horizontally(&mut self)
pub fn center_vertically(&mut self)
pub fn print_row_col_headers(&mut self)
pub fn repeat_rows(&mut self, range: RowRange) -> Result<()>
pub fn repeat_columns(&mut self, range: ColRange) -> Result<()>
pub fn print_area(&mut self, range: IndexRange) -> Result<()>
Sourcepub fn fit_to_pages(&mut self, width: u16, height: u16)
pub fn fit_to_pages(&mut self, width: u16, height: u16)
Store the vertical and horizontal number of pages that will define the maximum area printed.
pub fn set_start_page(&mut self, start_page: u16)
pub fn set_print_scale(&mut self, scale: u16)
pub fn right_to_left(&mut self)
pub fn hide_zero(&mut self)
Sourcepub fn set_tab_color(&mut self, color: RGB8)
pub fn set_tab_color(&mut self, color: RGB8)
Examples found in repository?
examples/tab_colors.rs (line 9)
4fn main() -> karo::Result<()> {
5 let mut workbook = Workbook::new();
6
7 {
8 let worksheet1 = workbook.add_worksheet(None)?;
9 worksheet1.set_tab_color(colors::RED);
10 }
11 {
12 let worksheet2 = workbook.add_worksheet(None)?;
13 worksheet2.set_tab_color(colors::GREEN);
14 }
15 {
16 let worksheet3 = workbook.add_worksheet(None)?;
17 worksheet3.set_tab_color(RGB8 {
18 r: 0xff,
19 g: 0x99,
20 b: 0x00,
21 });
22 }
23 {
24 let worksheet4 = workbook.add_worksheet(None)?;
25 worksheet4.write_string(index(0, 0)?, "Hello", None)?;
26 }
27
28 workbook.write_file("tab_colors.xlsx")?;
29
30 Ok(())
31}pub fn protect(&mut self, password: Option<&str>, options: Protection)
pub fn outline_settings( &mut self, visible: bool, symbols_below: bool, symbols_right: bool, auto_style: bool, )
pub fn set_default_row(&mut self, height: f64, hide_unused_rows: bool)
pub fn set_vba_name<N: AsRef<str>>(&mut self, name: N) -> Result<()>
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Worksheet
impl !RefUnwindSafe for Worksheet
impl !Send for Worksheet
impl !Sync for Worksheet
impl Unpin for Worksheet
impl !UnwindSafe for Worksheet
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more