Files
PhpSpreadsheet/samples/Basic1/18_Extendedcalculation.php
T
oleibman ecbd702628 Phpstan and Samples
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.
2025-04-05 13:54:20 -07:00

71 lines
2.9 KiB
PHP

<?php
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
require __DIR__ . '/../Header.php';
/** @var PhpOffice\PhpSpreadsheet\Helper\Sample $helper */
// List functions
$helper->log('List implemented functions');
$calc = Calculation::getInstance();
print_r($calc->getImplementedFunctionNames());
// Create new Spreadsheet object
$helper->log('Create new Spreadsheet object');
$spreadsheet = new Spreadsheet();
// Add some data, we will use some formulas here
$helper->log('Add some data');
$spreadsheet->getActiveSheet()->setCellValue('A14', 'Count:');
$spreadsheet->getActiveSheet()->setCellValue('B1', 'Range 1');
$spreadsheet->getActiveSheet()->setCellValue('B2', 2);
$spreadsheet->getActiveSheet()->setCellValue('B3', 8);
$spreadsheet->getActiveSheet()->setCellValue('B4', 10);
$spreadsheet->getActiveSheet()->setCellValue('B5', true);
$spreadsheet->getActiveSheet()->setCellValue('B6', false);
$spreadsheet->getActiveSheet()->setCellValue('B7', 'Text String');
$spreadsheet->getActiveSheet()->setCellValue('B9', '22');
$spreadsheet->getActiveSheet()->setCellValue('B10', 4);
$spreadsheet->getActiveSheet()->setCellValue('B11', 6);
$spreadsheet->getActiveSheet()->setCellValue('B12', 12);
$spreadsheet->getActiveSheet()->setCellValue('B14', '=COUNT(B2:B12)');
$spreadsheet->getActiveSheet()->setCellValue('C1', 'Range 2');
$spreadsheet->getActiveSheet()->setCellValue('C2', 1);
$spreadsheet->getActiveSheet()->setCellValue('C3', 2);
$spreadsheet->getActiveSheet()->setCellValue('C4', 2);
$spreadsheet->getActiveSheet()->setCellValue('C5', 3);
$spreadsheet->getActiveSheet()->setCellValue('C6', 3);
$spreadsheet->getActiveSheet()->setCellValue('C7', 3);
$spreadsheet->getActiveSheet()->setCellValue('C8', '0');
$spreadsheet->getActiveSheet()->setCellValue('C9', 4);
$spreadsheet->getActiveSheet()->setCellValue('C10', 4);
$spreadsheet->getActiveSheet()->setCellValue('C11', 4);
$spreadsheet->getActiveSheet()->setCellValue('C12', 4);
$spreadsheet->getActiveSheet()->setCellValue('C14', '=COUNT(C2:C12)');
$spreadsheet->getActiveSheet()->setCellValue('D1', 'Range 3');
$spreadsheet->getActiveSheet()->setCellValue('D2', 2);
$spreadsheet->getActiveSheet()->setCellValue('D3', 3);
$spreadsheet->getActiveSheet()->setCellValue('D4', 4);
$spreadsheet->getActiveSheet()->setCellValue('D5', '=((D2 * D3) + D4) & " should be 10"');
$spreadsheet->getActiveSheet()->setCellValue('E1', 'Other functions');
$spreadsheet->getActiveSheet()->setCellValue('E2', '=PI()');
$spreadsheet->getActiveSheet()->setCellValue('E3', '=RAND()');
$spreadsheet->getActiveSheet()->setCellValue('E4', '=RANDBETWEEN(5, 10)');
$spreadsheet->getActiveSheet()->setCellValue('E14', 'Count of both ranges:');
$spreadsheet->getActiveSheet()->setCellValue('F14', '=COUNT(B2:C12)');
// Calculated data
$helper->log('Calculated data');
$helper->log('Value of B14 [=COUNT(B2:B12)]: ' . $spreadsheet->getActiveSheet()->getCell('B14')->getCalculatedValueString());
$helper->logEndingNotes();