| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- <?php
- namespace PhpOffice\PhpSpreadsheetTests;
- use PhpOffice\PhpSpreadsheet\NamedFormula;
- use PhpOffice\PhpSpreadsheet\Spreadsheet;
- use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
- use PHPUnit\Framework\TestCase;
- class NamedFormulaTest extends TestCase
- {
- /** @var Spreadsheet */
- private $spreadsheet;
- protected function setUp(): void
- {
- parent::setUp();
- $this->spreadsheet = new Spreadsheet();
- $this->spreadsheet->getActiveSheet()
- ->setTitle('Sheet #1');
- $worksheet2 = new Worksheet();
- $worksheet2->setTitle('Sheet #2');
- $this->spreadsheet->addSheet($worksheet2);
- $this->spreadsheet->setActiveSheetIndex(0);
- }
- public function testAddNamedRange(): void
- {
- $this->spreadsheet->addNamedFormula(
- new NamedFormula('Foo', $this->spreadsheet->getActiveSheet(), '=19%')
- );
- self::assertCount(1, $this->spreadsheet->getDefinedNames());
- self::assertCount(1, $this->spreadsheet->getNamedFormulae());
- self::assertCount(0, $this->spreadsheet->getNamedRanges());
- }
- public function testAddDuplicateNamedRange(): void
- {
- $this->spreadsheet->addNamedFormula(
- new NamedFormula('Foo', $this->spreadsheet->getActiveSheet(), '=19%')
- );
- $this->spreadsheet->addNamedFormula(
- new NamedFormula('FOO', $this->spreadsheet->getActiveSheet(), '=16%')
- );
- self::assertCount(1, $this->spreadsheet->getNamedFormulae());
- $formula = $this->spreadsheet->getNamedFormula('foo', $this->spreadsheet->getActiveSheet());
- self::assertNotNull($formula);
- self::assertSame(
- '=16%',
- $formula->getValue()
- );
- }
- public function testAddScopedNamedFormulaWithSameName(): void
- {
- $this->spreadsheet->addNamedFormula(
- new NamedFormula('Foo', $this->spreadsheet->getActiveSheet(), '=19%')
- );
- $this->spreadsheet->addNamedFormula(
- new NamedFormula('FOO', $this->spreadsheet->getSheetByNameOrThrow('Sheet #2'), '=16%', true)
- );
- self::assertCount(2, $this->spreadsheet->getNamedFormulae());
- $formula = $this->spreadsheet->getNamedFormula('foo', $this->spreadsheet->getActiveSheet());
- self::assertNotNull($formula);
- self::assertSame(
- '=19%',
- $formula->getValue()
- );
- $formula = $this->spreadsheet->getNamedFormula('foo', $this->spreadsheet->getSheetByNameOrThrow('Sheet #2'));
- self::assertNotNull($formula);
- self::assertSame(
- '=16%',
- $formula->getValue()
- );
- }
- public function testRemoveNamedFormula(): void
- {
- $this->spreadsheet->addDefinedName(
- new NamedFormula('Foo', $this->spreadsheet->getActiveSheet(), '=16%')
- );
- $this->spreadsheet->addDefinedName(
- new NamedFormula('Bar', $this->spreadsheet->getActiveSheet(), '=19%')
- );
- $this->spreadsheet->removeNamedFormula('Foo', $this->spreadsheet->getActiveSheet());
- self::assertCount(1, $this->spreadsheet->getNamedFormulae());
- }
- public function testRemoveGlobalNamedFormulaWhenDuplicateNames(): void
- {
- $this->spreadsheet->addNamedFormula(
- new NamedFormula('Foo', $this->spreadsheet->getActiveSheet(), '=19%')
- );
- $this->spreadsheet->addNamedFormula(
- new NamedFormula('FOO', $this->spreadsheet->getSheetByNameOrThrow('Sheet #2'), '=16%', true)
- );
- $this->spreadsheet->removeNamedFormula('Foo', $this->spreadsheet->getActiveSheet());
- self::assertCount(1, $this->spreadsheet->getNamedFormulae());
- $formula = $this->spreadsheet->getNamedFormula('foo', $this->spreadsheet->getSheetByNameOrThrow('Sheet #2'));
- self::assertNotNull($formula);
- self::assertSame(
- '=16%',
- $formula->getValue()
- );
- }
- public function testRemoveScopedNamedFormulaWhenDuplicateNames(): void
- {
- $this->spreadsheet->addNamedFormula(
- new NamedFormula('Foo', $this->spreadsheet->getActiveSheet(), '=19%')
- );
- $this->spreadsheet->addNamedFormula(
- new NamedFormula('FOO', $this->spreadsheet->getSheetByNameOrThrow('Sheet #2'), '=16%', true)
- );
- $this->spreadsheet->removeNamedFormula('Foo', $this->spreadsheet->getSheetByNameOrThrow('Sheet #2'));
- self::assertCount(1, $this->spreadsheet->getNamedFormulae());
- $formula = $this->spreadsheet->getNamedFormula('foo');
- self::assertNotNull($formula);
- self::assertSame(
- '=19%',
- $formula->getValue()
- );
- }
- public function testRemoveNonExistentNamedFormula(): void
- {
- self::assertCount(0, $this->spreadsheet->getNamedFormulae());
- $this->spreadsheet->removeNamedFormula('Any');
- }
- }
|