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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
// format - A module for representing Excel worksheet formulas.
//
// SPDX-License-Identifier: MIT OR Apache-2.0
//
// Copyright 2022-2023, John McNamara, jmcnamara@cpan.org

#![warn(missing_docs)]

use regex::Regex;
use std::borrow::Cow;

/// The `Formula` struct is used to define a worksheet formula.
///
/// The `Formula` struct creates a formula type that can be used to write
/// worksheet formulas.
///
/// In general you would use the
/// [`worksheet.write_formula()`](crate::Worksheet::write_formula) with a string
/// representation of the formula, like this:
///
/// ```
/// # // This code is available in examples/doc_working_with_formulas_intro.rs
/// #
/// # use rust_xlsxwriter::{Workbook, XlsxError};
/// #
/// # fn main() -> Result<(), XlsxError> {
/// #     let mut workbook = Workbook::new();
/// #     let worksheet = workbook.add_worksheet();
/// #
///      worksheet.write_formula(0, 0, "=10*B1 + C1")?;
/// #
/// #      worksheet.write_number(0, 1, 5)?;
/// #      worksheet.write_number(0, 2, 1)?;
/// #
/// #     workbook.save("formula.xlsx")?;
/// #
/// #     Ok(())
/// # }
/// ```
///
/// The formula will then be displayed as expected in Excel:
///
/// <img
/// src="https://rustxlsxwriter.github.io/images/working_with_formulas1.png">
///
/// In order to differentiate a formula from an ordinary string (for example
/// when storing it in a data structure) you can also represent the formula with
/// a [`Formula`] struct:
///
/// ```
/// # // This code is available in examples/doc_working_with_formulas_intro2.rs
/// #
/// # use rust_xlsxwriter::{Formula, Workbook, XlsxError};
/// #
/// # fn main() -> Result<(), XlsxError> {
/// #     let mut workbook = Workbook::new();
/// #     let worksheet = workbook.add_worksheet();
/// #
///     worksheet.write_formula(0, 0, Formula::new("=10*B1 + C1"))?;
/// #
/// #     worksheet.write_number(0, 1, 5)?;
/// #     worksheet.write_number(0, 2, 1)?;
/// #
/// #     workbook.save("formula.xlsx")?;
/// #
/// #     Ok(())
/// # }
/// ```
///
/// Using a `Formula` struct also allows you to write a formula using the
/// generic [`worksheet.write()`](crate::Worksheet::write) method:
///
/// ```
/// # // This code is available in examples/doc_working_with_formulas_intro3.rs
/// #
/// # use rust_xlsxwriter::{Formula, Workbook, XlsxError};
/// #
/// # fn main() -> Result<(), XlsxError> {
/// #     let mut workbook = Workbook::new();
/// #     let worksheet = workbook.add_worksheet();
/// #
///     worksheet.write(0, 0, Formula::new("=10*B1 + C1"))?;
/// #
/// #     worksheet.write_number(0, 1, 5)?;
/// #     worksheet.write_number(0, 2, 1)?;
/// #
/// #     workbook.save("formula.xlsx")?;
/// #
/// #     Ok(())
/// # }
/// ```
///
/// As shown in the examples above you can write a formula and expect to have
/// the result appear in Excel. However, there are a few potential issues and
/// differences that the user of `rust_xlsxwriter` should be aware of. These are
/// explained in the sections below.
///
/// # Formula Results
///
/// The `rust_xlsxwriter` library doesn't calculate the result of a formula and
/// instead stores the value "0" as the formula result. It then sets a global
/// flag in the XLSX file to say that all formulas and functions should be
/// recalculated when the file is opened.
///
/// This works fine with Excel and the majority of spreadsheet applications.
/// However, applications that don't have a facility to calculate formulas will
/// only display the "0" result. Examples of such applications are Excel viewers,
/// PDF converters, and some mobile device applications.
///
/// If required, it is also possible to specify the calculated result of the
/// formula using the [`worksheet.set_formula_result()`] method or the
/// [`formula.set_result()`](Formula::set_result) method:
///
/// ```
/// # // This code is available in examples/doc_worksheet_set_formula_result.rs
/// #
/// # use rust_xlsxwriter::{Formula, Workbook, XlsxError};
/// #
/// # fn main() -> Result<(), XlsxError> {
/// #     // Create a new Excel file object.
/// #     let mut workbook = Workbook::new();
/// #
/// #     // Add a worksheet to the workbook.
/// #     let worksheet = workbook.add_worksheet();
/// #
///     // Using the formula string syntax.
///     worksheet
///         .write_formula(0, 0, "1+1")?
///         .set_formula_result(0, 0, "2");
///
///     // Or using a Formula type.
///     worksheet.write_formula(1, 0, Formula::new("2+2").set_result("4"))?;
/// #
/// #     workbook.save("formulas.xlsx")?;
/// #
/// #     Ok(())
/// # }
/// ```
///
/// <img src="https://rustxlsxwriter.github.io/images/worksheet_set_formula_result.png">
///
/// One common spreadsheet application where the formula recalculation doesn't
/// work is `LibreOffice` (see the following [issue report]). If you wish to
/// force recalculation in `LibreOffice` you can use the
/// [`worksheet.set_formula_result_default()`] method to set the default result
/// to an empty string:
///
/// ```
/// # // This code is available in examples/doc_worksheet_set_formula_result_default.rs
/// #
/// # use rust_xlsxwriter::{Workbook, XlsxError};
/// #
/// # fn main() -> Result<(), XlsxError> {
/// #     // Create a new Excel file object.
/// #     let mut workbook = Workbook::new();
/// #
/// #     // Add a worksheet to the workbook.
/// #     let worksheet = workbook.add_worksheet();
/// #
///     worksheet.set_formula_result_default("");
/// #
/// #     workbook.save("formulas.xlsx")?;
/// #
/// #     Ok(())
/// # }
/// ```
///
/// [`worksheet.set_formula_result()`]: crate::Worksheet::set_formula_result
/// [`worksheet.set_formula_result_default()`]:
///     crate::Worksheet::set_formula_result_default
/// [issue report]: https://bugs.documentfoundation.org/show_bug.cgi?id=144819
///
/// # Non US Excel functions and syntax
///
/// Excel stores formulas in the format of the US English version, regardless of
/// the language or locale of the end-user's version of Excel. Therefore all
/// formula function names written using `rust_xlsxwriter` must be in English.
/// In addition, formulas must be written with the US style separator/range
/// operator which is a comma (not semi-colon).
///
/// Some examples of how formulas should and shouldn't be written are shown
/// below:
///
/// ```
/// # // This code is available in examples/doc_working_with_formulas_syntax.rs
/// #
/// # use rust_xlsxwriter::{Workbook, XlsxError};
/// #
/// # fn main() -> Result<(), XlsxError> {
/// #     let mut workbook = Workbook::new();
/// #     let worksheet = workbook.add_worksheet();
/// #
///     // OK.
///     worksheet.write_formula(0, 0, "=SUM(1, 2, 3)")?;
///
///     // Semi-colon separator. Causes Excel error on file opening.
///     worksheet.write_formula(1, 0, "=SUM(1; 2; 3)")?;
///
///     // French function name. Causes Excel error on file opening.
///     worksheet.write_formula(2, 0, "=SOMME(1, 2, 3)")?;
/// #
/// #     workbook.save("formula.xlsx")?;
/// #
/// #     Ok(())
/// # }
/// ```
///
/// If you have a non-English version of Excel you can use the following
/// multi-lingual [Formula Translator](http://en.excel-translator.de/language/)
/// to help you convert the formula. It can also replace semi-colons with
/// commas.
///
///
/// # Dynamic Array support
///
/// In Office 365 Excel introduced the concept of "Dynamic Arrays" and new
/// functions that use them. The new functions are:
///
/// - `BYCOL`
/// - `BYROW`
/// - `CHOOSECOLS`
/// - `CHOOSEROWS`
/// - `DROP`
/// - `EXPAND`
/// - `FILTER`
/// - `HSTACK`
/// - `LAMBDA`
/// - `MAKEARRAY`
/// - `MAP`
/// - `RANDARRAY`
/// - `REDUCE`
/// - `SCAN`
/// - `SEQUENCE`
/// - `SORT`
/// - `SORTBY`
/// - `SWITCH`
/// - `TAKE`
/// - `TEXTSPLIT`
/// - `TOCOL`
/// - `TOROW`
/// - `UNIQUE`
/// - `VSTACK`
/// - `WRAPCOLS`
/// - `WRAPROWS`
/// - `XLOOKUP`
///
/// The following special case functions were also added with Dynamic Arrays:
///
/// - `SINGLE`: Explained below in [The Implicit Intersection Operator
///   "@"](#the-implicit-intersection-operator-)
/// - `ANCHORARRAY`:  Explained below in [The Spilled Range Operator
///   "#"](#the-spilled-range-operator-)
///
///
/// ## Dynamic Arrays - An introduction
///
/// Dynamic arrays in Excel are ranges of return values that can change size
/// based on the results. For example, a function such as `FILTER()` returns an
/// array of values that can vary in size depending on the the filter results
///
/// ```text
/// worksheet.write_dynamic_formula(1, 5, "=FILTER(A1:D17,C1:C17=K2)")?;
/// ```
///
/// This formula gives the results shown in the image below. The dynamic range
/// here is "F2:I5" but it can vary based on the filter criteria.
///
/// <img src="https://rustxlsxwriter.github.io/images/dynamic_arrays02.png">
///
/// It is also possible to get dynamic array behavior with older Excel
/// functions. For example, the Excel function `"=LEN(A1)"` applies to a single
/// cell and returns a single value but it can also apply to a range of cells
/// and return a range of values using an array formula like `"{=LEN(A1:A3)}"`.
/// This type of "static" array behavior is referred to as a CSE
/// (Ctrl+Shift+Enter) formula and has existed in Excel since early versions. In
/// Office 365 Excel updated and extended this behavior to create the concept of
/// dynamic arrays. In Excel 365 you can now write the previous LEN function as
/// `"=LEN(A1:A3)"` and get a dynamic range of return values:
///
/// <img src="https://rustxlsxwriter.github.io/images/intersection03.png">
///
/// The difference between the two types of array functions is explained in the
/// Microsoft documentation on [Dynamic array formulas vs. legacy CSE array
/// formulas].
///
///  In `rust_xlsxwriter` you can use the [`worksheet.write_array_formula()`]
/// function to get a static/CSE range and
/// [`worksheet.write_dynamic_array_formula()`] or
/// [`worksheet.write_dynamic_formula()`] to get a dynamic range.
///
/// [`worksheet.write_array_formula()`]: crate::Worksheet::write_array_formula
/// [`worksheet.write_dynamic_formula()`]:
///     crate::Worksheet::write_dynamic_formula
/// [`worksheet.write_dynamic_array_formula()`]:
///     crate::Worksheet::write_dynamic_array_formula
///
/// [Dynamic array formulas in Excel]:
///     https://exceljet.net/dynamic-array-formulas-in-excel
/// [Dynamic array formulas vs. legacy CSE array formulas]:
///     https://support.microsoft.com/en-us/office/dynamic-array-formulas-vs-legacy-cse-array-formulas-ca421f1b-fbb2-4c99-9924-df571bd4f1b4
///
/// The `worksheet.write_dynamic_array_formula()` function takes a `(first_row,
/// first_col, last_row, last_col)` cell range to define the area that the
/// formula applies to. However, since the range is dynamic this generally won't
/// be known in advance in which case you can specify the range with the same
/// start and end cell. The following range is "F2:F2":
///
/// ```text
///     worksheet.write_dynamic_array_formula(1, 5, 1, 5, "=FILTER(A1:D17,C1:C17=K2)")?;
/// ```
/// As a syntactic shortcut you can use the `worksheet.write_dynamic_formula()`
/// function which only requires the start cell:
///
/// ```text
///    worksheet.write_dynamic_formula(1, 5, "=FILTER(A1:D17,C1:C17=K2)")?;
/// ```
///
/// For a wider and more general introduction to dynamic arrays see the
/// following: [Dynamic array formulas in Excel].
///
///
/// ## The Implicit Intersection Operator "@"
///
/// The Implicit Intersection Operator, "@", is used by Excel 365 to indicate a
/// position in a formula that is implicitly returning a single value when a
/// range or an array could be returned.
///
/// We can see how this operator works in practice by considering the formula we
/// used in the last section: `=LEN(A1:A3)`. In Excel versions without support
/// for dynamic arrays, i.e. prior to Excel 365, this formula would operate on a
/// single value from the input range and return a single value, like the
/// following in Excel 2011:
///
/// <img src="https://rustxlsxwriter.github.io/images/intersection01.png">
///
/// There is an implicit conversion here of the range of input values, "A1:A3",
/// to a single value "A1". Since this was the default behavior of older
/// versions of Excel this conversion isn't highlighted in any way. But if you
/// open the same file in Excel 365 it will appear as follows:
///
/// <img src="https://rustxlsxwriter.github.io/images/intersection02.png">
///
/// The result of the formula is the same (this is important to note) and it
/// still operates on, and returns, a single value. However the formula now
/// contains a "@" operator to show that it is implicitly using a single value
/// from the given range.
///
/// In Excel 365, and with [`worksheet.write_dynamic_formula()`] in
/// `rust_xlsxwriter`, it would operate on the entire range and return an array
/// of values:
///
/// <img src="https://rustxlsxwriter.github.io/images/intersection03.png">
///
/// If you are encountering the Implicit Intersection Operator "@" for the first
/// time then it is probably from a point of view of "why is Excel or
/// `rust_xlsxwriter` putting @s in my formulas". In practical terms if you
/// encounter this operator, and you don't intend it to be there, then you
/// should probably write the formula as a CSE or dynamic array function using
/// [`worksheet.write_array_formula()`] or
/// [`worksheet.write_dynamic_array_formula()`]
///
///
/// A full explanation of this operator is given in the Microsoft documentation
/// on the [Implicit intersection operator: @].
///
/// [Implicit intersection operator: @]:
///     https://support.microsoft.com/en-us/office/implicit-intersection-operator-ce3be07b-0101-4450-a24e-c1c999be2b34?ui=en-us&rs=en-us&ad=us>
///
/// One important thing to note is that the "@" operator isn't stored with the
/// formula. It is just displayed by Excel 365 when reading "legacy" formulas.
/// However, it is possible to write it to a formula, if necessary, using
/// `SINGLE()`. The rare cases where this may be necessary are shown in the
/// linked document in the previous paragraph.
///
///
/// ## The Spilled Range Operator "#"
///
/// In the sections above we saw that dynamic array formulas can return variable
/// sized ranges of results. The Excel documentation refers to this as a
/// "Spilled" range/array from the idea that the results spill into the required
/// number of cells. This is explained in the Microsoft documentation on
/// [Dynamic array formulas and spilled array behavior].
///
/// [Dynamic array formulas and spilled array behavior]:
///     https://support.microsoft.com/en-us/office/dynamic-array-formulas-and-spilled-array-behavior-205c6b06-03ba-4151-89a1-87a7eb36e531
///
///
/// Since a spilled range is variable in size a new operator is required to
/// refer to the range. This operator is the [Spilled range operator] and it is
/// represented by "#". For example, the range `F2#` in the image below is used
/// to refer to a dynamic array returned by `UNIQUE()` in the cell `F2`:
///
/// [Spilled range operator]:
///     https://support.microsoft.com/en-us/office/spilled-range-operator-3dd5899f-bca2-4b9d-a172-3eae9ac22efd
///
/// <img src="https://rustxlsxwriter.github.io/images/spill01.png">
///
/// Unfortunately, Excel doesn't store the operator in the formula like this and
/// in `rust_xlsxwriter` you need to use the explicit function `ANCHORARRAY()`
/// to refer to a spilled range. The example in the image above was generated
/// using the following formula:
///
/// ```text
///     worksheet.write_dynamic_formula(1, 9, "=COUNTA(ANCHORARRAY(F2))")?;
/// ```
///
/// ## The Excel 365 LAMBDA() function
///
/// Recent versions of Excel 365 have introduced a powerful new function/feature
/// called `LAMBDA()`. This is similar to closure expressions in Rust or [lambda
/// expressions] in C++ (and other languages).
///
/// [lambda expressions]:
///     https://docs.microsoft.com/en-us/cpp/cpp/lambda-expressions-in-cpp?view=msvc-160
///
///
/// Consider the following Excel example which converts the variable `temp` from
/// Fahrenheit to Celsius:
///
/// ```text
///     LAMBDA(temp, (5/9) * (temp-32))
/// ```
///
/// This could be called in Excel with an argument:
///
/// ```text
///     =LAMBDA(temp, (5/9) * (temp-32))(212)
/// ```
///
/// Or assigned to a defined name and called as a user defined function:
///
/// ```text
///     =ToCelsius(212)
/// ```
///
/// A rust xlsxwriter example that replicates the described Excel functionality
/// is shown below:
///
///
/// ```
/// # // This code is available in examples/app_lambda.rs
/// #
/// use rust_xlsxwriter::{Workbook, XlsxError};
///
/// fn main() -> Result<(), XlsxError> {
///     // Create a new Excel file object.
///     let mut workbook = Workbook::new();
///
///     // Write a Lambda function to convert Fahrenheit to Celsius to a cell as a
///     // defined name and use that to calculate a value.
///     //
///     // Note that the formula name is prefixed with "_xlfn." (this is normally
///     // converted automatically by write_formula*() but isn't for defined names)
///     // and note that the lambda function parameters are prefixed with "_xlpm.".
///     // These prefixes won't show up in Excel.
///     workbook.define_name(
///         "ToCelsius",
///         "=_xlfn.LAMBDA(_xlpm.temp, (5/9) * (_xlpm.temp-32))",
///     )?;
///
///     // Add a worksheet to the workbook.
///     let worksheet = workbook.add_worksheet();
///
///     // Write the same Lambda function as a cell formula.
///     //
///     // Note that the lambda function parameters must be prefixed with "_xlpm.".
///     // These prefixes won't show up in Excel.
///     worksheet.write_formula(0, 0, "=LAMBDA(_xlpm.temp, (5/9) * (_xlpm.temp-32))(32)")?;
///
///     // The user defined name needs to be written explicitly as a dynamic array
///     // formula.
///     worksheet.write_dynamic_formula(1, 0, "=ToCelsius(212)")?;
///
///     // Save the file to disk.
///     workbook.save("lambda.xlsx")?;
///
///     Ok(())
/// }
/// ```
///
/// Note, that the formula name must have a `_xlfn.` prefix and the parameters
/// in the `LAMBDA()` function must have a `_xlpm.`  prefix for compatibility
/// with how the formulas are stored in Excel. These prefixes won't show up in
/// the formula, as shown in the image below.
///
/// <img src="https://rustxlsxwriter.github.io/images/app_lambda.png">
///
/// The `LET()` function is often used in conjunction with `LAMBDA()` to assign
/// names to calculation results.
///
///
/// # Formulas added in Excel 2010 and later
///
/// Excel 2010 and later versions added functions which weren't defined in the
/// original file specification. These functions are referred to by Microsoft as
/// "Future Functions". Examples of these functions are `ACOT`, `CHISQ.DIST.RT`
/// , `CONFIDENCE.NORM`, `STDEV.P`, `STDEV.S` and `WORKDAY.INTL`.
///
/// When written using [`worksheet.write_formula()`] these functions need to be
/// fully qualified with a prefix such as `_xlfn.`, as shown the table in the
/// next section below.
///
/// [`worksheet.write_formula()`]: crate::Worksheet::method.write_formula
///
/// If the prefix isn't included you will get an Excel function name error. For
/// example:
///
/// ```text
///     worksheet.write_formula(0, 0, "=STDEV.S(B1:B5)")?;
/// ```
///
/// <img
/// src="https://rustxlsxwriter.github.io/images/working_with_formulas3.png">
///
/// If the `_xlfn.` prefix is included you will get the correct result:
///
/// ```text
///     worksheet.write_formula(0, 0, "=_xlfn.STDEV.S(B1:B5)")?;
/// ```
///
/// <img
/// src="https://rustxlsxwriter.github.io/images/working_with_formulas2.png">
///
/// Note that the function is displayed by Excel without the prefix.
///
/// Alternatively you can use the [`worksheet.use_future_functions()`] function
/// to have `rust_xlsxwriter` automatically handle future functions for you:
///
/// [`worksheet.use_future_functions()`]: crate::Worksheet::use_future_functions
///
/// ```text
///    worksheet.use_future_functions(true);
///    worksheet.write_formula(0, 0, "=STDEV.S(B1:B5)")?;
/// ```
///
/// Or if you are using a [`Formula`] struct you can use the
/// [`Formula::use_future_functions()`](Formula::use_future_functions) method:
///
/// ```text
///     worksheet.write_formula(0, 0, Formula::new("=STDEV.S(B1:B5)").use_future_functions())?;
/// ```
///
/// This will give the same correct result as the image above.
///
///
/// ## List of Future Functions
///
/// The following list is taken from [MS XLSX extensions documentation on future
/// functions].
///
/// [MS XLSX extensions documentation on future functions]:
///     http://msdn.microsoft.com/en-us/library/dd907480%28v=office.12%29.aspx
///

