Enum NumFormat

Source
pub enum NumFormat {
    Builtin(Builtin),
    Custom(String),
}

Variants§

§

Builtin(Builtin)

§

Custom(String)

Implementations§

Source§

impl NumFormat

Source

pub fn to_builtin_if_possible(self) -> Self

Source

pub fn from_format_string(s: &str) -> Self

Examples found in repository?
examples/tutorial2.rs (line 43)
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}
More examples
Hide additional examples
examples/tutorial3.rs (line 49)
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}
examples/format_num_format.rs (line 22)
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}

Trait Implementations§

Source§

impl Clone for NumFormat

Source§

fn clone(&self) -> NumFormat

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for NumFormat

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for NumFormat

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Hash for NumFormat

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for NumFormat

Source§

fn eq(&self, other: &NumFormat) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for NumFormat

Source§

impl StructuralPartialEq for NumFormat

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.