ironcalc_base/formatter/mod.rs
1pub mod dates;
2pub mod format;
3pub mod lexer;
4pub mod parser;
5
6#[cfg(test)]
7mod test;
8
9// Excel formatting is extremely tricky and I think implementing all it's rules might be borderline impossible.
10// But the essentials are easy to understand.
11//
12// A general Excel formatting string is divided iun four parts:
13//
14// <POSITIVE>;<NEGATIVE>;<ZERO>;<TEXT>
15//
16// * How many decimal digits do you need?
17//
18// 0.000 for exactly three
19// 0.00??? for at least two and up to five
20//
21// * Do you need a thousands separator?
22//
23// #,##
24// # will just write the number
25// #, will write the number up to the thousand separator (if there is nothing else)
26//
27// But #,# and any number of '#' to the right will work just as good. So the following all produce the same results:
28// #,##0.00 #,######0.00 #,0.00
29//
30// For us in IronCalc the most general format string for a number (non-scientific notation) will be:
31//
32// 1. Will have #,## at the beginning if we use the thousand separator
33// 2. Then 0.0* with as many 0 as mandatory decimal places
34// 3. Then ?* with as many question marks as possible decimal places
35//
36// Valid examples:
37// #,##0.??? Thousand separator, up to three decimal digits
38// 0.00 No thousand separator. Two mandatory decimal places
39// 0.0? No thousand separator. One mandatory decimal digit and one extra if present.
40//
41// * Do you what the text in color?
42//
43// Use [RED] or any color in https://www.excelsupersite.com/what-are-the-56-colorindex-colors-in-excel/
44
45// Weird things
46// ============
47//
48// ####0.0E+00 of 12345467.890123 (changing the number of '#' produces results I do not understand)
49// ?www??.????0220000 will format 1234567.890123 to 12345www67.89012223000
50//
51// Things we will not implement
52// ============================
53//
54// 1.- The accounting format can leave white spaces of the size of a particular character. For instance:
55//
56// #,##0.00_);[Red](#,##0.00)
57//
58// Will leave a white space to the right of positive numbers so that they are always aligned with negative numbers
59//
60// 2.- Excel can repeat a character as many times as needed to fill the cell:
61//
62// _($* #,##0_);_($* (#,##0))
63//
64// This will put a '$' sign to the left most (leaving a space the size of '(') and then as many empty spaces as possible
65// and then the number:
66// | $ 234 |
67// | $ 1234 |
68// We can't do this easily in IronCalc
69//
70// 3.- You can use ?/? to format fractions in Excel (this is probably not too hard)
71
72// TOKENs
73// ======
74//
75// * Color [Red] or [Color 23] or [Color23]
76// * Conditions [<100]
77// * Currency [$€]
78// * Space _X when X is any given char
79// * A spacer of chars: *X where X is repeated as much as possible
80// * Literals: $, (, ), :, +, - and space
81// * Text: "Some Text"
82// * Escaped char: \X where X is anything
83// * % appears literal and multiplies number by 100
84// * , If it's in between digit characters it uses the thousand separator. If it is after the digit characters it multiplies by 1000
85// * Digit characters: 0, #, ?
86// * ; Types formatter divider
87// * @ inserts raw text
88// * Scientific literals E+, E-, e+, e-
89// * . period. First one is the decimal point, subsequent are literals.
90
91// d day of the month
92// dd day of the month (padded i.e 05)
93// ddd day of the week abbreviation
94// dddd+ day of the week
95// mmm Abbreviation month
96// mmmm Month name
97// mmmmm First letter of the month
98// y or yy 2-digit year
99// yyy+ 4 digit year
100
101// References
102// ==========
103//
104// [1] https://support.microsoft.com/en-us/office/number-format-codes-5026bbd6-04bc-48cd-bf33-80f18b4eae68?ui=en-us&rs=en-us&ad=us
105// [2] https://developers.google.com/sheets/api/guides/formats
106// [3] https://docs.microsoft.com/en-us/openspecs/office_standards/ms-oe376/0e59abdb-7f4e-48fc-9b89-67832fa11789