///
/// | Future Functions                 |
/// | -------------------------------- |
/// | `_xlfn.ACOTH`                    |
/// | `_xlfn.ACOT`                     |
/// | `_xlfn.AGGREGATE`                |
/// | `_xlfn.ARABIC`                   |
/// | `_xlfn.ARRAYTOTEXT`              |
/// | `_xlfn.BASE`                     |
/// | `_xlfn.BETA.DIST`                |
/// | `_xlfn.BETA.INV`                 |
/// | `_xlfn.BINOM.DIST.RANGE`         |
/// | `_xlfn.BINOM.DIST`               |
/// | `_xlfn.BINOM.INV`                |
/// | `_xlfn.BITAND`                   |
/// | `_xlfn.BITLSHIFT`                |
/// | `_xlfn.BITOR`                    |
/// | `_xlfn.BITRSHIFT`                |
/// | `_xlfn.BITXOR`                   |
/// | `_xlfn.CEILING.MATH`             |
/// | `_xlfn.CEILING.PRECISE`          |
/// | `_xlfn.CHISQ.DIST.RT`            |
/// | `_xlfn.CHISQ.DIST`               |
/// | `_xlfn.CHISQ.INV.RT`             |
/// | `_xlfn.CHISQ.INV`                |
/// | `_xlfn.CHISQ.TEST`               |
/// | `_xlfn.COMBINA`                  |
/// | `_xlfn.CONCAT`                   |
/// | `_xlfn.CONFIDENCE.NORM`          |
/// | `_xlfn.CONFIDENCE.T`             |
/// | `_xlfn.COTH`                     |
/// | `_xlfn.COT`                      |
/// | `_xlfn.COVARIANCE.P`             |
/// | `_xlfn.COVARIANCE.S`             |
/// | `_xlfn.CSCH`                     |
/// | `_xlfn.CSC`                      |
/// | `_xlfn.DAYS`                     |
/// | `_xlfn.DECIMAL`                  |
/// | `ECMA.CEILING`                   |
/// | `_xlfn.ERF.PRECISE`              |
/// | `_xlfn.ERFC.PRECISE`             |
/// | `_xlfn.EXPON.DIST`               |
/// | `_xlfn.F.DIST.RT`                |
/// | `_xlfn.F.DIST`                   |
/// | `_xlfn.F.INV.RT`                 |
/// | `_xlfn.F.INV`                    |
/// | `_xlfn.F.TEST`                   |
/// | `_xlfn.FILTERXML`                |
/// | `_xlfn.FLOOR.MATH`               |
/// | `_xlfn.FLOOR.PRECISE`            |
/// | `_xlfn.FORECAST.ETS.CONFINT`     |
/// | `_xlfn.FORECAST.ETS.SEASONALITY` |
/// | `_xlfn.FORECAST.ETS.STAT`        |
/// | `_xlfn.FORECAST.ETS`             |
/// | `_xlfn.FORECAST.LINEAR`          |
/// | `_xlfn.FORMULATEXT`              |
/// | `_xlfn.GAMMA.DIST`               |
/// | `_xlfn.GAMMA.INV`                |
/// | `_xlfn.GAMMALN.PRECISE`          |
/// | `_xlfn.GAMMA`                    |
/// | `_xlfn.GAUSS`                    |
/// | `_xlfn.HYPGEOM.DIST`             |
/// | `_xlfn.IFNA`                     |
/// | `_xlfn.IFS`                      |
/// | `_xlfn.IMAGE`                   |
/// | `_xlfn.IMCOSH`                   |
/// | `_xlfn.IMCOT`                    |
/// | `_xlfn.IMCSCH`                   |
/// | `_xlfn.IMCSC`                    |
/// | `_xlfn.IMSECH`                   |
/// | `_xlfn.IMSEC`                    |
/// | `_xlfn.IMSINH`                   |
/// | `_xlfn.IMTAN`                    |
/// | `_xlfn.ISFORMULA`                |
/// | `_xlfn.ISOMITTED`                |
/// | `_xlfn.ISOWEEKNUM`               |
/// | `_xlfn.LET`                      |
/// | `_xlfn.LOGNORM.DIST`             |
/// | `_xlfn.LOGNORM.INV`              |
/// | `_xlfn.MAXIFS`                   |
/// | `_xlfn.MINIFS`                   |
/// | `_xlfn.MODE.MULT`                |
/// | `_xlfn.MODE.SNGL`                |
/// | `_xlfn.MUNIT`                    |
/// | `_xlfn.NEGBINOM.DIST`            |
/// | `NETWORKDAYS.INTL`               |
/// | `_xlfn.NORM.DIST`                |
/// | `_xlfn.NORM.INV`                 |
/// | `_xlfn.NORM.S.DIST`              |
/// | `_xlfn.NORM.S.INV`               |
/// | `_xlfn.NUMBERVALUE`              |
/// | `_xlfn.PDURATION`                |
/// | `_xlfn.PERCENTILE.EXC`           |
/// | `_xlfn.PERCENTILE.INC`           |
/// | `_xlfn.PERCENTRANK.EXC`          |
/// | `_xlfn.PERCENTRANK.INC`          |
/// | `_xlfn.PERMUTATIONA`             |
/// | `_xlfn.PHI`                      |
/// | `_xlfn.POISSON.DIST`             |
/// | `_xlfn.QUARTILE.EXC`             |
/// | `_xlfn.QUARTILE.INC`             |
/// | `_xlfn.QUERYSTRING`              |
/// | `_xlfn.RANK.AVG`                 |
/// | `_xlfn.RANK.EQ`                  |
/// | `_xlfn.RRI`                      |
/// | `_xlfn.SECH`                     |
/// | `_xlfn.SEC`                      |
/// | `_xlfn.SHEETS`                   |
/// | `_xlfn.SHEET`                    |
/// | `_xlfn.SKEW.P`                   |
/// | `_xlfn.STDEV.P`                  |
/// | `_xlfn.STDEV.S`                  |
/// | `_xlfn.T.DIST.2T`                |
/// | `_xlfn.T.DIST.RT`                |
/// | `_xlfn.T.DIST`                   |
/// | `_xlfn.T.INV.2T`                 |
/// | `_xlfn.T.INV`                    |
/// | `_xlfn.T.TEST`                   |
/// | `_xlfn.TEXTAFTER`                |
/// | `_xlfn.TEXTBEFORE`               |
/// | `_xlfn.TEXTJOIN`                 |
/// | `_xlfn.UNICHAR`                  |
/// | `_xlfn.UNICODE`                  |
/// | `_xlfn.VALUETOTEXT`              |
/// | `_xlfn.VAR.P`                    |
/// | `_xlfn.VAR.S`                    |
/// | `_xlfn.WEBSERVICE`               |
/// | `_xlfn.WEIBULL.DIST`             |
/// | `WORKDAY.INTL`                   |
/// | `_xlfn.XMATCH`                   |
/// | `_xlfn.XOR`                      |
/// | `_xlfn.Z.TEST`                   |
///

