excelize-rs 0.1.0

Rust language library for reading and writing Microsoft Excel (XLAM / XLSM / XLSX / XLTM / XLTX) spreadsheets
Documentation
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
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
//! PivotTable support.
//!
//! Ported from Go `pivotTable.go`.

use std::collections::HashMap;

use quick_xml::de::from_reader as xml_from_reader;
use quick_xml::se::to_string as xml_to_string;
use regex::Regex;

use crate::constants::{
    EXT_URI_PIVOT_DATA_FIELD, MAX_FIELD_LENGTH, NAMESPACE_SPREADSHEET, NAMESPACE_SPREADSHEET_X14,
    PIVOT_TABLE_REFRESHED_VERSION, PIVOT_TABLE_VERSION, SOURCE_RELATIONSHIP_PIVOT_CACHE,
    SOURCE_RELATIONSHIP_PIVOT_TABLE,
};
use crate::errors::{
    ErrNameLength, ErrParameterInvalid, ErrParameterRequired, ErrPivotTableClassicLayout,
    ErrPivotTableShowValuesAsBaseField, ErrPivotTableShowValuesAsBaseItem, ErrSheetNotExist,
    ErrUnsupportedPivotTableShowValuesAsType, Result, new_no_exist_table_error,
    new_pivot_table_col_fields_error, new_pivot_table_data_range_error,
    new_pivot_table_range_error, new_pivot_table_row_fields_error,
    new_pivot_table_selected_item_error, new_pivot_table_show_values_as_base_field_error,
    new_unsupported_pivot_cache_source_type_error,
};
use crate::file::{File, namespace_strict_to_transitional};
use crate::lib_util::{
    coordinates_to_cell_name, count_utf16_string, in_str_slice, is_numeric,
    range_ref_to_coordinates, truncate_utf16_units,
};
use crate::numfmt::built_in_num_fmt_code;
use crate::xml::common::{XlsxExt, XlsxExtLst};
use crate::xml::pivot_cache::{
    DecodeX14PivotCacheDefinition, XlsxCacheField, XlsxCacheFields, XlsxCacheSource,
    XlsxPivotCacheDefinition, XlsxSharedItem, XlsxSharedItemData, XlsxSharedItems,
    XlsxWorksheetSource,
};
use crate::xml::pivot_table::{
    XlsxColFields, XlsxColItems, XlsxDataField, XlsxDataFields, XlsxField, XlsxI, XlsxItem,
    XlsxItems, XlsxLocation, XlsxPageField, XlsxPageFields, XlsxPivotField, XlsxPivotFields,
    XlsxPivotTableDefinition, XlsxPivotTableStyleInfo, XlsxRowFields, XlsxRowItems, XlsxX,
    XlsxX14DataField,
};
use crate::xml::workbook::{XlsxPivotCache, XlsxPivotCaches};

// ------------------------------------------------------------------
// Public types
// ------------------------------------------------------------------

/// Pivot table show-values-as type.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
pub struct PivotTableShowValuesAsType(pub u8);

impl PivotTableShowValuesAsType {
    pub const NO_CALCULATION: PivotTableShowValuesAsType = PivotTableShowValuesAsType(0);
    pub const PERCENT_OF_GRAND_TOTAL: PivotTableShowValuesAsType = PivotTableShowValuesAsType(1);
    pub const PERCENT_OF_COLUMN_TOTAL: PivotTableShowValuesAsType = PivotTableShowValuesAsType(2);
    pub const PERCENT_OF_ROW_TOTAL: PivotTableShowValuesAsType = PivotTableShowValuesAsType(3);
    pub const PERCENT_OF: PivotTableShowValuesAsType = PivotTableShowValuesAsType(4);
    pub const PERCENT_OF_PARENT_ROW_TOTAL: PivotTableShowValuesAsType =
        PivotTableShowValuesAsType(5);
    pub const PERCENT_OF_PARENT_COLUMN_TOTAL: PivotTableShowValuesAsType =
        PivotTableShowValuesAsType(6);
    pub const PERCENT_OF_PARENT_TOTAL: PivotTableShowValuesAsType = PivotTableShowValuesAsType(7);
    pub const DIFFERENCE_FROM: PivotTableShowValuesAsType = PivotTableShowValuesAsType(8);
    pub const PERCENT_DIFFERENCE_FROM: PivotTableShowValuesAsType = PivotTableShowValuesAsType(9);
    pub const RUNNING_TOTAL_IN: PivotTableShowValuesAsType = PivotTableShowValuesAsType(10);
    pub const PERCENT_RUNNING_TOTAL_IN: PivotTableShowValuesAsType = PivotTableShowValuesAsType(11);
    pub const RANK_SMALLEST_TO_LARGEST: PivotTableShowValuesAsType = PivotTableShowValuesAsType(12);
    pub const RANK_LARGEST_TO_SMALLEST: PivotTableShowValuesAsType = PivotTableShowValuesAsType(13);
    pub const INDEX: PivotTableShowValuesAsType = PivotTableShowValuesAsType(14);
}

/// Pivot table show-values-as settings.
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct PivotTableShowValuesAs {
    pub r#type: PivotTableShowValuesAsType,
    pub base_field: String,
    pub base_item: String,
}

/// Pivot table field options.
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct PivotTableField {
    pub compact: bool,
    pub data: String,
    pub name: String,
    pub outline: bool,
    pub show_all: bool,
    pub insert_blank_row: bool,
    pub subtotal: String,
    pub default_subtotal: bool,
    pub num_fmt: i32,
    pub selected_items: Vec<String>,
    pub show_values_as: PivotTableShowValuesAs,
}

/// Pivot table creation options.
#[derive(Debug, Default, Clone, PartialEq)]
pub struct PivotTableOptions {
    pub data_range: String,
    pub pivot_table_range: String,
    pub name: String,
    pub rows: Vec<PivotTableField>,
    pub columns: Vec<PivotTableField>,
    pub data: Vec<PivotTableField>,
    pub filter: Vec<PivotTableField>,
    pub row_grand_totals: bool,
    pub col_grand_totals: bool,
    pub show_drill: bool,
    pub use_auto_formatting: bool,
    pub page_over_then_down: bool,
    pub merge_item: bool,
    pub classic_layout: bool,
    pub compact_data: bool,
    pub show_error: bool,
    pub show_row_headers: bool,
    pub show_col_headers: bool,
    pub show_row_stripes: bool,
    pub show_col_stripes: bool,
    pub show_last_column: bool,
    pub field_print_titles: bool,
    pub item_print_titles: bool,
    pub pivot_table_style_name: String,

    // Internal state used while building the pivot table parts.
    items: HashMap<String, Vec<XlsxItem>>,
    shared_items: HashMap<String, XlsxSharedItems>,
    pivot_table_xml: String,
    pivot_cache_xml: String,
    pivot_sheet_name: String,
    pivot_data_range: String,
    named_data_range: bool,
}

// ------------------------------------------------------------------
// Internal helpers
// ------------------------------------------------------------------

const FORMULA_ERRORS: &[&str] = &[
    "#DIV/0!",
    "#NAME?",
    "#N/A",
    "#NUM!",
    "#VALUE!",
    "#REF!",
    "#NULL!",
    "#SPILL!",
    "#CALC!",
    "#GETTING_DATA",
];

fn show_values_as_map() -> HashMap<PivotTableShowValuesAsType, &'static str> {
    [
        (
            PivotTableShowValuesAsType::PERCENT_OF_GRAND_TOTAL,
            "percentOfTotal",
        ),
        (
            PivotTableShowValuesAsType::PERCENT_OF_COLUMN_TOTAL,
            "percentOfCol",
        ),
        (
            PivotTableShowValuesAsType::PERCENT_OF_ROW_TOTAL,
            "percentOfRow",
        ),
        (PivotTableShowValuesAsType::PERCENT_OF, "percent"),
        (
            PivotTableShowValuesAsType::PERCENT_OF_PARENT_ROW_TOTAL,
            "percentOfParentRow",
        ),
        (
            PivotTableShowValuesAsType::PERCENT_OF_PARENT_COLUMN_TOTAL,
            "percentOfParentCol",
        ),
        (
            PivotTableShowValuesAsType::PERCENT_OF_PARENT_TOTAL,
            "percentOfParent",
        ),
        (PivotTableShowValuesAsType::DIFFERENCE_FROM, "difference"),
        (
            PivotTableShowValuesAsType::PERCENT_DIFFERENCE_FROM,
            "percentDiff",
        ),
        (PivotTableShowValuesAsType::RUNNING_TOTAL_IN, "runTotal"),
        (
            PivotTableShowValuesAsType::PERCENT_RUNNING_TOTAL_IN,
            "percentOfRunningTotal",
        ),
        (
            PivotTableShowValuesAsType::RANK_SMALLEST_TO_LARGEST,
            "rankAscending",
        ),
        (
            PivotTableShowValuesAsType::RANK_LARGEST_TO_SMALLEST,
            "rankDescending",
        ),
        (PivotTableShowValuesAsType::INDEX, "index"),
    ]
    .into_iter()
    .collect()
}

fn x14_show_values_as_types() -> HashMap<PivotTableShowValuesAsType, bool> {
    [
        PivotTableShowValuesAsType::PERCENT_OF_PARENT_ROW_TOTAL,
        PivotTableShowValuesAsType::PERCENT_OF_PARENT_COLUMN_TOTAL,
        PivotTableShowValuesAsType::PERCENT_OF_PARENT_TOTAL,
        PivotTableShowValuesAsType::PERCENT_RUNNING_TOTAL_IN,
        PivotTableShowValuesAsType::RANK_SMALLEST_TO_LARGEST,
        PivotTableShowValuesAsType::RANK_LARGEST_TO_SMALLEST,
        PivotTableShowValuesAsType::INDEX,
    ]
    .into_iter()
    .map(|t| (t, true))
    .collect()
}

fn base_field_required() -> HashMap<PivotTableShowValuesAsType, bool> {
    [
        PivotTableShowValuesAsType::PERCENT_OF,
        PivotTableShowValuesAsType::PERCENT_OF_PARENT_TOTAL,
        PivotTableShowValuesAsType::DIFFERENCE_FROM,
        PivotTableShowValuesAsType::PERCENT_DIFFERENCE_FROM,
        PivotTableShowValuesAsType::RUNNING_TOTAL_IN,
        PivotTableShowValuesAsType::PERCENT_RUNNING_TOTAL_IN,
        PivotTableShowValuesAsType::RANK_SMALLEST_TO_LARGEST,
        PivotTableShowValuesAsType::RANK_LARGEST_TO_SMALLEST,
    ]
    .into_iter()
    .map(|t| (t, true))
    .collect()
}

fn base_item_required() -> HashMap<PivotTableShowValuesAsType, bool> {
    [
        PivotTableShowValuesAsType::PERCENT_OF,
        PivotTableShowValuesAsType::DIFFERENCE_FROM,
        PivotTableShowValuesAsType::PERCENT_DIFFERENCE_FROM,
    ]
    .into_iter()
    .map(|t| (t, true))
    .collect()
}

#[derive(Debug, Clone, Copy, PartialEq)]
enum CellType {
    Unset,
    Number,
    Bool,
    Error,
    Formula,
    InlineString,
    SharedString,
}

// ------------------------------------------------------------------
// File implementation
// ------------------------------------------------------------------

