File tree Expand file tree Collapse file tree 2 files changed +35
-0
lines changed
src/PhpSpreadsheet/Worksheet
tests/PhpSpreadsheetTests/Worksheet Expand file tree Collapse file tree 2 files changed +35
-0
lines changed Original file line number Diff line number Diff line change @@ -2236,13 +2236,23 @@ public function getTableNames(): array
22362236 return $ tableNames ;
22372237 }
22382238
2239+ /**
2240+ * @param string $name the table name to search
2241+ *
2242+ * @return null|Table The table from the tables collection, or null if not found
2243+ */
22392244 public function getTableByName (string $ name ): ?Table
22402245 {
22412246 $ tableIndex = $ this ->getTableIndexByName ($ name );
22422247
22432248 return ($ tableIndex === null ) ? null : $ this ->tableCollection [$ tableIndex ];
22442249 }
22452250
2251+ /**
2252+ * @param string $name the table name to search
2253+ *
2254+ * @return null|int The index of the located table in the tables collection, or null if not found
2255+ */
22462256 protected function getTableIndexByName (string $ name ): ?int
22472257 {
22482258 $ name = Shared \StringHelper::strToUpper ($ name );
Original file line number Diff line number Diff line change 44
55use Exception ;
66use PhpOffice \PhpSpreadsheet \Cell \DataType ;
7+ use PhpOffice \PhpSpreadsheet \Reader \Xlsx ;
78use PhpOffice \PhpSpreadsheet \Spreadsheet ;
89use PhpOffice \PhpSpreadsheet \Worksheet \CellIterator ;
10+ use PhpOffice \PhpSpreadsheet \Worksheet \Table ;
911use PhpOffice \PhpSpreadsheet \Worksheet \Worksheet ;
1012use PHPUnit \Framework \TestCase ;
1113
@@ -506,4 +508,27 @@ public function emptyColumnProvider(): array
506508 ['I ' , true ],
507509 ];
508510 }
511+
512+ public function testGetTableNames (): void
513+ {
514+ $ reader = new Xlsx ();
515+ $ spreadsheet = $ reader ->load ('tests/data/Worksheet/Table/TableFormulae.xlsx ' );
516+ $ worksheet = $ spreadsheet ->getActiveSheet ();
517+
518+ $ tables = $ worksheet ->getTableNames ();
519+ self ::assertSame (['DeptSales ' ], $ tables );
520+ }
521+
522+ public function testGetTableByName (): void
523+ {
524+ $ reader = new Xlsx ();
525+ $ spreadsheet = $ reader ->load ('tests/data/Worksheet/Table/TableFormulae.xlsx ' );
526+ $ worksheet = $ spreadsheet ->getActiveSheet ();
527+
528+ $ table = $ worksheet ->getTableByName ('Non-existent Table ' );
529+ self ::assertNull ($ table );
530+
531+ $ table = $ worksheet ->getTableByName ('DeptSales ' );
532+ self ::assertInstanceOf (Table::class, $ table );
533+ }
509534}
You can’t perform that action at this time.
0 commit comments