pub fn col_range<C>(first: C, last: C) -> Result<ColRange>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}