/// The dynamic array functions shown in the [Dynamic Array
/// support](dynamic_arrays.md) section are also future functions, however the
/// `rust_xlsxwriter` library automatically adds the required prefixes on the
/// fly so you don't have to add them explicitly.

///
/// | Dynamic Array Functions          |
/// | -------------------------------- |
/// | `_xlfn.ANCHORARRAY`              |
/// | `_xlfn.BYCOL`                    |
/// | `_xlfn.BYROW`                    |
/// | `_xlfn.CHOOSECOLS`               |
/// | `_xlfn.CHOOSEROWS`               |
/// | `_xlfn.DROP`                     |
/// | `_xlfn.EXPAND`                   |
/// | `_xlfn._xlws.FILTER`             |
/// | `_xlfn.HSTACK`                   |
/// | `_xlfn.LAMBDA`                   |
/// | `_xlfn.MAKEARRAY`                |
/// | `_xlfn.MAP`                      |
/// | `_xlfn.RANDARRAY`                |
/// | `_xlfn.REDUCE`                   |
/// | `_xlfn.SCAN`                     |
/// | `_xlfn.SINGLE`                   |
/// | `_xlfn.SEQUENCE`                 |
/// | `_xlfn._xlws.SORT`               |
/// | `_xlfn.SORTBY`                   |
/// | `_xlfn.SWITCH`                   |
/// | `_xlfn.TAKE`                     |
/// | `_xlfn.TEXTSPLIT`                |
/// | `_xlfn.TOCOL`                    |
/// | `_xlfn.TOROW`                    |
/// | `_xlfn.UNIQUE`                   |
/// | `_xlfn.VSTACK`                   |
/// | `_xlfn.WRAPCOLS`                 |
/// | `_xlfn.WRAPROWS`                 |
/// | `_xlfn.XLOOKUP`                  |
///

