mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-11-09 11:26:17 +00:00
9565757138
A follow-on activity to PR #5061. That PR added scrollbars to the dropdown lists when samples were run in a browser. Earlier changes had broken up a number of categories into multiple entries because the dropdown lists were too long and wound up truncated. With the scrollbars, it makes sense to re-consolidate many of those lists, such as ComplexNumbers, which are all essentially the same test as each other. Some break-ups are retained (e.g. Basic/1/2/3/4/5) because they differ substantially from sample to sample; that's kind of an arbitrary decision, but it will have to do for now. I also renamed some of the members so that like members sort together when listing the directory (e.g. all pivot table samples now start with `pvt`). The sample code is mostly unchanged; only the directory and/or member names are changed. The download samples were moved to their own new directory, and now represent a complete set of all the write formats that we permit. Studying their code, an improvement for `Helper\Downloader` became evident. It had been used merely to copy an existing file to be downloaded. However, by changing the first parameter of `download` to be either the name of the file directory (the existing usage) *or* a `Writer` object poised to issue `save`, you can use it to download your spreadsheet without having to worry about the headers which the browser requires. It then becomes generally useful beyond being used in samples: ```php $writer = new WriterType($spreadsheet); $downloader = new Downloader($writer, 'suggestedFileName.xlsx'); $downloader->download(); ```
92 lines
3.1 KiB
PHP
92 lines
3.1 KiB
PHP
<?php
|
|
|
|
use PhpOffice\PhpSpreadsheet\NamedFormula;
|
|
use PhpOffice\PhpSpreadsheet\Shared\Date;
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
|
|
|
require_once __DIR__ . '/../Header.php';
|
|
/** @var PhpOffice\PhpSpreadsheet\Helper\Sample $helper */
|
|
$spreadsheet = new Spreadsheet();
|
|
|
|
$data2019 = [
|
|
[151600, 21600],
|
|
[160320, 30320],
|
|
[243500, 73500],
|
|
[113450, 13450],
|
|
[143200, 23200],
|
|
[134000, 14000],
|
|
[89400, -10600],
|
|
[184500, 24500],
|
|
[100800, 800],
|
|
[241850, 5150],
|
|
[142425, 12425],
|
|
[243400, 43400],
|
|
];
|
|
|
|
$data2020 = [
|
|
[183250, 33250],
|
|
[210350, 40350],
|
|
[298650, 48650],
|
|
[140550, 20550],
|
|
[183145, 33145],
|
|
[172355, 22355],
|
|
];
|
|
|
|
$worksheet = $spreadsheet->setActiveSheetIndex(0);
|
|
setYearlyData($worksheet, '2019', $data2019);
|
|
$worksheet = $spreadsheet->addSheet(new Worksheet($spreadsheet));
|
|
setYearlyData($worksheet, '2020', $data2020);
|
|
$worksheet = $spreadsheet->addSheet(new Worksheet($spreadsheet));
|
|
setYearlyData($worksheet, '2020', [], 'GROWTH');
|
|
|
|
/** @param array<int, array<int, int>> $yearlyData */
|
|
function setYearlyData(Worksheet $worksheet, string $year, array $yearlyData, ?string $title = null): void
|
|
{
|
|
// Set up some basic data
|
|
$worksheetTitle = $title ?: $year;
|
|
$worksheet
|
|
->setTitle($worksheetTitle)
|
|
->setCellValue('A1', 'Month')
|
|
->setCellValue('B1', $worksheetTitle === 'GROWTH' ? 'Growth' : 'Sales')
|
|
->setCellValue('C1', $worksheetTitle === 'GROWTH' ? 'Profit Growth' : 'Margin')
|
|
->setCellValue('A2', Date::stringToExcel("{$year}-01-01"));
|
|
for ($row = 3; $row <= 13; ++$row) {
|
|
$worksheet->setCellValue("A{$row}", '=NEXT_MONTH');
|
|
}
|
|
|
|
if (!empty($yearlyData)) {
|
|
$worksheet->fromArray($yearlyData, null, 'B2');
|
|
} else {
|
|
for ($row = 2; $row <= 13; ++$row) {
|
|
$worksheet->setCellValue("B{$row}", '=GROWTH');
|
|
$worksheet->setCellValue("C{$row}", '=PROFIT_GROWTH');
|
|
}
|
|
}
|
|
|
|
$worksheet->getStyle('A1:C1')
|
|
->getFont()->setBold(true);
|
|
$worksheet->getStyle('A2:A13')
|
|
->getNumberFormat()
|
|
->setFormatCode('mmmm');
|
|
$worksheet->getStyle('B2:C13')
|
|
->getNumberFormat()
|
|
->setFormatCode($worksheetTitle === 'GROWTH' ? '0.00%' : '_-€* #,##0_-');
|
|
}
|
|
|
|
// Add some Named Formulae
|
|
// The first to store our tax rate
|
|
$spreadsheet->addNamedFormula(new NamedFormula('NEXT_MONTH', $worksheet, '=EDATE(OFFSET($A1,-1,0),1)'));
|
|
$spreadsheet->addNamedFormula(new NamedFormula('GROWTH', $worksheet, "=IF('2020'!\$B1=\"\",\"-\",(('2020'!\$B1/'2019'!\$B1)-1))"));
|
|
$spreadsheet->addNamedFormula(new NamedFormula('PROFIT_GROWTH', $worksheet, "=IF('2020'!\$C1=\"\",\"-\",(('2020'!\$C1/'2019'!\$C1)-1))"));
|
|
|
|
for ($row = 2; $row <= 7; ++$row) {
|
|
$month = $worksheet->getCell("A{$row}")->getFormattedValue();
|
|
$growth = $worksheet->getCell("B{$row}")->getFormattedValue();
|
|
$profitGrowth = $worksheet->getCell("C{$row}")->getFormattedValue();
|
|
|
|
$helper->log("Growth for {$month} is {$growth}, with a Profit Growth of {$profitGrowth}");
|
|
}
|
|
|
|
$helper->write($spreadsheet, __FILE__, ['Xlsx']);
|