19_Namedrange.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. use PhpOffice\PhpSpreadsheet\NamedRange;
  3. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  4. require __DIR__ . '/../Header.php';
  5. // Create new Spreadsheet object
  6. $helper->log('Create new Spreadsheet object');
  7. $spreadsheet = new Spreadsheet();
  8. // Set document properties
  9. $helper->log('Set document properties');
  10. $spreadsheet->getProperties()->setCreator('Maarten Balliauw')
  11. ->setLastModifiedBy('Maarten Balliauw')
  12. ->setTitle('Office 2007 XLSX Test Document')
  13. ->setSubject('Office 2007 XLSX Test Document')
  14. ->setDescription('Test document for Office 2007 XLSX, generated using PHP classes.')
  15. ->setKeywords('office 2007 openxml php')
  16. ->setCategory('Test result file');
  17. // Add some data
  18. $helper->log('Add some data');
  19. $spreadsheet->setActiveSheetIndex(0);
  20. $spreadsheet->getActiveSheet()->setCellValue('A1', 'Firstname:')
  21. ->setCellValue('A2', 'Lastname:')
  22. ->setCellValue('A3', 'Fullname:')
  23. ->setCellValue('B1', 'Maarten')
  24. ->setCellValue('B2', 'Balliauw')
  25. ->setCellValue('B3', '=B1 & " " & B2');
  26. // Define named ranges
  27. $helper->log('Define named ranges');
  28. $spreadsheet->addNamedRange(new NamedRange('PersonName', $spreadsheet->getActiveSheet(), 'B1'));
  29. $spreadsheet->addNamedRange(new NamedRange('PersonLN', $spreadsheet->getActiveSheet(), 'B2'));
  30. // Rename named ranges
  31. $helper->log('Rename named ranges');
  32. $spreadsheet->getNamedRange('PersonName')->setName('PersonFN');
  33. // Rename worksheet
  34. $helper->log('Rename worksheet');
  35. $spreadsheet->getActiveSheet()->setTitle('Person');
  36. // Create a new worksheet, after the default sheet
  37. $helper->log('Create new Worksheet object');
  38. $spreadsheet->createSheet();
  39. // Add some data to the second sheet, resembling some different data types
  40. $helper->log('Add some data');
  41. $spreadsheet->setActiveSheetIndex(1);
  42. $spreadsheet->getActiveSheet()->setCellValue('A1', 'Firstname:')
  43. ->setCellValue('A2', 'Lastname:')
  44. ->setCellValue('A3', 'Fullname:')
  45. ->setCellValue('B1', '=PersonFN')
  46. ->setCellValue('B2', '=PersonLN')
  47. ->setCellValue('B3', '=PersonFN & " " & PersonLN');
  48. // Resolve range
  49. $helper->log('Resolve range');
  50. $helper->log('Cell B1 {=PersonFN}: ' . $spreadsheet->getActiveSheet()->getCell('B1')->getCalculatedValue());
  51. $helper->log('Cell B3 {=PersonFN & " " & PersonLN}: ' . $spreadsheet->getActiveSheet()->getCell('B3')->getCalculatedValue());
  52. $helper->log('Cell Person!B1: ' . $spreadsheet->getActiveSheet()->getCell('Person!B1')->getCalculatedValue());
  53. // Rename worksheet
  54. $helper->log('Rename worksheet');
  55. $spreadsheet->getActiveSheet()->setTitle('Person (cloned)');
  56. // Save
  57. $helper->write($spreadsheet, __FILE__);