/// # Dealing with formula errors
///
/// If there is an error in the syntax of a formula it is usually displayed in
/// Excel as `#NAME?`. Alternatively you may get a warning from Excel when the
/// file is loaded. If you encounter an error like this you can debug it using
/// the following steps:
///
/// 1. Ensure the formula is valid in Excel by copying and pasting it into a
///    cell. Note, this should be done in Excel and **not** other applications
///    such as `OpenOffice` or `LibreOffice` since they may have slightly
///    different syntax.
///
/// 2. Ensure the formula is using comma separators instead of semi-colons, see
///    [Non US Excel functions and syntax](#non-us-excel-functions-and-syntax).
///
/// 3. Ensure the formula is in English, see [Non US Excel functions and
///    syntax](#non-us-excel-functions-and-syntax).
///
/// 4. Ensure that the formula doesn't contain an Excel 2010+ future function,
///    see [Formulas added in Excel 2010 and
///    later](#formulas-added-in-excel-2010-and-later). If it does then ensure
///    that the correct prefix is used.
///
/// 5. If the function loads in Excel but appears with one or more `@` symbols
///    added then it is probably an array function and should be written using
///    [`worksheet.write_array_formula()`] or
///    [`worksheet.write_dynamic_array_formula()`] (see also [Dynamic Array
///    support](dynamic_arrays.md)).
///
/// Finally if you have completed all the previous steps and still get a
/// `#NAME?` error you can examine a valid Excel file to see what the correct
/// syntax should be. To do this you should create a valid formula in Excel and
/// save the file. You can then examine the XML in the unzipped file.
///
/// The following shows how to do that using Linux `unzip` and libxml's
/// [xmllint](http://xmlsoft.org/xmllint.html) to format the XML for clarity:
///
/// ```bash
///     $ unzip myfile.xlsx -d myfile
///     $ xmllint --format myfile/xl/worksheets/sheet1.xml | grep '</f>'
///
///             <f>SUM(1, 2, 3)</f>
/// ```
///
#[derive(Clone)]
pub struct Formula {
    formula_string: String,
    expand_future_functions: bool,
    expand_table_functions: bool,
    pub(crate) result: Box<str>,
}

