mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-08-17 18:41:36 +00:00
ecbd702628
Phpstan hasn't identified any errors in Samples in a long time. But, as it turns out, one particular error is suppressed: ``` Variable $helper might not be defined. ``` This error would be generated by almost all samples, and the errors that it suppresses will become problematic if we move to Level 10. We are not yet committed to doing that, but it is pretty easy to write a script to change the samples so that error no longer happens, and there isn't really any reason to delay doing so. The results of that script constitute this PR.
106 lines
3.6 KiB
PHP
106 lines
3.6 KiB
PHP
<?php
|
|
|
|
use PhpOffice\PhpSpreadsheet\IOFactory;
|
|
use PhpOffice\PhpSpreadsheet\Settings;
|
|
|
|
require __DIR__ . '/../Header.php';
|
|
/** @var PhpOffice\PhpSpreadsheet\Helper\Sample $helper */
|
|
|
|
// Change these values to select the Rendering library that you wish to use
|
|
//Settings::setChartRenderer(\PhpOffice\PhpSpreadsheet\Chart\Renderer\JpGraph::class);
|
|
Settings::setChartRenderer(PhpOffice\PhpSpreadsheet\Chart\Renderer\MtJpGraphRenderer::class);
|
|
|
|
$inputFileType = 'Xlsx';
|
|
$inputFileNames = __DIR__ . '/../templates/32readwrite*[0-9].xlsx';
|
|
//$inputFileNames = __DIR__ . '/../templates/32readwriteStockChart5.xlsx';
|
|
|
|
if ((isset($argc)) && ($argc > 1)) {
|
|
$inputFileNames = [];
|
|
for ($i = 1; $i < $argc; ++$i) {
|
|
$inputFileNames[] = __DIR__ . '/../templates/' . $argv[$i];
|
|
}
|
|
} else {
|
|
$inputFileNames = glob($inputFileNames) ?: [];
|
|
}
|
|
if (count($inputFileNames) === 1) {
|
|
/** @var string[] */
|
|
$unresolvedErrors = [];
|
|
} else {
|
|
/** @var string[] */
|
|
$unresolvedErrors = [
|
|
// The following spreadsheet was created by 3rd party software,
|
|
// and doesn't include the data that usually accompanies a chart.
|
|
// That is good enough for Excel, but not for JpGraph.
|
|
'32readwriteBubbleChart2.xlsx',
|
|
];
|
|
}
|
|
foreach ($inputFileNames as $inputFileName) {
|
|
$inputFileNameShort = basename($inputFileName);
|
|
|
|
if (!file_exists($inputFileName)) {
|
|
$helper->log('File ' . $inputFileNameShort . ' does not exist');
|
|
|
|
continue;
|
|
}
|
|
if (in_array($inputFileNameShort, $unresolvedErrors, true)) {
|
|
$helper->log('*****');
|
|
$helper->log('***** File ' . $inputFileNameShort . ' does not yet work with this script');
|
|
$helper->log('*****');
|
|
|
|
continue;
|
|
}
|
|
|
|
$helper->log("Load Test from $inputFileType file " . $inputFileNameShort);
|
|
|
|
$reader = IOFactory::createReader($inputFileType);
|
|
$reader->setIncludeCharts(true);
|
|
$spreadsheet = $reader->load($inputFileName);
|
|
|
|
$helper->log('Iterate worksheets looking at the charts');
|
|
$renderedCharts = 0;
|
|
foreach ($spreadsheet->getWorksheetIterator() as $worksheet) {
|
|
$sheetName = $worksheet->getTitle();
|
|
$helper->log('Worksheet: ' . $sheetName);
|
|
|
|
$chartNames = $worksheet->getChartNames();
|
|
if (empty($chartNames)) {
|
|
$helper->log(' There are no charts in this worksheet');
|
|
} else {
|
|
natsort($chartNames);
|
|
foreach ($chartNames as $j => $chartName) {
|
|
$i = $renderedCharts + $j;
|
|
$chart = $worksheet->getChartByNameOrThrow($chartName);
|
|
if ($chart->getTitle() !== null) {
|
|
$caption = '"' . $chart->getTitle()->getCaptionText($spreadsheet) . '"';
|
|
} else {
|
|
$caption = 'Untitled';
|
|
}
|
|
$helper->log(' ' . $chartName . ' - ' . $caption);
|
|
|
|
$pngFile = $helper->getFilename('35-' . $inputFileNameShort, 'png');
|
|
if ($i !== 0) {
|
|
$pngFile = substr($pngFile, 0, -3) . "$i.png";
|
|
}
|
|
if (file_exists($pngFile)) {
|
|
unlink($pngFile);
|
|
}
|
|
|
|
try {
|
|
$chart->render($pngFile);
|
|
$helper->log('Rendered image: ' . $pngFile);
|
|
} catch (Exception $e) {
|
|
$helper->log('Error rendering chart: ' . $e->getMessage());
|
|
}
|
|
|
|
++$renderedCharts;
|
|
}
|
|
}
|
|
}
|
|
|
|
$spreadsheet->disconnectWorksheets();
|
|
unset($spreadsheet);
|
|
gc_collect_cycles();
|
|
}
|
|
|
|
$helper->log('Done rendering charts as images');
|