forked from jmcnamara/rust_xlsxwriter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat.rs
3293 lines (3083 loc) · 114 KB
/
format.rs
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// format - A module for representing Excel cell formats.
//
// SPDX-License-Identifier: MIT OR Apache-2.0
//
// Copyright 2022-2023, John McNamara, [email protected]
#![warn(missing_docs)]
use std::{collections::HashMap, fmt, hash::Hash};
/// The `Format` struct is used to define cell formatting for data in a worksheet.
///
/// The properties of a cell that can be formatted include: fonts, colors,
/// patterns, borders, alignment and number formatting.
///
/// <img src="https://rustxlsxwriter.github.io/images/format_intro.png">
///
/// The output file above was created with the following code:
///
/// ```
/// # // This code is available in examples/doc_format_intro.rs
/// #
/// use rust_xlsxwriter::{Format, Workbook, FormatBorder, Color, XlsxError};
///
/// fn main() -> Result<(), XlsxError> {
/// // Create a new Excel file object.
/// let mut workbook = Workbook::new();
///
/// // Add a worksheet.
/// let worksheet = workbook.add_worksheet();
///
/// // Make the first column wider for clarity.
/// worksheet.set_column_width(0, 14)?;
///
/// // Create some sample formats to display
/// let format1 = Format::new().set_font_name("Arial");
/// worksheet.write_string_with_format(0, 0, "Fonts", &format1)?;
///
/// let format2 = Format::new().set_font_name("Algerian").set_font_size(14);
/// worksheet.write_string_with_format(1, 0, "Fonts", &format2)?;
///
/// let format3 = Format::new().set_font_name("Comic Sans MS");
/// worksheet.write_string_with_format(2, 0, "Fonts", &format3)?;
///
/// let format4 = Format::new().set_font_name("Edwardian Script ITC");
/// worksheet.write_string_with_format(3, 0, "Fonts", &format4)?;
///
/// let format5 = Format::new().set_font_color(Color::Red);
/// worksheet.write_string_with_format(4, 0, "Font color", &format5)?;
///
/// let format6 = Format::new().set_background_color(Color::RGB(0xDAA520));
/// worksheet.write_string_with_format(5, 0, "Fills", &format6)?;
///
/// let format7 = Format::new().set_border(FormatBorder::Thin);
/// worksheet.write_string_with_format(6, 0, "Borders", &format7)?;
///
/// let format8 = Format::new().set_bold();
/// worksheet.write_string_with_format(7, 0, "Bold", &format8)?;
///
/// let format9 = Format::new().set_italic();
/// worksheet.write_string_with_format(8, 0, "Italic", &format9)?;
///
/// let format10 = Format::new().set_bold().set_italic();
/// worksheet.write_string_with_format(9, 0, "Bold and Italic", &format10)?;
///
/// workbook.save("formats.xlsx")?;
///
/// Ok(())
/// }
/// ```
///
///
/// # Creating and using a Format object
///
/// Formats are created by calling the `Format::new()` method and properties as
/// set using the various methods shown is this section of the document. Once
/// the Format has been created it can be passed to one of the worksheet
/// `write_*()` methods. Multiple properties can be set by chaining them
/// together:
///
/// ```
/// # // This code is available in examples/doc_format_create.rs
/// #
/// # use rust_xlsxwriter::{Format, Workbook, Color, XlsxError};
/// #
/// # fn main() -> Result<(), XlsxError> {
/// # // Create a new Excel file object.
/// # let mut workbook = Workbook::new();
/// #
/// # // Add a worksheet.
/// # let worksheet = workbook.add_worksheet();
/// #
/// // Create a new format and set some properties.
/// let format = Format::new()
/// .set_bold()
/// .set_italic()
/// .set_font_color(Color::Red);
///
/// worksheet.write_string_with_format(0, 0, "Hello", &format)?;
///
/// # workbook.save("formats.xlsx")?;
/// #
/// # Ok(())
/// # }
/// ```
///
/// <img src="https://rustxlsxwriter.github.io/images/format_create.png">
///
/// Formats can be cloned in the usual way:
///
/// ```
/// # // This code is available in examples/doc_format_clone.rs
/// #
/// # use rust_xlsxwriter::{Format, Workbook, Color, XlsxError};
/// #
/// # fn main() -> Result<(), XlsxError> {
/// # // Create a new Excel file object.
/// # let mut workbook = Workbook::new();
/// #
/// # // Add a worksheet.
/// # let worksheet = workbook.add_worksheet();
/// #
/// # // Create a new format and set some properties.
/// let format1 = Format::new()
/// .set_bold();
///
/// // Clone a new format and set some properties.
/// let format2 = format1.clone()
/// .set_font_color(Color::Blue);
///
/// worksheet.write_string_with_format(0, 0, "Hello", &format1)?;
/// worksheet.write_string_with_format(1, 0, "Hello", &format2)?;
///
/// # workbook.save("formats.xlsx")?;
/// #
/// # Ok(())
/// # }
/// ```
///
/// <img src="https://rustxlsxwriter.github.io/images/format_clone.png">
///
///
/// # Format methods and Format properties
///
/// The following table shows the Excel format categories, in the order shown in
/// the Excel "Format Cell" dialog, and the equivalent `rust_xlsxwriter` Format
/// method:
///
/// | Category | Description | Method Name |
/// | :-------------- | :-------------------- | :------------------------------------------------------------------- |
/// | **Number** | Numeric format | [`set_num_format()`](Format::set_num_format()) |
/// | **Alignment** | Horizontal align | [`set_align()`](Format::set_align()) |
/// | | Vertical align | [`set_align()`](Format::set_align()) |
/// | | Rotation | [`set_rotation()`](Format::set_rotation()) |
/// | | Text wrap | [`set_text_wrap()`](Format::set_text_wrap()) |
/// | | Indentation | [`set_indent()`](Format::set_indent()) |
/// | | Reading direction | [`set_reading_direction()`](Format::set_reading_direction()) |
/// | | Shrink to fit | [`set_shrink()`](Format::set_shrink()) |
/// | **Font** | Font type | [`set_font_name()`](Format::set_font_name()) |
/// | | Font size | [`set_font_size()`](Format::set_font_size()) |
/// | | Font color | [`set_font_color()`](Format::set_font_color()) |
/// | | Bold | [`set_bold()`](Format::set_bold()) |
/// | | Italic | [`set_italic()`](Format::set_italic()) |
/// | | Underline | [`set_underline()`](Format::set_underline()) |
/// | | Strikethrough | [`set_font_strikethrough()`](Format::set_font_strikethrough()) |
/// | | Super/Subscript | [`set_font_script()`](Format::set_font_script()) |
/// | **Border** | Cell border | [`set_border()`](Format::set_border()) |
/// | | Bottom border | [`set_border_bottom()`](Format::set_border_bottom()) |
/// | | Top border | [`set_border_top()`](Format::set_border_top()) |
/// | | Left border | [`set_border_left()`](Format::set_border_left()) |
/// | | Right border | [`set_border_right()`](Format::set_border_right()) |
/// | | Border color | [`set_border_color()`](Format::set_border_color()) |
/// | | Bottom color | [`set_border_bottom_color()`](Format::set_border_bottom_color()) |
/// | | Top color | [`set_border_top_color()`](Format::set_border_top_color()) |
/// | | Left color | [`set_border_left_color()`](Format::set_border_left_color()) |
/// | | Right color | [`set_border_right_color()`](Format::set_border_right_color()) |
/// | | Diagonal border | [`set_border_diagonal()`](Format::set_border_diagonal()) |
/// | | Diagonal border color | [`set_border_diagonal_color()`](Format::set_border_diagonal_color()) |
/// | | Diagonal border type | [`set_border_diagonal_type()`](Format::set_border_diagonal_type()) |
/// | **Fill** | Cell pattern | [`set_pattern()`](Format::set_pattern()) |
/// | | Background color | [`set_background_color()`](Format::set_background_color()) |
/// | | Foreground color | [`set_foreground_color()`](Format::set_foreground_color()) |
/// | **Protection** | Unlock cells | [`set_unlocked()`](Format::set_unlocked()) |
/// | | Hide formulas | [`set_hidden()`](Format::set_hidden()) |
///
/// # Format Colors
///
/// Format property colors are specified by using the [`Color`] enum with a
/// Html style RGB integer value or a limited number of defined colors:
///
/// ```
/// # // This code is available in examples/doc_enum_Color.rs
/// #
/// # use rust_xlsxwriter::{Format, Workbook, Color, XlsxError};
/// #
/// # fn main() -> Result<(), XlsxError> {
/// // Create a new Excel file object.
/// let mut workbook = Workbook::new();
///
/// let format1 = Format::new().set_font_color(Color::Red);
/// let format2 = Format::new().set_font_color(Color::Green);
/// let format3 = Format::new().set_font_color(Color::RGB(0x4F026A));
/// let format4 = Format::new().set_font_color(Color::RGB(0x73CC5F));
/// let format5 = Format::new().set_font_color(Color::RGB(0xFFACFF));
/// let format6 = Format::new().set_font_color(Color::RGB(0xCC7E16));
///
/// let worksheet = workbook.add_worksheet();
/// worksheet.write_string_with_format(0, 0, "Red", &format1)?;
/// worksheet.write_string_with_format(1, 0, "Green", &format2)?;
/// worksheet.write_string_with_format(2, 0, "#4F026A", &format3)?;
/// worksheet.write_string_with_format(3, 0, "#73CC5F", &format4)?;
/// worksheet.write_string_with_format(4, 0, "#FFACFF", &format5)?;
/// worksheet.write_string_with_format(5, 0, "#CC7E16", &format6)?;
///
/// # workbook.save("colors.xlsx")?;
/// #
/// # Ok(())
/// # }
/// ```
///
/// <img src="https://rustxlsxwriter.github.io/images/enum_xlsxcolor.png">
///
/// # Format Defaults
///
/// The default Excel 365 cell format is a font setting of Calibri size 11 with
/// all other properties turned off.
///
/// It is occasionally useful to use a default format with a method that
/// requires a format but where you don't actually want to change the
/// formatting.
///
/// ```
/// # // This code is available in examples/doc_format_default.rs
/// #
/// # use rust_xlsxwriter::{Format, Workbook, XlsxError};
/// #
/// # fn main() -> Result<(), XlsxError> {
/// # // Create a new Excel file object.
/// # let mut workbook = Workbook::new();
/// #
/// # // Add a worksheet.
/// # let worksheet = workbook.add_worksheet();
/// #
/// // Create a new default format.
/// let format = Format::default();
///
/// // These methods calls are equivalent.
/// worksheet.write_string(0, 0, "Hello")?;
/// worksheet.write_string_with_format(1, 0, "Hello", &format)?;
/// #
/// # workbook.save("formats.xlsx")?;
/// #
/// # Ok(())
/// # }
/// ```
///
/// <img src="https://rustxlsxwriter.github.io/images/format_default.png">
///
///
/// # Number Format Categories
///
/// The [`set_num_format()`](Format::set_num_format) method is used to set the
/// number format for numbers used with
/// [`write_number_with_format()`](crate::Worksheet::write_number_with_format()):
///
/// ```
/// # // This code is available in examples/doc_format_currency1.rs
///
/// use rust_xlsxwriter::{Format, Workbook, XlsxError};
///
/// fn main() -> Result<(), XlsxError> {
/// // Create a new Excel file object.
/// let mut workbook = Workbook::new();
///
/// // Add a worksheet.
/// let worksheet = workbook.add_worksheet();
///
/// // Add a format.
/// let currency_format = Format::new().set_num_format("$#,##0.00");
///
/// worksheet.write_number_with_format(0, 0, 1234.56, ¤cy_format)?;
///
/// workbook.save("currency_format.xlsx")?;
///
/// Ok(())
/// }
/// ```
///
/// If the number format you use is the same as one of Excel's built in number
/// formats then it will have a number category other than "General" or
/// "Number". The Excel number categories are:
///
/// - General
/// - Number
/// - Currency
/// - Accounting
/// - Date
/// - Time
/// - Percentage
/// - Fraction
/// - Scientific
/// - Text
/// - Custom
///
/// In the case of the example above the formatted output shows up as a Number
/// category:
///
/// <img src="https://rustxlsxwriter.github.io/images/format_currency1.png">
///
/// If we wanted to have the number format display as a different category, such
/// as Currency, then would need to match the number format string used in the
/// code with the number format used by Excel. The easiest way to do this is to
/// open the Number Formatting dialog in Excel and set the required format:
///
/// <img src="https://rustxlsxwriter.github.io/images/format_currency2.png">
///
/// Then, while still in the dialog, change to Custom. The format displayed is
/// the format used by Excel.
///
/// <img src="https://rustxlsxwriter.github.io/images/format_currency3.png">
///
/// If we put the format that we found (`"[$$-409]#,##0.00"`) into our previous
/// example and rerun it we will get a number format in the Currency category:
///
/// ```
/// # // This code is available in examples/doc_format_currency2.rs
/// #
/// # use rust_xlsxwriter::{Format, Workbook, XlsxError};
/// #
/// # fn main() -> Result<(), XlsxError> {
/// # // Create a new Excel file object.
/// # let mut workbook = Workbook::new();
/// #
/// # // Add a worksheet.
/// # let worksheet = workbook.add_worksheet();
/// #
/// # // Add a format.
/// let currency_format = Format::new().set_num_format("[$$-409]#,##0.00");
///
/// worksheet.write_number_with_format(0, 0, 1234.56, ¤cy_format)?;
///
/// # workbook.save("currency_format.xlsx")?;
/// #
/// # Ok(())
/// # }
/// ```
///
/// That give us the following updated output. Note that the number category is
/// now shown as Currency:
///
/// <img src="https://rustxlsxwriter.github.io/images/format_currency4.png">
///
/// The same process can be used to find format strings for "Date" or
/// "Accountancy" formats.
///
/// # Number Formats in different locales
///
/// As shown in the previous section the `format.set_num_format()` method is
/// used to set the number format for `rust_xlsxwriter` formats. A common use
/// case is to set a number format with a "grouping/thousands" separator and a
/// "decimal" point:
///
/// ```
/// # // This code is available in examples/doc_format_locale.rs
/// #
/// use rust_xlsxwriter::{Format, Workbook, XlsxError};
///
/// fn main() -> Result<(), XlsxError> {
/// // Create a new Excel file object.
/// let mut workbook = Workbook::new();
///
///
/// // Add a worksheet.
/// let worksheet = workbook.add_worksheet();
///
/// // Add a format.
/// let currency_format = Format::new().set_num_format("#,##0.00");
///
/// worksheet.write_number_with_format(0, 0, 1234.56, ¤cy_format)?;
///
/// workbook.save("number_format.xlsx")?;
///
/// Ok(())
/// }
/// ```
///
/// In the US locale (and some others) where the number "grouping/thousands"
/// separator is `","` and the "decimal" point is `"."` which would be shown in
/// Excel as:
///
/// <img src="https://rustxlsxwriter.github.io/images/format_currency5.png">
///
/// In other locales these values may be reversed or different. They are
/// generally set in the "Region" settings of Windows or Mac OS. Excel handles
/// this by storing the number format in the file format in the US locale, in
/// this case `#,##0.00`, but renders it according to the regional settings of
/// the host OS. For example, here is the same, unmodified, output file shown
/// above in a German locale:
///
/// <img src="https://rustxlsxwriter.github.io/images/format_currency6.png">
///
/// And here is the same file in a Russian locale. Note the use of a space as
/// the "grouping/thousands" separator:
///
/// <img src="https://rustxlsxwriter.github.io/images/format_currency7.png">
///
/// In order to replicate Excel's behavior all `rust_xlsxwriter` programs should
/// use US locale formatting which will then be rendered in the settings of your
/// host OS.
///
#[derive(Debug, Clone, Eq)]
pub struct Format {
pub(crate) dxf_index: u32,
pub(crate) font_index: u16,
pub(crate) fill_index: u16,
pub(crate) border_index: u16,
pub(crate) has_font: bool,
pub(crate) has_fill: bool,
pub(crate) has_border: bool,
// Properties listed in terms of the Excel dialog.
// Number properties.
pub(crate) num_format: String,
pub(crate) num_format_index: u16,
// Font properties.
pub(crate) font: Font,
// Alignment properties
pub(crate) alignment: Alignment,
// Border properties
pub(crate) borders: Border,
// Fill properties
pub(crate) fill: Fill,
// Protection properties.
pub(crate) hidden: bool,
pub(crate) locked: bool,
// Non-UI properties.
pub(crate) quote_prefix: bool,
}
impl Hash for Format {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.font.hash(state);
self.alignment.hash(state);
self.borders.hash(state);
self.fill.hash(state);
self.num_format.hash(state);
self.num_format_index.hash(state);
self.hidden.hash(state);
self.locked.hash(state);
self.quote_prefix.hash(state);
}
}
impl PartialEq for Format {
fn eq(&self, other: &Self) -> bool {
self.font == other.font
&& self.alignment == other.alignment
&& self.borders == other.borders
&& self.fill == other.fill
&& self.num_format == other.num_format
&& self.num_format_index == other.num_format_index
&& self.hidden == other.hidden
&& self.locked == other.locked
&& self.quote_prefix == other.quote_prefix
}
}
impl Default for Format {
fn default() -> Self {
Self::new()
}
}
impl Format {
/// Create a new Format object.
///
/// Create a new Format object to use with worksheet formatting.
///
///
/// # Examples
///
/// The following example demonstrates creating a new format.
///
///
/// ```
/// # // This code is available in examples/doc_format_new.rs
/// use rust_xlsxwriter::Format;
///
/// # #[allow(unused_variables)]
/// fn main() {
///
/// let format = Format::new();
///
/// }
/// ```
pub fn new() -> Format {
Format {
dxf_index: 0,
font_index: 0,
fill_index: 0,
border_index: 0,
has_font: false,
has_fill: false,
has_border: false,
font: Font::default(),
alignment: Alignment::default(),
fill: Fill::default(),
borders: Border::default(),
hidden: false,
locked: true,
num_format: String::new(),
num_format_index: 0,
quote_prefix: false,
}
}
// -----------------------------------------------------------------------
// Crate private methods.
// -----------------------------------------------------------------------
pub(crate) fn set_font_index(&mut self, font_index: u16, has_font: bool) {
self.font_index = font_index;
self.has_font = has_font;
}
// For DXF formats (Table and Conditional) check if the font has changed.
pub(crate) fn has_dxf_font(&self) -> bool {
self.font.bold
|| self.font.italic
|| self.font.underline != FormatUnderline::None
|| self.font.strikethrough
|| !self.font.color.is_auto_or_default()
}
// For DXF formats (Table and Conditional) check if the fill has changed.
pub(crate) fn has_dxf_fill(&self) -> bool {
self.fill.pattern != FormatPattern::None
|| !self.fill.background_color.is_auto_or_default()
|| !self.fill.foreground_color.is_auto_or_default()
}
pub(crate) fn set_fill_index(&mut self, fill_index: u16, has_fill: bool) {
self.fill_index = fill_index;
self.has_fill = has_fill;
}
pub(crate) fn set_border_index(&mut self, border_index: u16, has_border: bool) {
self.border_index = border_index;
self.has_border = has_border;
}
pub(crate) fn set_num_format_index_u16(&mut self, num_format_index: u16) {
self.num_format_index = num_format_index;
}
// Check if the format has an alignment property set and requires a Styles
// <alignment> element. This also handles a special case where Excel ignores
// Bottom as a default.
pub(crate) fn has_alignment(&self) -> bool {
self.alignment.horizontal != FormatAlign::General
|| !(self.alignment.vertical == FormatAlign::General
|| self.alignment.vertical == FormatAlign::Bottom)
|| self.alignment.indent != 0
|| self.alignment.rotation != 0
|| self.alignment.text_wrap
|| self.alignment.shrink
|| self.alignment.reading_direction != 0
}
// Check if the format has an alignment property set and requires a Styles
// "applyAlignment" attribute.
pub(crate) fn apply_alignment(&self) -> bool {
self.alignment.horizontal != FormatAlign::General
|| self.alignment.vertical != FormatAlign::General
|| self.alignment.indent != 0
|| self.alignment.rotation != 0
|| self.alignment.text_wrap
|| self.alignment.shrink
|| self.alignment.reading_direction != 0
}
// Check if the format has protection properties set.
pub(crate) fn has_protection(&self) -> bool {
self.hidden || !self.locked
}
// Check if the format is in the default/unmodified condition.
pub(crate) fn is_default(&self) -> bool {
lazy_static! {
static ref DEFAULT_STATE: Format = Format::default();
};
self == &*DEFAULT_STATE
}
// -----------------------------------------------------------------------
// Public methods.
// -----------------------------------------------------------------------
/// Set the number format for a Format.
///
/// This method is used to define the numerical format of a number in Excel.
/// It controls whether a number is displayed as an integer, a floating
/// point number, a date, a currency value or some other user defined
/// format.
///
/// See also [Number Format Categories] and [Number Formats in different
/// locales].
///
/// [Number Format Categories]: struct.Format.html#number-format-categories
/// [Number Formats in different locales]:
/// struct.Format.html#number-formats-in-different-locales
///
/// # Parameters
///
/// * `num_format` - The number format property.
///
/// # Examples
///
/// The following example demonstrates setting different types of Excel
/// number formatting.
///
/// ```
/// # // This code is available in examples/doc_format_set_num_format.rs
/// #
/// # use rust_xlsxwriter::{Format, Workbook, XlsxError};
/// #
/// # fn main() -> Result<(), XlsxError> {
/// # // Create a new Excel file object.
/// # let mut workbook = Workbook::new();
/// # let worksheet = workbook.add_worksheet();
/// #
/// # // Set column width for clarity.
/// # worksheet.set_column_width(0, 20)?;
/// #
/// let format1 = Format::new().set_num_format("0.00");
/// let format2 = Format::new().set_num_format("0.000");
/// let format3 = Format::new().set_num_format("#,##0");
/// let format4 = Format::new().set_num_format("#,##0.00");
/// let format5 = Format::new().set_num_format("mm/dd/yy");
/// let format6 = Format::new().set_num_format("mmm d yyyy");
/// let format7 = Format::new().set_num_format("d mmmm yyyy");
/// let format8 = Format::new().set_num_format("dd/mm/yyyy hh:mm AM/PM");
///
/// worksheet.write_number_with_format(0, 0, 1.23456, &format1)?;
/// worksheet.write_number_with_format(1, 0, 1.23456 , &format2)?;
/// worksheet.write_number_with_format(2, 0, 1234.56, &format3)?;
/// worksheet.write_number_with_format(3, 0, 1234.56, &format4)?;
/// worksheet.write_number_with_format(4, 0, 44927.521, &format5)?;
/// worksheet.write_number_with_format(5, 0, 44927.521, &format6)?;
/// worksheet.write_number_with_format(6, 0, 44927.521, &format7)?;
/// worksheet.write_number_with_format(7, 0, 44927.521, &format8)?;
/// #
/// # workbook.save("formats.xlsx")?;
/// #
/// # Ok(())
/// # }
/// ```
///
/// Note how the numbers above have been displayed by Excel in the output
/// file according to the given number format:
///
/// <img
/// src="https://rustxlsxwriter.github.io/images/format_set_num_format.png">
///
pub fn set_num_format(mut self, num_format: impl Into<String>) -> Format {
self.num_format = num_format.into();
self
}
/// Set the number format for a Format using a legacy format index.
///
/// This method is similar to [`set_num_format()`](Format::set_num_format)
/// except that it uses an index to a limited number of Excel's built-in,
/// and legacy, number formats.
///
/// Unless you need to specifically access one of Excel's built-in number
/// formats the [`set_num_format()`](Format::set_num_format) method is a
/// better solution. This method is mainly included for backward
/// compatibility and completeness.
///
/// The Excel built-in number formats as shown in the table below:
///
/// | Index | Format String |
/// | :---- | :--------------------------------------------------- |
/// | 1 | `0` |
/// | 2 | `0.00` |
/// | 3 | `#,##0` |
/// | 4 | `#,##0.00` |
/// | 5 | `($#,##0_);($#,##0)` |
/// | 6 | `($#,##0_);[Red]($#,##0)` |
/// | 7 | `($#,##0.00_);($#,##0.00)` |
/// | 8 | `($#,##0.00_);[Red]($#,##0.00)` |
/// | 9 | `0%` |
/// | 10 | `0.00%` |
/// | 11 | `0.00E+00` |
/// | 12 | `# ?/?` |
/// | 13 | `# ??/??` |
/// | 14 | `m/d/yy` |
/// | 15 | `d-mmm-yy` |
/// | 16 | `d-mmm` |
/// | 17 | `mmm-yy` |
/// | 18 | `h:mm AM/PM` |
/// | 19 | `h:mm:ss AM/PM` |
/// | 20 | `h:mm` |
/// | 21 | `h:mm:ss` |
/// | 22 | `m/d/yy h:mm` |
/// | ... | ... |
/// | 37 | `(#,##0_);(#,##0)` |
/// | 38 | `(#,##0_);[Red](#,##0)` |
/// | 39 | `(#,##0.00_);(#,##0.00)` |
/// | 40 | `(#,##0.00_);[Red](#,##0.00)` |
/// | 41 | `_(* #,##0_);_(* (#,##0);_(* "-"_);_(@_)` |
/// | 42 | `_($* #,##0_);_($* (#,##0);_($* "-"_);_(@_)` |
/// | 43 | `_(* #,##0.00_);_(* (#,##0.00);_(* "-"??_);_(@_)` |
/// | 44 | `_($* #,##0.00_);_($* (#,##0.00);_($* "-"??_);_(@_)` |
/// | 45 | `mm:ss` |
/// | 46 | `[h]:mm:ss` |
/// | 47 | `mm:ss.0` |
/// | 48 | `##0.0E+0` |
/// | 49 | `@` |
///
/// Notes:
///
/// - Numeric formats 23 to 36 are not documented by Microsoft and may
/// differ in international versions. The listed date and currency
/// formats may also vary depending on system settings.
/// - The dollar sign in the above format appears as the defined local
/// currency symbol.
/// - These formats can also be set via
/// [`set_num_format()`](Format::set_num_format).
///
/// # Parameters
///
/// * `num_format_index` - The index to one of the inbuilt formats shown in
/// the table above.
///
/// # Examples
///
/// The following example demonstrates setting the bold property for a
/// format.
///
/// ```
/// # // This code is available in examples/doc_format_set_num_format_index.rs
/// #
/// # use rust_xlsxwriter::{Format, Workbook, XlsxError};
/// #
/// # fn main() -> Result<(), XlsxError> {
/// # // Create a new Excel file object.
/// # let mut workbook = Workbook::new();
/// # let worksheet = workbook.add_worksheet();
/// #
/// let format = Format::new().set_num_format_index(15);
///
/// worksheet.write_number_with_format(0, 0, 44927.521, &format)?;
/// #
/// # workbook.save("formats.xlsx")?;
/// #
/// # Ok(())
/// # }
/// ```
///
/// Output file:
///
/// <img
/// src="https://rustxlsxwriter.github.io/images/format_set_num_format_index.png">
///
pub fn set_num_format_index(mut self, num_format_index: u8) -> Format {
self.num_format_index = u16::from(num_format_index);
// Also map the index to a format string. Mainly for DXF formats.
let num_formats = HashMap::from([
(1, "0"),
(2, "0.00"),
(3, "#,##0"),
(4, "#,##0.00"),
(5, "($#,##0_);($#,##0)"),
(6, "($#,##0_);[Red]($#,##0)"),
(7, "($#,##0.00_);($#,##0.00)"),
(8, "($#,##0.00_);[Red]($#,##0.00)"),
(9, "0%"),
(10, "0.00%"),
(11, "0.00E+00"),
(12, "# ?/?"),
(13, "# ??/??"),
(14, "m/d/yy"),
(15, "d-mmm-yy"),
(16, "d-mmm"),
(17, "mmm-yy"),
(18, "h:mm AM/PM"),
(19, "h:mm:ss AM/PM"),
(20, "h:mm"),
(21, "h:mm:ss"),
(22, "m/d/yy h:mm"),
(37, "(#,##0_);(#,##0)"),
(38, "(#,##0_);[Red](#,##0)"),
(39, "(#,##0.00_);(#,##0.00)"),
(40, "(#,##0.00_);[Red](#,##0.00)"),
(41, "_(* #,##0_);_(* (#,##0);_(* \"-\"_);_(_)"),
(42, "_($* #,##0_);_($* (#,##0);_($* \"-\"_);_(_)"),
(43, "_(* #,##0.00_);_(* (#,##0.00);_(* \"-\"??_);_(_)"),
(44, "_($* #,##0.00_);_($* (#,##0.00);_($* \"-\"??_);_(_)"),
(45, "mm:ss"),
(46, "[h]:mm:ss"),
(47, "mm:ss.0"),
(48, "##0.0E+0"),
(49, "@"),
]);
if let Some(num_format) = num_formats.get(&num_format_index) {
self.num_format = (*num_format).to_string();
}
self
}
/// Set the bold property for a Format font.
///
/// # Examples
///
/// The following example demonstrates setting the bold property for a format.
///
/// ```
/// # // This code is available in examples/doc_format_set_bold.rs
/// #
/// # use rust_xlsxwriter::{Format, Workbook, XlsxError};
/// #
/// # fn main() -> Result<(), XlsxError> {
/// # // Create a new Excel file object.
/// # let mut workbook = Workbook::new();
/// # let worksheet = workbook.add_worksheet();
/// #
/// let format = Format::new().set_bold();
///
/// worksheet.write_string_with_format(0, 0, "Hello", &format)?;
/// #
/// # workbook.save("formats.xlsx")?;
/// #
/// # Ok(())
/// # }
/// ```
///
/// Output file:
///
/// <img src="https://rustxlsxwriter.github.io/images/format_set_bold.png">
///
pub fn set_bold(mut self) -> Format {
self.font.bold = true;
self
}
/// Set the italic property for the Format font.
///
/// # Examples
///
/// The following example demonstrates setting the italic property for a format.
///
/// ```
/// # // This code is available in examples/doc_format_set_italic.rs
/// #
/// # use rust_xlsxwriter::{Format, Workbook, XlsxError};
/// #
/// # fn main() -> Result<(), XlsxError> {
/// # // Create a new Excel file object.
/// # let mut workbook = Workbook::new();
/// # let worksheet = workbook.add_worksheet();
/// #
/// let format = Format::new().set_italic();
///
/// worksheet.write_string_with_format(0, 0, "Hello", &format)?;
/// #
/// # workbook.save("formats.xlsx")?;
/// #
/// # Ok(())
/// # }
/// ```
///
/// Output file:
///
/// <img src="https://rustxlsxwriter.github.io/images/format_set_italic.png">
///
pub fn set_italic(mut self) -> Format {
self.font.italic = true;
self
}
/// Set the color property for the Format font.
///
/// The `set_font_color()` method is used to set the font color in a cell.
/// To set the color of a cell background use the `set_bg_color()` and
/// `set_pattern()` methods.
///
/// # Parameters
///
/// * `color` - The font color property defined by a [`Color`] enum
/// value.
///
/// # Examples
///
/// The following example demonstrates setting the italic property for a
/// format.
///
/// ```
/// # // This code is available in examples/doc_format_set_font_color.rs
/// #
/// # use rust_xlsxwriter::{Format, Workbook, Color, XlsxError};
/// #
/// # fn main() -> Result<(), XlsxError> {
/// # // Create a new Excel file object.
/// # let mut workbook = Workbook::new();
/// # let worksheet = workbook.add_worksheet();
/// #
/// let format = Format::new().set_font_color(Color::Red);
///
/// worksheet.write_string_with_format(0, 0, "Wheelbarrow", &format)?;
/// #
/// # workbook.save("formats.xlsx")?;
/// #
/// # Ok(())
/// # }
/// ```
///
/// Output file:
///
/// <img
/// src="https://rustxlsxwriter.github.io/images/format_set_font_color.png">
///
pub fn set_font_color<T>(mut self, color: T) -> Format
where
T: IntoColor,
{
let color = color.new_color();
if color.is_valid() {
self.font.color = color;
}
self
}
/// Set the Format font name property.
///
/// Set the font for a cell format. Excel can only display fonts that are
/// installed on the system that it is running on. Therefore it is generally
/// best to use standard Excel fonts.
///
/// # Parameters
///
/// * `font_name` - The font name property.
///
/// # Examples
///
/// The following example demonstrates setting the font name/type for a
/// format.
///
/// ```
/// # // This code is available in examples/doc_format_set_font_name.rs
/// #
/// # use rust_xlsxwriter::{Format, Workbook, XlsxError};
/// #
/// # fn main() -> Result<(), XlsxError> {
/// # // Create a new Excel file object.
/// # let mut workbook = Workbook::new();
/// # let worksheet = workbook.add_worksheet();
/// #
/// let format = Format::new().set_font_name("Avenir Black Oblique");
///
/// worksheet.write_string_with_format(0, 0, "Avenir Black Oblique", &format)?;
///
/// # workbook.save("formats.xlsx")?;
/// #
/// # Ok(())
/// # }
/// ```
/// Output file:
///
/// <img src="https://rustxlsxwriter.github.io/images/format_set_font_name.png">
///
pub fn set_font_name(mut self, font_name: impl Into<String>) -> Format {
self.font.name = font_name.into();
if self.font.name != "Calibri" {
self.font.scheme = String::new();
}
self
}
/// Set the Format font size property.
///
/// Set the font size of the cell format. The size is generally an integer