impl Formula {
    /// Create a new `Formula` struct instance.
    ///
    /// # Parameters
    ///
    /// `formula` - A string like type representing an Excel formula.
    ///
    pub fn new(formula: impl Into<String>) -> Formula {
        Formula {
            formula_string: formula.into(),
            expand_future_functions: false,
            expand_table_functions: false,
            result: Box::from(""),
        }
    }

    /// Specify the result of a formula.
    ///
    /// As explained above in the section on [Formula
    /// Results](#formula-results) it is occasionally necessary to specify the
    /// result of a formula. This can be done using the `set_result()` method.
    ///
    /// # Parameters
    ///
    /// `result` - The formula result, as a string or string like type.
    ///
    /// # Examples
    ///
    /// The following example demonstrates manually setting the result of a
    /// formula. Note, this is only required for non-Excel applications that
    /// don't calculate formula results.
    ///
    /// ```
    /// # // This code is available in examples/doc_worksheet_set_formula_result.rs
    /// #
    /// # use rust_xlsxwriter::{Formula, Workbook, XlsxError};
    /// #
    /// # fn main() -> Result<(), XlsxError> {
    /// #     // Create a new Excel file object.
    /// #     let mut workbook = Workbook::new();
    /// #
    /// #     // Add a worksheet to the workbook.
    /// #     let worksheet = workbook.add_worksheet();
    /// #
    ///     // Using string syntax.
    ///     worksheet
    ///         .write_formula(0, 0, "1+1")?
    ///         .set_formula_result(0, 0, "2");
    ///
    ///     // Or using a Formula type.
    ///     worksheet.write_formula(1, 0, Formula::new("2+2").set_result("4"))?;
    /// #
    /// #     workbook.save("formulas.xlsx")?;
    /// #
    /// #     Ok(())
    /// # }
    /// ```
    ///
    /// Output file:
    ///
    /// <img
    /// src="https://rustxlsxwriter.github.io/images/worksheet_set_formula_result.png">
    ///
    pub fn set_result(mut self, result: impl Into<String>) -> Formula {
        self.result = Box::from(result.into());
        self
    }

    /// Enable the use of newer Excel future functions in the formula.
    ///
    /// As explained above in [Formulas added in Excel 2010 and
    /// later](#formulas-added-in-excel-2010-and-later), functions have been
    /// added to Excel which weren't defined in the original file specification.
    /// These functions are referred to by Microsoft as "Future Functions".
    ///
    /// When written using
    /// [`write_formula()`](crate::Worksheet::write_formula()) these functions
    /// need to be fully qualified with a prefix such as `_xlfn.`
    ///
    /// Alternatively you can use the
    /// [`worksheet.use_future_functions()`](crate::Worksheet::use_future_functions)
    /// function to have `rust_xlsxwriter` automatically handle future functions
    /// for you, or use a [`Formula`] struct and the
    /// [`Formula::use_future_functions()`](Formula::use_future_functions)
    /// method, see below.
    ///
    /// # Examples
    ///
    /// The following example demonstrates different ways to handle writing
    /// Future Functions to a worksheet.
    ///
    /// ```
    /// # // This code is available in examples/doc_worksheet_use_future_functions.rs
    /// #
    /// # use rust_xlsxwriter::{Formula, Workbook, XlsxError};
    /// #
    /// # fn main() -> Result<(), XlsxError> {
    /// #     let mut workbook = Workbook::new();
    /// #
    /// #     // Add a worksheet to the workbook.
    /// #     let worksheet = workbook.add_worksheet();
    /// #
    ///     // The following is a "Future" function and will generate a "#NAME?" warning
    ///     // in Excel.
    ///     worksheet.write_formula(0, 0, "=ISFORMULA($B$1)")?;
    ///
    ///     // The following adds the required prefix. This will work without a warning.
    ///     worksheet.write_formula(1, 0, "=_xlfn.ISFORMULA($B$1)")?;
    ///
    ///     // The following uses a Formula object and expands out any future functions.
    ///     // This also works without a warning.
    ///     worksheet.write_formula(2, 0, Formula::new("=ISFORMULA($B$1)").use_future_functions())?;
    ///
    ///     // The following expands out all future functions used in the worksheet from
    ///     // this point forward. This also works without a warning.
    ///     worksheet.use_future_functions(true);
    ///     worksheet.write_formula(3, 0, "=ISFORMULA($B$1)")?;
    /// #
    /// #     workbook.save("worksheet.xlsx")?;
    /// #
    /// #     Ok(())
    /// # }
    /// ```
    ///
    /// Output file:
    ///
    /// <img
    /// src="https://rustxlsxwriter.github.io/images/worksheet_use_future_functions.png">
    ///
    pub fn use_future_functions(mut self) -> Formula {
        self.expand_future_functions = true;
        self
    }

    /// Enable backward compatible formulas in table.
    ///
    /// Worksheet tables in Excel (see [`Table`](crate::Table)) use "Structured
    /// References" in formulas like this:
    ///
    /// ```text
    ///    "SUM(Table1[@[Column1]:[Column3]])"
    /// ```
    ///
    /// The `@` is a shorthand for the more explicit, but more verbose, `[#This
    /// Row],` syntax. Excel automatically converts the structured row reference
    /// to the shorter version if the table has more than one row. However, it
    /// **stores** the formula in the longer `[#This Row],` syntax so
    /// `rust_xlsxwriter` must also store it in that format.
    ///
    /// Setting the `use_table_functions()` property will ensure this conversion
    /// is made automatically when writing the formula. In addition, the
    /// conversion is done automatically if you add a column formula to a table
    /// that is passed to
    /// [`worksheet.add_table()`](crate::Worksheet::add_table()).
    ///
    pub fn use_table_functions(mut self) -> Formula {
        self.expand_table_functions = true;
        self
    }

