Skip to content

Commit

Permalink
Make DefinedNames Samples Consistent With Other Samples (PHPOffice#1707)
Browse files Browse the repository at this point in the history
All other Samples write to temporary directory. DefinedNames samples
write to main directory, which (a) means they aren't stored with others,
and (b) they aren't ignored by git so look like changed files.
The tests are also simplified by requiring Header rather than Bootstrap,
making use of Helper.
  • Loading branch information
oleibman authored and Gianluca Giovinazzo committed Dec 14, 2020
1 parent 3468199 commit 62fd60b
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 129 deletions.
17 changes: 4 additions & 13 deletions samples/DefinedNames/AbsoluteNamedRange.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
<?php

use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\NamedRange;
use PhpOffice\PhpSpreadsheet\Spreadsheet;

error_reporting(E_ALL);
set_time_limit(0);

date_default_timezone_set('UTC');

// Adjust the path as required to reference the PHPSpreadsheet Bootstrap file
require_once __DIR__ . '/../Bootstrap.php';
require __DIR__ . '/../Header.php';

$spreadsheet = new Spreadsheet();
$worksheet = $spreadsheet->setActiveSheetIndex(0);
Expand Down Expand Up @@ -51,13 +44,11 @@
->setCellValue("B{$row}", "=SUM(B{$startRow}:B{$endRow})")
->setCellValue("C{$row}", "=SUM(C{$startRow}:C{$endRow})");

echo sprintf(
$helper->log(sprintf(
'Worked %.2f hours at a rate of %.2f - Charge to the client is %.2f',
$worksheet->getCell("B{$row}")->getCalculatedValue(),
$worksheet->getCell('B1')->getValue(),
$worksheet->getCell("C{$row}")->getCalculatedValue()
), PHP_EOL;
));

$outputFileName = 'AbsoluteNamedRange.xlsx';
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save($outputFileName);
$helper->write($spreadsheet, __FILE__, ['Xlsx']);
15 changes: 3 additions & 12 deletions samples/DefinedNames/CrossWorksheetNamedFormula.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
<?php

use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\NamedFormula;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

error_reporting(E_ALL);
set_time_limit(0);

date_default_timezone_set('UTC');

// Adjust the path as required to reference the PHPSpreadsheet Bootstrap file
require_once __DIR__ . '/../Bootstrap.php';
require_once __DIR__ . '/../Header.php';

$spreadsheet = new Spreadsheet();

Expand Down Expand Up @@ -91,9 +84,7 @@ function setYearlyData(Worksheet $worksheet, string $year, $yearlyData, ?string
$growth = $worksheet->getCell("B{$row}")->getFormattedValue();
$profitGrowth = $worksheet->getCell("C{$row}")->getFormattedValue();

echo "Growth for {$month} is {$growth}, with a Profit Growth of {$profitGrowth}", PHP_EOL;
$helper->log("Growth for {$month} is {$growth}, with a Profit Growth of {$profitGrowth}");
}

$outputFileName = 'CrossWorksheetNamedFormula.xlsx';
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save($outputFileName);
$helper->write($spreadsheet, __FILE__, ['Xlsx']);
17 changes: 4 additions & 13 deletions samples/DefinedNames/NamedFormulaeAndRanges.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
<?php

use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\NamedFormula;
use PhpOffice\PhpSpreadsheet\NamedRange;
use PhpOffice\PhpSpreadsheet\Spreadsheet;

error_reporting(E_ALL);
set_time_limit(0);

date_default_timezone_set('UTC');

// Adjust the path as required to reference the PHPSpreadsheet Bootstrap file
require_once __DIR__ . '/../Bootstrap.php';
require_once __DIR__ . '/../Header.php';

$spreadsheet = new Spreadsheet();
$worksheet = $spreadsheet->setActiveSheetIndex(0);
Expand Down Expand Up @@ -62,13 +55,11 @@
->setCellValue("B{$row}", '=COLUMN_TOTALS')
->setCellValue("C{$row}", '=COLUMN_TOTALS');

echo sprintf(
$helper->log(sprintf(
'Worked %.2f hours at a rate of %.2f - Charge to the client is %.2f',
$worksheet->getCell("B{$row}")->getCalculatedValue(),
$worksheet->getCell('B1')->getValue(),
$worksheet->getCell("C{$row}")->getCalculatedValue()
), PHP_EOL;
));

$outputFileName = 'NamedFormulaeAndRanges.xlsx';
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save($outputFileName);
$helper->write($spreadsheet, __FILE__, ['Xlsx']);
17 changes: 4 additions & 13 deletions samples/DefinedNames/RelativeNamedRange.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
<?php

use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\NamedRange;
use PhpOffice\PhpSpreadsheet\Spreadsheet;

error_reporting(E_ALL);
set_time_limit(0);

date_default_timezone_set('UTC');

// Adjust the path as required to reference the PHPSpreadsheet Bootstrap file
require_once __DIR__ . '/../Bootstrap.php';
require_once __DIR__ . '/../Header.php';

$spreadsheet = new Spreadsheet();
$worksheet = $spreadsheet->setActiveSheetIndex(0);
Expand Down Expand Up @@ -54,13 +47,11 @@
->setCellValue("B{$row}", "=SUM(B{$startRow}:B{$endRow})")
->setCellValue("C{$row}", "=SUM(C{$startRow}:C{$endRow})");

echo sprintf(
$helper->log(sprintf(
'Worked %.2f hours at a rate of %.2f - Charge to the client is %.2f',
$worksheet->getCell("B{$row}")->getCalculatedValue(),
$worksheet->getCell('B1')->getValue(),
$worksheet->getCell("C{$row}")->getCalculatedValue()
), PHP_EOL;
));

$outputFileName = 'RelativeNamedRange.xlsx';
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save($outputFileName);
$helper->write($spreadsheet, __FILE__, ['Xlsx']);
17 changes: 4 additions & 13 deletions samples/DefinedNames/RelativeNamedRange2.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
<?php

use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\NamedRange;
use PhpOffice\PhpSpreadsheet\Spreadsheet;

error_reporting(E_ALL);
set_time_limit(0);

date_default_timezone_set('UTC');

// Adjust the path as required to reference the PHPSpreadsheet Bootstrap file
require_once __DIR__ . '/../Bootstrap.php';
require_once __DIR__ . '/../Header.php';

$spreadsheet = new Spreadsheet();
$worksheet = $spreadsheet->setActiveSheetIndex(0);
Expand Down Expand Up @@ -57,13 +50,11 @@
->setCellValue("B{$row}", '=SUM(COLUMN_DATA_VALUES)')
->setCellValue("C{$row}", '=SUM(COLUMN_DATA_VALUES)');

echo sprintf(
$helper->log(sprintf(
'Worked %.2f hours at a rate of %.2f - Charge to the client is %.2f',
$worksheet->getCell("B{$row}")->getCalculatedValue(),
$worksheet->getCell('B1')->getValue(),
$worksheet->getCell("C{$row}")->getCalculatedValue()
), PHP_EOL;
));

$outputFileName = 'RelativeNamedRange2.xlsx';
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save($outputFileName);
$helper->write($spreadsheet, __FILE__, ['Xlsx']);
17 changes: 4 additions & 13 deletions samples/DefinedNames/RelativeNamedRangeAsFunction.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
<?php

use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\NamedFormula;
use PhpOffice\PhpSpreadsheet\NamedRange;
use PhpOffice\PhpSpreadsheet\Spreadsheet;

error_reporting(E_ALL);
set_time_limit(0);

date_default_timezone_set('UTC');

// Adjust the path as required to reference the PHPSpreadsheet Bootstrap file
require_once __DIR__ . '/../Bootstrap.php';
require_once __DIR__ . '/../Header.php';

$spreadsheet = new Spreadsheet();
$worksheet = $spreadsheet->setActiveSheetIndex(0);
Expand Down Expand Up @@ -60,13 +53,11 @@
->setCellValue("B{$row}", '=SUM(COLUMN_DATA_VALUES)')
->setCellValue("C{$row}", '=SUM(COLUMN_DATA_VALUES)');

echo sprintf(
$helper->log(sprintf(
'Worked %.2f hours at a rate of %.2f - Charge to the client is %.2f',
$worksheet->getCell("B{$row}")->getCalculatedValue(),
$worksheet->getCell('B1')->getValue(),
$worksheet->getCell("C{$row}")->getCalculatedValue()
), PHP_EOL;
));

$outputFileName = 'RelativeNamedRangeAsFunction.xlsx';
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save($outputFileName);
$helper->write($spreadsheet, __FILE__, ['Xlsx']);
17 changes: 4 additions & 13 deletions samples/DefinedNames/ScopedNamedRange.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
<?php

use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\NamedRange;
use PhpOffice\PhpSpreadsheet\Spreadsheet;

error_reporting(E_ALL);
set_time_limit(0);

date_default_timezone_set('UTC');

// Adjust the path as required to reference the PHPSpreadsheet Bootstrap file
require_once __DIR__ . '/../Bootstrap.php';
require_once __DIR__ . '/../Header.php';

$spreadsheet = new Spreadsheet();
$worksheet = $spreadsheet->setActiveSheetIndex(0);
Expand Down Expand Up @@ -67,15 +60,13 @@
->setCellValue("B{$row}", '=SUM(COLUMN_DATA_VALUES)')
->setCellValue("C{$row}", '=SUM(COLUMN_DATA_VALUES)');

echo sprintf(
$helper->log(sprintf(
'Worked %.2f hours at a rate of %s - Charge to the client is %.2f',
$worksheet->getCell("B{$row}")->getCalculatedValue(),
$chargeRateCellValue = $spreadsheet
->getSheetByName($spreadsheet->getNamedRange('CHARGE_RATE')->getWorksheet()->getTitle())
->getCell($spreadsheet->getNamedRange('CHARGE_RATE')->getCellsInRange()[0])->getValue(),
$worksheet->getCell("C{$row}")->getCalculatedValue()
), PHP_EOL;
));

$outputFileName = 'ScopedNamedRange.xlsx';
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save($outputFileName);
$helper->write($spreadsheet, __FILE__, ['Xlsx']);
17 changes: 4 additions & 13 deletions samples/DefinedNames/ScopedNamedRange2.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
<?php

use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\NamedRange;
use PhpOffice\PhpSpreadsheet\Spreadsheet;

error_reporting(E_ALL);
set_time_limit(0);

date_default_timezone_set('UTC');

// Adjust the path as required to reference the PHPSpreadsheet Bootstrap file
require_once __DIR__ . '/../Bootstrap.php';
require_once __DIR__ . '/../Header.php';

$spreadsheet = new Spreadsheet();
$worksheet = $spreadsheet->setActiveSheetIndex(0);
Expand Down Expand Up @@ -83,16 +76,14 @@
->setCellValue('B1', 4.5);

foreach ($spreadsheet->getAllSheets() as $worksheet) {
echo sprintf(
$helper->log(sprintf(
'Worked %.2f hours for "%s" at a rate of %.2f - Charge to the client is %.2f',
$worksheet->getCell("B{$row}")->getCalculatedValue(),
$worksheet->getTitle(),
$worksheet->getCell('B1')->getValue(),
$worksheet->getCell("C{$row}")->getCalculatedValue()
), PHP_EOL;
));
}
$worksheet = $spreadsheet->setActiveSheetIndex(0);

$outputFileName = 'ScopedNamedRange2.xlsx';
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save($outputFileName);
$helper->write($spreadsheet, __FILE__, ['Xlsx']);
17 changes: 4 additions & 13 deletions samples/DefinedNames/SimpleNamedFormula.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
<?php

use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\NamedFormula;
use PhpOffice\PhpSpreadsheet\NamedRange;
use PhpOffice\PhpSpreadsheet\Spreadsheet;

error_reporting(E_ALL);
set_time_limit(0);

date_default_timezone_set('UTC');

// Adjust the path as required to reference the PHPSpreadsheet Bootstrap file
require_once __DIR__ . '/../Bootstrap.php';
require_once __DIR__ . '/../Header.php';

$spreadsheet = new Spreadsheet();
$worksheet = $spreadsheet->setActiveSheetIndex(0);
Expand Down Expand Up @@ -39,14 +32,12 @@
->setCellValue('B4', '=TAX')
->setCellValue('B5', '=PRICE+TAX');

echo sprintf(
$helper->log(sprintf(
'With a Tax Rate of %.2f and a net price of %.2f, Tax is %.2f and the gross price is %.2f',
$worksheet->getCell('B1')->getCalculatedValue(),
$worksheet->getCell('B3')->getValue(),
$worksheet->getCell('B4')->getCalculatedValue(),
$worksheet->getCell('B5')->getCalculatedValue()
), PHP_EOL;
));

$outputFileName = 'SimpleNamedFormula.xlsx';
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save($outputFileName);
$helper->write($spreadsheet, __FILE__, ['Xlsx']);
17 changes: 4 additions & 13 deletions samples/DefinedNames/SimpleNamedRange.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
<?php

use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\NamedRange;
use PhpOffice\PhpSpreadsheet\Spreadsheet;

error_reporting(E_ALL);
set_time_limit(0);

date_default_timezone_set('UTC');

// Adjust the path as required to reference the PHPSpreadsheet Bootstrap file
require_once __DIR__ . '/../Bootstrap.php';
require_once __DIR__ . '/../Header.php';

$spreadsheet = new Spreadsheet();
$worksheet = $spreadsheet->setActiveSheetIndex(0);
Expand All @@ -33,14 +26,12 @@
->setCellValue('B4', '=PRICE*TAX_RATE')
->setCellValue('B5', '=PRICE*(1+TAX_RATE)');

echo sprintf(
$helper->log(sprintf(
'With a Tax Rate of %.2f and a net price of %.2f, Tax is %.2f and the gross price is %.2f',
$worksheet->getCell('B1')->getCalculatedValue(),
$worksheet->getCell('B3')->getValue(),
$worksheet->getCell('B4')->getCalculatedValue(),
$worksheet->getCell('B5')->getCalculatedValue()
), PHP_EOL;
));

$outputFileName = 'SimpleNamedRange.xlsx';
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save($outputFileName);
$helper->write($spreadsheet, __FILE__, ['Xlsx']);

0 comments on commit 62fd60b

Please sign in to comment.