impl File {
    /// Add a pivot table.
    pub fn add_pivot_table(&self, opts: &mut PivotTableOptions) -> Result<()> {
        let (_, pivot_table_sheet_path) = self.parse_format_pivot_table_set(opts)?;
        self.clear_calc_cache();
        let pivot_table_id = self.count_pivot_tables() + 1;
        let pivot_cache_id = self.count_pivot_cache() + 1;

        let sheet_relationships_pivot_table_xml =
            format!("../pivotTables/pivotTable{pivot_table_id}.xml");
        opts.pivot_table_xml = sheet_relationships_pivot_table_xml.replace("..", "xl");
        opts.pivot_cache_xml = format!("xl/pivotCache/pivotCacheDefinition{pivot_cache_id}.xml");
        self.add_pivot_cache(opts)?;

        let workbook_pivot_cache_rid = self.add_rels(
            &self.get_workbook_rels_path(),
            SOURCE_RELATIONSHIP_PIVOT_CACHE,
            &opts.pivot_cache_xml.trim_start_matches("xl/"),
            "",
        );
        let cache_id = self.add_workbook_pivot_cache(workbook_pivot_cache_rid);

        let pivot_cache_rels = format!("xl/pivotTables/_rels/pivotTable{pivot_table_id}.xml.rels");
        let _ = self.add_rels(
            &pivot_cache_rels,
            SOURCE_RELATIONSHIP_PIVOT_CACHE,
            &format!("../pivotCache/pivotCacheDefinition{pivot_cache_id}.xml"),
            "",
        );
        self.add_pivot_table_internal(cache_id, pivot_table_id, opts)?;

        let pivot_table_sheet_rels = format!(
            "xl/worksheets/_rels/{}.rels",
            pivot_table_sheet_path.trim_start_matches("xl/worksheets/")
        );
        self.add_rels(
            &pivot_table_sheet_rels,
            SOURCE_RELATIONSHIP_PIVOT_TABLE,
            &sheet_relationships_pivot_table_xml,
            "",
        );
        self.add_content_type_part(pivot_table_id, "pivotTable")?;
        self.add_content_type_part(pivot_cache_id, "pivotCache")
    }