    // Check of a dynamic function/formula.
    pub(crate) fn is_dynamic_function(&self) -> bool {
        lazy_static! {
            static ref DYNAMIC_FUNCTION: Regex = Regex::new(
                r"\b(ANCHORARRAY|BYCOL|BYROW|CHOOSECOLS|CHOOSEROWS|DROP|EXPAND|FILTER|HSTACK|LAMBDA|MAKEARRAY|MAP|RANDARRAY|REDUCE|SCAN|SEQUENCE|SINGLE|SORT|SORTBY|SWITCH|TAKE|TEXTSPLIT|TOCOL|TOROW|UNIQUE|VSTACK|WRAPCOLS|WRAPROWS|XLOOKUP)\("
            )
            .unwrap();
        }
        DYNAMIC_FUNCTION.is_match(&self.formula_string)
    }

    // Utility method to optionally strip equal sign and array braces from a
    // formula and also expand out future and dynamic array formulas.
    pub(crate) fn expand_formula(&self, global_expand_future_functions: bool) -> Box<str> {
        let mut formula = self.formula_string.as_str();

        // Remove array formula braces and the leading = if they exist.
        if let Some(stripped) = formula.strip_prefix('{') {
            formula = stripped;
        }
        if let Some(stripped) = formula.strip_prefix('=') {
            formula = stripped;
        }
        if let Some(stripped) = formula.strip_suffix('}') {
            formula = stripped;
        }

        // Exit if formula is already expanded by the user.
        if formula.contains("_xlfn.") {
            return Box::from(formula);
        }

        // Expand dynamic formulas.
        let escaped_formula = Self::escape_dynamic_formulas1(formula);
        let escaped_formula = Self::escape_dynamic_formulas2(&escaped_formula);

        let formula = if self.expand_future_functions || global_expand_future_functions {
            Self::escape_future_functions(&escaped_formula)
        } else {
            escaped_formula
        };

        let formula = if self.expand_table_functions {
            Self::escape_table_functions(&formula)
        } else {
            formula
        };

        Box::from(formula)
    }

    // Escape/expand the dynamic formula _xlfn functions.
    fn escape_dynamic_formulas1(formula: &str) -> Cow<str> {
        lazy_static! {
            static ref XLFN: Regex = Regex::new(
                r"\b(ANCHORARRAY|BYCOL|BYROW|CHOOSECOLS|CHOOSEROWS|DROP|EXPAND|HSTACK|LAMBDA|MAKEARRAY|MAP|RANDARRAY|REDUCE|SCAN|SEQUENCE|SINGLE|SORTBY|SWITCH|TAKE|TEXTSPLIT|TOCOL|TOROW|UNIQUE|VSTACK|WRAPCOLS|WRAPROWS|XLOOKUP)\("
            )
            .unwrap();
        }
        XLFN.replace_all(formula, "_xlfn.$1(")
    }

    // Escape/expand the dynamic formula _xlfn._xlws. functions.
    fn escape_dynamic_formulas2(formula: &str) -> Cow<str> {
        lazy_static! {
            static ref XLWS: Regex = Regex::new(r"\b(FILTER|SORT)\(").unwrap();
        }
        XLWS.replace_all(formula, "_xlfn._xlws.$1(")
    }

    // Escape/expand future/_xlfn functions.
    fn escape_future_functions(formula: &str) -> Cow<str> {
        lazy_static! {
            static ref FUTURE: Regex = Regex::new(
                r"\b(ACOTH|ACOT|AGGREGATE|ARABIC|ARRAYTOTEXT|BASE|BETA.DIST|BETA.INV|BINOM.DIST.RANGE|BINOM.DIST|BINOM.INV|BITAND|BITLSHIFT|BITOR|BITRSHIFT|BITXOR|CEILING.MATH|CEILING.PRECISE|CHISQ.DIST.RT|CHISQ.DIST|CHISQ.INV.RT|CHISQ.INV|CHISQ.TEST|COMBINA|CONCAT|CONFIDENCE.NORM|CONFIDENCE.T|COTH|COT|COVARIANCE.P|COVARIANCE.S|CSCH|CSC|DAYS|DECIMAL|ERF.PRECISE|ERFC.PRECISE|EXPON.DIST|F.DIST.RT|F.DIST|F.INV.RT|F.INV|F.TEST|FILTERXML|FLOOR.MATH|FLOOR.PRECISE|FORECAST.ETS.CONFINT|FORECAST.ETS.SEASONALITY|FORECAST.ETS.STAT|FORECAST.ETS|FORECAST.LINEAR|FORMULATEXT|GAMMA.DIST|GAMMA.INV|GAMMALN.PRECISE|GAMMA|GAUSS|HYPGEOM.DIST|IFNA|IFS|IMAGE|IMCOSH|IMCOT|IMCSCH|IMCSC|IMSECH|IMSEC|IMSINH|IMTAN|ISFORMULA|ISOMITTED|ISOWEEKNUM|LET|LOGNORM.DIST|LOGNORM.INV|MAXIFS|MINIFS|MODE.MULT|MODE.SNGL|MUNIT|NEGBINOM.DIST|NORM.DIST|NORM.INV|NORM.S.DIST|NORM.S.INV|NUMBERVALUE|PDURATION|PERCENTILE.EXC|PERCENTILE.INC|PERCENTRANK.EXC|PERCENTRANK.INC|PERMUTATIONA|PHI|POISSON.DIST|QUARTILE.EXC|QUARTILE.INC|QUERYSTRING|RANK.AVG|RANK.EQ|RRI|SECH|SEC|SHEETS|SHEET|SKEW.P|STDEV.P|STDEV.S|T.DIST.2T|T.DIST.RT|T.DIST|T.INV.2T|T.INV|T.TEST|TEXTAFTER|TEXTBEFORE|TEXTJOIN|UNICHAR|UNICODE|VALUETOTEXT|VAR.P|VAR.S|WEBSERVICE|WEIBULL.DIST|XMATCH|XOR|Z.TEST)\("
            )
            .unwrap();
        }
        FUTURE.replace_all(formula, "_xlfn.$1(")
    }

    // Escape/expand table functions.
    fn escape_table_functions(formula: &str) -> Cow<str> {
        // Convert Excel 2010 "@" table ref to 2007 "#This Row".
        lazy_static! {
            static ref TABLE: Regex = Regex::new(r"@").unwrap();
        }
        TABLE.replace_all(formula, "[#This Row],")
    }
}

impl From<&str> for Formula {
    fn from(value: &str) -> Formula {
        Formula::new(value)
    }
}

impl From<&Formula> for Formula {
    fn from(value: &Formula) -> Formula {
        (*value).clone()
    }
}

// -----------------------------------------------------------------------
// Tests.
// -----------------------------------------------------------------------
#[cfg(test)]
mod tests {

    use crate::Formula;

