| 
									
										
											  
											
												Named formula implementation, and improved handling of Defined Names generally  (#1535)
* Initial work modifying the way named ranges are stored, and handled by the calculation engine
This should provide better support for:
  - both union and intersection operators in composite named range values
  - MS Excel implementation of the union operator duplicating values
  - named formulae
  - named ranges and formulae that reference other named ranges and formulae
  - ranges and formulae that reference multiple ranges across multiple worksheets
* Initial work on handling defined names (named ranges and named formulae) correctly
 - UTF-8 names (already extracted as a separate PR and merged)
 - distinction between named ranges and named formulae
 - correct handling of union and intersection operators in named ranges
 - correct evaluation of named range operators in calculations
 - calculation support for named formulae
 - support for nested ranges and formulae (named ranges and formulae that reference other named ranges/formulae) in calculations
* Minor tweaks before resolving merge conflicts
* Fix extractSheetTitle() method to work on the last ! in a cell reference rather than the first
* Throw exception if a the reference to a defined name in a formula doesn't exist as a defined name
* Properly assess scope for defined names in calculation engine
* Elimination of some redundant code
* Minor tweaks to simplify entries o the stack where we need to check type
* Ensure correct scoping rules are applied when evaluating named ranges and formulae
* Adjustments to Gnumeric Reader for new defined names structure
* Initial work modifying the Ods Reader to handle named ranges, they weren't actually supported previously... this is still ongoing work
* Handle Ranges formatted as 3-d ranges, as long as the references are both to the same worksheet
* Additional testing for Named Ranges formatted as 3-d ranges, as long as the references are both to the same worksheet
* Skip composite named range tests for the moment
* Clean handling for `undefined name` exception when thrown in the calculation engine. Catch and replace with `#NAME?`
* Adjust method we use to determine whether a defined name is a range or a formula
* PHPCS Recommendations
* PHP doesn't support `mixed` yet, at least not at the minium version that we're working with
* More phpcs fixes
* More phpcs appeasements
* Final phpcs fixes for the moment
Still have a lot of echo and var_dump() statements in the code that scrutinizer will hate, but they stay for the moment while this is still WIP
* Please let this be the last of the phpcs fixes
* Unit tests to determine whether a defined name value is a range value or a formula
* phpcs appeasement
* Named tests from provider
* Initial steps for named ranges and formulae in the Ods Reader
* Reading pseudo-3d range addresses in Ods; treat second sheet reference as being identical to the first, which is the majority of cases where this will occur
* Initial work on Gnumeric reader for named ranges and formulae
* Suppress debug logging again
* Remove more debugging displays
* Last minor tweaks before phase two
* Minor refinements
* And all for the want of a space
* A little tidying up
* More tidying up
* phpcs fix
* Modify defined names in rebindParent()
* Renaming variables
* Resolve an issue with locally scoped defined names that don't contain any worksheet reference
* Keep phpcs happy
* Fix quote handling in regexp
* Fix a couple of scrutinizer issues
* Fix a couple of scrutinizer issues
* Update Xlsx Writer to work with the new defined name internal definition
Additional validation checks
* When adding new defined names through the readers, worksheet may not exist if we're only loading selected sheets rather than the full spreadsheet
* If the only thing that phpcs can pickup on is strings in double quotes instead of single quotes, then I know I'm getting close to ready
* Refactor Defined Names logic for Xlsx Writer into its own class
* phpcs keeping me on my toes
* Restore a couple of files that I managed to change without intending to
* Initial work on Ods Write to provide support for saving named ranges and formulae
* Resolve commas to semi-colons s argument separator when writing named formulae for Ods
* Extract Named Expression Writer for Ods into its own class
* Keep phpcs happy
* Refactoring of formula conversion when reading SpreadsheetML; preparation for reading named ranges because they will also need to use the same conversion method
* First pass at reading Named Ranges/Formulae from SpreadsheetML format xml files
* Remove unused namespace reference
* Defined names being written correctly for Xls; but not yet writing cell formulae that reference those defined names... that's the next big step
And I anticipate that defined names that reference other defined names will also be a problem
* Just to keep phpcs happy
... and yes, I know that there are still diagnostic echo statements in the code
* I had to miss some of the phpcs issues didn't I
* Work on the Xls Writer's Parser Tree to identify named range tokens in a formula, and to distinguish them from function tokens
* Still working on packing that d*** defined name reference in the writer
* Throw an exception in the Parser for saving Xls output if we encounter a defined name in a formula... writer will simply write the calculated cell value, and not the formula as at present
Strip out diagnostic output
* Some phpcs appeasement
* Fix a couple of Scrutinizer issues
* Additional verifications to differentiate a formula from a range value
Add explicit getters/setters for named ranges, named formulae and defined names
Additional unit tests
* Styling for closures
* Remove redundant docblocks
* Spaces
* Gah! Namespace use complaints
* Consistency of making calls to DefinedName rather than NamedRange; NamedRange should now be used only for Named Ranges, and should exclude Named Formulae
* Styling
* spurious newline
* No need to test for variable === null when we're typing it in the function argument definition
* Additional unit tests for local/global scoped named ranges and formulae; and a fix to getNamedFormula()
* Fix silly typo that led to breaking test
* Void return signature for unit tests
* Why weren't these picked up in the last pass?
* Refactoring of getNamedRange()/getNamedFormula()
* Eliminate unused constants, and defaults for private method parameters when always called with a value
* Use strict comparisons when comparing object hash codes
* Initial update to documentation for working with named formulae
* Fix for calculation of relative cell references in named ranges/formulae
* Fix current named range tests, because we should be using absolute references; tests for relative named ranges to be added later
* Fix for calculation of relative cell references in named ranges/formulae
* Updates to changelog and documentation for handling of absolute/relative references in named ranges
* Fix last remaining unit test with a named range reference
* Refactor formula conversion for Ods into a separate class; I hadn't realised that it previously wrote formulae as the MS Excel syntax without any conversion to Ods format
* Fix Ods Writer test xml to reflect Ods-native format for formula
* Docblocks
* Drop dollar prefix from Ods formulae and ranges unless it's necessary
* Set the formula convertor in the content writer constructor
* Documentation update
* Minor updates
* Remove var_dumps from file
* Fix the spurious single quote that was breaking named expressions in the Ods Writer... big sigh of relief that I finally spotted it
* Starting work on documentation for Defined Names, and some examples of using Named Ranges and Formulae
* Starting work on documentation for Defined Names, and some examples of using Named Ranges and Formulae
* Example of a relative named range for the documentation
* Mustn't have phpcs problems in sample code either
* More updates to the documentation
* That should conclude the documentation for Named Ranges, now time to move on to documenting Named Formulae
* That should conclude the documentation for Named Ranges, now time to move on to documenting Named Formulae
* PHPCS appeasement in sample code
* Initial documentation on Named Formulae
* PHPCS appeasements
* Additional comments in the documentation, and modify the named range name validation to support a \ as the first character in a name
* Fix breaking build
* Make defined names case-insensitive
* Fix case-insensitivity
* Improved documentation, and additional unit tests
* Additional unit tests, and a fix for removing a globally scoped defined name even if a worksheet is specified in the method call
* Fix unit test for removing named formulae
* Use assertCount instead of assertSame
* Forgotten voids
* Fix arguments for assertCount
* Unit tests for removing defined names, and a fix for removing locally scoped names
* Unit tests for absolute and relative named ranges in calculation engine, and fix an issue with worksheet name in the offset adjustments for relative references
* PHPCS Appeasement
* Additional unit tests, more documentation, and a fix to the calculation engine when no worksheet reference is provided with a named formula
* PHPCS appeasements
* Additional documentation and examples of using Named Formulae
* Additional examples to go with documentation
* A few minor phpcs appeasements
* Minor refactor of updateFormulaReferencesAnyWorksheet() method
* Discard an unused method argument
* Additional unit tests
* Additional unit tests
* Remove unused argument
* Stricter typing
* Fix return typehinting from remove named range/formula; should return the Spreadsheet object
* Use return typehint of self rather than explicit object type
* Redundant code just to keep scrutinizer happy
* Minor change to handle merge conflict
* phpcs fixes after merge
* Namespace usage ordering
* Please let this be the last phpcs fix needed
Co-authored-by: Adrien Crivelli <adrien.crivelli@gmail.com>
											
										 
											2020-07-26 10:00:06 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace PhpOffice\PhpSpreadsheetTests; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use PhpOffice\PhpSpreadsheet\DefinedName; | 
					
						
							| 
									
										
										
										
											2020-11-27 14:50:01 +00:00
										 |  |  | use PhpOffice\PhpSpreadsheet\NamedRange; | 
					
						
							| 
									
										
											  
											
												Named formula implementation, and improved handling of Defined Names generally  (#1535)
* Initial work modifying the way named ranges are stored, and handled by the calculation engine
This should provide better support for:
  - both union and intersection operators in composite named range values
  - MS Excel implementation of the union operator duplicating values
  - named formulae
  - named ranges and formulae that reference other named ranges and formulae
  - ranges and formulae that reference multiple ranges across multiple worksheets
* Initial work on handling defined names (named ranges and named formulae) correctly
 - UTF-8 names (already extracted as a separate PR and merged)
 - distinction between named ranges and named formulae
 - correct handling of union and intersection operators in named ranges
 - correct evaluation of named range operators in calculations
 - calculation support for named formulae
 - support for nested ranges and formulae (named ranges and formulae that reference other named ranges/formulae) in calculations
* Minor tweaks before resolving merge conflicts
* Fix extractSheetTitle() method to work on the last ! in a cell reference rather than the first
* Throw exception if a the reference to a defined name in a formula doesn't exist as a defined name
* Properly assess scope for defined names in calculation engine
* Elimination of some redundant code
* Minor tweaks to simplify entries o the stack where we need to check type
* Ensure correct scoping rules are applied when evaluating named ranges and formulae
* Adjustments to Gnumeric Reader for new defined names structure
* Initial work modifying the Ods Reader to handle named ranges, they weren't actually supported previously... this is still ongoing work
* Handle Ranges formatted as 3-d ranges, as long as the references are both to the same worksheet
* Additional testing for Named Ranges formatted as 3-d ranges, as long as the references are both to the same worksheet
* Skip composite named range tests for the moment
* Clean handling for `undefined name` exception when thrown in the calculation engine. Catch and replace with `#NAME?`
* Adjust method we use to determine whether a defined name is a range or a formula
* PHPCS Recommendations
* PHP doesn't support `mixed` yet, at least not at the minium version that we're working with
* More phpcs fixes
* More phpcs appeasements
* Final phpcs fixes for the moment
Still have a lot of echo and var_dump() statements in the code that scrutinizer will hate, but they stay for the moment while this is still WIP
* Please let this be the last of the phpcs fixes
* Unit tests to determine whether a defined name value is a range value or a formula
* phpcs appeasement
* Named tests from provider
* Initial steps for named ranges and formulae in the Ods Reader
* Reading pseudo-3d range addresses in Ods; treat second sheet reference as being identical to the first, which is the majority of cases where this will occur
* Initial work on Gnumeric reader for named ranges and formulae
* Suppress debug logging again
* Remove more debugging displays
* Last minor tweaks before phase two
* Minor refinements
* And all for the want of a space
* A little tidying up
* More tidying up
* phpcs fix
* Modify defined names in rebindParent()
* Renaming variables
* Resolve an issue with locally scoped defined names that don't contain any worksheet reference
* Keep phpcs happy
* Fix quote handling in regexp
* Fix a couple of scrutinizer issues
* Fix a couple of scrutinizer issues
* Update Xlsx Writer to work with the new defined name internal definition
Additional validation checks
* When adding new defined names through the readers, worksheet may not exist if we're only loading selected sheets rather than the full spreadsheet
* If the only thing that phpcs can pickup on is strings in double quotes instead of single quotes, then I know I'm getting close to ready
* Refactor Defined Names logic for Xlsx Writer into its own class
* phpcs keeping me on my toes
* Restore a couple of files that I managed to change without intending to
* Initial work on Ods Write to provide support for saving named ranges and formulae
* Resolve commas to semi-colons s argument separator when writing named formulae for Ods
* Extract Named Expression Writer for Ods into its own class
* Keep phpcs happy
* Refactoring of formula conversion when reading SpreadsheetML; preparation for reading named ranges because they will also need to use the same conversion method
* First pass at reading Named Ranges/Formulae from SpreadsheetML format xml files
* Remove unused namespace reference
* Defined names being written correctly for Xls; but not yet writing cell formulae that reference those defined names... that's the next big step
And I anticipate that defined names that reference other defined names will also be a problem
* Just to keep phpcs happy
... and yes, I know that there are still diagnostic echo statements in the code
* I had to miss some of the phpcs issues didn't I
* Work on the Xls Writer's Parser Tree to identify named range tokens in a formula, and to distinguish them from function tokens
* Still working on packing that d*** defined name reference in the writer
* Throw an exception in the Parser for saving Xls output if we encounter a defined name in a formula... writer will simply write the calculated cell value, and not the formula as at present
Strip out diagnostic output
* Some phpcs appeasement
* Fix a couple of Scrutinizer issues
* Additional verifications to differentiate a formula from a range value
Add explicit getters/setters for named ranges, named formulae and defined names
Additional unit tests
* Styling for closures
* Remove redundant docblocks
* Spaces
* Gah! Namespace use complaints
* Consistency of making calls to DefinedName rather than NamedRange; NamedRange should now be used only for Named Ranges, and should exclude Named Formulae
* Styling
* spurious newline
* No need to test for variable === null when we're typing it in the function argument definition
* Additional unit tests for local/global scoped named ranges and formulae; and a fix to getNamedFormula()
* Fix silly typo that led to breaking test
* Void return signature for unit tests
* Why weren't these picked up in the last pass?
* Refactoring of getNamedRange()/getNamedFormula()
* Eliminate unused constants, and defaults for private method parameters when always called with a value
* Use strict comparisons when comparing object hash codes
* Initial update to documentation for working with named formulae
* Fix for calculation of relative cell references in named ranges/formulae
* Fix current named range tests, because we should be using absolute references; tests for relative named ranges to be added later
* Fix for calculation of relative cell references in named ranges/formulae
* Updates to changelog and documentation for handling of absolute/relative references in named ranges
* Fix last remaining unit test with a named range reference
* Refactor formula conversion for Ods into a separate class; I hadn't realised that it previously wrote formulae as the MS Excel syntax without any conversion to Ods format
* Fix Ods Writer test xml to reflect Ods-native format for formula
* Docblocks
* Drop dollar prefix from Ods formulae and ranges unless it's necessary
* Set the formula convertor in the content writer constructor
* Documentation update
* Minor updates
* Remove var_dumps from file
* Fix the spurious single quote that was breaking named expressions in the Ods Writer... big sigh of relief that I finally spotted it
* Starting work on documentation for Defined Names, and some examples of using Named Ranges and Formulae
* Starting work on documentation for Defined Names, and some examples of using Named Ranges and Formulae
* Example of a relative named range for the documentation
* Mustn't have phpcs problems in sample code either
* More updates to the documentation
* That should conclude the documentation for Named Ranges, now time to move on to documenting Named Formulae
* That should conclude the documentation for Named Ranges, now time to move on to documenting Named Formulae
* PHPCS appeasement in sample code
* Initial documentation on Named Formulae
* PHPCS appeasements
* Additional comments in the documentation, and modify the named range name validation to support a \ as the first character in a name
* Fix breaking build
* Make defined names case-insensitive
* Fix case-insensitivity
* Improved documentation, and additional unit tests
* Additional unit tests, and a fix for removing a globally scoped defined name even if a worksheet is specified in the method call
* Fix unit test for removing named formulae
* Use assertCount instead of assertSame
* Forgotten voids
* Fix arguments for assertCount
* Unit tests for removing defined names, and a fix for removing locally scoped names
* Unit tests for absolute and relative named ranges in calculation engine, and fix an issue with worksheet name in the offset adjustments for relative references
* PHPCS Appeasement
* Additional unit tests, more documentation, and a fix to the calculation engine when no worksheet reference is provided with a named formula
* PHPCS appeasements
* Additional documentation and examples of using Named Formulae
* Additional examples to go with documentation
* A few minor phpcs appeasements
* Minor refactor of updateFormulaReferencesAnyWorksheet() method
* Discard an unused method argument
* Additional unit tests
* Additional unit tests
* Remove unused argument
* Stricter typing
* Fix return typehinting from remove named range/formula; should return the Spreadsheet object
* Use return typehint of self rather than explicit object type
* Redundant code just to keep scrutinizer happy
* Minor change to handle merge conflict
* phpcs fixes after merge
* Namespace usage ordering
* Please let this be the last phpcs fix needed
Co-authored-by: Adrien Crivelli <adrien.crivelli@gmail.com>
											
										 
											2020-07-26 10:00:06 +00:00
										 |  |  | use PhpOffice\PhpSpreadsheet\Spreadsheet; | 
					
						
							|  |  |  | use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; | 
					
						
							|  |  |  | use PHPUnit\Framework\TestCase; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class DefinedNameTest 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 testAddDefinedName(): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->spreadsheet->addDefinedName( | 
					
						
							|  |  |  |             DefinedName::createInstance('Foo', $this->spreadsheet->getActiveSheet(), '=A1') | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self::assertCount(1, $this->spreadsheet->getDefinedNames()); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function testAddDuplicateDefinedName(): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->spreadsheet->addDefinedName( | 
					
						
							|  |  |  |             DefinedName::createInstance('Foo', $this->spreadsheet->getActiveSheet(), '=A1') | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |         $this->spreadsheet->addDefinedName( | 
					
						
							|  |  |  |             DefinedName::createInstance('FOO', $this->spreadsheet->getActiveSheet(), '=B1') | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self::assertCount(1, $this->spreadsheet->getDefinedNames()); | 
					
						
							|  |  |  |         self::assertSame( | 
					
						
							|  |  |  |             '=B1', | 
					
						
							|  |  |  |             $this->spreadsheet->getDefinedName('foo', $this->spreadsheet->getActiveSheet())->getValue() | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function testAddScopedDefinedNameWithSameName(): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->spreadsheet->addDefinedName( | 
					
						
							|  |  |  |             DefinedName::createInstance('Foo', $this->spreadsheet->getActiveSheet(), '=A1') | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |         $this->spreadsheet->addDefinedName( | 
					
						
							|  |  |  |             DefinedName::createInstance('FOO', $this->spreadsheet->getSheetByName('Sheet #2'), '=B1', true) | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self::assertCount(2, $this->spreadsheet->getDefinedNames()); | 
					
						
							|  |  |  |         self::assertSame( | 
					
						
							|  |  |  |             '=A1', | 
					
						
							|  |  |  |             $this->spreadsheet->getDefinedName('foo', $this->spreadsheet->getActiveSheet())->getValue() | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |         self::assertSame( | 
					
						
							|  |  |  |             '=B1', | 
					
						
							|  |  |  |             $this->spreadsheet->getDefinedName('foo', $this->spreadsheet->getSheetByName('Sheet #2'))->getValue() | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function testRemoveDefinedName(): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->spreadsheet->addDefinedName( | 
					
						
							|  |  |  |             DefinedName::createInstance('Foo', $this->spreadsheet->getActiveSheet(), '=A1') | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |         $this->spreadsheet->addDefinedName( | 
					
						
							|  |  |  |             DefinedName::createInstance('Bar', $this->spreadsheet->getActiveSheet(), '=B1') | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->spreadsheet->removeDefinedName('Foo', $this->spreadsheet->getActiveSheet()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self::assertCount(1, $this->spreadsheet->getDefinedNames()); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function testRemoveGlobalDefinedNameWhenDuplicateNames(): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->spreadsheet->addDefinedName( | 
					
						
							|  |  |  |             DefinedName::createInstance('Foo', $this->spreadsheet->getActiveSheet(), '=A1') | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |         $this->spreadsheet->addDefinedName( | 
					
						
							|  |  |  |             DefinedName::createInstance('FOO', $this->spreadsheet->getSheetByName('Sheet #2'), '=B1', true) | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->spreadsheet->removeDefinedName('Foo', $this->spreadsheet->getActiveSheet()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self::assertCount(1, $this->spreadsheet->getDefinedNames()); | 
					
						
							|  |  |  |         self::assertSame( | 
					
						
							|  |  |  |             '=B1', | 
					
						
							|  |  |  |             $this->spreadsheet->getDefinedName('foo', $this->spreadsheet->getSheetByName('Sheet #2'))->getValue() | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function testRemoveScopedDefinedNameWhenDuplicateNames(): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->spreadsheet->addDefinedName( | 
					
						
							|  |  |  |             DefinedName::createInstance('Foo', $this->spreadsheet->getActiveSheet(), '=A1') | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |         $this->spreadsheet->addDefinedName( | 
					
						
							|  |  |  |             DefinedName::createInstance('FOO', $this->spreadsheet->getSheetByName('Sheet #2'), '=B1', true) | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->spreadsheet->removeDefinedName('Foo', $this->spreadsheet->getSheetByName('Sheet #2')); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self::assertCount(1, $this->spreadsheet->getDefinedNames()); | 
					
						
							|  |  |  |         self::assertSame( | 
					
						
							|  |  |  |             '=A1', | 
					
						
							|  |  |  |             $this->spreadsheet->getDefinedName('foo')->getValue() | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-11-27 14:50:01 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function testDefinedNameNoWorksheetNoScope(): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->expectException(\PhpOffice\PhpSpreadsheet\Exception::class); | 
					
						
							|  |  |  |         new NamedRange('xyz'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function testSetAndGetRange(): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->spreadsheet->addDefinedName( | 
					
						
							|  |  |  |             DefinedName::createInstance('xyz', $this->spreadsheet->getActiveSheet(), 'A1') | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $namedRange = $this->spreadsheet->getDefinedName('XYZ'); | 
					
						
							|  |  |  |         self::assertInstanceOf(NamedRange::class, $namedRange); | 
					
						
							| 
									
										
										
										
											2020-11-27 15:16:23 +00:00
										 |  |  |         self::assertEquals('A1', $namedRange->getRange()); | 
					
						
							|  |  |  |         self::assertEquals('A1', $namedRange->getValue()); | 
					
						
							|  |  |  |         $namedRange->setRange('A2'); | 
					
						
							|  |  |  |         self::assertEquals('A2', $namedRange->getValue()); | 
					
						
							| 
									
										
										
										
											2020-11-27 14:50:01 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function testChangeWorksheet(): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $sheet1 = $this->spreadsheet->getSheetByName('Sheet #1'); | 
					
						
							|  |  |  |         $sheet2 = $this->spreadsheet->getSheetByName('Sheet #2'); | 
					
						
							|  |  |  |         $sheet1->getCell('A1')->setValue(1); | 
					
						
							|  |  |  |         $sheet2->getCell('A1')->setValue(2); | 
					
						
							|  |  |  |         $namedRange = new NamedRange('xyz', $sheet2, '$A$1'); | 
					
						
							|  |  |  |         $namedRange->setWorksheet($sheet1); | 
					
						
							|  |  |  |         $this->spreadsheet->addNamedRange($namedRange); | 
					
						
							|  |  |  |         $sheet1->getCell('B2')->setValue('=XYZ'); | 
					
						
							|  |  |  |         self::assertEquals(1, $sheet1->getCell('B2')->getCalculatedValue()); | 
					
						
							|  |  |  |         $sheet2->getCell('B2')->setValue('=XYZ'); | 
					
						
							|  |  |  |         self::assertEquals(1, $sheet2->getCell('B2')->getCalculatedValue()); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function testLocalOnly(): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $sheet1 = $this->spreadsheet->getSheetByName('Sheet #1'); | 
					
						
							|  |  |  |         $sheet2 = $this->spreadsheet->getSheetByName('Sheet #2'); | 
					
						
							|  |  |  |         $sheet1->getCell('A1')->setValue(1); | 
					
						
							|  |  |  |         $sheet2->getCell('A1')->setValue(2); | 
					
						
							|  |  |  |         $namedRange = new NamedRange('abc', $sheet2, '$A$1'); | 
					
						
							|  |  |  |         $namedRange->setWorksheet($sheet1)->setLocalOnly(true); | 
					
						
							|  |  |  |         $this->spreadsheet->addNamedRange($namedRange); | 
					
						
							|  |  |  |         $sheet1->getCell('C2')->setValue('=ABC'); | 
					
						
							|  |  |  |         self::assertEquals(1, $sheet1->getCell('C2')->getCalculatedValue()); | 
					
						
							|  |  |  |         $sheet2->getCell('C2')->setValue('=ABC'); | 
					
						
							|  |  |  |         self::assertEquals('#NAME?', $sheet2->getCell('C2')->getCalculatedValue()); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function testScope(): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $sheet1 = $this->spreadsheet->getSheetByName('Sheet #1'); | 
					
						
							|  |  |  |         $sheet2 = $this->spreadsheet->getSheetByName('Sheet #2'); | 
					
						
							|  |  |  |         $sheet1->getCell('A1')->setValue(1); | 
					
						
							|  |  |  |         $sheet2->getCell('A1')->setValue(2); | 
					
						
							|  |  |  |         $namedRange = new NamedRange('abc', $sheet2, '$A$1'); | 
					
						
							|  |  |  |         $namedRange->setScope($sheet1); | 
					
						
							|  |  |  |         $this->spreadsheet->addNamedRange($namedRange); | 
					
						
							|  |  |  |         $sheet1->getCell('C2')->setValue('=ABC'); | 
					
						
							|  |  |  |         self::assertEquals(2, $sheet1->getCell('C2')->getCalculatedValue()); | 
					
						
							|  |  |  |         $sheet2->getCell('C2')->setValue('=ABC'); | 
					
						
							|  |  |  |         self::assertEquals('#NAME?', $sheet2->getCell('C2')->getCalculatedValue()); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function testClone(): void | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $sheet1 = $this->spreadsheet->getSheetByName('Sheet #1'); | 
					
						
							|  |  |  |         $sheet2 = $this->spreadsheet->getSheetByName('Sheet #2'); | 
					
						
							|  |  |  |         $sheet1->getCell('A1')->setValue(1); | 
					
						
							|  |  |  |         $sheet2->getCell('A1')->setValue(2); | 
					
						
							|  |  |  |         $namedRange = new NamedRange('abc', $sheet2, '$A$1'); | 
					
						
							|  |  |  |         $namedRangeClone = clone $namedRange; | 
					
						
							|  |  |  |         $ss1 = $namedRange->getWorksheet(); | 
					
						
							|  |  |  |         $ss2 = $namedRangeClone->getWorksheet(); | 
					
						
							|  |  |  |         self::assertNotSame($ss1, $ss2); | 
					
						
							|  |  |  |         self::assertEquals($ss1->getTitle(), $ss2->getTitle()); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
											  
											
												Named formula implementation, and improved handling of Defined Names generally  (#1535)
* Initial work modifying the way named ranges are stored, and handled by the calculation engine
This should provide better support for:
  - both union and intersection operators in composite named range values
  - MS Excel implementation of the union operator duplicating values
  - named formulae
  - named ranges and formulae that reference other named ranges and formulae
  - ranges and formulae that reference multiple ranges across multiple worksheets
* Initial work on handling defined names (named ranges and named formulae) correctly
 - UTF-8 names (already extracted as a separate PR and merged)
 - distinction between named ranges and named formulae
 - correct handling of union and intersection operators in named ranges
 - correct evaluation of named range operators in calculations
 - calculation support for named formulae
 - support for nested ranges and formulae (named ranges and formulae that reference other named ranges/formulae) in calculations
* Minor tweaks before resolving merge conflicts
* Fix extractSheetTitle() method to work on the last ! in a cell reference rather than the first
* Throw exception if a the reference to a defined name in a formula doesn't exist as a defined name
* Properly assess scope for defined names in calculation engine
* Elimination of some redundant code
* Minor tweaks to simplify entries o the stack where we need to check type
* Ensure correct scoping rules are applied when evaluating named ranges and formulae
* Adjustments to Gnumeric Reader for new defined names structure
* Initial work modifying the Ods Reader to handle named ranges, they weren't actually supported previously... this is still ongoing work
* Handle Ranges formatted as 3-d ranges, as long as the references are both to the same worksheet
* Additional testing for Named Ranges formatted as 3-d ranges, as long as the references are both to the same worksheet
* Skip composite named range tests for the moment
* Clean handling for `undefined name` exception when thrown in the calculation engine. Catch and replace with `#NAME?`
* Adjust method we use to determine whether a defined name is a range or a formula
* PHPCS Recommendations
* PHP doesn't support `mixed` yet, at least not at the minium version that we're working with
* More phpcs fixes
* More phpcs appeasements
* Final phpcs fixes for the moment
Still have a lot of echo and var_dump() statements in the code that scrutinizer will hate, but they stay for the moment while this is still WIP
* Please let this be the last of the phpcs fixes
* Unit tests to determine whether a defined name value is a range value or a formula
* phpcs appeasement
* Named tests from provider
* Initial steps for named ranges and formulae in the Ods Reader
* Reading pseudo-3d range addresses in Ods; treat second sheet reference as being identical to the first, which is the majority of cases where this will occur
* Initial work on Gnumeric reader for named ranges and formulae
* Suppress debug logging again
* Remove more debugging displays
* Last minor tweaks before phase two
* Minor refinements
* And all for the want of a space
* A little tidying up
* More tidying up
* phpcs fix
* Modify defined names in rebindParent()
* Renaming variables
* Resolve an issue with locally scoped defined names that don't contain any worksheet reference
* Keep phpcs happy
* Fix quote handling in regexp
* Fix a couple of scrutinizer issues
* Fix a couple of scrutinizer issues
* Update Xlsx Writer to work with the new defined name internal definition
Additional validation checks
* When adding new defined names through the readers, worksheet may not exist if we're only loading selected sheets rather than the full spreadsheet
* If the only thing that phpcs can pickup on is strings in double quotes instead of single quotes, then I know I'm getting close to ready
* Refactor Defined Names logic for Xlsx Writer into its own class
* phpcs keeping me on my toes
* Restore a couple of files that I managed to change without intending to
* Initial work on Ods Write to provide support for saving named ranges and formulae
* Resolve commas to semi-colons s argument separator when writing named formulae for Ods
* Extract Named Expression Writer for Ods into its own class
* Keep phpcs happy
* Refactoring of formula conversion when reading SpreadsheetML; preparation for reading named ranges because they will also need to use the same conversion method
* First pass at reading Named Ranges/Formulae from SpreadsheetML format xml files
* Remove unused namespace reference
* Defined names being written correctly for Xls; but not yet writing cell formulae that reference those defined names... that's the next big step
And I anticipate that defined names that reference other defined names will also be a problem
* Just to keep phpcs happy
... and yes, I know that there are still diagnostic echo statements in the code
* I had to miss some of the phpcs issues didn't I
* Work on the Xls Writer's Parser Tree to identify named range tokens in a formula, and to distinguish them from function tokens
* Still working on packing that d*** defined name reference in the writer
* Throw an exception in the Parser for saving Xls output if we encounter a defined name in a formula... writer will simply write the calculated cell value, and not the formula as at present
Strip out diagnostic output
* Some phpcs appeasement
* Fix a couple of Scrutinizer issues
* Additional verifications to differentiate a formula from a range value
Add explicit getters/setters for named ranges, named formulae and defined names
Additional unit tests
* Styling for closures
* Remove redundant docblocks
* Spaces
* Gah! Namespace use complaints
* Consistency of making calls to DefinedName rather than NamedRange; NamedRange should now be used only for Named Ranges, and should exclude Named Formulae
* Styling
* spurious newline
* No need to test for variable === null when we're typing it in the function argument definition
* Additional unit tests for local/global scoped named ranges and formulae; and a fix to getNamedFormula()
* Fix silly typo that led to breaking test
* Void return signature for unit tests
* Why weren't these picked up in the last pass?
* Refactoring of getNamedRange()/getNamedFormula()
* Eliminate unused constants, and defaults for private method parameters when always called with a value
* Use strict comparisons when comparing object hash codes
* Initial update to documentation for working with named formulae
* Fix for calculation of relative cell references in named ranges/formulae
* Fix current named range tests, because we should be using absolute references; tests for relative named ranges to be added later
* Fix for calculation of relative cell references in named ranges/formulae
* Updates to changelog and documentation for handling of absolute/relative references in named ranges
* Fix last remaining unit test with a named range reference
* Refactor formula conversion for Ods into a separate class; I hadn't realised that it previously wrote formulae as the MS Excel syntax without any conversion to Ods format
* Fix Ods Writer test xml to reflect Ods-native format for formula
* Docblocks
* Drop dollar prefix from Ods formulae and ranges unless it's necessary
* Set the formula convertor in the content writer constructor
* Documentation update
* Minor updates
* Remove var_dumps from file
* Fix the spurious single quote that was breaking named expressions in the Ods Writer... big sigh of relief that I finally spotted it
* Starting work on documentation for Defined Names, and some examples of using Named Ranges and Formulae
* Starting work on documentation for Defined Names, and some examples of using Named Ranges and Formulae
* Example of a relative named range for the documentation
* Mustn't have phpcs problems in sample code either
* More updates to the documentation
* That should conclude the documentation for Named Ranges, now time to move on to documenting Named Formulae
* That should conclude the documentation for Named Ranges, now time to move on to documenting Named Formulae
* PHPCS appeasement in sample code
* Initial documentation on Named Formulae
* PHPCS appeasements
* Additional comments in the documentation, and modify the named range name validation to support a \ as the first character in a name
* Fix breaking build
* Make defined names case-insensitive
* Fix case-insensitivity
* Improved documentation, and additional unit tests
* Additional unit tests, and a fix for removing a globally scoped defined name even if a worksheet is specified in the method call
* Fix unit test for removing named formulae
* Use assertCount instead of assertSame
* Forgotten voids
* Fix arguments for assertCount
* Unit tests for removing defined names, and a fix for removing locally scoped names
* Unit tests for absolute and relative named ranges in calculation engine, and fix an issue with worksheet name in the offset adjustments for relative references
* PHPCS Appeasement
* Additional unit tests, more documentation, and a fix to the calculation engine when no worksheet reference is provided with a named formula
* PHPCS appeasements
* Additional documentation and examples of using Named Formulae
* Additional examples to go with documentation
* A few minor phpcs appeasements
* Minor refactor of updateFormulaReferencesAnyWorksheet() method
* Discard an unused method argument
* Additional unit tests
* Additional unit tests
* Remove unused argument
* Stricter typing
* Fix return typehinting from remove named range/formula; should return the Spreadsheet object
* Use return typehint of self rather than explicit object type
* Redundant code just to keep scrutinizer happy
* Minor change to handle merge conflict
* phpcs fixes after merge
* Namespace usage ordering
* Please let this be the last phpcs fix needed
Co-authored-by: Adrien Crivelli <adrien.crivelli@gmail.com>
											
										 
											2020-07-26 10:00:06 +00:00
										 |  |  | } |