mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-10-09 05:16:18 +00:00
Reorganize code samples
This introduce a helper class that should be used to log things, avoiding a lot of boilerplate code. Also all output are made in /tmp folder instead of beside the script itself. This is because there is a high chance that the folder containing the script is not writtable by webserver. So using the /tmp folder makes it more likely to works in a variety of setup.
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
|
||||
require __DIR__ . '/Header.php';
|
||||
|
||||
// Change these values to select the Rendering library that you wish to use
|
||||
// and its directory location on your server
|
||||
$rendererName = PhpSpreadsheet\Settings::CHART_RENDERER_JPGRAPH;
|
||||
$rendererLibrary = 'jpgraph3.5.0b1/src/';
|
||||
$rendererLibraryPath = '/php/libraries/Charts/' . $rendererLibrary;
|
||||
|
||||
if (!PhpSpreadsheet\Settings::setChartRenderer($rendererName, $rendererLibraryPath)) {
|
||||
$helper->log('NOTICE: Please set the $rendererName and $rendererLibraryPath values at the top of this script as appropriate for your directory structure');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$inputFileType = 'Excel2007';
|
||||
$inputFileNames = __DIR__ . '/templates/36write*.xlsx';
|
||||
|
||||
if ((isset($argc)) && ($argc > 1)) {
|
||||
$inputFileNames = [];
|
||||
for ($i = 1; $i < $argc; ++$i) {
|
||||
$inputFileNames[] = __DIR__ . '/templates/' . $argv[$i];
|
||||
}
|
||||
} else {
|
||||
$inputFileNames = glob($inputFileNames);
|
||||
}
|
||||
foreach ($inputFileNames as $inputFileName) {
|
||||
$inputFileNameShort = basename($inputFileName);
|
||||
|
||||
if (!file_exists($inputFileName)) {
|
||||
$helper->log('File ' . $inputFileNameShort . ' does not exist');
|
||||
continue;
|
||||
}
|
||||
|
||||
$helper->log("Load Test from $inputFileType file " . $inputFileNameShort);
|
||||
|
||||
$reader = \PhpSpreadsheet\IOFactory::createReader($inputFileType);
|
||||
$reader->setIncludeCharts(true);
|
||||
$spreadsheet = $reader->load($inputFileName);
|
||||
|
||||
$helper->log('Iterate worksheets looking at the charts');
|
||||
foreach ($spreadsheet->getWorksheetIterator() as $worksheet) {
|
||||
$sheetName = $worksheet->getTitle();
|
||||
echo 'Worksheet: ', $sheetName, EOL;
|
||||
|
||||
$chartNames = $worksheet->getChartNames();
|
||||
if (empty($chartNames)) {
|
||||
echo ' There are no charts in this worksheet', EOL;
|
||||
} else {
|
||||
natsort($chartNames);
|
||||
foreach ($chartNames as $i => $chartName) {
|
||||
$chart = $worksheet->getChartByName($chartName);
|
||||
if (!is_null($chart->getTitle())) {
|
||||
$caption = '"' . implode(' ', $chart->getTitle()->getCaption()) . '"';
|
||||
} else {
|
||||
$caption = 'Untitled';
|
||||
}
|
||||
echo ' ', $chartName, ' - ', $caption, EOL;
|
||||
echo str_repeat(' ', strlen($chartName) + 3);
|
||||
$groupCount = $chart->getPlotArea()->getPlotGroupCount();
|
||||
if ($groupCount == 1) {
|
||||
$chartType = $chart->getPlotArea()->getPlotGroupByIndex(0)->getPlotType();
|
||||
echo ' ', $chartType, EOL;
|
||||
} else {
|
||||
$chartTypes = [];
|
||||
for ($i = 0; $i < $groupCount; ++$i) {
|
||||
$chartTypes[] = $chart->getPlotArea()->getPlotGroupByIndex($i)->getPlotType();
|
||||
}
|
||||
$chartTypes = array_unique($chartTypes);
|
||||
if (count($chartTypes) == 1) {
|
||||
$chartType = 'Multiple Plot ' . array_pop($chartTypes);
|
||||
echo ' ', $chartType, EOL;
|
||||
} elseif (count($chartTypes) == 0) {
|
||||
echo ' *** Type not yet implemented', EOL;
|
||||
} else {
|
||||
echo ' Combination Chart', EOL;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Save
|
||||
$filename = $helper->getFilename($inputFileName);
|
||||
$writer = \PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'HTML');
|
||||
$writer->setIncludeCharts(true);
|
||||
$callStartTime = microtime(true);
|
||||
$writer->save($filename);
|
||||
$helper->logWrite($writer, $filename, $callStartTime);
|
||||
|
||||
$spreadsheet->disconnectWorksheets();
|
||||
unset($spreadsheet);
|
||||
}
|
||||
Reference in New Issue
Block a user