    #[test]
    fn test_dynamic_function_escapes() {
        let formula_strings = vec![
            // Test simple escapes for formulas.
            ("=foo()", "foo()"),
            ("{foo()}", "foo()"),
            ("{=foo()}", "foo()"),
            // Dynamic functions.
            ("SEQUENCE(10)", "_xlfn.SEQUENCE(10)"),
            ("UNIQUES(A1:A10)", "UNIQUES(A1:A10)"),
            ("UUNIQUE(A1:A10)", "UUNIQUE(A1:A10)"),
            ("SINGLE(A1:A3)", "_xlfn.SINGLE(A1:A3)"),
            ("UNIQUE(A1:A10)", "_xlfn.UNIQUE(A1:A10)"),
            ("_xlfn.SEQUENCE(10)", "_xlfn.SEQUENCE(10)"),
            ("SORT(A1:A10)", "_xlfn._xlws.SORT(A1:A10)"),
            ("RANDARRAY(10,1)", "_xlfn.RANDARRAY(10,1)"),
            ("ANCHORARRAY(C1)", "_xlfn.ANCHORARRAY(C1)"),
            ("SORTBY(A1:A10,B1)", "_xlfn.SORTBY(A1:A10,B1)"),
            ("FILTER(A1:A10,1)", "_xlfn._xlws.FILTER(A1:A10,1)"),
            ("XMATCH(B1:B2,A1:A10)", "_xlfn.XMATCH(B1:B2,A1:A10)"),
            ("COUNTA(ANCHORARRAY(C1))", "COUNTA(_xlfn.ANCHORARRAY(C1))"),
            (
                "SEQUENCE(10)*SEQUENCE(10)",
                "_xlfn.SEQUENCE(10)*_xlfn.SEQUENCE(10)",
            ),
            (
                "XLOOKUP(\"India\",A22:A23,B22:B23)",
                "_xlfn.XLOOKUP(\"India\",A22:A23,B22:B23)",
            ),
            (
                "XLOOKUP(B1,A1:A10,ANCHORARRAY(D1))",
                "_xlfn.XLOOKUP(B1,A1:A10,_xlfn.ANCHORARRAY(D1))",
            ),
            (
                "LAMBDA(_xlpm.number, _xlpm.number + 1)(1)",
                "_xlfn.LAMBDA(_xlpm.number, _xlpm.number + 1)(1)",
            ),
            // Newer dynamic functions (some duplicates with above).
            ("BYCOL(E1:G2)", "_xlfn.BYCOL(E1:G2)"),
            ("BYROW(E1:G2)", "_xlfn.BYROW(E1:G2)"),
            ("CHOOSECOLS(E1:G2,1)", "_xlfn.CHOOSECOLS(E1:G2,1)"),
            ("CHOOSEROWS(E1:G2,1)", "_xlfn.CHOOSEROWS(E1:G2,1)"),
            ("DROP(E1:G2,1)", "_xlfn.DROP(E1:G2,1)"),
            ("EXPAND(E1:G2,2)", "_xlfn.EXPAND(E1:G2,2)"),
            ("FILTER(E1:G2,H1:H2)", "_xlfn._xlws.FILTER(E1:G2,H1:H2)"),
            ("HSTACK(E1:G2)", "_xlfn.HSTACK(E1:G2)"),
            (
                "LAMBDA(_xlpm.number, _xlpm.number + 1)",
                "_xlfn.LAMBDA(_xlpm.number, _xlpm.number + 1)",
            ),
            (
                "MAKEARRAY(1,1,LAMBDA(_xlpm.row,_xlpm.col,TRUE)",
                "_xlfn.MAKEARRAY(1,1,_xlfn.LAMBDA(_xlpm.row,_xlpm.col,TRUE)",
            ),
            ("MAP(E1:G2,LAMBDA()", "_xlfn.MAP(E1:G2,_xlfn.LAMBDA()"),
            ("RANDARRAY(1)", "_xlfn.RANDARRAY(1)"),
            (
                "REDUCE(\"1,2,3\",E1:G2,LAMBDA()",
                "_xlfn.REDUCE(\"1,2,3\",E1:G2,_xlfn.LAMBDA()",
            ),
            (
                "SCAN(\"1,2,3\",E1:G2,LAMBDA()",
                "_xlfn.SCAN(\"1,2,3\",E1:G2,_xlfn.LAMBDA()",
            ),
            ("SEQUENCE(E1:E2)", "_xlfn.SEQUENCE(E1:E2)"),
            ("SORT(F1)", "_xlfn._xlws.SORT(F1)"),
            ("SORTBY(E1:G1,E2:G2)", "_xlfn.SORTBY(E1:G1,E2:G2)"),
            ("SWITCH(WEEKDAY(E1)", "_xlfn.SWITCH(WEEKDAY(E1)"),
            ("TAKE(E1:G2,1)", "_xlfn.TAKE(E1:G2,1)"),
            (
                "TEXTSPLIT(\"foo bar\", \" \")",
                "_xlfn.TEXTSPLIT(\"foo bar\", \" \")",
            ),
            ("TOCOL(E1:G1)", "_xlfn.TOCOL(E1:G1)"),
            ("TOROW(E1:E2)", "_xlfn.TOROW(E1:E2)"),
            ("UNIQUE(E1:G1)", "_xlfn.UNIQUE(E1:G1)"),
            ("VSTACK(E1:G2)", "_xlfn.VSTACK(E1:G2)"),
            ("WRAPCOLS(E1:F1,2)", "_xlfn.WRAPCOLS(E1:F1,2)"),
            ("WRAPROWS(E1:F1,2)", "_xlfn.WRAPROWS(E1:F1,2)"),
            (
                "XLOOKUP(M34,I35:I42,J35:K42)",
                "_xlfn.XLOOKUP(M34,I35:I42,J35:K42)",
            ),
            // Future functions.
            ("COT()", "_xlfn.COT()"),
            ("CSC()", "_xlfn.CSC()"),
            ("IFS()", "_xlfn.IFS()"),
            ("LET()", "_xlfn.LET()"),
            ("PHI()", "_xlfn.PHI()"),
            ("RRI()", "_xlfn.RRI()"),
            ("SEC()", "_xlfn.SEC()"),
            ("XOR()", "_xlfn.XOR()"),
            ("ACOT()", "_xlfn.ACOT()"),
            ("BASE()", "_xlfn.BASE()"),
            ("COTH()", "_xlfn.COTH()"),
            ("CSCH()", "_xlfn.CSCH()"),
            ("DAYS()", "_xlfn.DAYS()"),
            ("IFNA()", "_xlfn.IFNA()"),
            ("SECH()", "_xlfn.SECH()"),
            ("ACOTH()", "_xlfn.ACOTH()"),
            ("BITOR()", "_xlfn.BITOR()"),
            ("F.INV()", "_xlfn.F.INV()"),
            ("GAMMA()", "_xlfn.GAMMA()"),
            ("GAUSS()", "_xlfn.GAUSS()"),
            ("IMAGE()", "_xlfn.IMAGE()"),
            ("IMCOT()", "_xlfn.IMCOT()"),
            ("IMCSC()", "_xlfn.IMCSC()"),
            ("IMSEC()", "_xlfn.IMSEC()"),
            ("IMTAN()", "_xlfn.IMTAN()"),
            ("MUNIT()", "_xlfn.MUNIT()"),
            ("SHEET()", "_xlfn.SHEET()"),
            ("T.INV()", "_xlfn.T.INV()"),
            ("VAR.P()", "_xlfn.VAR.P()"),
            ("VAR.S()", "_xlfn.VAR.S()"),
            ("ARABIC()", "_xlfn.ARABIC()"),
            ("BITAND()", "_xlfn.BITAND()"),
            ("BITXOR()", "_xlfn.BITXOR()"),
            ("CONCAT()", "_xlfn.CONCAT()"),
            ("F.DIST()", "_xlfn.F.DIST()"),
            ("F.TEST()", "_xlfn.F.TEST()"),
            ("IMCOSH()", "_xlfn.IMCOSH()"),
            ("IMCSCH()", "_xlfn.IMCSCH()"),
            ("IMSECH()", "_xlfn.IMSECH()"),
            ("IMSINH()", "_xlfn.IMSINH()"),
            ("MAXIFS()", "_xlfn.MAXIFS()"),
            ("MINIFS()", "_xlfn.MINIFS()"),
            ("SHEETS()", "_xlfn.SHEETS()"),
            ("SKEW.P()", "_xlfn.SKEW.P()"),
            ("SWITCH()", "_xlfn.SWITCH()"),
            ("T.DIST()", "_xlfn.T.DIST()"),
            ("T.TEST()", "_xlfn.T.TEST()"),
            ("Z.TEST()", "_xlfn.Z.TEST()"),
            ("XMATCH()", "_xlfn.XMATCH()"),
            ("COMBINA()", "_xlfn.COMBINA()"),
            ("DECIMAL()", "_xlfn.DECIMAL()"),
            ("RANK.EQ()", "_xlfn.RANK.EQ()"),
            ("STDEV.P()", "_xlfn.STDEV.P()"),
            ("STDEV.S()", "_xlfn.STDEV.S()"),
            ("UNICHAR()", "_xlfn.UNICHAR()"),
            ("UNICODE()", "_xlfn.UNICODE()"),
            ("BETA.INV()", "_xlfn.BETA.INV()"),
            ("F.INV.RT()", "_xlfn.F.INV.RT()"),
            ("ISO.CEILING()", "ISO.CEILING()"),
            ("NORM.INV()", "_xlfn.NORM.INV()"),
            ("RANK.AVG()", "_xlfn.RANK.AVG()"),
            ("T.INV.2T()", "_xlfn.T.INV.2T()"),
            ("TEXTJOIN()", "_xlfn.TEXTJOIN()"),
            ("TEXTJOIN()", "_xlfn.TEXTJOIN()"),
            ("AGGREGATE()", "_xlfn.AGGREGATE()"),
            ("BETA.DIST()", "_xlfn.BETA.DIST()"),
            ("BINOM.INV()", "_xlfn.BINOM.INV()"),
            ("BITLSHIFT()", "_xlfn.BITLSHIFT()"),
            ("BITRSHIFT()", "_xlfn.BITRSHIFT()"),
            ("CHISQ.INV()", "_xlfn.CHISQ.INV()"),
            ("ECMA.CEILING()", "ECMA.CEILING()"),
            ("F.DIST.RT()", "_xlfn.F.DIST.RT()"),
            ("FILTERXML()", "_xlfn.FILTERXML()"),
            ("GAMMA.INV()", "_xlfn.GAMMA.INV()"),
            ("ISFORMULA()", "_xlfn.ISFORMULA()"),
            ("MODE.MULT()", "_xlfn.MODE.MULT()"),
            ("MODE.SNGL()", "_xlfn.MODE.SNGL()"),
            ("NORM.DIST()", "_xlfn.NORM.DIST()"),
            ("PDURATION()", "_xlfn.PDURATION()"),
            ("T.DIST.2T()", "_xlfn.T.DIST.2T()"),
            ("T.DIST.RT()", "_xlfn.T.DIST.RT()"),
            ("WORKDAY.INTL()", "WORKDAY.INTL()"),
            ("ISOMITTED()", "_xlfn.ISOMITTED()"),
            ("TEXTAFTER()", "_xlfn.TEXTAFTER()"),
            ("BINOM.DIST()", "_xlfn.BINOM.DIST()"),
            ("CHISQ.DIST()", "_xlfn.CHISQ.DIST()"),
            ("CHISQ.TEST()", "_xlfn.CHISQ.TEST()"),
            ("EXPON.DIST()", "_xlfn.EXPON.DIST()"),
            ("FLOOR.MATH()", "_xlfn.FLOOR.MATH()"),
            ("GAMMA.DIST()", "_xlfn.GAMMA.DIST()"),
            ("ISOWEEKNUM()", "_xlfn.ISOWEEKNUM()"),
            ("NORM.S.INV()", "_xlfn.NORM.S.INV()"),
            ("WEBSERVICE()", "_xlfn.WEBSERVICE()"),
            ("TEXTBEFORE()", "_xlfn.TEXTBEFORE()"),
            ("ERF.PRECISE()", "_xlfn.ERF.PRECISE()"),
            ("FORMULATEXT()", "_xlfn.FORMULATEXT()"),
            ("LOGNORM.INV()", "_xlfn.LOGNORM.INV()"),
            ("NORM.S.DIST()", "_xlfn.NORM.S.DIST()"),
            ("NUMBERVALUE()", "_xlfn.NUMBERVALUE()"),
            ("QUERYSTRING()", "_xlfn.QUERYSTRING()"),
            ("ARRAYTOTEXT()", "_xlfn.ARRAYTOTEXT()"),
            ("VALUETOTEXT()", "_xlfn.VALUETOTEXT()"),
            ("CEILING.MATH()", "_xlfn.CEILING.MATH()"),
            ("CHISQ.INV.RT()", "_xlfn.CHISQ.INV.RT()"),
            ("CONFIDENCE.T()", "_xlfn.CONFIDENCE.T()"),
            ("COVARIANCE.P()", "_xlfn.COVARIANCE.P()"),
            ("COVARIANCE.S()", "_xlfn.COVARIANCE.S()"),
            ("ERFC.PRECISE()", "_xlfn.ERFC.PRECISE()"),
            ("FORECAST.ETS()", "_xlfn.FORECAST.ETS()"),
            ("HYPGEOM.DIST()", "_xlfn.HYPGEOM.DIST()"),
            ("LOGNORM.DIST()", "_xlfn.LOGNORM.DIST()"),
            ("PERMUTATIONA()", "_xlfn.PERMUTATIONA()"),
            ("POISSON.DIST()", "_xlfn.POISSON.DIST()"),
            ("QUARTILE.EXC()", "_xlfn.QUARTILE.EXC()"),
            ("QUARTILE.INC()", "_xlfn.QUARTILE.INC()"),
            ("WEIBULL.DIST()", "_xlfn.WEIBULL.DIST()"),
            ("CHISQ.DIST.RT()", "_xlfn.CHISQ.DIST.RT()"),
            ("FLOOR.PRECISE()", "_xlfn.FLOOR.PRECISE()"),
            ("NEGBINOM.DIST()", "_xlfn.NEGBINOM.DIST()"),
            ("NETWORKDAYS.INTL()", "NETWORKDAYS.INTL()"),
            ("PERCENTILE.EXC()", "_xlfn.PERCENTILE.EXC()"),
            ("PERCENTILE.INC()", "_xlfn.PERCENTILE.INC()"),
            ("CEILING.PRECISE()", "_xlfn.CEILING.PRECISE()"),
            ("CONFIDENCE.NORM()", "_xlfn.CONFIDENCE.NORM()"),
            ("FORECAST.LINEAR()", "_xlfn.FORECAST.LINEAR()"),
            ("GAMMALN.PRECISE()", "_xlfn.GAMMALN.PRECISE()"),
            ("PERCENTRANK.EXC()", "_xlfn.PERCENTRANK.EXC()"),
            ("PERCENTRANK.INC()", "_xlfn.PERCENTRANK.INC()"),
            ("BINOM.DIST.RANGE()", "_xlfn.BINOM.DIST.RANGE()"),
            ("FORECAST.ETS.STAT()", "_xlfn.FORECAST.ETS.STAT()"),
            ("FORECAST.ETS.CONFINT()", "_xlfn.FORECAST.ETS.CONFINT()"),
            (
                "FORECAST.ETS.SEASONALITY()",
                "_xlfn.FORECAST.ETS.SEASONALITY()",
            ),
            (
                "Z.TEST(Z.TEST(Z.TEST()))",
                "_xlfn.Z.TEST(_xlfn.Z.TEST(_xlfn.Z.TEST()))",
            ),
        ];

        for &(formula_string, expected) in &formula_strings {
            let formula = Formula::new(formula_string);
            let prepared_formula = formula.expand_formula(true);
            assert_eq!(prepared_formula.as_ref(), expected);

            let formula = Formula::new(formula_string).use_future_functions();
            let prepared_formula = formula.expand_formula(false);
            assert_eq!(prepared_formula.as_ref(), expected);
        }
    }
}