@@ -121,7 +121,19 @@ public function save(bool $close = true): void
121
121
}
122
122
123
123
/**
124
- * Get a worksheet number (ID) from its name.
124
+ * Count the number of worksheets in the workbook.
125
+ */
126
+ public function getWorksheetCount (): int
127
+ {
128
+ $ dom = $ this ->getDomFromPath (self ::WORKBOOK_PATH );
129
+ $ xpath = new \DOMXPath ($ dom );
130
+ $ xpath ->registerNamespace ('o ' , self ::OXML_NAMESPACE );
131
+ $ count = $ xpath ->evaluate ('count(/o:workbook/o:sheets/o:sheet) ' );
132
+ return is_numeric ($ count ) ? (int )$ count : 0 ;
133
+ }
134
+
135
+ /**
136
+ * Get a worksheet number (ID) from its name (base 1).
125
137
* @param string $sheetName The name of the worksheet to look up.
126
138
* @return int The worksheet ID, or -1 if not found.
127
139
*/
@@ -137,6 +149,20 @@ public function getWorksheetNumber(string $sheetName): int
137
149
return -1 ;
138
150
}
139
151
152
+ /**
153
+ * Get a worksheet name from its number (ID).
154
+ * @param int $sheetNumber The number of the worksheet to look up.
155
+ * @return string|null The worksheet name, or null if not found.
156
+ */
157
+ public function getWorksheetName (int $ sheetNumber ): ?string
158
+ {
159
+ $ dom = $ this ->getDomFromPath (self ::WORKBOOK_PATH );
160
+ $ xpath = new \DOMXPath ($ dom );
161
+ $ xpath ->registerNamespace ('o ' , self ::OXML_NAMESPACE );
162
+ $ sheetName = $ xpath ->evaluate ("normalize-space(/o:workbook/o:sheets/o:sheet[ $ sheetNumber][1]/@name) " );
163
+ return is_string ($ sheetName ) ? $ sheetName : null ;
164
+ }
165
+
140
166
private static function getWorksheetPath (int $ sheetNumber ): string
141
167
{
142
168
return "xl/worksheets/sheet {$ sheetNumber }.xml " ;
0 commit comments