Using PHPExcel, I would like to loop through all the named ranges in a workbook and then create a new tab for each one.
I am currently doing this in excel with a macro, like so:
Dim sheetName As String
sheetName = ActiveSheet.Name
Dim nName As Name
For Each nName In Names
If InStr(1, nName.RefersTo, sheetName) > 0 Then
Application.Goto Reference:=nName.Name
Selection.Copy
Sheets.Add After:=Sheets(Sheets.Count)
ActiveSheet.Name = nName.Name
Selection.PasteSpecial Paste:=xlPasteValues
End If
Next nName
mystring = Sheets(1).Name
ActiveWorkbook.SaveCopyAs Filename:="P:\DP\CWBI\" & mystring & "_parsed.xls"
I can loop through active sheets in PHPExcel and dump them into an array for processing, like this:
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$sheets = $objReader->listWorksheetNames($file);
print_r ($sheets);
Does anyone know if it's possible to loop through all the named ranges?
Edit:
I'm trying the first suggestion below, but not getting any output. Here's the code:
$file="span_test.xlsx";
$objPHPExcel = PHPExcel_IOFactory::load($file);
foreach($objPHPExcel->getNamedRanges() as $name => $namedRange) {
echo $namedRange . "</br>";
}
Got it working now, my mistake in trying to echo an object. should be
print_r($name)