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
use hashbrown::HashMap;
use helper::coordinate::*;
use helper::range::*;
use structs::drawing::spreadsheet::WorksheetDrawing;
use structs::raw::RawWorksheet;
use structs::AutoFilter;
use structs::Cell;
use structs::CellValue;
use structs::Cells;
use structs::Chart;
use structs::Color;
use structs::Column;
use structs::ColumnBreaks;
use structs::Columns;
use structs::Comment;
use structs::ConditionalFormatting;
use structs::DataValidations;
use structs::DefinedName;
use structs::HeaderFooter;
use structs::Hyperlink;
use structs::Image;
use structs::MediaObject;
use structs::MergeCells;
use structs::OleObjects;
use structs::PageMargins;
use structs::PageSetup;
use structs::PrintOptions;
use structs::Range;
use structs::Row;
use structs::RowBreaks;
use structs::Rows;
use structs::SharedStringTable;
use structs::SheetFormatProperties;
use structs::SheetViews;
use structs::Style;
use structs::Stylesheet;
use reader::xlsx::worksheet::*;
/// A Worksheet Object.
#[derive(Clone, Debug, Default)]
pub struct Worksheet {
raw_data_of_worksheet: Option<RawWorksheet>,
r_id: String,
sheet_id: String,
title: String,
cell_collection: Cells,
row_dimensions: Rows,
column_dimensions: Columns,
worksheet_drawing: Box<WorksheetDrawing>,
sheet_state: String,
page_setup: PageSetup,
page_margins: PageMargins,
header_footer: HeaderFooter,
sheet_views: SheetViews,
conditional_formatting_collection: Vec<ConditionalFormatting>,
merge_cells: MergeCells,
auto_filter: Option<AutoFilter>,
comments: Vec<Comment>,
active_cell: String,
tab_color: Option<Color>,
code_name: Option<String>,
ole_objects: OleObjects,
defined_names: Vec<DefinedName>,
print_options: PrintOptions,
column_breaks: ColumnBreaks,
row_breaks: RowBreaks,
data_validations: Option<DataValidations>,
sheet_format_properties: SheetFormatProperties,
}
impl Worksheet {
// ************************
// Value
// ************************
/// Get value.
/// # Arguments
/// * `coordinate` - Specify the coordinates. ex) `"A1"` or `(1, 1)` or `(&1, &1)`
/// # Return value
/// * `String` - Value of the specified cell.
/// # Examples
/// ```
/// let book = umya_spreadsheet::new_file();
/// let worksheet = book.get_sheet(&0).unwrap();
/// let value = worksheet.get_value("A1");
/// // or pass in a tuple `(col, row)`, both col and row starting at `1`
/// let value = worksheet.get_value((1, 1));
/// ```
pub fn get_value<T>(&self, coordinate: T) -> String
where
T: Into<CellCoordinates>,
{
let CellCoordinates { col, row } = coordinate.into();
self.get_cell((col, row))
.map(|v| v.get_value().into())
.unwrap_or("".into())
}
/// Get value number.
/// # Arguments
/// * `coordinate` - Specify the coordinates. ex) `"A1"` or `(1, 1)`
/// # Return value
/// * `Option<f64>` - Value of the specified cell.
/// # Examples
/// ```
/// let book = umya_spreadsheet::new_file();
/// let worksheet = book.get_sheet(&0).unwrap();
/// let value = worksheet.get_value_number("A1");
/// // or pass in a tuple `(col, row)`, both col and row starting at `1`
/// let value = worksheet.get_value_number((1, 1));
/// ```
pub fn get_value_number<T>(&self, coordinate: T) -> Option<f64>
where
T: Into<CellCoordinates>,
{
let CellCoordinates { col, row } = coordinate.into();
self.get_cell((col, row)).and_then(|v| v.get_value_number())
}
/// Get formatted value.
/// # Arguments
/// * `coordinate` - Specify the coordinates. ex) `"A1"` or `(1, 1)` or `(&1, &1)`
/// # Return value
/// * `String` - Formatted value of the specified cell.
/// # Examples
/// ```
/// let book = umya_spreadsheet::new_file();
/// let worksheet = book.get_sheet(&0).unwrap();
/// let value = worksheet.get_formatted_value("A1");
/// // or pass in a tuple `(col, row)`, both col and row starting at `1`
/// let value = worksheet.get_formatted_value((1, 1));
/// ```
pub fn get_formatted_value<T>(&self, coordinate: T) -> String
where
T: Into<CellCoordinates>,
{
let CellCoordinates { col, row } = coordinate.into();
self.cell_collection
.get_formatted_value_by_column_and_row(&col, &row)
}
// ************************
// Cell
// ************************
/// Get Cell List.
pub fn get_cell_collection(&self) -> Vec<&Cell> {
self.cell_collection.get_collection()
}
/// Get Cell List in mutable.
pub fn get_cell_collection_mut(&mut self) -> Vec<&mut Cell> {
self.cell_collection.get_collection_mut()
}
pub fn get_collection_to_hashmap(&self) -> &HashMap<(u32, u32), Cell> {
self.cell_collection.get_collection_to_hashmap()
}
pub fn get_collection_to_hashmap_mut(&mut self) -> &mut HashMap<(u32, u32), Cell> {
self.cell_collection.get_collection_to_hashmap_mut()
}
pub(crate) fn get_cell_collection_stream(
&self,
shared_string_table: &SharedStringTable,
stylesheet: &Stylesheet,
) -> Cells {
if self.is_deserialized() {
panic!("This Worksheet is Deserialized.");
}
read_lite(
self.raw_data_of_worksheet.as_ref().unwrap(),
shared_string_table,
stylesheet,
)
.unwrap()
}
/// (This method is crate only.)
/// Get Cells.
pub(crate) fn get_cell_collection_crate(&self) -> &Cells {
&self.cell_collection
}
/// (This method is crate only.)
/// Get Cells in mutable.
pub(crate) fn get_cell_collection_crate_mut(&mut self) -> &mut Cells {
&mut self.cell_collection
}
/// Get cell.
/// # Arguments
/// * `coordinate` - Specify the coordinates. ex) `"A1"` or `(1, 1)` or `(&1, &1)`
/// # Return value
/// * `Option` - Cell in the Some.
/// # Examples
/// ```
/// let book = umya_spreadsheet::new_file();
/// let worksheet = book.get_sheet(&0).unwrap();
/// let cell = worksheet.get_cell("A1");
/// // or pass in a tuple `(col, row)`, both col and row starting at `1`
/// let cell = worksheet.get_cell((1, 1));
/// ```
pub fn get_cell<T>(&self, coordinate: T) -> Option<&Cell>
where
T: Into<CellCoordinates>,
{
self.cell_collection.get(coordinate)
}
/// Get cell with mutable.
/// # Arguments
/// * `coordinate` - Specify the coordinates. ex) `"A1"` or `(1, 1)` or `(&1, &1)`
/// # Return value
/// * `&mut Cell` - Cell with mutable.
/// # Examples
/// ```
/// let mut book = umya_spreadsheet::new_file();
/// let mut worksheet = book.get_sheet_mut(&0).unwrap();
/// let cell = worksheet.get_cell_mut("A1");
/// // or pass in a tuple `(col, row)`, both col and row starting at `1`
/// let cell = worksheet.get_cell_mut((1, 1));
/// ```
pub fn get_cell_mut<T>(&mut self, coordinate: T) -> &mut Cell
where
T: Into<CellCoordinates>,
{
let CellCoordinates { col, row } = coordinate.into();
self.get_row_dimension_mut(&row);
self.cell_collection.get_mut((col, row))
}
pub fn get_collection_by_column(&self, column_num: &u32) -> Vec<&Cell> {
self.cell_collection.get_collection_by_column(column_num)
}
pub fn get_collection_by_row(&self, row_num: &u32) -> Vec<&Cell> {
self.cell_collection.get_collection_by_row(row_num)
}
pub fn get_collection_by_column_to_hashmap(&self, column_num: &u32) -> HashMap<u32, &Cell> {
self.cell_collection
.get_collection_by_column_to_hashmap(column_num)
}
pub fn get_collection_by_row_to_hashmap(&self, row_num: &u32) -> HashMap<u32, &Cell> {
self.cell_collection
.get_collection_by_row_to_hashmap(row_num)
}
/// Set Cell
/// # Arguments
/// * `cell` - Cell
pub fn set_cell(&mut self, cell: Cell) -> &mut Self {
self.get_row_dimension_mut(cell.get_coordinate().get_row_num());
self.cell_collection.set(cell);
self
}
/// Remove Cell
/// # Arguments
/// * `coordinate` - Specify the coordinates. ex) `"A1"` or `(1, 1)` or `(&1, &1)`
/// # Examples
/// ```
/// worksheet.remove_cell("A1");
/// // or pass in a tuple `(col, row)`, both col and row starting at `1`
/// worksheet.remove_cell((1, 1));
/// ```
pub fn remove_cell<T>(&mut self, coordinate: T) -> bool
where
T: Into<CellCoordinates>,
{
let CellCoordinates { col, row } = coordinate.into();
self.cell_collection.remove(&col, &row)
}
/// Get cell value.
/// # Arguments
/// * `coordinate` - Specify the coordinates. ex) `"A1"` or `(1, 1)` or `(&1, &1)`
/// # Return value
/// * `&CellValue` - CellValue.
/// # Examples
/// ```
/// let book = umya_spreadsheet::new_file();
/// let worksheet = book.get_sheet(&0).unwrap();
/// let cell_value = worksheet.get_cell_value("A1");
/// // or pass in a tuple `(col, row)`, both col and row starting at `1`
/// let cell_value = worksheet.get_cell_value((1, 1));
/// ```
pub fn get_cell_value<T>(&self, coordinate: T) -> &CellValue
where
T: Into<CellCoordinates>,
{
self.cell_collection.get_cell_value(coordinate)
}
/// Get cell value with mutable.
/// # Arguments
/// * `coordinate` - Specify the coordinates. ex) `"A1"` or `(1, 1)` or `(&1, &1)`
/// # Return value
/// * `&mut CellValue` - CellValue with mutable.
/// # Examples
/// ```
/// let mut book = umya_spreadsheet::new_file();
/// let mut worksheet = book.get_sheet_mut(&0).unwrap();
/// let cell_value = worksheet.get_cell_value_mut("A1");
/// // or pass in a tuple `(col, row)`, both col and row starting at `1`
/// let cell_value = worksheet.get_cell_value_mut((1, 1));
/// ```
pub fn get_cell_value_mut<T>(&mut self, coordinate: T) -> &mut CellValue
where
T: Into<CellCoordinates>,
{
let CellCoordinates { col, row } = coordinate.into();
self.get_row_dimension_mut(&row);
self.cell_collection
.get_mut((col, row))
.get_cell_value_mut()
}
/// Gets the cell value by specifying an range.
/// # Arguments
/// * `range` - range. ex) "A1:C5"
/// # Return value
/// *`Vec<&CellValue>` - CellValue List.
/// # Examples
/// ```
/// let mut book = umya_spreadsheet::new_file();
/// let mut worksheet = book.get_sheet_mut(&0).unwrap();
/// let mut cell_value_List = worksheet.get_cell_value_by_range("A1:C5");
/// ```
pub fn get_cell_value_by_range(&self, range: &str) -> Vec<&CellValue> {
self.cell_collection.get_cell_value_by_range(range)
}
/// Get style.
/// # Arguments
/// * `coordinate` - Specify the coordinates. ex) `"A1"` or `(1, 1)` or `(&1, &1)`
/// # Return value
/// * `&Style` - Style.
/// # Examples
/// ```
/// let book = umya_spreadsheet::new_file();
/// let worksheet = book.get_sheet(&0).unwrap();
/// let style = worksheet.get_style("A1");
/// // or pass in a tuple `(col, row)`, both col and row starting at `1`
/// let style = worksheet.get_style((1, 1));
/// ```
pub fn get_style<T>(&self, coordinate: T) -> &Style
where
T: Into<CellCoordinates>,
{
self.cell_collection.get_style(coordinate)
}
/// Get style with mutable.
/// # Arguments
/// * `coordinate` - Specify the coordinates. ex) `"A1"` or `(1, 1)` or `(&1, &1)`
/// # Return value
/// * `&mut Style` - Style with mutable.
/// # Examples
/// ```
/// let mut book = umya_spreadsheet::new_file();
/// let mut worksheet = book.get_sheet_mut(&0).unwrap();
/// let style = worksheet.get_style_mut("A1");
/// // or pass in a tuple `(col, row)`, both col and row starting at `1`
/// let style = worksheet.get_style_mut((1, 1));
/// ```
pub fn get_style_mut<T>(&mut self, coordinate: T) -> &mut Style
where
T: Into<CellCoordinates>,
{
let CellCoordinates { col, row } = coordinate.into();
self.get_row_dimension_mut(&row);
self.cell_collection.get_mut((col, row)).get_style_mut()
}
pub fn set_style<T>(&mut self, coordinate: T, style: Style) -> &mut Self
where
T: Into<CellCoordinates>,
{
let CellCoordinates { col, row } = coordinate.into();
self.get_row_dimension_mut(&row);
self.cell_collection.get_mut((&col, &row)).set_style(style);
self
}
/// Set style by range.
/// # Arguments
/// * `range` - Specify the range. ex) "A1:B2"
/// * `style` - Style
/// # Return value
/// * `&mut Self` - Self.
/// # Examples
/// ```
/// let mut book = umya_spreadsheet::new_file();
/// let mut worksheet = book.get_sheet_mut(&0).unwrap();
/// let mut style = umya_spreadsheet::Style::default();
/// style.get_borders_mut().get_bottom_mut().set_border_style(umya_spreadsheet::Border::BORDER_MEDIUM);
/// worksheet.set_style_by_range("A1:A3", style);
/// ```
pub fn set_style_by_range(&mut self, range: &str, style: Style) -> &mut Self {
let range_upper = range.to_uppercase();
let coordinate_list = get_coordinate_list(&range_upper);
let (col_num_start, row_num_start) = coordinate_list[0];
if col_num_start == 0 {
let (_, row_num_end) = coordinate_list[1];
for row_num in row_num_start..=row_num_end {
self.get_row_dimension_mut(&row_num)
.set_style(style.clone());
}
return self;
}
if row_num_start == 0 {
let (col_num_end, _) = coordinate_list[1];
for col_num in col_num_start..=col_num_end {
self.get_column_dimension_by_number_mut(&col_num)
.set_style(style.clone());
}
return self;
}
for (col_num, row_num) in coordinate_list {
self.set_style((col_num, row_num), style.clone());
}
self
}
// ************************
// Comment
// ************************
/// Get Comments
pub fn get_comments(&self) -> &Vec<Comment> {
&self.comments
}
/// Get Comments in mutable.
pub fn get_comments_mut(&mut self) -> &mut Vec<Comment> {
&mut self.comments
}
/// Get Comments convert to hashmap.
pub fn get_comments_to_hashmap(&self) -> HashMap<String, &Comment> {
let mut result = HashMap::default();
for comment in &self.comments {
let coordinate = comment.get_coordinate().get_coordinate();
result.insert(coordinate, comment);
}
result
}
/// Set Comments.
/// # Arguments
/// * `value` - Comment List (Vec)
pub fn set_comments(&mut self, value: Vec<Comment>) {
self.comments = value;
}
/// Add Comments.
/// # Arguments
/// * `value` - Comment
pub fn add_comments(&mut self, value: Comment) {
self.comments.push(value);
}
/// Has Comments.
pub fn has_comments(&self) -> bool {
!self.comments.is_empty()
}
// ************************
// Conditional
// ************************
/// Get ConditionalFormatting list.
pub fn get_conditional_formatting_collection(&self) -> &Vec<ConditionalFormatting> {
&self.conditional_formatting_collection
}
/// Set ConditionalFormatting.
/// # Arguments
/// * `value` - ConditionalSet List (Vec)
pub fn set_conditional_formatting_collection(&mut self, value: Vec<ConditionalFormatting>) {
self.conditional_formatting_collection = value;
}
/// Add ConditionalFormatting.
/// # Arguments
/// * `value` - ConditionalFormatting
pub fn add_conditional_formatting_collection(&mut self, value: ConditionalFormatting) {
self.conditional_formatting_collection.push(value);
}
// ************************
// Hyperlink
// ************************
/// (This method is crate only.)
/// Get Hyperlink convert to hashmap.
pub(crate) fn get_hyperlink_collection_to_hashmap(&self) -> HashMap<String, &Hyperlink> {
let mut result: HashMap<String, &Hyperlink> = HashMap::new();
for cell in self.cell_collection.get_collection() {
match cell.get_hyperlink() {
Some(hyperlink) => {
let coordition = coordinate_from_index(
cell.get_coordinate().get_col_num(),
cell.get_coordinate().get_row_num(),
);
result.insert(coordition, hyperlink);
}
None => {}
}
}
result
}
/// (This method is crate only.)
/// Has Hyperlink
pub(crate) fn has_hyperlink(&self) -> bool {
self.cell_collection.has_hyperlink()
}
// ************************
// Merge Cells
// ************************
// Get Merge Cells
pub fn get_merge_cells(&self) -> &Vec<Range> {
self.merge_cells.get_range_collection()
}
// Get Merge Cells in mutable.
pub fn get_merge_cells_mut(&mut self) -> &mut Vec<Range> {
self.merge_cells.get_range_collection_mut()
}
// Add Merge Cells.
/// # Arguments
/// * `range` - Range. ex) "A1:C5"
/// # Examples
/// ```
/// let mut book = umya_spreadsheet::new_file();
/// let mut worksheet = book.get_sheet_mut(&0).unwrap();
/// worksheet.add_merge_cells("A1:C5");
/// ```
pub fn add_merge_cells<S: Into<String>>(&mut self, range: S) -> &mut Self {
self.merge_cells.add_range(range);
self
}
/// (This method is crate only.)
// Get Merge Cells Object
pub(crate) fn get_merge_cells_crate(&self) -> &MergeCells {
&self.merge_cells
}
/// (This method is crate only.)
// Get Merge Cells Object in mutable.
pub(crate) fn get_merge_cells_crate_mut(&mut self) -> &mut MergeCells {
&mut self.merge_cells
}
// ************************
// Auto Filter
// ************************
// Get Auto Filter (Option).
pub fn get_auto_filter(&self) -> &Option<AutoFilter> {
&self.auto_filter
}
// Get Auto Filter (Option) in mutable.
pub fn get_auto_filter_mut(&mut self) -> &mut Option<AutoFilter> {
&mut self.auto_filter
}
// Set Auto Filter.
/// # Arguments
/// * `range` - Range. ex) "A2:K2"
/// # Examples
/// ```
/// let mut book = umya_spreadsheet::new_file();
/// let mut worksheet = book.get_sheet_mut(&0).unwrap();
/// worksheet.set_auto_filter("A2:K2");
/// ```
pub fn set_auto_filter<S: Into<String>>(&mut self, range: S) {
let mut auto_filter = AutoFilter::default();
auto_filter.set_range(range);
self.auto_filter = Some(auto_filter);
}
// Remove Auto Filter.
pub fn remove_auto_filter(&mut self) {
self.auto_filter = None;
}
// ************************
// Column Dimensions
// ************************
/// Get Column Dimension List.
pub fn get_column_dimensions(&self) -> &Vec<Column> {
self.column_dimensions.get_column_collection()
}
/// Get Column Dimension List in mutable.
pub fn get_column_dimensions_mut(&mut self) -> &mut Vec<Column> {
self.column_dimensions.get_column_collection_mut()
}
/// Calculation Auto Width.
pub fn calculation_auto_width(&mut self) -> &mut Self {
let cells = self.get_cell_collection_crate().clone();
let merge_cells = self.get_merge_cells_crate().clone();
self.get_column_dimensions_crate_mut()
.calculation_auto_width(&cells, &merge_cells);
self
}
/// Get Column Dimension.
/// # Arguments
/// * `column` - Column Char. ex) "A"
pub fn get_column_dimension(&self, column: &str) -> Option<&Column> {
let column_upper = column.to_uppercase();
let col = column_index_from_string(column_upper);
self.get_column_dimension_by_number(&col)
}
/// Get Column Dimension in mutable.
/// # Arguments
/// * `column` - Column Char. ex) "A"
pub fn get_column_dimension_mut(&mut self, column: &str) -> &mut Column {
let column_upper = column.to_uppercase();
let col = column_index_from_string(column_upper);
self.get_column_dimension_by_number_mut(&col)
}
/// Get Column Dimension.
/// # Arguments
/// * `col` - Column Number.
pub fn get_column_dimension_by_number(&self, col: &u32) -> Option<&Column> {
self.get_column_dimensions_crate().get_column(col)
}
/// Get Column Dimension in mutable.
/// # Arguments
/// * `col` - Column Number.
pub fn get_column_dimension_by_number_mut(&mut self, col: &u32) -> &mut Column {
self.get_column_dimensions_crate_mut().get_column_mut(col)
}
/// (This method is crate only.)
/// Get Column Dimension.
pub(crate) fn get_column_dimensions_crate(&self) -> &Columns {
&self.column_dimensions
}
/// (This method is crate only.)
/// Get Column Dimension in mutable.
pub(crate) fn get_column_dimensions_crate_mut(&mut self) -> &mut Columns {
&mut self.column_dimensions
}
/// (This method is crate only.)
/// Set Column Dimension.
pub(crate) fn set_column_dimensions_crate(&mut self, value: Columns) -> &mut Self {
self.column_dimensions = value;
self
}
// ************************
// Row Dimensions
// ************************
pub fn has_sheet_data(&self) -> bool {
self.row_dimensions.has_sheet_data()
}
/// Get Row Dimension List.
pub fn get_row_dimensions(&self) -> Vec<&Row> {
self.row_dimensions.get_row_dimensions()
}
/// Get Row Dimension List in mutable.
pub fn get_row_dimensions_mut(&mut self) -> Vec<&mut Row> {
self.row_dimensions.get_row_dimensions_mut()
}
/// Get Row Dimension convert Hashmap.
pub fn get_row_dimensions_to_hashmap(&self) -> &HashMap<u32, Row> {
self.row_dimensions.get_row_dimensions_to_hashmap()
}
pub fn get_row_dimensions_to_hashmap_mut(&mut self) -> &mut HashMap<u32, Row> {
self.row_dimensions.get_row_dimensions_to_hashmap_mut()
}
/// Get Row Dimension.
pub fn get_row_dimension(&self, row: &u32) -> Option<&Row> {
self.row_dimensions.get_row_dimension(row)
}
/// Get Row Dimension in mutable.
pub fn get_row_dimension_mut(&mut self, row: &u32) -> &mut Row {
self.row_dimensions.get_row_dimension_mut(row)
}
/// (This method is crate only.)
/// Set Row Dimension.
pub(crate) fn set_row_dimension(&mut self, value: Row) -> &mut Self {
self.row_dimensions.set_row_dimension(value);
self
}
/// (This method is crate only.)
/// Get Row Dimension in mutable.
pub(crate) fn get_row_dimensions_crate_mut(&mut self) -> &mut Rows {
&mut self.row_dimensions
}
/// (This method is crate only.)
/// Get Row Dimension.
pub(crate) fn _get_row_dimensions_crate(&self) -> &Rows {
&self.row_dimensions
}
// ************************
// WorksheetDrawing
// ************************
/// Get WorksheetDrawing.
pub fn get_worksheet_drawing(&self) -> &WorksheetDrawing {
&self.worksheet_drawing
}
/// Get WorksheetDrawing in mutable.
pub fn get_worksheet_drawing_mut(&mut self) -> &mut WorksheetDrawing {
&mut self.worksheet_drawing
}
/// Set WorksheetDrawing.
/// # Arguments
/// * `value` - WorksheetDrawing
pub fn set_worksheet_drawing(&mut self, value: WorksheetDrawing) {
self.worksheet_drawing = Box::new(value);
}
/// Has WorksheetDrawing.
pub fn has_drawing_object(&self) -> bool {
self.worksheet_drawing.has_drawing_object()
}
// ************************
// update Coordinate
// ************************
/// Insert new rows.
/// # Arguments
/// * `row_index` - Specify point of insert. ex) 1
/// * `num_rows` - Specify number to insert. ex) 2
/// # Examples
/// ```
/// let mut book = umya_spreadsheet::new_file();
/// let mut worksheet = book.get_sheet_mut(&0).unwrap();
/// worksheet.insert_new_row(&2, &3);
/// ```
pub fn insert_new_row(&mut self, row_index: &u32, num_rows: &u32) {
self.adjustment_insert_coordinate(&0, &0, row_index, num_rows);
}
/// Adjust for references to other sheets.
pub fn insert_new_row_from_other_sheet(
&mut self,
sheet_name: &str,
row_index: &u32,
num_rows: &u32,
) {
self.adjustment_insert_coordinate_from_other_sheet(sheet_name, &0, &0, row_index, num_rows);
}
/// Insert new columns.
/// # Arguments
/// * `column` - Specify point of insert. ex) "B"
/// * `num_columns` - Specify number to insert. ex) 3
/// # Examples
/// ```
/// let mut book = umya_spreadsheet::new_file();
/// let mut worksheet = book.get_sheet_mut(&0).unwrap();
/// worksheet.insert_new_column("B", &3);
/// ```
pub fn insert_new_column(&mut self, column: &str, num_columns: &u32) {
let column_upper = column.to_uppercase();
let column_index = column_index_from_string(column_upper);
self.insert_new_column_by_index(&column_index, num_columns);
}
/// Adjust for references to other sheets.
pub fn insert_new_column_from_other_sheet(
&mut self,
sheet_name: &str,
column: &str,
num_columns: &u32,
) {
let column_upper = column.to_uppercase();
let column_index = column_index_from_string(column_upper);
self.insert_new_column_by_index_from_other_sheet(sheet_name, &column_index, num_columns);
}
/// Insert new columns.
/// # Arguments
/// * `column_index` - Specify point of insert. ex) 2
/// * `num_columns` - Specify number to insert. ex) 3
/// # Examples
/// ```
/// let mut book = umya_spreadsheet::new_file();
/// let mut worksheet = book.get_sheet_mut(&0).unwrap();
/// worksheet.insert_new_column_by_index(&2, &3);
/// ```
pub fn insert_new_column_by_index(&mut self, column_index: &u32, num_columns: &u32) {
self.adjustment_insert_coordinate(column_index, num_columns, &0, &0);
}
/// Adjust for references to other sheets.
pub fn insert_new_column_by_index_from_other_sheet(
&mut self,
sheet_name: &str,
column_index: &u32,
num_columns: &u32,
) {
self.adjustment_insert_coordinate_from_other_sheet(
sheet_name,
column_index,
num_columns,
&0,
&0,
);
}
/// Remove rows.
/// # Arguments
/// * `row_index` - Specify point of remove. ex) 1
/// * `num_rows` - Specify number to remove. ex) 2
/// # Examples
/// ```
/// let mut book = umya_spreadsheet::new_file();
/// let mut worksheet = book.get_sheet_mut(&0).unwrap();
/// worksheet.remove_row(&2, &3);
/// ```
pub fn remove_row(&mut self, row_index: &u32, num_rows: &u32) {
self.adjustment_remove_coordinate(&0, &0, row_index, num_rows);
}
/// Adjust for references to other sheets.
pub fn remove_row_from_other_sheet(
&mut self,
sheet_name: &str,
row_index: &u32,
num_rows: &u32,
) {
self.adjustment_remove_coordinate_from_other_sheet(sheet_name, &0, &0, row_index, num_rows);
}
/// Remove columns.
/// # Arguments
/// * `sheet_name` - Specify the sheet name. ex) "Sheet1"
/// * `column` - Specify point of remove. ex) "B"
/// * `num_columns` - Specify number to remove. ex) 3
/// # Examples
/// ```
/// let mut book = umya_spreadsheet::new_file();
/// let mut worksheet = book.get_sheet_mut(&0).unwrap();
/// worksheet.remove_column("B", &3);
/// ```
pub fn remove_column(&mut self, column: &str, num_columns: &u32) {
let column_upper = column.to_uppercase();
let column_index = column_index_from_string(column_upper);
self.remove_column_by_index(&column_index, num_columns);
}
/// Adjust for references to other sheets.
pub fn remove_column_from_other_sheet(
&mut self,
sheet_name: &str,
column: &str,
num_columns: &u32,
) {
let column_upper = column.to_uppercase();
let column_index = column_index_from_string(column_upper);
self.remove_column_by_index_from_other_sheet(sheet_name, &column_index, num_columns);
}
/// Remove columns.
/// # Arguments
/// * `column_index` - Specify point of remove. ex) 2
/// * `num_columns` - Specify number to remove. ex) 3
/// # Examples
/// ```
/// let mut book = umya_spreadsheet::new_file();
/// let mut worksheet = book.get_sheet_mut(&0).unwrap();
/// worksheet.remove_column_by_index(&2, &3);
/// ```
pub fn remove_column_by_index(&mut self, column_index: &u32, num_columns: &u32) {
self.adjustment_remove_coordinate(column_index, num_columns, &0, &0);
}
/// Adjust for references to other sheets.
pub fn remove_column_by_index_from_other_sheet(
&mut self,
sheet_name: &str,
column_index: &u32,
num_columns: &u32,
) {
self.adjustment_remove_coordinate_from_other_sheet(
sheet_name,
column_index,
num_columns,
&0,
&0,
);
}
/// (This method is crate only.)
/// Adjustment Insert Coordinate
pub(crate) fn adjustment_insert_coordinate(
&mut self,
root_col_num: &u32,
offset_col_num: &u32,
root_row_num: &u32,
offset_row_num: &u32,
) {
if offset_col_num != &0 {
// column dimensions
self.column_dimensions
.adjustment_insert_coordinate(root_col_num, offset_col_num);
}
if offset_row_num != &0 {
// row dimensions
self.get_row_dimensions_crate_mut()
.adjustment_insert_coordinate(root_row_num, offset_row_num);
}
if offset_col_num != &0 || offset_row_num != &0 {
// defined_names
let title = self.title.clone();
for defined_name in &mut self.defined_names {
defined_name
.get_address_obj_mut()
.adjustment_insert_coordinate(
&title,
root_col_num,
offset_col_num,
root_row_num,
offset_row_num,
);
}
// cell
self.get_cell_collection_crate_mut()
.adjustment_insert_coordinate(
root_col_num,
offset_col_num,
root_row_num,
offset_row_num,
);
// comments
for comment in &mut self.comments {
comment.adjustment_insert_coordinate(
root_col_num,
offset_col_num,
root_row_num,
offset_row_num,
);
}
// conditional styles
for conditional_styles in &mut self.conditional_formatting_collection {
for range in conditional_styles
.get_sequence_of_references_mut()
.get_range_collection_mut()
{
range.adjustment_insert_coordinate(
root_col_num,
offset_col_num,
root_row_num,
offset_row_num,
);
}
}
// merge cells
for merge_cell in self.get_merge_cells_mut() {
merge_cell.adjustment_insert_coordinate(
root_col_num,
offset_col_num,
root_row_num,
offset_row_num,
);
}
// auto filter
match self.get_auto_filter_mut() {
Some(v) => {
v.get_range_mut().adjustment_insert_coordinate(
root_col_num,
offset_col_num,
root_row_num,
offset_row_num,
);
}
None => {}
};
}
}
pub(crate) fn adjustment_insert_coordinate_from_other_sheet(
&mut self,
sheet_name: &str,
root_col_num: &u32,
offset_col_num: &u32,
root_row_num: &u32,
offset_row_num: &u32,
) {
if offset_col_num != &0 || offset_row_num != &0 {
// cell formula coordinate
let title = self.title.clone();
self.get_cell_collection_crate_mut()
.adjustment_insert_formula_coordinate(
&title,
sheet_name,
root_col_num,
offset_col_num,
root_row_num,
offset_row_num,
);
// chart
self.worksheet_drawing.adjustment_insert_coordinate(
sheet_name,
root_col_num,
offset_col_num,
root_row_num,
offset_row_num,
);
}
}
/// (This method is crate only.)
/// Adjustment Remove Coordinate
pub(crate) fn adjustment_remove_coordinate(
&mut self,
root_col_num: &u32,
offset_col_num: &u32,
root_row_num: &u32,
offset_row_num: &u32,
) {
if offset_col_num != &0 {
// column dimensions
self.column_dimensions
.adjustment_remove_coordinate(root_col_num, offset_col_num);
}
if offset_row_num != &0 {
// row dimensions
self.get_row_dimensions_crate_mut()
.adjustment_remove_coordinate(root_row_num, offset_row_num);
}
if offset_col_num != &0 || offset_row_num != &0 {
// defined_names
let title = self.title.clone();
self.defined_names.retain(|x| {
!(x.get_address_obj().is_remove(
&title,
root_col_num,
offset_col_num,
root_row_num,
offset_row_num,
))
});
for defined_name in &mut self.defined_names {
defined_name
.get_address_obj_mut()
.adjustment_remove_coordinate(
&title,
root_col_num,
offset_col_num,
root_row_num,
offset_row_num,
);
}
// cell
self.get_cell_collection_crate_mut()
.adjustment_remove_coordinate(
root_col_num,
offset_col_num,
root_row_num,
offset_row_num,
);
// comments
self.comments.retain(|x| {
!(x.get_coordinate().is_remove(
root_col_num,
offset_col_num,
root_row_num,
offset_row_num,
))
});
for comment in &mut self.comments {
comment.adjustment_remove_coordinate(
root_col_num,
offset_col_num,
root_row_num,
offset_row_num,
);
}
// conditional styles
for conditional_styles in &mut self.conditional_formatting_collection {
conditional_styles
.get_sequence_of_references_mut()
.get_range_collection_mut()
.retain(|x| {
!(x.is_remove(root_col_num, offset_col_num, root_row_num, offset_row_num))
});
}
self.conditional_formatting_collection.retain(|x| {
!x.get_sequence_of_references()
.get_range_collection()
.is_empty()
});
for conditional_styles in &mut self.conditional_formatting_collection {
for range in conditional_styles
.get_sequence_of_references_mut()
.get_range_collection_mut()
{
range.adjustment_remove_coordinate(
root_col_num,
offset_col_num,
root_row_num,
offset_row_num,
);
}
}
// merge cells
self.get_merge_cells_mut().retain(|x| {
!(x.is_remove(root_col_num, offset_col_num, root_row_num, offset_row_num))
});
for merge_cell in self.get_merge_cells_mut() {
merge_cell.adjustment_remove_coordinate(
root_col_num,
offset_col_num,
root_row_num,
offset_row_num,
);
}
// auto filter
let is_remove = match self.get_auto_filter() {
Some(v) => v.get_range().is_remove(
root_col_num,
offset_col_num,
root_row_num,
offset_row_num,
),
None => false,
};
if is_remove {
self.remove_auto_filter();
}
match self.get_auto_filter_mut() {
Some(v) => {
v.get_range_mut().adjustment_remove_coordinate(
root_col_num,
offset_col_num,
root_row_num,
offset_row_num,
);
}
None => {}
};
}
}
/// (This method is crate only.)
/// Adjustment Remove Coordinate
pub(crate) fn adjustment_remove_coordinate_from_other_sheet(
&mut self,
sheet_name: &str,
root_col_num: &u32,
offset_col_num: &u32,
root_row_num: &u32,
offset_row_num: &u32,
) {
if offset_col_num != &0 || offset_row_num != &0 {
// cell formula coordinate
let title = self.title.clone();
self.get_cell_collection_crate_mut()
.adjustment_remove_formula_coordinate(
&title,
sheet_name,
root_col_num,
offset_col_num,
root_row_num,
offset_row_num,
);
// chart
self.worksheet_drawing.adjustment_remove_coordinate(
sheet_name,
root_col_num,
offset_col_num,
root_row_num,
offset_row_num,
);
}
}
/// Get Code Name.
pub fn get_code_name(&self) -> &Option<String> {
&self.code_name
}
/// Set Code Name.
/// # Arguments
/// * `value` - Code Name
pub fn set_code_name<S: Into<String>>(&mut self, value: S) {
self.code_name = Some(value.into());
}
/// Get Header Footer.
pub fn get_header_footer(&self) -> &HeaderFooter {
&self.header_footer
}
/// Get Header Footer in mutable.
pub fn get_header_footer_mut(&mut self) -> &mut HeaderFooter {
&mut self.header_footer
}
/// Set Header Footer.
/// # Arguments
/// * `value` - Header Footer
pub fn set_header_footer(&mut self, value: HeaderFooter) -> &mut Self {
self.header_footer = value;
self
}
/// Get Active Cell.
pub fn get_active_cell(&self) -> &str {
&self.active_cell
}
/// Set Active Cell.
/// # Arguments
/// * `cell` - Cell ex) "A1"
pub fn set_active_cell<S: Into<String>>(&mut self, cell: S) {
self.active_cell = cell.into();
}
/// Get R Id.
pub(crate) fn get_r_id(&self) -> &String {
&self.r_id
}
/// (This method is crate only.)
/// Set r Id.
pub(crate) fn set_r_id<S: Into<String>>(&mut self, value: S) {
self.r_id = value.into();
}
/// Get Sheet Id.
pub fn get_sheet_id(&self) -> &String {
&self.sheet_id
}
/// (This method is crate only.)
/// Set Sheet Id.
pub(crate) fn set_sheet_id<S: Into<String>>(&mut self, value: S) {
self.sheet_id = value.into();
}
/// Has Code Name.
pub fn has_code_name(&self) -> bool {
self.code_name.is_some()
}
/// Get Tab Color.
pub fn get_tab_color(&self) -> &Option<Color> {
&self.tab_color
}
/// Get Tab Color in mutable.
pub fn get_tab_color_mut(&mut self) -> &mut Color {
match &self.tab_color {
Some(_) => return self.tab_color.as_mut().unwrap(),
None => {}
}
self.set_tab_color(Color::default());
self.tab_color.as_mut().unwrap()
}
/// Set Tab Color.
/// # Arguments
/// * `value` - Color
pub fn set_tab_color(&mut self, value: Color) -> &mut Self {
self.tab_color = Some(value);
self
}
/// Remove Tab Color.
pub fn remove_tab_color(&mut self) -> &mut Self {
self.tab_color = None;
self
}
/// Calculate Worksheet Dimension.
pub fn calculate_worksheet_dimension(&self) -> String {
let (column, row) = self.cell_collection.get_highest_column_and_row();
if row == 0 {
return "A1".to_string();
}
let column_str = string_from_column_index(&column);
format!("A1:{}{}", column_str, row)
}
// Get Highest Column and Row Index
/// # Return value
/// *`(u32, u32)` - (column, row)
pub fn get_highest_column_and_row(&self) -> (u32, u32) {
self.cell_collection.get_highest_column_and_row()
}
// Get Highest Column Index
pub fn get_highest_column(&self) -> u32 {
let (column, _row) = self.cell_collection.get_highest_column_and_row();
column
}
// Get Highest Row Index
pub fn get_highest_row(&self) -> u32 {
let (_column, row) = self.cell_collection.get_highest_column_and_row();
row
}
/// Get SheetName.
pub fn get_name(&self) -> &str {
&self.title
}
/// Set SheetName.
/// # Arguments
/// * `sheet_name` - Sheet Name. [Caution] no duplicate other worksheet.
pub fn set_name<S: Into<String>>(&mut self, sheet_name: S) -> &mut Self {
self.title = sheet_name.into();
let title = self.get_name().to_string();
for defined_name in self.get_defined_names_mut() {
defined_name.get_address_obj_mut().set_sheet_name(&title);
}
self
}
// Get Sheet State
pub fn get_sheet_state(&self) -> &str {
&self.sheet_state
}
/// Set Sheet State.
/// # Arguments
/// * `value` - Sheet State.
pub fn set_sheet_state(&mut self, value: String) -> &mut Self {
self.sheet_state = value;
self
}
// Get Page Setup.
pub fn get_page_setup(&self) -> &PageSetup {
&self.page_setup
}
// Get Page Setup in mutable.
pub fn get_page_setup_mut(&mut self) -> &mut PageSetup {
&mut self.page_setup
}
/// Set Page Setup.
/// # Arguments
/// * `value` - PageSetup.
pub fn set_page_setup(&mut self, value: PageSetup) -> &mut Self {
self.page_setup = value;
self
}
// Get Page Margins.
pub fn get_page_margins(&self) -> &PageMargins {
&self.page_margins
}
// Get Page Margins in mutable.
pub fn get_page_margins_mut(&mut self) -> &mut PageMargins {
&mut self.page_margins
}
/// Set Page Margins.
/// # Arguments
/// * `value` - PageMargins.
pub fn set_page_margins(&mut self, value: PageMargins) -> &mut Self {
self.page_margins = value;
self
}
// Get SheetViews.
pub fn get_sheets_views(&self) -> &SheetViews {
&self.sheet_views
}
// Get SheetViews in mutable.
pub fn get_sheet_views_mut(&mut self) -> &mut SheetViews {
&mut self.sheet_views
}
/// Set SheetViews.
/// # Arguments
/// * `value` - SheetViews.
pub fn set_sheets_views(&mut self, value: SheetViews) -> &mut Self {
self.sheet_views = value;
self
}
// Get Ole Objects.
pub fn get_ole_objects(&self) -> &OleObjects {
&self.ole_objects
}
// Get Ole Objects in mutable.
pub fn get_ole_objects_mut(&mut self) -> &mut OleObjects {
&mut self.ole_objects
}
/// Set Ole Objects.
/// # Arguments
/// * `value` - OleObjects.
pub fn set_ole_objects(&mut self, value: OleObjects) -> &mut Self {
self.ole_objects = value;
self
}
/// Get Defined Name (Vec).
pub fn get_defined_names(&self) -> &Vec<DefinedName> {
&self.defined_names
}
/// Get Defined Name (Vec) in mutable.
pub fn get_defined_names_mut(&mut self) -> &mut Vec<DefinedName> {
&mut self.defined_names
}
/// Set Defined Name (Vec).
/// # Arguments
/// * `value` - Vec<DefinedName>.
pub fn set_defined_names(&mut self, value: Vec<DefinedName>) {
self.defined_names = value;
}
/// Add Defined Name.
/// # Arguments
/// * `value` - DefinedName.
pub fn add_defined_names(&mut self, value: DefinedName) {
self.defined_names.push(value);
}
/// Add Defined Name.
/// # Arguments
/// * `name` - Name. ex) "DefinedName01"
/// * `address` - Address. ex) "A1:A2"
pub fn add_defined_name<S: Into<String>>(&mut self, name: S, address: S) -> Result<(), &str> {
let mut defined_name = DefinedName::default();
defined_name.set_name(name.into());
defined_name.set_address(address.into());
self.add_defined_names(defined_name);
Ok(())
}
/// Get Print Options.
pub fn get_print_options(&self) -> &PrintOptions {
&self.print_options
}
/// Get Print Options in mutable.
pub fn get_print_options_mut(&mut self) -> &mut PrintOptions {
&mut self.print_options
}
/// Set Print Options.
/// # Arguments
/// * `value` - PrintOptions.
pub fn set_print_options(&mut self, value: PrintOptions) -> &mut Self {
self.print_options = value;
self
}
/// Get Column Breaks.
pub fn get_column_breaks(&self) -> &ColumnBreaks {
&self.column_breaks
}
/// Get Column Breaks in mutable.
pub fn get_column_breaks_mut(&mut self) -> &mut ColumnBreaks {
&mut self.column_breaks
}
/// Set Column Breaks.
/// # Arguments
/// * `value` - ColumnBreaks.
pub fn set_column_breaks(&mut self, value: ColumnBreaks) -> &mut Self {
self.column_breaks = value;
self
}
/// Get Row Breaks.
pub fn get_row_breaks(&self) -> &RowBreaks {
&self.row_breaks
}
/// Get Row Breaks in mutable.
pub fn get_row_breaks_mut(&mut self) -> &mut RowBreaks {
&mut self.row_breaks
}
/// Set Row Breaks.
/// # Arguments
/// * `value` - RowBreaks.
pub fn set_row_breaks(&mut self, value: RowBreaks) -> &mut Self {
self.row_breaks = value;
self
}
pub fn get_data_validations(&self) -> &Option<DataValidations> {
&self.data_validations
}
pub fn get_data_validations_mut(&mut self) -> &mut Option<DataValidations> {
&mut self.data_validations
}
pub fn set_data_validations(&mut self, value: DataValidations) -> &mut Self {
self.data_validations = Some(value);
self
}
pub fn remove_data_validations(&mut self) -> &mut Self {
self.data_validations = None;
self
}
pub fn get_sheet_format_properties(&self) -> &SheetFormatProperties {
&self.sheet_format_properties
}
pub fn get_sheet_format_properties_mut(&mut self) -> &mut SheetFormatProperties {
&mut self.sheet_format_properties
}
pub fn set_sheet_format_properties(&mut self, value: SheetFormatProperties) -> &mut Self {
self.sheet_format_properties = value;
self
}
/// Outputs all images contained in the worksheet.
/// # Return value
/// * `&Vec<Image>` - Image Object List.
pub fn get_image_collection(&self) -> &Vec<Image> {
self.get_worksheet_drawing().get_image_collection()
}
/// Outputs all images contained in the worksheet.
/// # Return value
/// * `&mut Vec<Image>` - Image Object List.
pub fn get_image_collection_mut(&mut self) -> &mut Vec<Image> {
self.get_worksheet_drawing_mut().get_image_collection_mut()
}
pub fn add_image(&mut self, value: Image) -> &mut Self {
self.get_worksheet_drawing_mut().add_image(value);
self
}
pub fn get_image<T>(&self, coordinate: T) -> Option<&Image>
where
T: Into<CellCoordinates>,
{
let CellCoordinates { col, row } = coordinate.into();
self.get_worksheet_drawing().get_image(&col, &row)
}
pub fn get_image_mut<T>(&mut self, coordinate: T) -> Option<&mut Image>
where
T: Into<CellCoordinates>,
{
let CellCoordinates { col, row } = coordinate.into();
self.get_worksheet_drawing_mut().get_image_mut(&col, &row)
}
pub fn get_image_by_column_and_row_mut(&mut self, col: &u32, row: &u32) -> Option<&mut Image> {
self.get_worksheet_drawing_mut().get_image_mut(col, row)
}
pub fn get_images<T>(&self, coordinate: T) -> Vec<&Image>
where
T: Into<CellCoordinates>,
{
let CellCoordinates { col, row } = coordinate.into();
self.get_worksheet_drawing().get_images(&col, &row)
}
pub fn get_images_mut<T>(&mut self, coordinate: T) -> Vec<&mut Image>
where
T: Into<CellCoordinates>,
{
let CellCoordinates { col, row } = coordinate.into();
self.get_worksheet_drawing_mut().get_images_mut(&col, &row)
}
/// Outputs all Charts contained in the worksheet.
/// # Return value
/// * `&Vec<Chart>` - Chart Object List.
pub fn get_chart_collection(&self) -> &Vec<Chart> {
self.get_worksheet_drawing().get_chart_collection()
}
/// Outputs all Charts contained in the worksheet.
/// # Return value
/// * `&mut Vec<Chart>` - Chart Object List.
pub fn get_chart_collection_mut(&mut self) -> &mut Vec<Chart> {
self.get_worksheet_drawing_mut().get_chart_collection_mut()
}
pub fn add_chart(&mut self, value: Chart) -> &mut Self {
self.get_worksheet_drawing_mut().add_chart_collection(value);
self
}
pub fn get_chart<T>(&self, coordinate: T) -> Option<&Chart>
where
T: Into<CellCoordinates>,
{
let CellCoordinates { col, row } = coordinate.into();
self.get_worksheet_drawing().get_chart(&col, &row)
}
pub fn get_chart_mut<T>(&mut self, coordinate: T) -> Option<&mut Chart>
where
T: Into<CellCoordinates>,
{
let CellCoordinates { col, row } = coordinate.into();
self.get_worksheet_drawing_mut().get_chart_mut(&col, &row)
}
pub fn get_charts<T>(&self, coordinate: T) -> Vec<&Chart>
where
T: Into<CellCoordinates>,
{
let CellCoordinates { col, row } = coordinate.into();
self.get_worksheet_drawing().get_charts(&col, &row)
}
pub fn get_charts_mut<T>(&mut self, coordinate: T) -> Vec<&mut Chart>
where
T: Into<CellCoordinates>,
{
let CellCoordinates { col, row } = coordinate.into();
self.get_worksheet_drawing_mut().get_charts_mut(&col, &row)
}
/// Outputs all media contained in the worksheet.
/// # Return value
/// * `Vec<&MediaObject>` - Media Object List.
pub(crate) fn get_media_object_collection(&self) -> Vec<&MediaObject> {
let mut result: Vec<&MediaObject> = Vec::new();
for image in self.get_worksheet_drawing().get_image_collection() {
let media_object = image.get_media_object();
let mut is_new = true;
for v in &result {
if v.get_image_name() == media_object.get_image_name() {
is_new = false;
}
}
if is_new {
result.push(media_object);
}
}
for ole_objects in self.get_ole_objects().get_ole_object() {
let media_object = ole_objects.get_embedded_object_properties().get_image();
let mut is_new = true;
for v in &result {
if v.get_image_name() == media_object.get_image_name() {
is_new = false;
}
}
if is_new {
result.push(media_object);
}
}
result
}
pub(crate) fn get_pivot_cache_definition_collection(&self) -> Vec<&str> {
let mut result: Vec<&str> = Vec::new();
match &self.raw_data_of_worksheet {
Some(raw_data) => {
for relationships in raw_data.get_relationships_list() {
for row in relationships.get_relationship_list() {
if row.get_type() == "http://schemas.openxmlformats.org/officeDocument/2006/relationships/pivotCacheDefinition" {
result.push(row.get_raw_file().get_file_target());
}
}
}
}
None => {}
}
result
}
/// (This method is crate only.)
/// Has Defined Names.
pub(crate) fn has_defined_names(&self) -> bool {
if !self.get_defined_names().is_empty() {
return true;
}
false
}
pub(crate) fn is_deserialized(&self) -> bool {
self.raw_data_of_worksheet.is_none()
}
pub(crate) fn get_raw_data_of_worksheet(&self) -> &RawWorksheet {
match &self.raw_data_of_worksheet {
Some(v) => {
return v;
}
None => {}
}
panic!("Not found at raw data of worksheet.");
}
pub(crate) fn set_raw_data_of_worksheet(&mut self, value: RawWorksheet) -> &mut Self {
self.raw_data_of_worksheet = Some(value);
self
}
pub(crate) fn remove_raw_data_of_worksheet(&mut self) -> &mut Self {
self.raw_data_of_worksheet = None;
self
}
/// (This method is crate only.)
/// Has Ole Objects.
pub(crate) fn has_ole_objects(&self) -> bool {
!self.ole_objects.get_ole_object().is_empty()
}
/// (This method is crate only.)
/// Has Legacy Drawing.
pub(crate) fn has_legacy_drawing(&self) -> bool {
self.has_comments() || self.has_ole_objects()
}
/// Moving a section of the sheet
/// # Arguments
/// 'range' - Specify like "A1:G8"
/// 'row' - The number of rows to move by (negative numbers mean move 'left')
/// 'column' - the number of columns to move by (negative numbers mean move 'up')
pub fn move_range(&mut self, range: &str, row: &i32, column: &i32) -> &mut Self {
// Check to ensure coordinates to move are within range (eg: moving A1 cells to the left is
// impossible)
let range_upper = range.to_uppercase();
let (row_start, row_end, col_start, col_end) = get_start_and_end_point(&range_upper);
if (col_start as i32 + column) < 1 {
panic!("Out of Range.");
}
if (row_start as i32 + row) < 1 {
panic!("Out of Range.");
}
if (col_end as i32 + column) > 16384 {
panic!("Out of Range.");
}
if (row_end as i32 + row) > 1048576 {
panic!("Out of Range.");
}
// Iterate row by row, collecting cell information (do I copy)
let mut copy_cells: Vec<Cell> = Vec::new();
let cells = self.cell_collection.get_cell_by_range(range);
for cell in cells {
match cell {
Some(v) => {
copy_cells.push(v.clone());
}
None => {}
}
}
// Delete cell information as iterating through
let coordinate_list = get_coordinate_list(&range_upper);
for (col_num, row_num) in &coordinate_list {
self.cell_collection.remove(col_num, row_num);
self.cell_collection.remove(
&((*col_num as i32 + column) as u32),
&((*row_num as i32 + row) as u32),
);
}
// repaste by setting cell values
for cell in &mut copy_cells {
cell.get_coordinate_mut().offset_col_num(*column);
cell.get_coordinate_mut().offset_row_num(*row);
self.set_cell(cell.clone());
}
self
}
}