32_Chart_read_write.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. use PhpOffice\PhpSpreadsheet\IOFactory;
  3. require __DIR__ . '/../Header.php';
  4. $inputFileType = 'Xlsx';
  5. $inputFileNames = __DIR__ . '/../templates/32readwrite*[0-9].xlsx';
  6. if ((isset($argc)) && ($argc > 1)) {
  7. $inputFileNames = [];
  8. for ($i = 1; $i < $argc; ++$i) {
  9. $inputFileNames[] = __DIR__ . '/../templates/' . $argv[$i];
  10. }
  11. } else {
  12. $inputFileNames = glob($inputFileNames);
  13. }
  14. foreach ($inputFileNames as $inputFileName) {
  15. $inputFileNameShort = basename($inputFileName);
  16. if (!file_exists($inputFileName)) {
  17. $helper->log('File ' . $inputFileNameShort . ' does not exist');
  18. continue;
  19. }
  20. $reader = IOFactory::createReader($inputFileType);
  21. $reader->setIncludeCharts(true);
  22. $callStartTime = microtime(true);
  23. $spreadsheet = $reader->load($inputFileName);
  24. $helper->logRead($inputFileType, $inputFileName, $callStartTime);
  25. $helper->log('Iterate worksheets looking at the charts');
  26. foreach ($spreadsheet->getWorksheetIterator() as $worksheet) {
  27. $sheetName = $worksheet->getTitle();
  28. $helper->log('Worksheet: ' . $sheetName);
  29. $chartNames = $worksheet->getChartNames();
  30. if (empty($chartNames)) {
  31. $helper->log(' There are no charts in this worksheet');
  32. } else {
  33. natsort($chartNames);
  34. foreach ($chartNames as $i => $chartName) {
  35. $chart = $worksheet->getChartByName($chartName);
  36. if ($chart->getTitle() !== null) {
  37. $caption = '"' . implode(' ', $chart->getTitle()->getCaption()) . '"';
  38. } else {
  39. $caption = 'Untitled';
  40. }
  41. $helper->log(' ' . $chartName . ' - ' . $caption);
  42. $indentation = str_repeat(' ', strlen($chartName) + 3);
  43. $groupCount = $chart->getPlotArea()->getPlotGroupCount();
  44. if ($groupCount == 1) {
  45. $chartType = $chart->getPlotArea()->getPlotGroupByIndex(0)->getPlotType();
  46. $helper->log($indentation . ' ' . $chartType);
  47. } else {
  48. $chartTypes = [];
  49. for ($i = 0; $i < $groupCount; ++$i) {
  50. $chartTypes[] = $chart->getPlotArea()->getPlotGroupByIndex($i)->getPlotType();
  51. }
  52. $chartTypes = array_unique($chartTypes);
  53. if (count($chartTypes) == 1) {
  54. $chartType = 'Multiple Plot ' . array_pop($chartTypes);
  55. $helper->log($indentation . ' ' . $chartType);
  56. } elseif (count($chartTypes) == 0) {
  57. $helper->log($indentation . ' *** Type not yet implemented');
  58. } else {
  59. $helper->log($indentation . ' Combination Chart');
  60. }
  61. }
  62. }
  63. }
  64. }
  65. $outputFileName = $helper->getFilename($inputFileName);
  66. $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
  67. $writer->setIncludeCharts(true);
  68. $callStartTime = microtime(true);
  69. $writer->save($outputFileName);
  70. $helper->logWrite($writer, $outputFileName, $callStartTime);
  71. $spreadsheet->disconnectWorksheets();
  72. unset($spreadsheet);
  73. }