    /// Get pivot tables on a worksheet.
    pub fn get_pivot_tables(&self, sheet: &str) -> Result<Vec<PivotTableOptions>> {
        let mut pivot_tables = Vec::new();
        let Some(name) = self.get_sheet_xml_path(sheet) else {
            return Err(Box::new(ErrSheetNotExist {
                sheet_name: sheet.to_string(),
            }));
        };
        let rels = format!(
            "xl/worksheets/_rels/{}.rels",
            name.trim_start_matches("xl/worksheets/")
        );
        let sheet_rels = match self.rels_reader(&rels)? {
            Some(r) => r,
            None => crate::xml::workbook::XlsxRelationships::default(),
        };
        for v in &sheet_rels.relationships {
            if v.r#type == SOURCE_RELATIONSHIP_PIVOT_TABLE {
                let pivot_table_xml = v.target.replace("..", "xl");
                let basename = std::path::Path::new(&v.target)
                    .file_name()
                    .and_then(|s| s.to_str())
                    .unwrap_or("");
                let pivot_cache_rels = format!("xl/pivotTables/_rels/{basename}.rels");
                pivot_tables.push(self.get_pivot_table(
                    sheet,
                    &pivot_table_xml,
                    &pivot_cache_rels,
                )?);
            }
        }
        Ok(pivot_tables)
    }

    /// Delete a pivot table.
    pub fn delete_pivot_table(&self, sheet: &str, name: &str) -> Result<()> {
        let Some(sheet_xml) = self.get_sheet_xml_path(sheet) else {
            return Err(Box::new(ErrSheetNotExist {
                sheet_name: sheet.to_string(),
            }));
        };
        let rels = format!(
            "xl/worksheets/_rels/{}.rels",
            sheet_xml.trim_start_matches("xl/worksheets/")
        );
        let sheet_rels = match self.rels_reader(&rels)? {
            Some(r) => r,
            None => crate::xml::workbook::XlsxRelationships::default(),
        };
        let opts = self.get_pivot_tables(sheet)?;
        self.clear_calc_cache();
        let mut pivot_table_caches: HashMap<String, i32> = HashMap::new();
        for sheet_name in self.get_sheet_list() {
            if let Ok(pts) = self.get_pivot_tables(&sheet_name) {
                for pt in pts {
                    *pivot_table_caches.entry(pt.pivot_cache_xml).or_default() += 1;
                }
            }
        }

        for v in &sheet_rels.relationships.clone() {
            if v.r#type == SOURCE_RELATIONSHIP_PIVOT_TABLE {
                let pivot_table_xml = v.target.replace("..", "xl");
                for opt in &opts {
                    if opt.name == name && opt.pivot_table_xml == pivot_table_xml {
                        if pivot_table_caches
                            .get(&opt.pivot_cache_xml)
                            .copied()
                            .unwrap_or(0)
                            == 1
                        {
                            self.delete_workbook_pivot_cache(opt)?;
                        }
                        self.delete_sheet_relationships(sheet, &v.id);
                        return Ok(());
                    }
                }
            }
        }
        Err(new_no_exist_table_error(name).into())
    }

    // ------------------------------------------------------------------
    // Build helpers
    // ------------------------------------------------------------------

    fn parse_format_pivot_table_set(
        &self,
        opts: &mut PivotTableOptions,
    ) -> Result<(crate::xml::worksheet::XlsxWorksheet, String)> {
        let (pivot_table_sheet_name, _) =
            self.adjust_range(&opts.pivot_table_range).map_err(|err| {
                let err: Box<dyn std::error::Error + Send + Sync> =
                    new_pivot_table_range_error(&err.to_string()).into();
                err
            })?;
        if count_utf16_string(&opts.name) > MAX_FIELD_LENGTH {
            return Err(Box::new(ErrNameLength));
        }
        opts.pivot_sheet_name = pivot_table_sheet_name.clone();
        self.get_pivot_table_data_range(opts)?;
        let (data_sheet_name, _) = self.adjust_range(&opts.pivot_data_range).map_err(|err| {
            let err: Box<dyn std::error::Error + Send + Sync> =
                new_pivot_table_data_range_error(&err.to_string()).into();
            err
        })?;
        let data_sheet = self.work_sheet_reader(&data_sheet_name)?;
        let Some(pivot_table_sheet_path) = self.get_sheet_xml_path(&pivot_table_sheet_name) else {
            return Err(Box::new(ErrSheetNotExist {
                sheet_name: pivot_table_sheet_name,
            }));
        };
        if opts.compact_data && opts.classic_layout {
            return Err(Box::new(ErrPivotTableClassicLayout));
        }

        let mut col_data_fields = Vec::new();
        let mut row_data_fields = Vec::new();
        for f in &opts.filter {
            if in_pivot_table_field(&opts.columns, &f.data) != -1 {
                col_data_fields.push(f.data.clone());
            }
            if in_pivot_table_field(&opts.rows, &f.data) != -1 {
                row_data_fields.push(f.data.clone());
            }
        }
        if !col_data_fields.is_empty() {
            return Err(new_pivot_table_col_fields_error(&col_data_fields).into());
        }
        if !row_data_fields.is_empty() {
            return Err(new_pivot_table_row_fields_error(&row_data_fields).into());
        }
        Ok((data_sheet, pivot_table_sheet_path))
    }

    fn adjust_range(&self, range_str: &str) -> Result<(String, Vec<i32>)> {
        if range_str.is_empty() {
            return Err(Box::new(ErrParameterRequired));
        }
        let rng: Vec<&str> = range_str.split('!').collect();
        if rng.len() != 2 {
            return Err(Box::new(ErrParameterInvalid));
        }
        let trim_rng = rng[1].replace('$', "");
        let mut coordinates = range_ref_to_coordinates(&trim_rng)?;
        let (x1, y1, x2, y2) = (
            coordinates[0],
            coordinates[1],
            coordinates[2],
            coordinates[3],
        );
        if x1 == x2 && y1 == y2 {
            return Err(Box::new(ErrParameterInvalid));
        }
        if x2 < x1 {
            coordinates.swap(0, 2);
        }
        if y2 < y1 {
            coordinates.swap(1, 3);
        }
        Ok((rng[0].to_string(), coordinates))
    }

    fn get_table_fields_order(&self, opts: &PivotTableOptions) -> Result<Vec<String>> {
        let mut order = Vec::new();
        let mut opts = opts.clone();
        self.get_pivot_table_data_range(&mut opts)?;
        let (data_sheet, coordinates) = self.adjust_range(&opts.pivot_data_range)?;
        for col in coordinates[0]..=coordinates[2] {
            let coordinate = coordinates_to_cell_name(col, coordinates[1], false)?;
            let name = self.get_cell_value(&data_sheet, &coordinate)?;
            if name.is_empty() {
                return Err(ErrParameterInvalid.into());
            }
            order.push(name);
        }
        Ok(order)
    }

    fn add_missing_item(shared_items: &mut XlsxSharedItems) {
        for item in &shared_items.items {
            if matches!(item, XlsxSharedItem::M(_)) {
                return;
            }
        }
        shared_items
            .items
            .push(XlsxSharedItem::M(XlsxSharedItemData {
                xmlns: Some("".to_string()),
                ..Default::default()
            }));
        shared_items.contains_blank = Some(true);
    }

    fn add_number_item(shared_items: &mut XlsxSharedItems, val: &str) {
        for item in &shared_items.items {
            if let XlsxSharedItem::N(data) = item {
                if data.v.as_deref() == Some(val) {
                    return;
                }
            }
        }
        shared_items
            .items
            .push(XlsxSharedItem::N(XlsxSharedItemData {
                v: Some(val.to_string()),
                xmlns: Some("".to_string()),
                ..Default::default()
            }));
        shared_items.contains_number = Some(true);
    }

    fn add_boolean_item(shared_items: &mut XlsxSharedItems, val: &str) {
        let v = (val.to_uppercase() == "TRUE").to_string();
        for item in &shared_items.items {
            if let XlsxSharedItem::B(data) = item {
                if data.v.as_deref() == Some(&v) {
                    return;
                }
            }
        }
        shared_items
            .items
            .push(XlsxSharedItem::B(XlsxSharedItemData {
                v: Some(v),
                xmlns: Some("".to_string()),
                ..Default::default()
            }));
    }

    fn add_error_item(shared_items: &mut XlsxSharedItems, val: &str) {
        for item in &shared_items.items {
            if let XlsxSharedItem::E(data) = item {
                if data.v.as_deref() == Some(val) {
                    return;
                }
            }
        }
        shared_items
            .items
            .push(XlsxSharedItem::E(XlsxSharedItemData {
                v: Some(val.to_string()),
                xmlns: Some("".to_string()),
                ..Default::default()
            }));
    }

    fn add_string_item(shared_items: &mut XlsxSharedItems, val: &str) {
        for item in &shared_items.items {
            if let XlsxSharedItem::S(data) = item {
                if data.v.as_deref() == Some(val) {
                    return;
                }
            }
        }
        shared_items
            .items
            .push(XlsxSharedItem::S(XlsxSharedItemData {
                v: Some(val.to_string()),
                xmlns: Some("".to_string()),
                ..Default::default()
            }));
        shared_items.contains_string = Some(true);
    }

    fn check_selected_items(
        shared_items: &XlsxSharedItems,
        field: &str,
        selected_items: &[String],
    ) -> Result<()> {
        for shared_item in selected_items {
            let mut found = false;
            for item in &shared_items.items {
                match item {
                    XlsxSharedItem::M(_) => {
                        if shared_item.is_empty() {
                            found = true;
                        }
                    }
                    XlsxSharedItem::B(data) => {
                        if data
                            .v
                            .as_deref()
                            .map(|v| v.eq_ignore_ascii_case(shared_item))
                            .unwrap_or(false)
                        {
                            found = true;
                        }
                    }
                    XlsxSharedItem::N(data)
                    | XlsxSharedItem::E(data)
                    | XlsxSharedItem::S(data)
                    | XlsxSharedItem::D(data) => {
                        if data.v.as_deref() == Some(shared_item.as_str()) {
                            found = true;
                        }
                    }
                }
                if found {
                    break;
                }
            }
            if !found {
                return Err(new_pivot_table_selected_item_error(shared_item, field).into());
            }
        }
        Ok(())
    }

    fn cell_type(&self, sheet: &str, cell: &str) -> Result<CellType> {
        let ws = self.work_sheet_reader(sheet)?;
        let c = match crate::cell::find_cell(&ws, cell) {
            Some(c) => c,
            None => return Ok(CellType::Unset),
        };
        match c.t.as_deref() {
            Some("b") => return Ok(CellType::Bool),
            Some("e") => return Ok(CellType::Error),
            Some("inlineStr") => return Ok(CellType::InlineString),
            Some("s") => return Ok(CellType::SharedString),
            Some("str") => return Ok(CellType::Formula),
            _ => {}
        }
        if c.f.is_some() {
            return Ok(CellType::Formula);
        }
        let val = c.v.as_deref().unwrap_or("");
        if val.is_empty() {
            return Ok(CellType::Unset);
        }
        if is_numeric(val).0 {
            return Ok(CellType::Number);
        }
        Ok(CellType::SharedString)
    }

    fn add_shared_items(
        &self,
        sheet: &str,
        col: i32,
        from_row: i32,
        to_row: i32,
    ) -> Result<XlsxSharedItems> {
        let mut si = XlsxSharedItems::default();
        for row in from_row..=to_row {
            let cell = coordinates_to_cell_name(col, row, false)?;
            let val = match self.calc_cell_value(sheet, &cell) {
                Ok(v) => v,
                Err(err) => {
                    let msg = err.to_string();
                    if FORMULA_ERRORS.iter().any(|e| msg.contains(e)) {
                        Self::add_error_item(&mut si, &msg);
                        continue;
                    }
                    return Err(err);
                }
            };
            if val.is_empty() {
                Self::add_missing_item(&mut si);
                continue;
            }
            let cell_type = self.cell_type(sheet, &cell)?;
            match cell_type {
                CellType::Unset | CellType::Number => Self::add_number_item(&mut si, &val),
                CellType::Bool => Self::add_boolean_item(&mut si, &val),
                CellType::Error => Self::add_error_item(&mut si, &val),
                CellType::Formula => {
                    if is_numeric(&val).0 {
                        Self::add_number_item(&mut si, &val);
                    } else {
                        Self::add_string_item(&mut si, &val);
                    }
                }
                CellType::InlineString | CellType::SharedString => {
                    Self::add_string_item(&mut si, &val)
                }
            }
        }
        Ok(si)
    }

    fn build_pivot_shared_items(
        &self,
        opts: &mut PivotTableOptions,
        idx: i32,
        coordinates: &[i32],
        field: &PivotTableField,
    ) -> Result<()> {
        let mut items = Vec::new();
        let mut shared_items = self.add_shared_items(
            &opts.pivot_sheet_name,
            coordinates[0] + idx,
            coordinates[1] + 1,
            coordinates[3],
        )?;
        let mut i = 0i32;
        for item in &shared_items.items {
            let hidden = if field.selected_items.is_empty() {
                false
            } else {
                match item {
                    XlsxSharedItem::M(_) => in_str_slice(&field.selected_items, "", true) == -1,
                    XlsxSharedItem::B(data) => {
                        in_str_slice(
                            &field.selected_items,
                            data.v.as_deref().unwrap_or(""),
                            false,
                        ) == -1
                    }
                    XlsxSharedItem::N(data)
                    | XlsxSharedItem::E(data)
                    | XlsxSharedItem::S(data)
                    | XlsxSharedItem::D(data) => {
                        in_str_slice(&field.selected_items, data.v.as_deref().unwrap_or(""), true)
                            == -1
                    }
                }
            };
            items.push(XlsxItem {
                h: Some(hidden),
                x: Some(i as i64),
                ..Default::default()
            });
            i += 1;
        }
        Self::check_selected_items(&shared_items, &field.data, &field.selected_items)?;

        let mut types_set = HashMap::new();
        let mut num_count = 0i32;
        for item in &shared_items.items {
            let tag = match item {
                XlsxSharedItem::M(_) => "m",
                XlsxSharedItem::N(_) => "n",
                XlsxSharedItem::B(_) => "b",
                XlsxSharedItem::E(_) => "e",
                XlsxSharedItem::S(_) => "s",
                XlsxSharedItem::D(_) => "d",
            };
            types_set.insert(tag, true);
            if tag == "n" {
                num_count += 1;
            }
        }
        shared_items.contains_mixed_types = Some(types_set.len() > 1);
        if num_count == i {
            shared_items.contains_integer = Some(true);
            shared_items.contains_string = Some(false);
            shared_items.contains_semi_mixed_types = Some(false);
        }
        shared_items.count = i;

        opts.items.insert(field.data.clone(), items);
        opts.shared_items.insert(field.data.clone(), shared_items);
        Ok(())
    }

    fn add_pivot_shared_items(
        &self,
        opts: &mut PivotTableOptions,
        coordinates: &[i32],
        fields_type: &str,
    ) -> Result<()> {
        let fields: Vec<PivotTableField> = match fields_type {
            "filters" => opts.filter.clone(),
            "cols" => opts.columns.clone(),
            "rows" => opts.rows.clone(),
            _ => Vec::new(),
        };
        let mut show_values_as_base_field_required = false;
        for field in &opts.data {
            if let Some(t) = show_values_as_map().get(&field.show_values_as.r#type) {
                let _ = t;
                if base_field_required()
                    .get(&field.show_values_as.r#type)
                    .copied()
                    .unwrap_or(false)
                {
                    show_values_as_base_field_required = true;
                }
            }
        }
        let field_refs: Vec<&PivotTableField> = fields.iter().collect();
        let fields_index = self.get_pivot_fields_index(&field_refs, opts)?;
        for (i, field) in fields.iter().enumerate() {
            if !field.selected_items.is_empty() || show_values_as_base_field_required {
                self.build_pivot_shared_items(opts, fields_index[i], coordinates, field)?;
            }
        }
        Ok(())
    }

    fn add_pivot_cache(&self, opts: &mut PivotTableOptions) -> Result<()> {
        let (data_sheet, coordinates) = self.adjust_range(&opts.pivot_data_range)?;
        let order = self.get_table_fields_order(opts)?;
        let top_left_cell = coordinates_to_cell_name(coordinates[0], coordinates[1], false)?;
        let bottom_right_cell = coordinates_to_cell_name(coordinates[2], coordinates[3], false)?;
        let mut pc = XlsxPivotCacheDefinition {
            xmlns: Some(NAMESPACE_SPREADSHEET.to_string()),
            save_data: false,
            refresh_on_load: Some(true),
            created_version: Some(PIVOT_TABLE_VERSION),
            refreshed_version: Some(PIVOT_TABLE_REFRESHED_VERSION),
            min_refreshable_version: Some(PIVOT_TABLE_VERSION),
            cache_source: Some(XlsxCacheSource {
                r#type: "worksheet".to_string(),
                worksheet_source: Some(XlsxWorksheetSource {
                    r#ref: Some(format!("{top_left_cell}:{bottom_right_cell}")),
                    sheet: Some(data_sheet),
                    ..Default::default()
                }),
                ..Default::default()
            }),
            cache_fields: Some(XlsxCacheFields::default()),
            ..Default::default()
        };
        if opts.named_data_range {
            if let Some(source) = pc.cache_source.as_mut() {
                source.worksheet_source = Some(XlsxWorksheetSource {
                    name: Some(opts.data_range.clone()),
                    ..Default::default()
                });
            }
        }
        self.add_pivot_shared_items(opts, &coordinates, "filters")?;
        self.add_pivot_shared_items(opts, &coordinates, "cols")?;
        self.add_pivot_shared_items(opts, &coordinates, "rows")?;

        let cache_fields = pc.cache_fields.as_mut().unwrap();
        for name in &order {
            let si = opts
                .shared_items
                .get(name)
                .cloned()
                .unwrap_or_else(|| XlsxSharedItems {
                    contains_blank: Some(true),
                    items: vec![XlsxSharedItem::M(XlsxSharedItemData {
                        xmlns: Some("".to_string()),
                        ..Default::default()
                    })],
                    ..Default::default()
                });
            cache_fields.cache_field.push(XlsxCacheField {
                name: name.clone(),
                shared_items: Some(si),
                ..Default::default()
            });
        }
        cache_fields.count = cache_fields.cache_field.len() as i32;
        let mut pivot_cache = xml_to_string(&pc)?.into_bytes();
        crate::file::strip_empty_attributes(&mut pivot_cache);
        self.save_file_list(&opts.pivot_cache_xml, &pivot_cache);
        Ok(())
    }

    fn add_pivot_table_internal(
        &self,
        cache_id: i64,
        pivot_table_id: i32,
        opts: &mut PivotTableOptions,
    ) -> Result<()> {
        let (_, coordinates) = self.adjust_range(&opts.pivot_table_range)?;
        let top_left_cell = coordinates_to_cell_name(coordinates[0], coordinates[1], false)?;
        let bottom_right_cell = coordinates_to_cell_name(coordinates[2], coordinates[3], false)?;

        let pivot_table_style = if opts.pivot_table_style_name.is_empty() {
            "PivotStyleLight16".to_string()
        } else {
            opts.pivot_table_style_name.clone()
        };
        let mut pt = XlsxPivotTableDefinition {
            xmlns: Some(NAMESPACE_SPREADSHEET.to_string()),
            name: opts.name.clone(),
            cache_id,
            row_grand_totals: Some(opts.row_grand_totals),
            col_grand_totals: Some(opts.col_grand_totals),
            updated_version: Some(PIVOT_TABLE_REFRESHED_VERSION as i64),
            min_refreshable_version: Some(PIVOT_TABLE_VERSION as i64),
            show_drill: Some(opts.show_drill),
            use_auto_formatting: Some(opts.use_auto_formatting),
            page_over_then_down: Some(opts.page_over_then_down),
            merge_item: Some(opts.merge_item),
            created_version: Some(PIVOT_TABLE_VERSION as i64),
            compact_data: Some(opts.compact_data),
            grid_drop_zones: Some(opts.classic_layout),
            show_error: Some(opts.show_error),
            field_print_titles: Some(opts.field_print_titles),
            item_print_titles: Some(opts.item_print_titles),
            data_caption: "Values".to_string(),
            location: Some(XlsxLocation {
                r#ref: format!("{top_left_cell}:{bottom_right_cell}"),
                first_data_col: 1,
                first_data_row: 1,
                first_header_row: 1,
                ..Default::default()
            }),
            pivot_fields: Some(XlsxPivotFields::default()),
            row_items: Some(XlsxRowItems {
                count: 1,
                i: vec![XlsxI {
                    x: vec![XlsxX::default()],
                }],
            }),
            col_items: Some(XlsxColItems {
                count: 1,
                i: vec![XlsxI::default()],
            }),
            pivot_table_style_info: Some(XlsxPivotTableStyleInfo {
                name: pivot_table_style,
                show_row_headers: opts.show_row_headers,
                show_col_headers: opts.show_col_headers,
                show_row_stripes: if opts.show_row_stripes {
                    Some(true)
                } else {
                    None
                },
                show_col_stripes: if opts.show_col_stripes {
                    Some(true)
                } else {
                    None
                },
                show_last_column: if opts.show_last_column {
                    Some(true)
                } else {
                    None
                },
            }),
            ..Default::default()
        };
        if pt.name.is_empty() {
            pt.name = format!("PivotTable{pivot_table_id}");
        }
        opts.name = pt.name.clone();
        if opts.classic_layout {
            pt.compact = Some(false);
            pt.compact_data = Some(false);
        }

        self.add_pivot_fields(&mut pt, opts)?;
        if let Some(pivot_fields) = pt.pivot_fields.as_mut() {
            pivot_fields.count = pivot_fields.pivot_field.len() as i64;
        }
        let _ = self.add_pivot_row_fields(&mut pt, opts);
        let _ = self.add_pivot_col_fields(&mut pt, opts);
        let _ = self.add_pivot_page_fields(&mut pt, opts);
        self.add_pivot_data_fields(&mut pt, opts)?;

        // Preserve data field extension lists as raw XML so that x14 elements
        // are not escaped by serde.
        let mut data_field_ext_xmls = Vec::new();
        if let Some(ref mut data_fields) = pt.data_fields {
            for df in &mut data_fields.data_field {
                if let Some(ext_lst) = df.ext_lst.take() {
                    data_field_ext_xmls.push(Some(serialize_data_field_ext_lst(&ext_lst)));
                } else {
                    data_field_ext_xmls.push(None);
                }
            }
        }

        let mut pivot_table = xml_to_string(&pt)?.into_bytes();
        crate::file::strip_empty_attributes(&mut pivot_table);
        inject_data_field_ext_lst(&mut pivot_table, &data_field_ext_xmls);
        self.save_file_list(&opts.pivot_table_xml, &pivot_table);
        Ok(())
    }

    fn add_pivot_row_fields(
        &self,
        pt: &mut XlsxPivotTableDefinition,
        opts: &PivotTableOptions,
    ) -> Result<()> {
        let row_fields_index =
            self.get_pivot_fields_index(&opts.rows.iter().collect::<Vec<_>>(), opts)?;
        for field_idx in row_fields_index {
            if pt.row_fields.is_none() {
                pt.row_fields = Some(XlsxRowFields::default());
            }
            pt.row_fields.as_mut().unwrap().field.push(XlsxField {
                x: field_idx as i64,
            });
        }
        if let Some(row_fields) = pt.row_fields.as_mut() {
            row_fields.count = row_fields.field.len() as i64;
        }
        Ok(())
    }

    fn add_pivot_page_fields(
        &self,
        pt: &mut XlsxPivotTableDefinition,
        opts: &PivotTableOptions,
    ) -> Result<()> {
        let page_fields_index =
            self.get_pivot_fields_index(&opts.filter.iter().collect::<Vec<_>>(), opts)?;
        let page_fields_name = self.get_pivot_table_fields_name(&opts.filter);
        for (idx, page_field) in page_fields_index.iter().enumerate() {
            if pt.page_fields.is_none() {
                pt.page_fields = Some(XlsxPageFields::default());
            }
            pt.page_fields
                .as_mut()
                .unwrap()
                .page_field
                .push(XlsxPageField {
                    fld: *page_field as i64,
                    name: Some(page_fields_name[idx].clone()),
                    ..Default::default()
                });
        }
        if let Some(page_fields) = pt.page_fields.as_mut() {
            page_fields.count = page_fields.page_field.len() as i64;
        }
        Ok(())
    }

    fn add_pivot_data_fields(
        &self,
        pt: &mut XlsxPivotTableDefinition,
        opts: &mut PivotTableOptions,
    ) -> Result<()> {
        let data_fields_index =
            self.get_pivot_fields_index(&opts.data.iter().collect::<Vec<_>>(), opts)?;
        let order = self.get_table_fields_order(opts)?;
        let data_fields_subtotals = self.get_pivot_table_fields_subtotal(&opts.data);
        let data_fields_name = self.get_pivot_table_fields_name(&opts.data);
        let data_fields_num_fmt_id = self.get_pivot_table_fields_num_fmt_id(&opts.data);
        for (idx, data_field) in data_fields_index.iter().enumerate() {
            if pt.data_fields.is_none() {
                pt.data_fields = Some(XlsxDataFields::default());
            }
            let mut df = XlsxDataField {
                name: Some(data_fields_name[idx].clone()),
                fld: *data_field as i64,
                subtotal: Some(data_fields_subtotals[idx].clone()),
                num_fmt_id: Some(data_fields_num_fmt_id[idx] as i64),
                ..Default::default()
            };
            self.set_pivot_table_show_values_as(&mut df, idx, &order, opts)?;
            pt.data_fields.as_mut().unwrap().data_field.push(df);
        }
        if let Some(data_fields) = pt.data_fields.as_mut() {
            data_fields.count = data_fields.data_field.len() as i64;
        }
        Ok(())
    }

    fn set_pivot_table_show_values_as(
        &self,
        df: &mut XlsxDataField,
        idx: usize,
        order: &[String],
        opts: &PivotTableOptions,
    ) -> Result<()> {
        let show_values_as_type = opts.data[idx].show_values_as.r#type;
        if show_values_as_type == PivotTableShowValuesAsType::NO_CALCULATION {
            return Ok(());
        }
        let map = show_values_as_map();
        let Some(show_data_as) = map.get(&show_values_as_type).copied() else {
            return Err(Box::new(ErrUnsupportedPivotTableShowValuesAsType));
        };
        df.show_data_as = Some(show_data_as.to_string());
        if x14_show_values_as_types()
            .get(&show_values_as_type)
            .copied()
            .unwrap_or(false)
        {
            df.show_data_as = None;
            let x14_df = XlsxX14DataField {
                xmlns_x14: Some(NAMESPACE_SPREADSHEET_X14.to_string()),
                pivot_show_as: Some(show_data_as.to_string()),
                ..Default::default()
            };
            let data_field_bytes = xml_to_string(&x14_df)?;
            let ext = XlsxExt {
                uri: Some(EXT_URI_PIVOT_DATA_FIELD.to_string()),
                content: data_field_bytes,
                ..Default::default()
            };
            df.ext_lst = Some(XlsxExtLst { ext: vec![ext] });
        }
        if base_field_required()
            .get(&show_values_as_type)
            .copied()
            .unwrap_or(false)
        {
            let base_field = &opts.data[idx].show_values_as.base_field;
            if base_field.is_empty() {
                return Err(ErrPivotTableShowValuesAsBaseField.into());
            }
            let Some(shared_items) = opts.shared_items.get(base_field) else {
                return Err(new_pivot_table_show_values_as_base_field_error(base_field).into());
            };
            let base_field_index = in_str_slice(order, base_field, true);
            df.base_field = Some(base_field_index as i64);
            if base_item_required()
                .get(&show_values_as_type)
                .copied()
                .unwrap_or(false)
            {
                self.set_pivot_table_show_values_as_base_item(
                    df,
                    base_field,
                    &opts.data[idx].show_values_as.base_item,
                    shared_items,
                )?;
            }
        }
        Ok(())
    }

    fn set_pivot_table_show_values_as_base_item(
        &self,
        df: &mut XlsxDataField,
        base_field: &str,
        base_item: &str,
        shared_items: &XlsxSharedItems,
    ) -> Result<()> {
        if base_item.is_empty() {
            return Err(Box::new(ErrPivotTableShowValuesAsBaseItem));
        }
        Self::check_selected_items(shared_items, base_field, &[base_item.to_string()])?;
        for (i, item) in shared_items.items.iter().enumerate() {
            match item {
                XlsxSharedItem::B(data) => {
                    if data
                        .v
                        .as_deref()
                        .map(|v| v.eq_ignore_ascii_case(base_item))
                        .unwrap_or(false)
                    {
                        df.base_item = Some(i as i64);
                    }
                }
                XlsxSharedItem::N(data)
                | XlsxSharedItem::E(data)
                | XlsxSharedItem::S(data)
                | XlsxSharedItem::D(data) => {
                    if data.v.as_deref() == Some(base_item) {
                        df.base_item = Some(i as i64);
                    }
                }
                _ => {}
            }
        }
        Ok(())
    }

    fn add_pivot_col_fields(
        &self,
        pt: &mut XlsxPivotTableDefinition,
        opts: &PivotTableOptions,
    ) -> Result<()> {
        if opts.columns.is_empty() {
            if opts.data.len() <= 1 {
                return Ok(());
            }
            pt.col_fields = Some(XlsxColFields {
                count: 1,
                field: vec![XlsxField { x: -2 }],
            });
            return Ok(());
        }
        pt.col_fields = Some(XlsxColFields::default());
        let col_fields_index =
            self.get_pivot_fields_index(&opts.columns.iter().collect::<Vec<_>>(), opts)?;
        for field_idx in col_fields_index {
            pt.col_fields.as_mut().unwrap().field.push(XlsxField {
                x: field_idx as i64,
            });
        }
        if opts.data.len() > 1 {
            pt.col_fields
                .as_mut()
                .unwrap()
                .field
                .push(XlsxField { x: -2 });
        }
        pt.col_fields.as_mut().unwrap().count = pt.col_fields.as_ref().unwrap().field.len() as i64;
        Ok(())
    }

    fn set_classic_layout(fld: &mut XlsxPivotField, classic_layout: bool) {
        if classic_layout {
            fld.compact = Some(false);
            fld.outline = Some(false);
        }
    }

    fn add_pivot_fields(
        &self,
        pt: &mut XlsxPivotTableDefinition,
        opts: &mut PivotTableOptions,
    ) -> Result<()> {
        let order = self.get_table_fields_order(opts)?;
        let x = 0i64;
        for name in &order {
            if in_pivot_table_field(&opts.rows, name) != -1 {
                let (row_options, ok) = self.get_pivot_table_field_options(name, &opts.rows);
                let mut items = opts.items.get(name).cloned().unwrap_or_default();
                if ok && row_options.default_subtotal {
                    items.push(XlsxItem {
                        t: Some("default".to_string()),
                        ..Default::default()
                    });
                }
                if items.is_empty() {
                    items.push(XlsxItem {
                        x: Some(x),
                        ..Default::default()
                    });
                }
                let mut fld = XlsxPivotField {
                    name: Some(self.get_pivot_table_field_name(name, &opts.rows)),
                    axis: Some("axisRow".to_string()),
                    data_field: Some(in_pivot_table_field(&opts.data, name) != -1),
                    compact: Some(row_options.compact),
                    outline: Some(row_options.outline),
                    multiple_item_selection_allowed: Some(
                        !opts.items.get(name).map(|v| v.is_empty()).unwrap_or(true),
                    ),
                    show_all: Some(row_options.show_all),
                    insert_blank_row: Some(row_options.insert_blank_row),
                    default_subtotal: Some(row_options.default_subtotal),
                    items: Some(XlsxItems {
                        count: items.len() as i64,
                        item: items,
                    }),
                    ..Default::default()
                };
                Self::set_classic_layout(&mut fld, opts.classic_layout);
                pt.pivot_fields.as_mut().unwrap().pivot_field.push(fld);
                continue;
            }
            if in_pivot_table_field(&opts.filter, name) != -1 {
                let mut items = opts.items.get(name).cloned().unwrap_or_default();
                items.push(XlsxItem {
                    t: Some("default".to_string()),
                    ..Default::default()
                });
                let mut fld = XlsxPivotField {
                    axis: Some("axisPage".to_string()),
                    data_field: Some(in_pivot_table_field(&opts.data, name) != -1),
                    multiple_item_selection_allowed: Some(
                        !opts.items.get(name).map(|v| v.is_empty()).unwrap_or(true),
                    ),
                    name: Some(self.get_pivot_table_field_name(name, &opts.columns)),
                    items: Some(XlsxItems {
                        count: items.len() as i64,
                        item: items,
                    }),
                    ..Default::default()
                };
                Self::set_classic_layout(&mut fld, opts.classic_layout);
                pt.pivot_fields.as_mut().unwrap().pivot_field.push(fld);
                continue;
            }
            if in_pivot_table_field(&opts.columns, name) != -1 {
                let (column_options, ok) = self.get_pivot_table_field_options(name, &opts.columns);
                let mut items = opts.items.get(name).cloned().unwrap_or_default();
                if ok && column_options.default_subtotal {
                    items.push(XlsxItem {
                        t: Some("default".to_string()),
                        ..Default::default()
                    });
                }
                if items.is_empty() {
                    items.push(XlsxItem {
                        x: Some(x),
                        ..Default::default()
                    });
                }
                let mut fld = XlsxPivotField {
                    name: Some(self.get_pivot_table_field_name(name, &opts.columns)),
                    axis: Some("axisCol".to_string()),
                    data_field: Some(in_pivot_table_field(&opts.data, name) != -1),
                    compact: Some(column_options.compact),
                    outline: Some(column_options.outline),
                    multiple_item_selection_allowed: Some(
                        !opts.items.get(name).map(|v| v.is_empty()).unwrap_or(true),
                    ),
                    show_all: Some(column_options.show_all),
                    insert_blank_row: Some(column_options.insert_blank_row),
                    default_subtotal: Some(column_options.default_subtotal),
                    items: Some(XlsxItems {
                        count: items.len() as i64,
                        item: items,
                    }),
                    ..Default::default()
                };
                Self::set_classic_layout(&mut fld, opts.classic_layout);
                pt.pivot_fields.as_mut().unwrap().pivot_field.push(fld);
                continue;
            }
            if in_pivot_table_field(&opts.data, name) != -1 {
                let mut fld = XlsxPivotField {
                    data_field: Some(true),
                    ..Default::default()
                };
                Self::set_classic_layout(&mut fld, opts.classic_layout);
                pt.pivot_fields.as_mut().unwrap().pivot_field.push(fld);
                continue;
            }
            let mut fld = XlsxPivotField::default();
            Self::set_classic_layout(&mut fld, opts.classic_layout);
            pt.pivot_fields.as_mut().unwrap().pivot_field.push(fld);
        }
        Ok(())
    }

    fn get_pivot_fields_index(
        &self,
        fields: &[&PivotTableField],
        opts: &PivotTableOptions,
    ) -> Result<Vec<i32>> {
        let mut pivot_fields_index = Vec::new();
        let orders = self.get_table_fields_order(opts)?;
        for field in fields {
            let pos = in_str_slice(&orders, &field.data, true);
            if pos != -1 {
                pivot_fields_index.push(pos);
            }
        }
        Ok(pivot_fields_index)
    }

    fn get_pivot_table_fields_subtotal(&self, fields: &[PivotTableField]) -> Vec<String> {
        let enums = [
            "average",
            "count",
            "countNums",
            "max",
            "min",
            "product",
            "stdDev",
            "stdDevp",
            "sum",
            "var",
            "varp",
        ];
        let mut result = Vec::with_capacity(fields.len());
        for fld in fields {
            let mut val = "sum".to_string();
            for e in &enums {
                if e.eq_ignore_ascii_case(&fld.subtotal) {
                    val = (*e).to_string();
                    break;
                }
            }
            result.push(val);
        }
        result
    }

    fn get_pivot_table_fields_name(&self, fields: &[PivotTableField]) -> Vec<String> {
        let mut result = Vec::with_capacity(fields.len());
        for fld in fields {
            if count_utf16_string(&fld.name) > MAX_FIELD_LENGTH {
                result.push(truncate_utf16_units(&fld.name, MAX_FIELD_LENGTH));
            } else {
                result.push(fld.name.clone());
            }
        }
        result
    }

    fn get_pivot_table_field_name(&self, name: &str, fields: &[PivotTableField]) -> String {
        let fields_name = self.get_pivot_table_fields_name(fields);
        for (idx, field) in fields.iter().enumerate() {
            if field.data == name {
                return fields_name[idx].clone();
            }
        }
        String::new()
    }

    fn get_pivot_table_fields_num_fmt_id(&self, fields: &[PivotTableField]) -> Vec<i32> {
        let mut result = Vec::with_capacity(fields.len());
        for fld in fields {
            if built_in_num_fmt_code(fld.num_fmt).is_some() {
                result.push(fld.num_fmt);
                continue;
            }
            if (27..=36).contains(&fld.num_fmt) || (50..=81).contains(&fld.num_fmt) {
                result.push(fld.num_fmt);
                continue;
            }
            result.push(0);
        }
        result
    }

    fn get_pivot_table_field_options(
        &self,
        name: &str,
        fields: &[PivotTableField],
    ) -> (PivotTableField, bool) {
        for field in fields {
            if field.data == name {
                return (field.clone(), true);
            }
        }
        (PivotTableField::default(), false)
    }

    fn add_workbook_pivot_cache(&self, rid: i32) -> i64 {
        let mut wb = self.workbook_reader().unwrap_or_default();
        if wb.pivot_caches.is_none() {
            wb.pivot_caches = Some(XlsxPivotCaches::default());
        }
        let mut cache_id = 1i64;
        if let Some(ref caches) = wb.pivot_caches {
            for pivot_cache in &caches.pivot_cache {
                if pivot_cache.cache_id > cache_id {
                    cache_id = pivot_cache.cache_id;
                }
            }
        }
        cache_id += 1;
        wb.pivot_caches
            .as_mut()
            .unwrap()
            .pivot_cache
            .push(XlsxPivotCache {
                cache_id,
                rid: Some(format!("rId{rid}")),
            });
        *self.workbook.lock().unwrap() = Some(wb);
        cache_id
    }

    // ------------------------------------------------------------------
    // Read helpers
    // ------------------------------------------------------------------

    fn get_pivot_table_data_range(&self, opts: &mut PivotTableOptions) -> Result<()> {
        if opts.data_range.is_empty() {
            return Err(new_pivot_table_data_range_error(&ErrParameterRequired.to_string()).into());
        }
        if !opts.pivot_data_range.is_empty() {
            return Ok(());
        }
        if opts.data_range.contains('!') {
            opts.pivot_data_range = opts.data_range.clone();
            return Ok(());
        }
        let tables = self.get_tables_for_workbook()?;
        for (sheet_name, sheet_tables) in tables {
            for table in sheet_tables {
                if table.name == opts.data_range {
                    opts.pivot_data_range = format!("{}!{}", sheet_name, table.range);
                    opts.named_data_range = true;
                    return Ok(());
                }
            }
        }
        if !opts.named_data_range {
            let refers_to = self.get_defined_name_ref_to(&opts.data_range, &opts.pivot_sheet_name);
            if !refers_to.is_empty() {
                opts.pivot_data_range = refers_to;
                opts.named_data_range = true;
                return Ok(());
            }
        }
        Err(new_pivot_table_data_range_error(&ErrParameterInvalid.to_string()).into())
    }

    fn get_pivot_table(
        &self,
        sheet: &str,
        pivot_table_xml: &str,
        pivot_cache_rels: &str,
    ) -> Result<PivotTableOptions> {
        let rels = match self.rels_reader(pivot_cache_rels)? {
            Some(r) => r,
            None => crate::xml::workbook::XlsxRelationships::default(),
        };
        let mut pivot_cache_xml = String::new();
        for v in &rels.relationships {
            if v.r#type == SOURCE_RELATIONSHIP_PIVOT_CACHE {
                pivot_cache_xml = v.target.replace("..", "xl");
                break;
            }
        }
        let pc = self.pivot_cache_reader(&pivot_cache_xml)?;
        let pt = self.pivot_table_reader(pivot_table_xml)?;
        let Some(ref cache_source) = pc.cache_source else {
            return Err(new_unsupported_pivot_cache_source_type_error("").into());
        };
        let Some(ref worksheet_source) = cache_source.worksheet_source else {
            return Err(new_unsupported_pivot_cache_source_type_error(&cache_source.r#type).into());
        };
        let data_range = format!(
            "{}!{}",
            worksheet_source.sheet.as_deref().unwrap_or(""),
            worksheet_source.r#ref.as_deref().unwrap_or("")
        );
        let location_ref = pt
            .location
            .as_ref()
            .map(|l| l.r#ref.clone())
            .unwrap_or_default();
        let mut opts = PivotTableOptions {
            pivot_table_xml: pivot_table_xml.to_string(),
            pivot_cache_xml,
            pivot_sheet_name: sheet.to_string(),
            data_range,
            pivot_table_range: format!("{}!{}", sheet, location_ref),
            name: pt.name.clone(),
            classic_layout: pt.grid_drop_zones.unwrap_or(false),
            field_print_titles: pt.field_print_titles.unwrap_or(false),
            item_print_titles: pt.item_print_titles.unwrap_or(false),
            ..Default::default()
        };
        if let Some(ref name) = worksheet_source.name {
            opts.data_range = name.clone();
            let _ = self.get_pivot_table_data_range(&mut opts);
        }
        opts.row_grand_totals = pt.row_grand_totals.unwrap_or(false);
        opts.col_grand_totals = pt.col_grand_totals.unwrap_or(false);
        opts.show_drill = pt.show_drill.unwrap_or(false);
        opts.use_auto_formatting = pt.use_auto_formatting.unwrap_or(false);
        opts.page_over_then_down = pt.page_over_then_down.unwrap_or(false);
        opts.merge_item = pt.merge_item.unwrap_or(false);
        opts.compact_data = pt.compact_data.unwrap_or(false);
        opts.show_error = pt.show_error.unwrap_or(false);
        if let Some(ref si) = pt.pivot_table_style_info {
            opts.show_row_headers = si.show_row_headers;
            opts.show_col_headers = si.show_col_headers;
            opts.show_row_stripes = si.show_row_stripes.unwrap_or(false);
            opts.show_col_stripes = si.show_col_stripes.unwrap_or(false);
            opts.show_last_column = si.show_last_column.unwrap_or(false);
            opts.pivot_table_style_name = si.name.clone();
        }
        let _ = self.get_pivot_table_data_range(&mut opts);
        self.extract_pivot_table_fields(&pt, &pc, &mut opts);
        Ok(opts)
    }

    fn pivot_table_reader(&self, path: &str) -> Result<XlsxPivotTableDefinition> {
        let content = namespace_strict_to_transitional(&self.read_xml(path));
        let mut pivot_table = XlsxPivotTableDefinition::default();
        if !content.is_empty() {
            let content_str = String::from_utf8_lossy(&content);
            let (content_without_ext, ext_xmls) = extract_data_field_ext_lst(&content_str);
            pivot_table = xml_from_reader(content_without_ext.as_bytes())?;
            if let Some(ref mut data_fields) = pivot_table.data_fields {
                for (i, df) in data_fields.data_field.iter_mut().enumerate() {
                    if let Some(inner) = ext_xmls.get(i).and_then(|o| o.as_ref()) {
                        df.ext_lst = Some(crate::xml::common::parse_ext_lst_content(inner)?);
                    }
                }
            }
        }
        Ok(pivot_table)
    }

    fn pivot_cache_reader(&self, path: &str) -> Result<XlsxPivotCacheDefinition> {
        let content = namespace_strict_to_transitional(&self.read_xml(path));
        let mut pivot_cache = XlsxPivotCacheDefinition::default();
        if !content.is_empty() {
            pivot_cache = xml_from_reader(content.as_slice())?;
        }
        Ok(pivot_cache)
    }

    fn extract_pivot_table_fields(
        &self,
        pt: &XlsxPivotTableDefinition,
        pc: &XlsxPivotCacheDefinition,
        opts: &mut PivotTableOptions,
    ) {
        let order = pc.get_pivot_cache_fields_name();
        if let Some(ref pivot_fields) = pt.pivot_fields {
            for (field_idx, field) in pivot_fields.pivot_field.iter().enumerate() {
                let name = order.get(field_idx).cloned().unwrap_or_default();
                match field.axis.as_deref() {
                    Some("axisRow") => opts.rows.push(pc.extract_pivot_table_field(&name, field)),
                    Some("axisCol") => opts
                        .columns
                        .push(pc.extract_pivot_table_field(&name, field)),
                    Some("axisPage") => {
                        opts.filter.push(pc.extract_pivot_table_field(&name, field))
                    }
                    _ => {}
                }
            }
        }
        if let Some(ref data_fields) = pt.data_fields {
            for field in &data_fields.data_field {
                let mut data_field = PivotTableField {
                    data: order.get(field.fld as usize).cloned().unwrap_or_default(),
                    name: field.name.clone().unwrap_or_default(),
                    subtotal: title_case(field.subtotal.as_deref().unwrap_or("sum")),
                    num_fmt: field.num_fmt_id.unwrap_or(0) as i32,
                    ..Default::default()
                };
                if field.show_data_as.is_some() || field.ext_lst.is_some() {
                    self.extract_pivot_table_show_values_as(pc, field, &mut data_field);
                }
                opts.data.push(data_field);
            }
        }
    }

    fn extract_pivot_table_show_values_as(
        &self,
        pc: &XlsxPivotCacheDefinition,
        df: &XlsxDataField,
        data_field: &mut PivotTableField,
    ) {
        let order = pc.get_pivot_cache_fields_name();
        let mut show_data_as = df.show_data_as.clone().unwrap_or_default();
        if let Some(ref ext_lst) = df.ext_lst {
            for ext in &ext_lst.ext {
                if ext.uri.as_deref() == Some(EXT_URI_PIVOT_DATA_FIELD) {
                    if let Ok(parsed) =
                        xml_from_reader::<_, XlsxX14DataField>(ext.content.as_bytes())
                    {
                        if let Some(ref psa) = parsed.pivot_show_as {
                            show_data_as = psa.clone();
                        }
                    }
                }
            }
        }
        for (k, v) in show_values_as_map() {
            if v == show_data_as {
                data_field.show_values_as.r#type = k;
                break;
            }
        }
        if let Some(base_field_idx) = df.base_field {
            if base_field_idx < order.len() as i64 {
                data_field.show_values_as.base_field = order[base_field_idx as usize].clone();
            }
        }
        if df.base_item.is_none() {
            return;
        }
        let base_item_idx = df.base_item.unwrap() as usize;
        for cache_field in pc
            .cache_fields
            .as_ref()
            .map(|f| &f.cache_field)
            .into_iter()
            .flatten()
        {
            if cache_field.name == data_field.show_values_as.base_field {
                if let Some(ref shared_items) = cache_field.shared_items {
                    if base_item_idx < shared_items.items.len() {
                        data_field.show_values_as.base_item =
                            shared_items.items[base_item_idx].v().unwrap_or_default();
                    }
                }
            }
        }
    }

    fn gen_pivot_cache_definition_id(&self) -> i32 {
        let mut id = 0i32;
        for entry in self.pkg.iter() {
            let k = entry.key();
            if k.contains("xl/pivotCache/pivotCacheDefinition") {
                if let Ok(pc) = self.pivot_cache_reader(k) {
                    if let Some(ref ext_lst) = pc.ext_lst {
                        for ext in &ext_lst.ext {
                            if ext.uri.as_deref()
                                == Some(crate::constants::EXT_URI_PIVOT_CACHE_DEFINITION)
                            {
                                if let Ok(parsed) =
                                    xml_from_reader::<_, DecodeX14PivotCacheDefinition>(
                                        ext.content.as_bytes(),
                                    )
                                {
                                    if parsed.pivot_cache_id > id {
                                        id = parsed.pivot_cache_id;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        id + 1
    }

    fn delete_workbook_pivot_cache(&self, opt: &PivotTableOptions) -> Result<()> {
        let target = opt
            .pivot_cache_xml
            .trim_start_matches('/')
            .trim_start_matches("xl/");
        let r_id = self.delete_workbook_rels(SOURCE_RELATIONSHIP_PIVOT_CACHE, target)?;
        let mut wb = self.workbook_reader()?;
        if let Some(ref mut caches) = wb.pivot_caches {
            caches
                .pivot_cache
                .retain(|c| c.rid.as_deref() != Some(&r_id));
            if caches.pivot_cache.is_empty() {
                wb.pivot_caches = None;
            }
        }
        *self.workbook.lock().unwrap() = Some(wb);
        Ok(())
    }
}

// ------------------------------------------------------------------
// Extension trait for XlsxPivotCacheDefinition
// ------------------------------------------------------------------

trait PivotCacheExt {
    fn get_pivot_cache_fields_name(&self) -> Vec<String>;
    fn extract_pivot_table_field(&self, data: &str, fld: &XlsxPivotField) -> PivotTableField;
}

impl PivotCacheExt for XlsxPivotCacheDefinition {
    fn get_pivot_cache_fields_name(&self) -> Vec<String> {
        let mut order = Vec::new();
        if let Some(ref cache_fields) = self.cache_fields {
            for cf in &cache_fields.cache_field {
                order.push(cf.name.clone());
            }
        }
        order
    }

    fn extract_pivot_table_field(&self, data: &str, fld: &XlsxPivotField) -> PivotTableField {
        let mut pivot_table_field = PivotTableField {
            data: data.to_string(),
            show_all: fld.show_all.unwrap_or(false),
            insert_blank_row: fld.insert_blank_row.unwrap_or(false),
            ..Default::default()
        };
        if let Some(ref items) = fld.items {
            for item in &items.item {
                if item.h.unwrap_or(false) || item.x.is_none() {
                    continue;
                }
                let idx = item.x.unwrap() as usize;
                if let Some(ref cache_fields) = self.cache_fields {
                    for field in &cache_fields.cache_field {
                        if field.name == data {
                            if let Some(ref shared_items) = field.shared_items {
                                if !shared_items.items.is_empty() && idx < shared_items.items.len()
                                {
                                    let value = shared_items.items[idx].v().unwrap_or_default();
                                    pivot_table_field.selected_items.push(value);
                                }
                            }
                        }
                    }
                }
            }
        }
        pivot_table_field.compact = fld.compact.unwrap_or(false);
        pivot_table_field.outline = fld.outline.unwrap_or(false);
        pivot_table_field.default_subtotal = fld.default_subtotal.unwrap_or(false);
        pivot_table_field
    }
}

// ------------------------------------------------------------------
// Free functions
// ------------------------------------------------------------------

fn in_pivot_table_field(fields: &[PivotTableField], x: &str) -> i32 {
    for (idx, n) in fields.iter().enumerate() {
        if n.data == x {
            return idx as i32;
        }
    }
    -1
}

fn title_case(s: &str) -> String {
    let mut chars = s.chars();
    match chars.next() {
        Some(first) => first.to_uppercase().to_string() + chars.as_str(),
        None => String::new(),
    }
}

/// Serialize the extension list that belongs to a data field as raw XML that
/// can be injected into the serialized pivot table definition.
fn serialize_data_field_ext_lst(ext_lst: &XlsxExtLst) -> String {
    format!(
        "<extLst>{}</extLst>",
        crate::xml::common::serialize_ext_lst(ext_lst)
    )
}

/// Inject raw `<extLst>` blocks into self-closing `<dataField/>` tags.
/// `ext_xmls` must be in the same order as the `<dataField/>` tags in `xml`.
fn inject_data_field_ext_lst(xml: &mut Vec<u8>, ext_xmls: &[Option<String>]) {
    let s = String::from_utf8_lossy(xml).to_string();
    let re = Regex::new(r#"<dataField\b[^>]*?/>"#).unwrap();
    let extra = ext_xmls
        .iter()
        .map(|o| o.as_ref().map_or(0, |x| x.len() + 20))
        .sum::<usize>();
    let mut out = String::with_capacity(s.len() + extra);
    let mut last = 0usize;
    let mut idx = 0usize;
    for m in re.find_iter(&s) {
        out.push_str(&s[last..m.start()]);
        let tag = m.as_str();
        if let Some(ext) = ext_xmls.get(idx).and_then(|o| o.as_ref()) {
            out.push_str(&tag[..tag.len() - 2]);
            out.push('>');
            out.push_str(ext);
            out.push_str("</dataField>");
        } else {
            out.push_str(tag);
        }
        last = m.end();
        idx += 1;
    }
    out.push_str(&s[last..]);
    *xml = out.into_bytes();
}

/// Extract the raw inner XML of each `<extLst>` inside a `<dataField>` element
/// and remove the `<extLst>` block so that serde can deserialize the remainder.
/// The returned vector is in data field order.
fn extract_data_field_ext_lst(xml: &str) -> (String, Vec<Option<String>>) {
    let data_field_re = Regex::new(r#"(?s)<dataField\b([^>]*?)(?:/>|>(.*?)</dataField>)"#).unwrap();
    let ext_re = Regex::new(r#"(?s)<extLst>(.*?)</extLst>"#).unwrap();
    let mut out = String::with_capacity(xml.len());
    let mut ext_xmls = Vec::new();
    let mut last = 0usize;
    for cap in data_field_re.captures_iter(xml) {
        let m = cap.get(0).unwrap();
        out.push_str(&xml[last..m.start()]);
        let attrs = cap.get(1).unwrap().as_str();
        if let Some(content_match) = cap.get(2) {
            let content = content_match.as_str();
            let mut ext_opt = None;
            let content_without_ext = if let Some(ext_cap) = ext_re.captures(content) {
                ext_opt = Some(ext_cap.get(1).unwrap().as_str().to_string());
                let full_ext = ext_cap.get(0).unwrap();
                let mut c = String::with_capacity(content.len());
                c.push_str(&content[..full_ext.start()]);
                c.push_str(&content[full_ext.end()..]);
                c
            } else {
                content.to_string()
            };
            ext_xmls.push(ext_opt);
            out.push_str("<dataField");
            out.push_str(attrs);
            out.push('>');
            out.push_str(&content_without_ext);
            out.push_str("</dataField>");
        } else {
            ext_xmls.push(None);
            out.push_str(m.as_str());
        }
        last = m.end();
    }
    out.push_str(&xml[last..]);
    (out, ext_xmls)
}

trait SharedItemValue {
    fn v(&self) -> Option<String>;
}

impl SharedItemValue for XlsxSharedItem {
    fn v(&self) -> Option<String> {
        match self {
            XlsxSharedItem::M(d)
            | XlsxSharedItem::N(d)
            | XlsxSharedItem::B(d)
            | XlsxSharedItem::E(d)
            | XlsxSharedItem::S(d)
            | XlsxSharedItem::D(d) => d.v.clone(),
        }
    }
}

// ------------------------------------------------------------------
// Tests
// ------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::options::Options;
    use crate::slicer::SlicerOptions;
    use crate::xml::table::Table;
    use crate::xml::workbook::DefinedName;

    fn new_file() -> File {
        File::new_with_options(Options::default())
    }

    fn create_sample_data(f: &File) {
        let headers = ["Month", "Year", "Type", "Revenue", "Region"];
        for (i, h) in headers.iter().enumerate() {
            f.set_cell_str("Sheet1", &format!("{}1", (b'A' + i as u8) as char), h)
                .unwrap();
        }
        let months = [
            "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
        ];
        let years = [2017i32, 2018, 2019];
        let types = ["Meat", "Dairy", "Beverages", "Produce"];
        let revenue = [3217, 4512, 3891, 4738, 3054, 4265, 3643, 4901, 3378, 4126];
        let regions = ["East", "West", "North", "South"];
        for row in 2..32 {
            f.set_cell_str(
                "Sheet1",
                &format!("A{row}"),
                months[(row - 2) % months.len()],
            )
            .unwrap();
            f.set_cell_value("Sheet1", &format!("B{row}"), years[(row - 2) % years.len()])
                .unwrap();
            f.set_cell_str("Sheet1", &format!("C{row}"), types[(row - 2) % types.len()])
                .unwrap();
            f.set_cell_value(
                "Sheet1",
                &format!("D{row}"),
                revenue[(row - 2) % revenue.len()],
            )
            .unwrap();
            f.set_cell_str(
                "Sheet1",
                &format!("E{row}"),
                regions[(row - 2) % regions.len()],
            )
            .unwrap();
        }
    }

    #[test]
    fn add_and_get_pivot_table() {
        let f = new_file();
        create_sample_data(&f);
        let mut expected = PivotTableOptions {
            data_range: "Sheet1!A1:E31".to_string(),
            pivot_table_range: "Sheet1!G4:M30".to_string(),
            rows: vec![
                PivotTableField {
                    data: "Month".to_string(),
                    show_all: true,
                    default_subtotal: true,
                    ..Default::default()
                },
                PivotTableField {
                    data: "Year".to_string(),
                    ..Default::default()
                },
            ],
            filter: vec![PivotTableField {
                data: "Region".to_string(),
                ..Default::default()
            }],
            columns: vec![PivotTableField {
                data: "Type".to_string(),
                show_all: true,
                insert_blank_row: true,
                default_subtotal: true,
                ..Default::default()
            }],
            data: vec![PivotTableField {
                data: "Revenue".to_string(),
                subtotal: "Sum".to_string(),
                name: "Summarize".to_string(),
                ..Default::default()
            }],
            row_grand_totals: true,
            col_grand_totals: true,
            show_drill: true,
            classic_layout: true,
            show_error: true,
            show_row_headers: true,
            show_col_headers: true,
            show_last_column: true,
            field_print_titles: true,
            item_print_titles: true,
            pivot_table_style_name: "PivotStyleLight16".to_string(),
            ..Default::default()
        };
        f.add_pivot_table(&mut expected).unwrap();

        // Fields without an explicit selection and without a default subtotal item
        // are read back with a placeholder selected item to match Go's behavior.
        expected.rows[1].selected_items = vec!["".to_string()];

        assert!(f.pkg.contains_key("xl/pivotTables/pivotTable1.xml"));
        assert!(
            f.pkg
                .contains_key("xl/pivotCache/pivotCacheDefinition1.xml")
        );

        let pivot_tables = f.get_pivot_tables("Sheet1").unwrap();
        assert_eq!(pivot_tables.len(), 1);
        assert_eq!(pivot_tables[0], expected);
    }

    #[test]
    fn pivot_table_show_values_as_round_trip() {
        let f = new_file();
        create_sample_data(&f);
        let mut opts = PivotTableOptions {
            data_range: "Sheet1!A1:E31".to_string(),
            pivot_table_range: "Sheet1!W2:AC28".to_string(),
            rows: vec![PivotTableField {
                data: "Month".to_string(),
                default_subtotal: true,
                ..Default::default()
            }],
            columns: vec![PivotTableField {
                data: "Region".to_string(),
                ..Default::default()
            }],
            data: vec![PivotTableField {
                data: "Revenue".to_string(),
                subtotal: "Count".to_string(),
                name: "Summarize by Count".to_string(),
                show_values_as: PivotTableShowValuesAs {
                    r#type: PivotTableShowValuesAsType::PERCENT_OF,
                    base_field: "Region".to_string(),
                    base_item: "East".to_string(),
                },
                ..Default::default()
            }],
            row_grand_totals: true,
            col_grand_totals: true,
            show_drill: true,
            show_row_headers: true,
            show_col_headers: true,
            show_last_column: true,
            ..Default::default()
        };
        f.add_pivot_table(&mut opts).unwrap();

        let pivot_tables = f.get_pivot_tables("Sheet1").unwrap();
        assert_eq!(pivot_tables.len(), 1);
        assert_eq!(pivot_tables[0].data.len(), 1);
        let data = &pivot_tables[0].data[0];
        assert_eq!(data.data, "Revenue");
        assert_eq!(data.subtotal, "Count");
        assert_eq!(
            data.show_values_as.r#type,
            PivotTableShowValuesAsType::PERCENT_OF
        );
        assert_eq!(data.show_values_as.base_field, "Region");
        assert_eq!(data.show_values_as.base_item, "East");
    }

    #[test]
    fn delete_pivot_table() {
        let f = new_file();
        create_sample_data(&f);
        let mut opts = PivotTableOptions {
            data_range: "Sheet1!A1:E31".to_string(),
            pivot_table_range: "Sheet1!G4:M30".to_string(),
            rows: vec![PivotTableField {
                data: "Month".to_string(),
                default_subtotal: true,
                ..Default::default()
            }],
            data: vec![PivotTableField {
                data: "Revenue".to_string(),
                subtotal: "Sum".to_string(),
                ..Default::default()
            }],
            ..Default::default()
        };
        f.add_pivot_table(&mut opts).unwrap();
        assert_eq!(f.get_pivot_tables("Sheet1").unwrap().len(), 1);
        f.delete_pivot_table("Sheet1", "PivotTable1").unwrap();
        assert!(f.get_pivot_tables("Sheet1").unwrap().is_empty());
    }

    #[test]
    fn pivot_table_errors() {
        let f = new_file();
        create_sample_data(&f);

        // Classic layout and compact data conflict.
        let mut opts = PivotTableOptions {
            data_range: "Sheet1!A1:E31".to_string(),
            pivot_table_range: "Sheet1!G2:M34".to_string(),
            compact_data: true,
            classic_layout: true,
            ..Default::default()
        };
        let err = f.add_pivot_table(&mut opts).unwrap_err();
        assert!(err.downcast_ref::<ErrPivotTableClassicLayout>().is_some());

        // Invalid data range.
        let mut opts = PivotTableOptions {
            data_range: "Sheet1!A1:A1".to_string(),
            pivot_table_range: "Sheet1!G2:M34".to_string(),
            rows: vec![PivotTableField {
                data: "Month".to_string(),
                ..Default::default()
            }],
            data: vec![PivotTableField {
                data: "Revenue".to_string(),
                ..Default::default()
            }],
            ..Default::default()
        };
        let err = f.add_pivot_table(&mut opts).unwrap_err();
        let msg = err.to_string();
        assert!(msg.contains("DataRange"));

        // Same field in filter and rows.
        let mut opts = PivotTableOptions {
            data_range: "Sheet1!A1:E31".to_string(),
            pivot_table_range: "Sheet1!G2:M34".to_string(),
            rows: vec![PivotTableField {
                data: "Month".to_string(),
                default_subtotal: true,
                ..Default::default()
            }],
            columns: vec![PivotTableField {
                data: "Type".to_string(),
                default_subtotal: true,
                ..Default::default()
            }],
            data: vec![PivotTableField {
                data: "Revenue".to_string(),
                ..Default::default()
            }],
            filter: vec![PivotTableField {
                data: "Month".to_string(),
                ..Default::default()
            }],
            ..Default::default()
        };
        let err = f.add_pivot_table(&mut opts).unwrap_err();
        let msg = err.to_string();
        assert!(msg.contains("row fields"));

        // Selected item does not exist.
        let mut opts = PivotTableOptions {
            data_range: "Sheet1!A1:E31".to_string(),
            pivot_table_range: "Sheet1!G2:M34".to_string(),
            rows: vec![PivotTableField {
                data: "Year".to_string(),
                ..Default::default()
            }],
            columns: vec![PivotTableField {
                data: "Type".to_string(),
                ..Default::default()
            }],
            data: vec![PivotTableField {
                data: "Revenue".to_string(),
                ..Default::default()
            }],
            filter: vec![PivotTableField {
                data: "Month".to_string(),
                selected_items: vec!["x".to_string()],
                ..Default::default()
            }],
            ..Default::default()
        };
        let err = f.add_pivot_table(&mut opts).unwrap_err();
        let msg = err.to_string();
        assert!(msg.contains("selected item x"));

        // Unsupported show value as type.
        let mut opts = PivotTableOptions {
            data_range: "Sheet1!A1:E31".to_string(),
            pivot_table_range: "Sheet1!G2:M34".to_string(),
            rows: vec![PivotTableField {
                data: "Year".to_string(),
                ..Default::default()
            }],
            columns: vec![PivotTableField {
                data: "Type".to_string(),
                ..Default::default()
            }],
            data: vec![PivotTableField {
                data: "Revenue".to_string(),
                show_values_as: PivotTableShowValuesAs {
                    r#type: PivotTableShowValuesAsType(15),
                    ..Default::default()
                },
                ..Default::default()
            }],
            filter: vec![PivotTableField {
                data: "Month".to_string(),
                ..Default::default()
            }],
            ..Default::default()
        };
        let err = f.add_pivot_table(&mut opts).unwrap_err();
        assert!(
            err.downcast_ref::<ErrUnsupportedPivotTableShowValuesAsType>()
                .is_some()
        );

        // Missing base field for show value as.
        let mut opts = PivotTableOptions {
            data_range: "Sheet1!A1:E31".to_string(),
            pivot_table_range: "Sheet1!G2:M34".to_string(),
            rows: vec![PivotTableField {
                data: "Year".to_string(),
                ..Default::default()
            }],
            columns: vec![PivotTableField {
                data: "Type".to_string(),
                ..Default::default()
            }],
            data: vec![PivotTableField {
                data: "Revenue".to_string(),
                show_values_as: PivotTableShowValuesAs {
                    r#type: PivotTableShowValuesAsType::RUNNING_TOTAL_IN,
                    ..Default::default()
                },
                ..Default::default()
            }],
            filter: vec![PivotTableField {
                data: "Month".to_string(),
                ..Default::default()
            }],
            ..Default::default()
        };
        let err = f.add_pivot_table(&mut opts).unwrap_err();
        assert!(
            err.downcast_ref::<ErrPivotTableShowValuesAsBaseField>()
                .is_some()
        );
    }

    #[test]
    fn pivot_table_comprehensive() {
        let f = new_file();
        create_sample_data(&f);

        let mut expected = PivotTableOptions {
            data_range: "Sheet1!A1:E31".to_string(),
            pivot_table_range: "Sheet1!G4:M30".to_string(),
            rows: vec![
                PivotTableField {
                    data: "Month".to_string(),
                    show_all: true,
                    default_subtotal: true,
                    ..Default::default()
                },
                PivotTableField {
                    data: "Year".to_string(),
                    ..Default::default()
                },
            ],
            filter: vec![PivotTableField {
                data: "Region".to_string(),
                ..Default::default()
            }],
            columns: vec![PivotTableField {
                data: "Type".to_string(),
                show_all: true,
                insert_blank_row: true,
                default_subtotal: true,
                ..Default::default()
            }],
            data: vec![PivotTableField {
                data: "Revenue".to_string(),
                subtotal: "Sum".to_string(),
                name: "Summarize by Sum".to_string(),
                num_fmt: 38,
                ..Default::default()
            }],
            row_grand_totals: true,
            col_grand_totals: true,
            show_drill: true,
            classic_layout: true,
            show_error: true,
            show_row_headers: true,
            show_col_headers: true,
            show_last_column: true,
            field_print_titles: true,
            item_print_titles: true,
            pivot_table_style_name: "PivotStyleLight16".to_string(),
            ..Default::default()
        };
        f.add_pivot_table(&mut expected).unwrap();

        // Fields without an explicit selection and without a default subtotal item
        // are read back with a placeholder selected item to match Go's behavior.
        expected.rows[1].selected_items = vec!["".to_string()];

        let pivot_tables = f.get_pivot_tables("Sheet1").unwrap();
        assert_eq!(pivot_tables.len(), 1);
        assert_eq!(pivot_tables[0], expected);

        // Different coordinate order should be normalized.
        let mut opts = PivotTableOptions {
            data_range: "Sheet1!A1:E31".to_string(),
            pivot_table_range: "Sheet1!U29:O2".to_string(),
            rows: vec![
                PivotTableField {
                    data: "Month".to_string(),
                    default_subtotal: true,
                    ..Default::default()
                },
                PivotTableField {
                    data: "Year".to_string(),
                    ..Default::default()
                },
            ],
            columns: vec![PivotTableField {
                data: "Type".to_string(),
                default_subtotal: true,
                ..Default::default()
            }],
            data: vec![PivotTableField {
                data: "Revenue".to_string(),
                subtotal: "Average".to_string(),
                name: "Summarize by Average".to_string(),
                ..Default::default()
            }],
            row_grand_totals: true,
            col_grand_totals: true,
            show_drill: true,
            show_row_headers: true,
            show_col_headers: true,
            show_last_column: true,
            ..Default::default()
        };
        f.add_pivot_table(&mut opts).unwrap();
        let pivot_tables = f.get_pivot_tables("Sheet1").unwrap();
        assert_eq!(pivot_tables.len(), 2);
        assert_eq!(pivot_tables[1].pivot_table_style_name, "PivotStyleLight16");

        // Show values as with base field and base item.
        let mut opts = PivotTableOptions {
            data_range: "Sheet1!A1:E31".to_string(),
            pivot_table_range: "Sheet1!W2:AC28".to_string(),
            rows: vec![PivotTableField {
                data: "Month".to_string(),
                default_subtotal: true,
                ..Default::default()
            }],
            columns: vec![PivotTableField {
                data: "Region".to_string(),
                ..Default::default()
            }],
            data: vec![PivotTableField {
                data: "Revenue".to_string(),
                subtotal: "Count".to_string(),
                name: "Summarize by Count".to_string(),
                show_values_as: PivotTableShowValuesAs {
                    r#type: PivotTableShowValuesAsType::PERCENT_OF,
                    base_field: "Region".to_string(),
                    base_item: "East".to_string(),
                },
                ..Default::default()
            }],
            row_grand_totals: true,
            col_grand_totals: true,
            show_drill: true,
            show_row_headers: true,
            show_col_headers: true,
            show_last_column: true,
            ..Default::default()
        };
        f.add_pivot_table(&mut opts).unwrap();

        let mut opts = PivotTableOptions {
            data_range: "Sheet1!A1:E31".to_string(),
            pivot_table_range: "Sheet1!G34:X49".to_string(),
            rows: vec![PivotTableField {
                data: "Month".to_string(),
                ..Default::default()
            }],
            columns: vec![
                PivotTableField {
                    data: "Region".to_string(),
                    default_subtotal: true,
                    ..Default::default()
                },
                PivotTableField {
                    data: "Year".to_string(),
                    ..Default::default()
                },
            ],
            data: vec![PivotTableField {
                data: "Revenue".to_string(),
                subtotal: "CountNums".to_string(),
                name: "Summarize by CountNums".to_string(),
                show_values_as: PivotTableShowValuesAs {
                    r#type: PivotTableShowValuesAsType::PERCENT_OF,
                    base_field: "Month".to_string(),
                    base_item: "Jan".to_string(),
                },
                ..Default::default()
            }],
            row_grand_totals: true,
            col_grand_totals: true,
            show_drill: true,
            show_row_headers: true,
            show_col_headers: true,
            show_last_column: true,
            ..Default::default()
        };
        f.add_pivot_table(&mut opts).unwrap();

        let pivot_tables = f.get_pivot_tables("Sheet1").unwrap();
        assert_eq!(pivot_tables.len(), 4);
        assert_eq!(pivot_tables[3].data.len(), 1);
        assert_eq!(
            pivot_tables[3].data[0].show_values_as,
            PivotTableShowValuesAs {
                r#type: PivotTableShowValuesAsType::PERCENT_OF,
                base_field: "Month".to_string(),
                base_item: "Jan".to_string(),
            }
        );

        // x14 show values as types.
        let mut opts = PivotTableOptions {
            data_range: "Sheet1!A1:E31".to_string(),
            pivot_table_range: "Sheet1!AE2:AH28".to_string(),
            rows: vec![
                PivotTableField {
                    data: "Month".to_string(),
                    default_subtotal: true,
                    ..Default::default()
                },
                PivotTableField {
                    data: "Year".to_string(),
                    ..Default::default()
                },
            ],
            data: vec![
                PivotTableField {
                    data: "Revenue".to_string(),
                    subtotal: "Max".to_string(),
                    name: "Summarize by Max".to_string(),
                    show_values_as: PivotTableShowValuesAs {
                        r#type: PivotTableShowValuesAsType::PERCENT_RUNNING_TOTAL_IN,
                        base_field: "Year".to_string(),
                        ..Default::default()
                    },
                    ..Default::default()
                },
                PivotTableField {
                    data: "Revenue".to_string(),
                    subtotal: "Average".to_string(),
                    name: "Average of Sales".to_string(),
                    show_values_as: PivotTableShowValuesAs {
                        r#type: PivotTableShowValuesAsType::RUNNING_TOTAL_IN,
                        base_field: "Year".to_string(),
                        ..Default::default()
                    },
                    ..Default::default()
                },
            ],
            row_grand_totals: true,
            col_grand_totals: true,
            show_drill: true,
            show_row_headers: true,
            show_col_headers: true,
            show_last_column: true,
            ..Default::default()
        };
        f.add_pivot_table(&mut opts).unwrap();

        let pivot_tables = f.get_pivot_tables("Sheet1").unwrap();
        let x14_pt = pivot_tables
            .iter()
            .find(|pt| pt.name == "PivotTable5")
            .unwrap();
        assert_eq!(x14_pt.data.len(), 2);
        assert_eq!(
            x14_pt.data[0].show_values_as.r#type,
            PivotTableShowValuesAsType::PERCENT_RUNNING_TOTAL_IN
        );
        assert_eq!(
            x14_pt.data[1].show_values_as.r#type,
            PivotTableShowValuesAsType::RUNNING_TOTAL_IN
        );

        // Empty subtotal field name and specified style.
        let mut opts = PivotTableOptions {
            data_range: "Sheet1!A1:E31".to_string(),
            pivot_table_range: "Sheet1!AJ4:AK30".to_string(),
            rows: vec![
                PivotTableField {
                    data: "Month".to_string(),
                    default_subtotal: true,
                    ..Default::default()
                },
                PivotTableField {
                    data: "Year".to_string(),
                    ..Default::default()
                },
            ],
            filter: vec![PivotTableField {
                data: "Region".to_string(),
                ..Default::default()
            }],
            columns: vec![],
            data: vec![PivotTableField {
                subtotal: "Sum".to_string(),
                name: "Summarize by Sum".to_string(),
                ..Default::default()
            }],
            row_grand_totals: true,
            col_grand_totals: true,
            show_drill: true,
            show_row_headers: true,
            show_col_headers: true,
            show_last_column: true,
            pivot_table_style_name: "PivotStyleLight19".to_string(),
            ..Default::default()
        };
        f.add_pivot_table(&mut opts).unwrap();

        // Cross-worksheet data range.
        f.new_sheet("Sheet2").unwrap();
        let mut opts = PivotTableOptions {
            data_range: "Sheet1!A1:E31".to_string(),
            pivot_table_range: "Sheet2!A1:AV17".to_string(),
            rows: vec![PivotTableField {
                data: "Month".to_string(),
                ..Default::default()
            }],
            columns: vec![
                PivotTableField {
                    data: "Region".to_string(),
                    default_subtotal: true,
                    ..Default::default()
                },
                PivotTableField {
                    data: "Type".to_string(),
                    default_subtotal: true,
                    ..Default::default()
                },
                PivotTableField {
                    data: "Year".to_string(),
                    ..Default::default()
                },
            ],
            data: vec![PivotTableField {
                data: "Revenue".to_string(),
                subtotal: "Min".to_string(),
                name: "Summarize by Min".to_string(),
                num_fmt: 32,
                ..Default::default()
            }],
            row_grand_totals: true,
            col_grand_totals: true,
            show_drill: true,
            show_row_headers: true,
            show_col_headers: true,
            show_last_column: true,
            ..Default::default()
        };
        f.add_pivot_table(&mut opts).unwrap();
        let pivot_tables = f.get_pivot_tables("Sheet2").unwrap();
        assert_eq!(pivot_tables.len(), 1);
        assert_eq!(pivot_tables[0].data_range, "Sheet1!A1:E31");

        // Selected items in rows, columns and filters.
        let mut opts = PivotTableOptions {
            data_range: "Sheet1!A1:E31".to_string(),
            pivot_table_range: "Sheet1!AM4:AQ12".to_string(),
            rows: vec![PivotTableField {
                data: "Month".to_string(),
                selected_items: vec![
                    "Jan".to_string(),
                    "Feb".to_string(),
                    "Mar".to_string(),
                    "Apr".to_string(),
                    "May".to_string(),
                    "Jun".to_string(),
                    "Jul".to_string(),
                    "Aug".to_string(),
                    "Sep".to_string(),
                    "Oct".to_string(),
                    "Nov".to_string(),
                ],
                ..Default::default()
            }],
            filter: vec![PivotTableField {
                data: "Year".to_string(),
                selected_items: vec!["2017".to_string(), "2018".to_string()],
                ..Default::default()
            }],
            columns: vec![PivotTableField {
                data: "Type".to_string(),
                selected_items: vec![
                    "Meat".to_string(),
                    "Dairy".to_string(),
                    "Beverages".to_string(),
                ],
                ..Default::default()
            }],
            data: vec![PivotTableField {
                data: "Revenue".to_string(),
                subtotal: "Sum".to_string(),
                name: "Summarize by Sum".to_string(),
                ..Default::default()
            }],
            row_grand_totals: true,
            col_grand_totals: true,
            show_drill: true,
            show_error: true,
            show_row_headers: true,
            show_col_headers: true,
            show_last_column: true,
            field_print_titles: true,
            item_print_titles: true,
            pivot_table_style_name: "PivotStyleLight16".to_string(),
            ..Default::default()
        };
        f.add_pivot_table(&mut opts).unwrap();
        let pivot_tables = f.get_pivot_tables("Sheet1").unwrap();
        assert_eq!(pivot_tables.len(), 7);

        f.delete_pivot_table("Sheet1", "PivotTable1").unwrap();
        let pivot_tables = f.get_pivot_tables("Sheet1").unwrap();
        assert_eq!(pivot_tables.len(), 6);
    }

    #[test]
    fn pivot_table_data_range_with_table() {
        let f = new_file();
        let table = Table {
            name: "Table1".to_string(),
            range: "A1:D5".to_string(),
            ..Default::default()
        };
        f.add_table("Sheet1", Some(&table)).unwrap();
        for row in 2..6 {
            f.set_cell_value("Sheet1", &format!("A{row}"), 1).unwrap();
            f.set_cell_value("Sheet1", &format!("B{row}"), 2).unwrap();
            f.set_cell_value("Sheet1", &format!("C{row}"), 3).unwrap();
            f.set_cell_value("Sheet1", &format!("D{row}"), 4).unwrap();
        }

        let mut opts = PivotTableOptions {
            data_range: "Table1".to_string(),
            pivot_table_range: "Sheet1!G2:K7".to_string(),
            rows: vec![PivotTableField {
                data: "Column1".to_string(),
                ..Default::default()
            }],
            columns: vec![PivotTableField {
                data: "Column2".to_string(),
                ..Default::default()
            }],
            row_grand_totals: true,
            col_grand_totals: true,
            show_drill: true,
            show_row_headers: true,
            show_col_headers: true,
            show_last_column: true,
            show_error: true,
            pivot_table_style_name: "PivotStyleLight16".to_string(),
            ..Default::default()
        };
        f.add_pivot_table(&mut opts).unwrap();
        f.delete_pivot_table("Sheet1", "PivotTable1").unwrap();
        let pivot_tables = f.get_pivot_tables("Sheet1").unwrap();
        assert!(pivot_tables.is_empty());
    }

    #[test]
    fn pivot_table_data_range_with_defined_name() {
        let f = new_file();
        create_sample_data(&f);
        f.set_defined_name(&DefinedName {
            name: "dataRange".to_string(),
            refers_to: "Sheet1!A1:E31".to_string(),
            comment: "Pivot Table Data Range".to_string(),
            scope: "Sheet1".to_string(),
        })
        .unwrap();

        let mut opts = PivotTableOptions {
            data_range: "dataRange".to_string(),
            pivot_table_range: "Sheet1!G2:M34".to_string(),
            rows: vec![PivotTableField {
                data: "Month".to_string(),
                default_subtotal: true,
                ..Default::default()
            }],
            data: vec![PivotTableField {
                data: "Revenue".to_string(),
                subtotal: "Sum".to_string(),
                ..Default::default()
            }],
            ..Default::default()
        };
        f.add_pivot_table(&mut opts).unwrap();
        let pivot_tables = f.get_pivot_tables("Sheet1").unwrap();
        assert_eq!(pivot_tables.len(), 1);
        assert_eq!(pivot_tables[0].data_range, "dataRange");
    }

    #[test]
    fn pivot_table_shared_items_mixed_types() {
        let f = new_file();
        for (r, row) in [
            vec!["Type", "Value"],
            vec!["Blank"],
            vec!["Blank"],
            vec!["Integer", "100"],
            vec!["Integer", "100"],
            vec!["Float", "0.01"],
            vec!["Float", "0.01"],
            vec!["Boolean", "true"],
            vec!["Boolean", "true"],
            vec!["String", "text"],
            vec!["String", "text"],
            vec!["Error"],
            vec!["Error"],
            vec!["Formula1"],
            vec!["Formula1"],
            vec!["Formula2"],
            vec!["Formula2"],
            vec!["FormulaError"],
            vec!["FormulaError"],
            vec!["InlineString"],
            vec!["InlineString"],
        ]
        .iter()
        .enumerate()
        {
            for (c, val) in row.iter().enumerate() {
                f.set_cell_str(
                    "Sheet1",
                    &format!("{}{}", (b'A' + c as u8) as char, r + 1),
                    val,
                )
                .unwrap();
            }
        }
        f.set_cell_formula("Sheet1", "B12", "1/0").unwrap();
        f.set_cell_formula("Sheet1", "B13", "1/0").unwrap();
        f.set_cell_formula("Sheet1", "B14", "1+1").unwrap();
        f.set_cell_formula("Sheet1", "B15", "1+1").unwrap();
        f.set_cell_formula("Sheet1", "B16", "_xlfn.TEXTAFTER(\"ab\", \"a\")")
            .unwrap();
        f.set_cell_formula("Sheet1", "B17", "_xlfn.TEXTAFTER(\"ab\", \"a\")")
            .unwrap();

        let selected_items = vec![
            "".to_string(),
            "100".to_string(),
            "0.01".to_string(),
            "true".to_string(),
            "text".to_string(),
            "#DIV/0!".to_string(),
            "2".to_string(),
        ];
        let mut opts = PivotTableOptions {
            data_range: "Sheet1!A1:B21".to_string(),
            pivot_table_range: "Sheet1!D4:E12".to_string(),
            rows: vec![PivotTableField {
                data: "Type".to_string(),
                ..Default::default()
            }],
            data: vec![PivotTableField {
                data: "Type".to_string(),
                subtotal: "Count".to_string(),
                name: "Count of Type".to_string(),
                ..Default::default()
            }],
            filter: vec![PivotTableField {
                data: "Value".to_string(),
                selected_items: selected_items.clone(),
                ..Default::default()
            }],
            row_grand_totals: true,
            col_grand_totals: true,
            show_drill: true,
            show_row_headers: true,
            show_col_headers: true,
            show_last_column: true,
            show_error: true,
            field_print_titles: true,
            item_print_titles: true,
            pivot_table_style_name: "PivotStyleLight16".to_string(),
            ..Default::default()
        };
        f.add_pivot_table(&mut opts).unwrap();
        let pivot_tables = f.get_pivot_tables("Sheet1").unwrap();
        assert_eq!(pivot_tables.len(), 1);
        assert_eq!(pivot_tables[0].filter[0].selected_items, selected_items);

        f.add_slicer(
            "Sheet1",
            &SlicerOptions {
                name: "Value".to_string(),
                cell: "G2".to_string(),
                table_sheet: "Sheet1".to_string(),
                table_name: "PivotTable1".to_string(),
                caption: "Value".to_string(),
                selected_items: vec!["true".to_string()],
                ..Default::default()
            },
        )
        .unwrap();
    }
}