From 8b0aaf7ecf92b3b501e9af120cee1a855ebe832b Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Sun, 26 Jul 2020 12:00:06 +0200 Subject: [PATCH] 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 --- CHANGELOG.md | 26 +- docs/topics/defined-names.md | 593 ++++++++++++++++++ docs/topics/recipes.md | 63 +- samples/DefinedNames/AbsoluteNamedRange.php | 63 ++ .../CrossWorksheetNamedFormula.php | 99 +++ .../DefinedNames/NamedFormulaeAndRanges.php | 74 +++ samples/DefinedNames/RelativeNamedRange.php | 66 ++ samples/DefinedNames/RelativeNamedRange2.php | 69 ++ .../RelativeNamedRangeAsFunction.php | 72 +++ samples/DefinedNames/ScopedNamedRange.php | 81 +++ samples/DefinedNames/ScopedNamedRange2.php | 98 +++ samples/DefinedNames/SimpleNamedFormula.php | 52 ++ samples/DefinedNames/SimpleNamedRange.php | 46 ++ .../Calculation/Calculation.php | 151 +++-- .../Calculation/Engine/Logger.php | 14 + src/PhpSpreadsheet/Calculation/Functions.php | 2 +- src/PhpSpreadsheet/Calculation/LookupRef.php | 2 +- src/PhpSpreadsheet/Cell/AddressHelper.php | 48 +- src/PhpSpreadsheet/Cell/Cell.php | 2 + src/PhpSpreadsheet/DefinedName.php | 263 ++++++++ src/PhpSpreadsheet/NamedFormula.php | 45 ++ src/PhpSpreadsheet/NamedRange.php | 230 +------ src/PhpSpreadsheet/Reader/Gnumeric.php | 21 +- src/PhpSpreadsheet/Reader/Ods.php | 127 +++- src/PhpSpreadsheet/Reader/Xlsx.php | 36 +- src/PhpSpreadsheet/Reader/Xml.php | 84 +-- src/PhpSpreadsheet/ReferenceHelper.php | 146 ++++- src/PhpSpreadsheet/Spreadsheet.php | 208 +++++- src/PhpSpreadsheet/Worksheet/BaseDrawing.php | 2 +- src/PhpSpreadsheet/Worksheet/Worksheet.php | 24 +- src/PhpSpreadsheet/Writer/Ods/Content.php | 19 +- src/PhpSpreadsheet/Writer/Ods/Formula.php | 119 ++++ .../Writer/Ods/NamedExpressions.php | 126 ++++ src/PhpSpreadsheet/Writer/Xls.php | 2 +- src/PhpSpreadsheet/Writer/Xls/Parser.php | 49 +- src/PhpSpreadsheet/Writer/Xls/Workbook.php | 77 ++- src/PhpSpreadsheet/Writer/Xls/Worksheet.php | 1 - .../Writer/Xlsx/DefinedNames.php | 223 +++++++ src/PhpSpreadsheet/Writer/Xlsx/Workbook.php | 185 +----- .../DefinedNamesCalculationTest.php | 98 +++ .../Calculation/Engine/RangeTest.php | 64 +- .../Calculation/FormulaAsStringTest.php | 4 +- .../DefinedNameFormulaTest.php | 167 +++++ tests/PhpSpreadsheetTests/DefinedNameTest.php | 124 ++++ .../PhpSpreadsheetTests/NamedFormulaTest.php | 126 ++++ tests/PhpSpreadsheetTests/NamedRangeTest.php | 126 ++++ .../ReferenceHelperTest.php | 17 + .../Writer/Xls/FormulaErrTest.php | 2 +- .../Writer/Xls/WorkbookTest.php | 2 +- .../DefinedNames/NamedFormulae.xlsx | Bin 0 -> 9216 bytes .../Calculation/DefinedNames/NamedRanges.xlsx | Bin 0 -> 9221 bytes ...renceHelperFormulaUpdatesMultipleSheet.php | 88 +++ tests/data/Writer/Ods/content-with-data.xml | 2 +- 53 files changed, 3771 insertions(+), 657 deletions(-) create mode 100644 docs/topics/defined-names.md create mode 100644 samples/DefinedNames/AbsoluteNamedRange.php create mode 100644 samples/DefinedNames/CrossWorksheetNamedFormula.php create mode 100644 samples/DefinedNames/NamedFormulaeAndRanges.php create mode 100644 samples/DefinedNames/RelativeNamedRange.php create mode 100644 samples/DefinedNames/RelativeNamedRange2.php create mode 100644 samples/DefinedNames/RelativeNamedRangeAsFunction.php create mode 100644 samples/DefinedNames/ScopedNamedRange.php create mode 100644 samples/DefinedNames/ScopedNamedRange2.php create mode 100644 samples/DefinedNames/SimpleNamedFormula.php create mode 100644 samples/DefinedNames/SimpleNamedRange.php create mode 100644 src/PhpSpreadsheet/DefinedName.php create mode 100644 src/PhpSpreadsheet/NamedFormula.php create mode 100644 src/PhpSpreadsheet/Writer/Ods/Formula.php create mode 100644 src/PhpSpreadsheet/Writer/Ods/NamedExpressions.php create mode 100644 src/PhpSpreadsheet/Writer/Xlsx/DefinedNames.php create mode 100644 tests/PhpSpreadsheetTests/Calculation/DefinedNamesCalculationTest.php create mode 100644 tests/PhpSpreadsheetTests/DefinedNameFormulaTest.php create mode 100644 tests/PhpSpreadsheetTests/DefinedNameTest.php create mode 100644 tests/PhpSpreadsheetTests/NamedFormulaTest.php create mode 100644 tests/PhpSpreadsheetTests/NamedRangeTest.php create mode 100644 tests/data/Calculation/DefinedNames/NamedFormulae.xlsx create mode 100644 tests/data/Calculation/DefinedNames/NamedRanges.xlsx create mode 100644 tests/data/ReferenceHelperFormulaUpdatesMultipleSheet.php diff --git a/CHANGELOG.md b/CHANGELOG.md index d96323dd..9dfe3dfe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,14 +11,36 @@ and this project adheres to [Semantic Versioning](https://semver.org). - Implemented Page Order for Xlsx and Xls Readers, and provided Page Settings (Orientation, Scale, Horizontal/Vertical Centering, Page Order, Margins) support for Ods, Gnumeric and Xls Readers [#1559](https://github.com/PHPOffice/PhpSpreadsheet/pull/1559) - Implementation of the Excel `LOGNORM.DIST()`, `NORM.S.DIST()`, `GAMMA()` and `GAUSS()` functions. [#1588](https://github.com/PHPOffice/PhpSpreadsheet/pull/1588) +- Named formula implementation, and improved handling of Defined Names generally [#1535](https://github.com/PHPOffice/PhpSpreadsheet/pull/1535) + - Defined Names are now case-insensitive + - 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 + - fix resolution of relative named range values in the calculation engine; previously all named range values had been treated as absolute. + - Calculation support for named formulae + - Support for nested ranges and formulae (named ranges and formulae that reference other named ranges/formulae) in calculations + - Introduction of a helper to convert address formats between R1C1 and A1 (and the reverse) + - Proper support for both named ranges and named formulae in all appropriate Readers + - **Xlsx** (Previously only simple named ranges were supported) + - **Xls** (Previously only simple named ranges were supported) + - **Gnumeric** (Previously neither named ranges nor formulae were supported) + - **Ods** (Previously neither named ranges nor formulae were supported) + - **Xml** (Previously neither named ranges nor formulae were supported) + - Proper support for named ranges and named formulae in all appropriate Writers + - **Xlsx** (Previously only simple named ranges were supported) + - **Xls** (Previously neither named ranges nor formulae were supported) - Still not supported, but some parser issues resolved that previously failed to differentiate between a defined name and a function name + - **Ods** (Previously neither named ranges nor formulae were supported) ### Changed - Improve Coverage for ODS Reader [#1545](https://github.com/phpoffice/phpspreadsheet/pull/1544) +- Named formula implementation, and improved handling of Defined Names generally [#1535](https://github.com/PHPOffice/PhpSpreadsheet/pull/1535) + - fix resolution of relative named range values in the calculation engine; previously all named range values had been treated as absolute. ### Deprecated -- Nothing. +- **IMPORTANT NOTE:** This Introduces a **BC break** in the handling of named ranges. Previously, a named range cell reference of `B2` would be treated identically to a named range cell reference of `$B2` or `B$2` or `$B$2` because the calculation engine treated then all as absolute references. These changes "fix" that, so the calculation engine now handles relative references in named ranges correctly. + This change that resolves previously incorrect behaviour in the calculation may affect users who have dynamically defined named ranges using relative references when they should have used absolute references. ### Removed @@ -41,8 +63,6 @@ and this project adheres to [Semantic Versioning](https://semver.org). ### Changed -- nothing - ## 1.14.0 - 2020-06-29 ### Added diff --git a/docs/topics/defined-names.md b/docs/topics/defined-names.md new file mode 100644 index 00000000..2dd4fe68 --- /dev/null +++ b/docs/topics/defined-names.md @@ -0,0 +1,593 @@ +# Defined Names + +There are two types of Defined Names in MS Excel and other Spreadsheet formats: Named Ranges and Named Formulae. Between them, they can add a lot of power to your Spreadsheets, but they need to be used correctly. + +Working examples for all the code shown in this document can be found in the `/samples/DefinedNames` folder. + +## Named Ranges + +A Named Range provides a name reference to a cell or a range of cells. You can then reference that cell or cells by that name within a formula. + +As an example, I'll create a simple Calculator that adds Tax to a Price. + +```php +// Set up some basic data +$worksheet + ->setCellValue('A1', 'Tax Rate:') + ->setCellValue('B1', '=19%') + ->setCellValue('A3', 'Net Price:') + ->setCellValue('B3', 12.99) + ->setCellValue('A4', 'Tax:') + ->setCellValue('A5', 'Price including Tax:'); + +// Define named ranges +$spreadsheet->addNamedRange( new \PhpOffice\PhpSpreadsheet\NamedRange('TAX_RATE', $worksheet, '=$B$1') ); +$spreadsheet->addNamedRange( new \PhpOffice\PhpSpreadsheet\NamedRange('PRICE', $worksheet, '=$B$3') ); + +// Reference that defined name in a formula +$worksheet + ->setCellValue('B4', '=PRICE*TAX_RATE') + ->setCellValue('B5', '=PRICE*(1+TAX_RATE)'); + +echo 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; +``` +`/samples/DefinedNames/SimpleNamedRange.php` + +This makes formulae in the generated spreadsheet easier to understand when viewing it them MS Excel. Using these Named Ranges (providing meaningful human-readable names for cells) makes the purpose of the formula immediately clear. We don't need to look for cell `B2` to see what it is, the name tells us. + +And, if the Tax Rate changes to 16%, then we only need to change the value in cell `B1` to the new Tax rate (`=16%`), or if we want to calculate the Tax Charges for a different net price, that will immediately be reflected in all the calculations that reference those Named Ranges. No matter whereabouts in the worksheet I used that Named Range, it always references the value in cell `B1`. + +In fact, because we were required to specify a worksheet when we defined the name, that name is available from any worksheet within the spreadsheet, and always means cell `B2` in this worksheet (but see the notes on Named Range Scope below). + +### Absolute Named Ranges + +In the above example, when I define the Named Range values (e.g. `'=$B$1'`), I used a `$` before both the row and the column. This made the Named Range an Absolute Reference. + +Another example: +```php +// Set up some basic data for a timesheet +$worksheet + ->setCellValue('A1', 'Charge Rate/hour:') + ->setCellValue('B1', '7.50') + ->setCellValue('A3', 'Date') + ->setCellValue('B3', 'Hours') + ->setCellValue('C3', 'Charge'); + +// Define named range using an absolute cell reference +$spreadsheet->addNamedRange( new NamedRange('CHARGE_RATE', $worksheet, '=$B$1') ); + +$workHours = [ + '2020-0-06' => 7.5, + '2020-0-07' => 7.25, + '2020-0-08' => 6.5, + '2020-0-09' => 7.0, + '2020-0-10' => 5.5, +]; + +// Populate the Timesheet +$startRow = 4; +$row = $startRow; +foreach ($workHours as $date => $hours) { + $worksheet + ->setCellValue("A{$row}", $date) + ->setCellValue("B{$row}", $hours) + ->setCellValue("C{$row}", "=B{$row}*CHARGE_RATE"); + $row++; +} +$endRow = $row - 1; + +++$row; +$worksheet + ->setCellValue("B{$row}", "=SUM(B{$startRow}:B{$endRow})") + ->setCellValue("C{$row}", "=SUM(C{$startRow}:C{$endRow})"); + + +echo 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; +``` +`/samples/DefinedNames/AbsoluteNamedRange.php` + +Because the Named Range `CHARGE_RATE` is defined as an Absolute cell reference, then it always references cell `B2` no matter where it is referenced in a formula in the spreadsheet. + +### Relative Named Ranges + +The previous example showed a simple timesheet using an Absolute Reference for the Charge Rate, used to calculate our billed charges to client. + +The use of `B{$row}` in our formula (at least it will appear as an actual cell reference in MS Excel if we save the file and open it) requires a bit of mental agility to remember that column `B` is our hours for that day. Why can't we use another Named Range called something like `HOURS_PER_DAY` to make the formula more easily readable and meaningful. + +But if we used an Absolute Named Range for `HOURS_PER_DAY`, then we'd need a different Named Range for each day (`MONDAY_HOURS_PER_DAY`, `TUESDAY_HOURS_PER_DAY`, etc), and a different formula for each day of the week; if we kept a monthly timesheet, we would have to defined a different Named Range for every day of the month... and that's a lot more trouble than it's worth, and quickly becomes unmanageable. + +This is where Relative Named Ranges are very useful. + +```php +// Set up some basic data for a timesheet +$worksheet + ->setCellValue('A1', 'Charge Rate/hour:') + ->setCellValue('B1', '7.50') + ->setCellValue('A3', 'Date') + ->setCellValue('B3', 'Hours') + ->setCellValue('C3', 'Charge'); + +// Define named ranges +// CHARGE_RATE is an absolute cell reference that always points to cell B1 +$spreadsheet->addNamedRange( new NamedRange('CHARGE_RATE', $worksheet, '=$B$1') ); +// HOURS_PER_DAY is a relative cell reference that always points to column B, but to a cell in the row where it is used +$spreadsheet->addNamedRange( new NamedRange('HOURS_PER_DAY', $worksheet, '=$B1') ); + +$workHours = [ + '2020-0-06' => 7.5, + '2020-0-07' => 7.25, + '2020-0-08' => 6.5, + '2020-0-09' => 7.0, + '2020-0-10' => 5.5, +]; + +// Populate the Timesheet +$startRow = 4; +$row = $startRow; +foreach ($workHours as $date => $hours) { + $worksheet + ->setCellValue("A{$row}", $date) + ->setCellValue("B{$row}", $hours) + ->setCellValue("C{$row}", "=HOURS_PER_DAY*CHARGE_RATE"); + $row++; +} +$endRow = $row - 1; + +++$row; +$worksheet + ->setCellValue("B{$row}", "=SUM(B{$startRow}:B{$endRow})") + ->setCellValue("C{$row}", "=SUM(C{$startRow}:C{$endRow})"); + + +echo 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; +``` +`/samples/DefinedNames/RelativeNamedRange.php` + +The difference in the cell definition for `HOURS_PER_DAY` (`'=$B1'`) is that we have a `$` in front of the column `B`, but not in front of the row number. The `$` makes the column absolute: no matter where in the worksheet we use this name, it always references column `B`. Without a `$`in front of the row number, we make the row number relative, relative to the row where the name appears in a formula, so it effectively replaces the `1` with its own row number when it executes the calculation. + +When it is used in the formula in row 4, then it references cell `B4`, when it appears in row 5, it references cell `B5`, and so on. Using a Relative Named Range, we can use the same Named Range to refer to cells in different rows (and/or different columns), so we can re-use the same Named Range to refer to different cells relative to the row (or column) where we use them. + +--- + +Named Ranges aren't limited to a single cell, but can point to a range of cells. A common use case might be to provide a series of column totals at the bottom of a dataset. Let's take our timesheet, and modify it just slightly to use a Relative column range for that purpose. + +I won't replicate the entire code from the previous example, because I'm only changing a few lines; but we just replace the block: +```php +++$row; +$worksheet + ->setCellValue("B{$row}", "=SUM(B{$startRow}:B{$endRow})") + ->setCellValue("C{$row}", "=SUM(C{$startRow}:C{$endRow})"); +``` +with: +```php +// COLUMN_TOTAL is another relative cell reference that always points to the same range of rows but to cell in the column where it is used +$spreadsheet->addNamedRange( new NamedRange('COLUMN_DATA_VALUES', $worksheet, "=A\${$startRow}:A\${$endRow}") ); + +++$row; +$worksheet + ->setCellValue("B{$row}", "=SUM(COLUMN_DATA_VALUES)") + ->setCellValue("C{$row}", "=SUM(COLUMN_DATA_VALUES)"); +``` +`/samples/DefinedNames/RelativeNamedRange2.php` + +Now that I've specified column as relative in the definition of `COLUMN_DATA_VALUES` with an address of column `A`, and the rows are absolute. When the same Relative Named Range is used in column `B`,it references cells in column `B` rather than `A`; and when it is used in column `C`, it references cells in column `C`. + +While we still have a piece of code (`"=A\${$startRow}:A\${$endRow}"`) that isn't easily human-readable, when we open the generated spreadsheet in MS Excel, the displayed formula in for the cells for the totals is immediately understandable. + +### Named Range Scope + +Whenever we define a Named Range, we are required to specify a worksheet, and that name is then available from any worksheet within the spreadsheet, and always means that cell or cell range in the specified worksheet. + +```php +// Set up some basic data for a timesheet +$worksheet + ->setCellValue('A1', 'Charge Rate/hour:') + ->setCellValue('B1', '7.50'); + +// Define a global named range on the first worksheet for our Charge Rate +// CHARGE_RATE is an absolute cell reference that always points to cell B1 +// Because it is defined globally, it will still be usable from any worksheet in the spreadsheet +$spreadsheet->addNamedRange( new NamedRange('CHARGE_RATE', $worksheet, '=$B$1') ); + +// Create a second worksheet as our client timesheet +$worksheet = $spreadsheet->addSheet(new \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet($spreadsheet, 'Client Timesheet')); + +// Define named ranges +// HOURS_PER_DAY is a relative cell reference that always points to column B, but to a cell in the row where it is used +$spreadsheet->addNamedRange( new NamedRange('HOURS_PER_DAY', $worksheet, '=$B1') ); + +// Set up some basic data for a timesheet +$worksheet + ->setCellValue('A1', 'Date') + ->setCellValue('B1', 'Hours') + ->setCellValue('C1', 'Charge'); + +$workHours = [ + '2020-0-06' => 7.5, + '2020-0-07' => 7.25, + '2020-0-08' => 6.5, + '2020-0-09' => 7.0, + '2020-0-10' => 5.5, +]; + +// Populate the Timesheet +$startRow = 2; +$row = $startRow; +foreach ($workHours as $date => $hours) { + $worksheet + ->setCellValue("A{$row}", $date) + ->setCellValue("B{$row}", $hours) + ->setCellValue("C{$row}", "=HOURS_PER_DAY*CHARGE_RATE"); + $row++; +} +$endRow = $row - 1; + +// COLUMN_TOTAL is another relative cell reference that always points to the same range of rows but to cell in the column where it is used +$spreadsheet->addNamedRange( new NamedRange('COLUMN_DATA_VALUES', $worksheet, "=A\${$startRow}:A\${$endRow}") ); + +++$row; +$worksheet + ->setCellValue("B{$row}", "=SUM(COLUMN_DATA_VALUES)") + ->setCellValue("C{$row}", "=SUM(COLUMN_DATA_VALUES)"); + +echo 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; +``` +`/samples/DefinedNames/ScopedNamedRange.php` + +Even though `CHARGE_RATE` references a cell on a different worksheet, because is set as global (the default) it is accessible from any worksheet in the spreadsheet. so when we reference it in formulae on the second timesheet worksheet, we are able to access the value from that first worksheet and use it in our calculations. + +--- + +However, a Named Range can be locally scoped so that it is only available when referenced from a specific worksheet, or it can be globally scoped. This means that you can use the same Named Range name with different values on different worksheets. + +Building further on our timesheet, perhaps we use a different worksheet for each client, and we use the same hourly rate when billing most of our clients; but for one particular client (perhaps doing work for a a friend) we use a lower rate. + +```php +$clients = [ + 'Client #1 - Full Hourly Rate' => [ + '2020-0-06' => 2.5, + '2020-0-07' => 2.25, + '2020-0-08' => 6.0, + '2020-0-09' => 3.0, + '2020-0-10' => 2.25, + ], + 'Client #2 - Full Hourly Rate' => [ + '2020-0-06' => 1.5, + '2020-0-07' => 2.75, + '2020-0-08' => 0.0, + '2020-0-09' => 4.5, + '2020-0-10' => 3.5, + ], + 'Client #3 - Reduced Hourly Rate' => [ + '2020-0-06' => 3.5, + '2020-0-07' => 2.5, + '2020-0-08' => 1.5, + '2020-0-09' => 0.0, + '2020-0-10' => 1.25, + ], +]; + +foreach ($clients as $clientName => $workHours) { + $worksheet = $spreadsheet->addSheet(new \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet($spreadsheet, $clientName)); + + // Set up some basic data for a timesheet + $worksheet + ->setCellValue('A1', 'Charge Rate/hour:') + ->setCellValue('B1', '7.50') + ->setCellValue('A3', 'Date') + ->setCellValue('B3', 'Hours') + ->setCellValue('C3', 'Charge'); + ; + + // Define named ranges + // CHARGE_RATE is an absolute cell reference that always points to cell B1 + $spreadsheet->addNamedRange( new NamedRange('CHARGE_RATE', $worksheet, '=$B$1', true) ); + // HOURS_PER_DAY is a relative cell reference that always points to column B, but to a cell in the row where it is used + $spreadsheet->addNamedRange( new NamedRange('HOURS_PER_DAY', $worksheet, '=$B1', true) ); + + // Populate the Timesheet + $startRow = 4; + $row = $startRow; + foreach ($workHours as $date => $hours) { + $worksheet + ->setCellValue("A{$row}", $date) + ->setCellValue("B{$row}", $hours) + ->setCellValue("C{$row}", "=HOURS_PER_DAY*CHARGE_RATE"); + $row++; + } + $endRow = $row - 1; + + // COLUMN_TOTAL is another relative cell reference that always points to the same range of rows but to cell in the column where it is used + $spreadsheet->addNamedRange( new NamedRange('COLUMN_TOTAL', $worksheet, "=A\${$startRow}:A\${$endRow}", true) ); + + ++$row; + $worksheet + ->setCellValue("B{$row}", "=SUM(COLUMN_TOTAL)") + ->setCellValue("C{$row}", "=SUM(COLUMN_TOTAL)"); +} +$spreadsheet->removeSheetByIndex(0); + +// Set the reduced charge rate for our special client +$worksheet + ->setCellValue("B1", 4.5); + +foreach ($spreadsheet->getAllSheets() as $worksheet) { + echo 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; +} +``` +`/samples/DefinedNames/ScopedNamedRange2.php` + +Now we are creating three worksheets for each of three different clients. Because each Named Range is linked to a worksheet, we need to create three sets of Named Ranges, so that we don't simply reference the cells on only one of the worksheets; but because we are locally scoping them (note the extra boolean argument used when we define the Named Ranges) we can use the same names on each worksheet, and they will reference the correct cells when we use them in our formulae on that worksheet. + +When Named Ranges are being evaluated, the logic looks first to see if there is a locally scoped Named Range defined for the current worksheet. If there is, then that is the Named Range that will be used in the calculation. If no locally scoped Named Range with that name is found, the logic then looks to see if there is a globally scoped Named Range definition, and will use that if it is found. If no Named Range of the required name is found scoped to the current worksheet, or globally scoped, then a `#NAME` error will be returned. + +## Named Formulae + +A Named Formula is a stored formula, or part of a formula, that can be referenced in cells by name, and re-used in many different places within the spreadsheet. + +As an example, I'll modify the simple Tax Calculator that I created as my example for Named Ranges. + +```php +// Add some Named Formulae +// The first to store our tax rate +$spreadsheet->addNamedFormula(new NamedFormula('TAX_RATE', $worksheet, '=19%')); +// The second to calculate the Tax on a Price value (Note that `PRICE` is defined later as a Named Range) +$spreadsheet->addNamedFormula(new NamedFormula('TAX', $worksheet, '=PRICE*TAX_RATE')); + +// Set up some basic data +$worksheet + ->setCellValue('A1', 'Tax Rate:') + ->setCellValue('B1', '=TAX_RATE') + ->setCellValue('A3', 'Net Price:') + ->setCellValue('B3', 19.99) + ->setCellValue('A4', 'Tax:') + ->setCellValue('A5', 'Price including Tax:'); + +// Define a named range that we can use in our formulae +$spreadsheet->addNamedRange(new NamedRange('PRICE', $worksheet, '=$B$3')); + +// Reference the defined formulae in worksheet formulae +$worksheet + ->setCellValue('B4', '=TAX') + ->setCellValue('B5', '=PRICE+TAX'); + +echo 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; +``` +`/samples/DefinedNames/SimpleNamedFormula.php` + +There are a few points to note here: + +Firstly. we are actually storing the tax rate in a named formula (`TAX_RATE`) rather than as a cell value. When we display the tax rate in cell `B1`, we are really storing an instruction for MS Excel to evaluate the formula and display the result in that cell. + +Then we are using a Named Formula `TAX` that references both another Named Formula (`TAX_RATE`) and a Named Range (`PRICE`) and executes a calculation using them both (`PRICE * TAX_RATE`). + +Finally, we are using the formula `TAX` in two different contexts. Once to display the tax value (in cell `B4`); and a second time as part of another formula (`PRICE + TAX`) in cell `B5`. + +--- + +Named Formulae aren't just restricted tosimple mathematics, but can include MS EXcel functions as well to provide a lot of flexibility; and they can reference values on other worksheets. + +```php +$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'); + +function setYearlyData(Worksheet $worksheet, string $year, $yearlyData, ?string $title = null) { + // 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(); + + echo "Growth for {$month} is {$growth}, with a Profit Growth of {$profitGrowth}", PHP_EOL; +} +``` +`/samples/DefinedNames/CrossWorksheetNamedFormula.php` + +Here we're creating two Named Formulae that both use the `IF()` function, and that compare values on two different worksheets, and calculate the percentage difference between the two. We're also creating a Named Formula that uses the `OFFSET()` function to reference the cell immediately above the current Relative cell reference. + +## Combining Named Ranges and Formulae + +For a slightly more complex example combining Named Ranges and Named Formulae, we can build on our client timesheet. + +```php +// Set up some basic data for a timesheet +$worksheet + ->setCellValue('A1', 'Charge Rate/hour:') + ->setCellValue('B1', '7.50') + ->setCellValue('A3', 'Date') + ->setCellValue('B3', 'Hours') + ->setCellValue('C3', 'Charge'); + +// Define named ranges +// CHARGE_RATE is an absolute cell reference that always points to cell B1 +$spreadsheet->addNamedRange(new NamedRange('CHARGE_RATE', $worksheet, '=$B$1')); +// HOURS_PER_DAY is a relative cell reference that always points to column B, but to a cell in the row where it is used +$spreadsheet->addNamedRange(new NamedRange('HOURS_PER_DAY', $worksheet, '=$B1')); +// Set up the formula for calculating the daily charge +$spreadsheet->addNamedFormula(new NamedFormula('DAILY_CHARGE', null, '=HOURS_PER_DAY*CHARGE_RATE')); +// Set up the formula for calculating the column totals +$spreadsheet->addNamedFormula(new NamedFormula('COLUMN_TOTALS', null, '=SUM(COLUMN_DATA_VALUES)')); + + +$workHours = [ + '2020-0-06' => 7.5, + '2020-0-07' => 7.25, + '2020-0-08' => 6.5, + '2020-0-09' => 7.0, + '2020-0-10' => 5.5, +]; + +// Populate the Timesheet +$startRow = 4; +$row = $startRow; +foreach ($workHours as $date => $hours) { + $worksheet + ->setCellValue("A{$row}", $date) + ->setCellValue("B{$row}", $hours) + ->setCellValue("C{$row}", '=DAILY_CHARGE'); + ++$row; +} +$endRow = $row - 1; + +// COLUMN_TOTAL is another relative cell reference that always points to the same range of rows but to cell in the column where it is used +$spreadsheet->addNamedRange(new NamedRange('COLUMN_DATA_VALUES', $worksheet, "=A\${$startRow}:A\${$endRow}")); + +++$row; +$worksheet + ->setCellValue("B{$row}", '=COLUMN_TOTALS') + ->setCellValue("C{$row}", '=COLUMN_TOTALS'); + +echo 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; +``` +`/samples/DefinedNames/NamedFormulaeAndRanges.php` + +The main point to notice in this example is that you must specify a Worksheet for Named Ranges, but that it isn't required for Named Formulae; in fact, specifying a Worksheet for named Formulae can lead to MS Excel errors when a saved file is opened. Generally, it is far safer to specify a null Worksheet value when creating a Named Formula, unless it references cell values explicitly, or you wish to scope it to that Worksheet. + +It also doesn't matter what order we define our Named Ranges and Formulae, even when some are dependent on others: this only matters when we try to use them in a cell calculation, or when we save the file; and as long as every Defined Name has been defined at that point, then it isn't important. In this case, we couldn't define `COLUMN_DATA_VALUES` until we new the range of rows that it needed to contain; but we could still define the `COLUMN_TOTALS` formula before that. + +## Additional Comments + +### Helper + +In all the examples so far, we have explicitly used the `NamedRange` and `NamedFormula` classes, and the Spreadsheet's `addNamedRange()` and `addNamedFormula()` methods, e.g. +```php +$spreadsheet->addNamedRange(new NamedRange('HOURS_PER_DAY', $worksheet, '=$B1')); +``` +However, this can lead to errors if we accidentally set a formula value for a Named Range, or a range value for a Named Formula. + +As a helper, the DefinedName class provides a static method that can identify whether the value expression is a Range or a Formula, and instantiate the appropriate class. +```php +$this->spreadsheet->addDefinedName( + DefinedName::createInstance('FOO', $this->spreadsheet->getSheetByName('Sheet #2'), '=16%', true) +); +``` + +### Naming Names + +The names that you assign to Defined Name must follow the following set of rules: + - The first character of a name must be one of the following characters: + - letter (including UTF-8 letters) + - underscore (`_`) + - Remaining characters in the name can be + - letters (including UTF-8 letters) + - numbers (including UTF-8 numbers) + - periods (`.`) + - underscore characters (`_`) + - The following are not allowed: + - Space characters are not allowed as part of a name. + - Names can't look like cell addresses, such as A35 or R2C2 + - Names are not case sensitive. For example, `North` and `NORTH` are treated as the same name. + +### Limitations + +PHPSpreadsheet doesn't yet fully validate the names that you use, so it is possible to create a spreadsheet in PHPSpreadsheet that will break when you save and try to open it in MS Excel; or that will break PHPSpreadsheet when they are referenced in a cell. +So please be sensible when creating names, and follow the rules listed above. + +--- + +There is nothing to stop you creating a Defined Name that matches an existing Function name +```php +$spreadsheet->addNamedFormula(new NamedFormula('SUM', $worksheet, '=SUM(A1:E5)')); +``` +And this will work without problems in MS Excel. However, it is not guaranteed to work correctly in PHPSpreadsheet; and will certainly cause confusion for anybody reading it; so it is not recommended. Names exist to give clarity to the person reading the spreadsheet, and a cell containing `=SUM` is even harder to understand (what is it the sum of?) than a cell containing `=SUM(B4:B8)`. Use names that provide meaning, like `SUM_OF_WORKED_HOURS`. + +--- + +You cannot have a Named Range and a Named Formula with the same name, unless they are differently scoped. + +--- + +MS Excel uses some "special tricks" to simulate Relative Named Ranges where the row or column comes before the current row or column, useful if you want to get column totals that don't include the current cell. These "tricks" aren't supported by PHPSpreadsheet, but can be simulated using the `OFFSET()` function in a Named Formula. +In our `RelativeNamedRange2.php` example, we explicitly created the `COLUMN_DATA_VALUES` Named Range using only the rows that we knew should be included, so that we weren't including the current row (where we were displaying the total) and creating a cyclic reference: +```php +// COLUMN_TOTAL is another relative cell reference that always points to the same range of rows but to cell in the column where it is used +$spreadsheet->addNamedRange(new NamedRange('COLUMN_DATA_VALUES', $worksheet, "=A\${$startRow}:A\${$endRow}")); +``` +We could instead have created a Named Function using `OFFSET()` to specify just the start row, and offset the end row by -1 row: +```php +// COLUMN_TOTAL is another relative cell reference that always points to the same range of rows but to cell in the column where it is used +// To avoid including the current row,or having to hard-code the range itself (as we did in the previous example) +// we wrap it in a named formula using the OFFSET() function +$spreadsheet->addNamedFormula(new NamedFormula('COLUMN_DATA_VALUES', $worksheet, "=OFFSET(A\$4:A1, -1, 0)")); +``` +as demonstrated in example `RelativeNamedRangeAsFunction.php`. diff --git a/docs/topics/recipes.md b/docs/topics/recipes.md index f85576a2..86448301 100644 --- a/docs/topics/recipes.md +++ b/docs/topics/recipes.md @@ -1368,14 +1368,73 @@ $spreadsheet->getActiveSheet()->setCellValue('B1', 'Maarten'); $spreadsheet->getActiveSheet()->setCellValue('B2', 'Balliauw'); // Define named ranges -$spreadsheet->addNamedRange( new \PhpOffice\PhpSpreadsheet\NamedRange('PersonFN', $spreadsheet->getActiveSheet(), 'B1') ); -$spreadsheet->addNamedRange( new \PhpOffice\PhpSpreadsheet\NamedRange('PersonLN', $spreadsheet->getActiveSheet(), 'B2') ); +$spreadsheet->addNamedRange( new \PhpOffice\PhpSpreadsheet\NamedRange('PersonFN', $spreadsheet->getActiveSheet(), '$B$1')); +$spreadsheet->addNamedRange( new \PhpOffice\PhpSpreadsheet\NamedRange('PersonLN', $spreadsheet->getActiveSheet(), '$B$2')); ``` Optionally, a fourth parameter can be passed defining the named range local (i.e. only usable on the current worksheet). Named ranges are global by default. +## Define a named formula + +In addition to named ranges, PhpSpreadsheet also supports the definition of named formulae. These can be +defined using the following code: + +```php +// Add some data +$spreadsheet->setActiveSheetIndex(0); +$worksheet = $spreadsheet->getActiveSheet(); +$worksheet + ->setCellValue('A1', 'Product') + ->setCellValue('B1', 'Quantity') + ->setCellValue('C1', 'Unit Price') + ->setCellValue('D1', 'Price') + ->setCellValue('E1', 'VAT') + ->setCellValue('F1', 'Total'); + +// Define named formula +$spreadsheet->addNamedFormula( new \PhpOffice\PhpSpreadsheet\NamedFormula('GERMAN_VAT_RATE', $worksheet, '=16.0%')); +$spreadsheet->addNamedFormula( new \PhpOffice\PhpSpreadsheet\NamedFormula('CALCULATED_PRICE', $worksheet, '=$B1*$C1')); +$spreadsheet->addNamedFormula( new \PhpOffice\PhpSpreadsheet\NamedFormula('GERMAN_VAT', $worksheet, '=$D1*GERMAN_VAT_RATE')); +$spreadsheet->addNamedFormula( new \PhpOffice\PhpSpreadsheet\NamedFormula('TOTAL_INCLUDING_VAT', $worksheet, '=$D1+$E1')); + +$worksheet + ->setCellValue('A2', 'Advanced Web Application Architecture') + ->setCellValue('B2', 2) + ->setCellValue('C2', 23.0) + ->setCellValue('D2', '=CALCULATED_PRICE') + ->setCellValue('E2', '=GERMAN_VAT') + ->setCellValue('F2', '=TOTAL_INCLUDING_VAT'); +$spreadsheet->getActiveSheet() + ->setCellValue('A3', 'Object Design Style Guide') + ->setCellValue('B3', 5) + ->setCellValue('C3', 12.0) + ->setCellValue('D3', '=CALCULATED_PRICE') + ->setCellValue('E3', '=GERMAN_VAT') + ->setCellValue('F3', '=TOTAL_INCLUDING_VAT'); +$spreadsheet->getActiveSheet() + ->setCellValue('A4', 'PHP For the Web') + ->setCellValue('B4', 3) + ->setCellValue('C4', 10.0) + ->setCellValue('D4', '=CALCULATED_PRICE') + ->setCellValue('E4', '=GERMAN_VAT') + ->setCellValue('F4', '=TOTAL_INCLUDING_VAT'); + +// Use a relative named range to provide the totals for rows 2-4 +$spreadsheet->addNamedRange( new \PhpOffice\PhpSpreadsheet\NamedRange('COLUMN_TOTAL', $worksheet, '=A$2:A$4') ); + +$spreadsheet->getActiveSheet() + ->setCellValue('B6', '=SUBTOTAL(109,COLUMN_TOTAL)') + ->setCellValue('D6', '=SUBTOTAL(109,COLUMN_TOTAL)') + ->setCellValue('E6', '=SUBTOTAL(109,COLUMN_TOTAL)') + ->setCellValue('F6', '=SUBTOTAL(109,COLUMN_TOTAL)'); +``` + +As with named ranges, an optional fourth parameter can be passed defining the named formula +scope as local (i.e. only usable on the specified worksheet). Otherwise, named formulae are +global by default. + ## Redirect output to a client's web browser Sometimes, one really wants to output a file to a client''s browser, diff --git a/samples/DefinedNames/AbsoluteNamedRange.php b/samples/DefinedNames/AbsoluteNamedRange.php new file mode 100644 index 00000000..450de667 --- /dev/null +++ b/samples/DefinedNames/AbsoluteNamedRange.php @@ -0,0 +1,63 @@ +setActiveSheetIndex(0); + +// Set up some basic data for a timesheet +$worksheet + ->setCellValue('A1', 'Charge Rate/hour:') + ->setCellValue('B1', '7.50') + ->setCellValue('A3', 'Date') + ->setCellValue('B3', 'Hours') + ->setCellValue('C3', 'Charge'); + +// Define named range using an absolute cell reference +$spreadsheet->addNamedRange(new NamedRange('CHARGE_RATE', $worksheet, '=$B$1')); + +$workHours = [ + '2020-0-06' => 7.5, + '2020-0-07' => 7.25, + '2020-0-08' => 6.5, + '2020-0-09' => 7.0, + '2020-0-10' => 5.5, +]; + +// Populate the Timesheet +$startRow = 4; +$row = $startRow; +foreach ($workHours as $date => $hours) { + $worksheet + ->setCellValue("A{$row}", $date) + ->setCellValue("B{$row}", $hours) + ->setCellValue("C{$row}", "=B{$row}*CHARGE_RATE"); + ++$row; +} +$endRow = $row - 1; + +++$row; +$worksheet + ->setCellValue("B{$row}", "=SUM(B{$startRow}:B{$endRow})") + ->setCellValue("C{$row}", "=SUM(C{$startRow}:C{$endRow})"); + +echo 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); diff --git a/samples/DefinedNames/CrossWorksheetNamedFormula.php b/samples/DefinedNames/CrossWorksheetNamedFormula.php new file mode 100644 index 00000000..b441e0f7 --- /dev/null +++ b/samples/DefinedNames/CrossWorksheetNamedFormula.php @@ -0,0 +1,99 @@ +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'); + +function setYearlyData(Worksheet $worksheet, string $year, $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(); + + echo "Growth for {$month} is {$growth}, with a Profit Growth of {$profitGrowth}", PHP_EOL; +} + +$outputFileName = 'CrossWorksheetNamedFormula.xlsx'; +$writer = IOFactory::createWriter($spreadsheet, 'Xlsx'); +$writer->save($outputFileName); diff --git a/samples/DefinedNames/NamedFormulaeAndRanges.php b/samples/DefinedNames/NamedFormulaeAndRanges.php new file mode 100644 index 00000000..b3f94efc --- /dev/null +++ b/samples/DefinedNames/NamedFormulaeAndRanges.php @@ -0,0 +1,74 @@ +setActiveSheetIndex(0); + +// Set up some basic data for a timesheet +$worksheet + ->setCellValue('A1', 'Charge Rate/hour:') + ->setCellValue('B1', '7.50') + ->setCellValue('A3', 'Date') + ->setCellValue('B3', 'Hours') + ->setCellValue('C3', 'Charge'); + +// Define named ranges +// CHARGE_RATE is an absolute cell reference that always points to cell B1 +$spreadsheet->addNamedRange(new NamedRange('CHARGE_RATE', $worksheet, '=$B$1')); +// HOURS_PER_DAY is a relative cell reference that always points to column B, but to a cell in the row where it is used +$spreadsheet->addNamedRange(new NamedRange('HOURS_PER_DAY', $worksheet, '=$B1')); +// Set up the formula for calculating the daily charge +$spreadsheet->addNamedFormula(new NamedFormula('DAILY_CHARGE', null, '=HOURS_PER_DAY*CHARGE_RATE')); +// Set up the formula for calculating the column totals +$spreadsheet->addNamedFormula(new NamedFormula('COLUMN_TOTALS', null, '=SUM(COLUMN_DATA_VALUES)')); + +$workHours = [ + '2020-0-06' => 7.5, + '2020-0-07' => 7.25, + '2020-0-08' => 6.5, + '2020-0-09' => 7.0, + '2020-0-10' => 5.5, +]; + +// Populate the Timesheet +$startRow = 4; +$row = $startRow; +foreach ($workHours as $date => $hours) { + $worksheet + ->setCellValue("A{$row}", $date) + ->setCellValue("B{$row}", $hours) + ->setCellValue("C{$row}", '=DAILY_CHARGE'); + ++$row; +} +$endRow = $row - 1; + +// COLUMN_TOTAL is another relative cell reference that always points to the same range of rows but to cell in the column where it is used +$spreadsheet->addNamedRange(new NamedRange('COLUMN_DATA_VALUES', $worksheet, "=A\${$startRow}:A\${$endRow}")); + +++$row; +$worksheet + ->setCellValue("B{$row}", '=COLUMN_TOTALS') + ->setCellValue("C{$row}", '=COLUMN_TOTALS'); + +echo 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); diff --git a/samples/DefinedNames/RelativeNamedRange.php b/samples/DefinedNames/RelativeNamedRange.php new file mode 100644 index 00000000..1f712017 --- /dev/null +++ b/samples/DefinedNames/RelativeNamedRange.php @@ -0,0 +1,66 @@ +setActiveSheetIndex(0); + +// Set up some basic data for a timesheet +$worksheet + ->setCellValue('A1', 'Charge Rate/hour:') + ->setCellValue('B1', '7.50') + ->setCellValue('A3', 'Date') + ->setCellValue('B3', 'Hours') + ->setCellValue('C3', 'Charge'); + +// Define named ranges +// CHARGE_RATE is an absolute cell reference that always points to cell B1 +$spreadsheet->addNamedRange(new NamedRange('CHARGE_RATE', $worksheet, '=$B$1')); +// HOURS_PER_DAY is a relative cell reference that always points to column B, but to a cell in the row where it is used +$spreadsheet->addNamedRange(new NamedRange('HOURS_PER_DAY', $worksheet, '=$B1')); + +$workHours = [ + '2020-0-06' => 7.5, + '2020-0-07' => 7.25, + '2020-0-08' => 6.5, + '2020-0-09' => 7.0, + '2020-0-10' => 5.5, +]; + +// Populate the Timesheet +$startRow = 4; +$row = $startRow; +foreach ($workHours as $date => $hours) { + $worksheet + ->setCellValue("A{$row}", $date) + ->setCellValue("B{$row}", $hours) + ->setCellValue("C{$row}", '=HOURS_PER_DAY*CHARGE_RATE'); + ++$row; +} +$endRow = $row - 1; + +++$row; +$worksheet + ->setCellValue("B{$row}", "=SUM(B{$startRow}:B{$endRow})") + ->setCellValue("C{$row}", "=SUM(C{$startRow}:C{$endRow})"); + +echo 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); diff --git a/samples/DefinedNames/RelativeNamedRange2.php b/samples/DefinedNames/RelativeNamedRange2.php new file mode 100644 index 00000000..b6a6f8d1 --- /dev/null +++ b/samples/DefinedNames/RelativeNamedRange2.php @@ -0,0 +1,69 @@ +setActiveSheetIndex(0); + +// Set up some basic data for a timesheet +$worksheet + ->setCellValue('A1', 'Charge Rate/hour:') + ->setCellValue('B1', '7.50') + ->setCellValue('A3', 'Date') + ->setCellValue('B3', 'Hours') + ->setCellValue('C3', 'Charge'); + +// Define named ranges +// CHARGE_RATE is an absolute cell reference that always points to cell B1 +$spreadsheet->addNamedRange(new NamedRange('CHARGE_RATE', $worksheet, '=$B$1')); +// HOURS_PER_DAY is a relative cell reference that always points to column B, but to a cell in the row where it is used +$spreadsheet->addNamedRange(new NamedRange('HOURS_PER_DAY', $worksheet, '=$B1')); + +$workHours = [ + '2020-0-06' => 7.5, + '2020-0-07' => 7.25, + '2020-0-08' => 6.5, + '2020-0-09' => 7.0, + '2020-0-10' => 5.5, +]; + +// Populate the Timesheet +$startRow = 4; +$row = $startRow; +foreach ($workHours as $date => $hours) { + $worksheet + ->setCellValue("A{$row}", $date) + ->setCellValue("B{$row}", $hours) + ->setCellValue("C{$row}", '=HOURS_PER_DAY*CHARGE_RATE'); + ++$row; +} +$endRow = $row - 1; + +// COLUMN_TOTAL is another relative cell reference that always points to the same range of rows but to cell in the column where it is used +$spreadsheet->addNamedRange(new NamedRange('COLUMN_DATA_VALUES', $worksheet, "=A\${$startRow}:A\${$endRow}")); + +++$row; +$worksheet + ->setCellValue("B{$row}", '=SUM(COLUMN_DATA_VALUES)') + ->setCellValue("C{$row}", '=SUM(COLUMN_DATA_VALUES)'); + +echo 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); diff --git a/samples/DefinedNames/RelativeNamedRangeAsFunction.php b/samples/DefinedNames/RelativeNamedRangeAsFunction.php new file mode 100644 index 00000000..527cd43b --- /dev/null +++ b/samples/DefinedNames/RelativeNamedRangeAsFunction.php @@ -0,0 +1,72 @@ +setActiveSheetIndex(0); + +// Set up some basic data for a timesheet +$worksheet + ->setCellValue('A1', 'Charge Rate/hour:') + ->setCellValue('B1', '7.50') + ->setCellValue('A3', 'Date') + ->setCellValue('B3', 'Hours') + ->setCellValue('C3', 'Charge'); + +// Define named ranges +// CHARGE_RATE is an absolute cell reference that always points to cell B1 +$spreadsheet->addNamedRange(new NamedRange('CHARGE_RATE', $worksheet, '=$B$1')); +// HOURS_PER_DAY is a relative cell reference that always points to column B, but to a cell in the row where it is used +$spreadsheet->addNamedRange(new NamedRange('HOURS_PER_DAY', $worksheet, '=$B1')); + +$workHours = [ + '2020-0-06' => 7.5, + '2020-0-07' => 7.25, + '2020-0-08' => 6.5, + '2020-0-09' => 7.0, + '2020-0-10' => 5.5, +]; + +// Populate the Timesheet +$startRow = 4; +$row = $startRow; +foreach ($workHours as $date => $hours) { + $worksheet + ->setCellValue("A{$row}", $date) + ->setCellValue("B{$row}", $hours) + ->setCellValue("C{$row}", '=HOURS_PER_DAY*CHARGE_RATE'); + ++$row; +} +$endRow = $row - 1; + +// COLUMN_TOTAL is another relative cell reference that always points to the same range of rows but to cell in the column where it is used +// To avoid including the current row,or having to hard-code the range itself (as we did in the previous example) +// we wrap it in a named formula using the OFFSET() function +$spreadsheet->addNamedFormula(new NamedFormula('COLUMN_DATA_VALUES', $worksheet, '=OFFSET(A$4:A1, -1, 0)')); + +++$row; +$worksheet + ->setCellValue("B{$row}", '=SUM(COLUMN_DATA_VALUES)') + ->setCellValue("C{$row}", '=SUM(COLUMN_DATA_VALUES)'); + +echo 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); diff --git a/samples/DefinedNames/ScopedNamedRange.php b/samples/DefinedNames/ScopedNamedRange.php new file mode 100644 index 00000000..5ea7e48a --- /dev/null +++ b/samples/DefinedNames/ScopedNamedRange.php @@ -0,0 +1,81 @@ +setActiveSheetIndex(0); +$worksheet->setTitle('Base Data'); + +// Set up some basic data for a timesheet +$worksheet + ->setCellValue('A1', 'Charge Rate/hour:') + ->setCellValue('B1', '7.50'); + +// Define a global named range on the first worksheet for our Charge Rate +// CHARGE_RATE is an absolute cell reference that always points to cell B1 +// Because it is defined globally, it will still be usable from any worksheet in the spreadsheet +$spreadsheet->addNamedRange(new NamedRange('CHARGE_RATE', $worksheet, '=$B$1')); + +// Create a second worksheet as our client timesheet +$worksheet = $spreadsheet->addSheet(new \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet($spreadsheet, 'Client Timesheet')); + +// Define named ranges +// HOURS_PER_DAY is a relative cell reference that always points to column B, but to a cell in the row where it is used +$spreadsheet->addNamedRange(new NamedRange('HOURS_PER_DAY', $worksheet, '=$B1')); + +// Set up some basic data for a timesheet +$worksheet + ->setCellValue('A1', 'Date') + ->setCellValue('B1', 'Hours') + ->setCellValue('C1', 'Charge'); + +$workHours = [ + '2020-0-06' => 7.5, + '2020-0-07' => 7.25, + '2020-0-08' => 6.5, + '2020-0-09' => 7.0, + '2020-0-10' => 5.5, +]; + +// Populate the Timesheet +$startRow = 2; +$row = $startRow; +foreach ($workHours as $date => $hours) { + $worksheet + ->setCellValue("A{$row}", $date) + ->setCellValue("B{$row}", $hours) + ->setCellValue("C{$row}", '=HOURS_PER_DAY*CHARGE_RATE'); + ++$row; +} +$endRow = $row - 1; + +// COLUMN_TOTAL is another relative cell reference that always points to the same range of rows but to cell in the column where it is used +$spreadsheet->addNamedRange(new NamedRange('COLUMN_DATA_VALUES', $worksheet, "=A\${$startRow}:A\${$endRow}")); + +++$row; +$worksheet + ->setCellValue("B{$row}", '=SUM(COLUMN_DATA_VALUES)') + ->setCellValue("C{$row}", '=SUM(COLUMN_DATA_VALUES)'); + +echo 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); diff --git a/samples/DefinedNames/ScopedNamedRange2.php b/samples/DefinedNames/ScopedNamedRange2.php new file mode 100644 index 00000000..8e610418 --- /dev/null +++ b/samples/DefinedNames/ScopedNamedRange2.php @@ -0,0 +1,98 @@ +setActiveSheetIndex(0); + +$clients = [ + 'Client #1 - Full Hourly Rate' => [ + '2020-0-06' => 2.5, + '2020-0-07' => 2.25, + '2020-0-08' => 6.0, + '2020-0-09' => 3.0, + '2020-0-10' => 2.25, + ], + 'Client #2 - Full Hourly Rate' => [ + '2020-0-06' => 1.5, + '2020-0-07' => 2.75, + '2020-0-08' => 0.0, + '2020-0-09' => 4.5, + '2020-0-10' => 3.5, + ], + 'Client #3 - Reduced Hourly Rate' => [ + '2020-0-06' => 3.5, + '2020-0-07' => 2.5, + '2020-0-08' => 1.5, + '2020-0-09' => 0.0, + '2020-0-10' => 1.25, + ], +]; + +foreach ($clients as $clientName => $workHours) { + $worksheet = $spreadsheet->addSheet(new \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet($spreadsheet, $clientName)); + + // Set up some basic data for a timesheet + $worksheet + ->setCellValue('A1', 'Charge Rate/hour:') + ->setCellValue('B1', '7.50') + ->setCellValue('A3', 'Date') + ->setCellValue('B3', 'Hours') + ->setCellValue('C3', 'Charge'); + + // Define named ranges + // CHARGE_RATE is an absolute cell reference that always points to cell B1 + $spreadsheet->addNamedRange(new NamedRange('CHARGE_RATE', $worksheet, '=$B$1', true)); + // HOURS_PER_DAY is a relative cell reference that always points to column B, but to a cell in the row where it is used + $spreadsheet->addNamedRange(new NamedRange('HOURS_PER_DAY', $worksheet, '=$B1', true)); + + // Populate the Timesheet + $startRow = 4; + $row = $startRow; + foreach ($workHours as $date => $hours) { + $worksheet + ->setCellValue("A{$row}", $date) + ->setCellValue("B{$row}", $hours) + ->setCellValue("C{$row}", '=HOURS_PER_DAY*CHARGE_RATE'); + ++$row; + } + $endRow = $row - 1; + + // COLUMN_TOTAL is another relative cell reference that always points to the same range of rows but to cell in the column where it is used + $spreadsheet->addNamedRange(new NamedRange('COLUMN_TOTAL', $worksheet, "=A\${$startRow}:A\${$endRow}", true)); + + ++$row; + $worksheet + ->setCellValue("B{$row}", '=SUM(COLUMN_TOTAL)') + ->setCellValue("C{$row}", '=SUM(COLUMN_TOTAL)'); +} +$spreadsheet->removeSheetByIndex(0); + +// Set the reduced charge rate for our special client +$worksheet + ->setCellValue('B1', 4.5); + +foreach ($spreadsheet->getAllSheets() as $worksheet) { + echo 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); diff --git a/samples/DefinedNames/SimpleNamedFormula.php b/samples/DefinedNames/SimpleNamedFormula.php new file mode 100644 index 00000000..8e0fc972 --- /dev/null +++ b/samples/DefinedNames/SimpleNamedFormula.php @@ -0,0 +1,52 @@ +setActiveSheetIndex(0); + +// Add some Named Formulae +// The first to store our tax rate +$spreadsheet->addNamedFormula(new NamedFormula('TAX_RATE', $worksheet, '=19%')); +// The second to calculate the Tax on a Price value (Note that `PRICE` is defined later as a Named Range) +$spreadsheet->addNamedFormula(new NamedFormula('TAX', $worksheet, '=PRICE*TAX_RATE')); + +// Set up some basic data +$worksheet + ->setCellValue('A1', 'Tax Rate:') + ->setCellValue('B1', '=TAX_RATE') + ->setCellValue('A3', 'Net Price:') + ->setCellValue('B3', 19.99) + ->setCellValue('A4', 'Tax:') + ->setCellValue('A5', 'Price including Tax:'); + +// Define a named range that we can use in our formulae +$spreadsheet->addNamedRange(new NamedRange('PRICE', $worksheet, '=$B$3')); + +// Reference the defined formulae in worksheet formulae +$worksheet + ->setCellValue('B4', '=TAX') + ->setCellValue('B5', '=PRICE+TAX'); + +echo 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); diff --git a/samples/DefinedNames/SimpleNamedRange.php b/samples/DefinedNames/SimpleNamedRange.php new file mode 100644 index 00000000..ee05c68f --- /dev/null +++ b/samples/DefinedNames/SimpleNamedRange.php @@ -0,0 +1,46 @@ +setActiveSheetIndex(0); + +// Set up some basic data +$worksheet + ->setCellValue('A1', 'Tax Rate:') + ->setCellValue('B1', '=19%') + ->setCellValue('A3', 'Net Price:') + ->setCellValue('B3', 12.99) + ->setCellValue('A4', 'Tax:') + ->setCellValue('A5', 'Price including Tax:'); + +// Define named ranges +$spreadsheet->addNamedRange(new NamedRange('TAX_RATE', $worksheet, '=$B$1')); +$spreadsheet->addNamedRange(new NamedRange('PRICE', $worksheet, '=$B$3')); + +// Reference that defined name in a formula +$worksheet + ->setCellValue('B4', '=PRICE*TAX_RATE') + ->setCellValue('B5', '=PRICE*(1+TAX_RATE)'); + +echo 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); diff --git a/src/PhpSpreadsheet/Calculation/Calculation.php b/src/PhpSpreadsheet/Calculation/Calculation.php index 10ba064a..8cb1c297 100644 --- a/src/PhpSpreadsheet/Calculation/Calculation.php +++ b/src/PhpSpreadsheet/Calculation/Calculation.php @@ -7,7 +7,8 @@ use PhpOffice\PhpSpreadsheet\Calculation\Engine\Logger; use PhpOffice\PhpSpreadsheet\Calculation\Token\Stack; use PhpOffice\PhpSpreadsheet\Cell\Cell; use PhpOffice\PhpSpreadsheet\Cell\Coordinate; -use PhpOffice\PhpSpreadsheet\NamedRange; +use PhpOffice\PhpSpreadsheet\DefinedName; +use PhpOffice\PhpSpreadsheet\ReferenceHelper; use PhpOffice\PhpSpreadsheet\Shared; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; @@ -27,8 +28,13 @@ class Calculation const CALCULATION_REGEXP_FUNCTION = '@?(?:_xlfn\.)?([\p{L}][\p{L}\p{N}\.]*)[\s]*\('; // Cell reference (cell or range of cells, with or without a sheet reference) const CALCULATION_REGEXP_CELLREF = '((([^\s,!&%^\/\*\+<>=-]*)|(\'[^\']*\')|(\"[^\"]*\"))!)?\$?\b([a-z]{1,3})\$?(\d{1,7})(?![\w.])'; - // Named Range of cells - const CALCULATION_REGEXP_NAMEDRANGE = '((([^\s,!&%^\/\*\+<>=-]*)|(\'[^\']*\')|(\"[^\"]*\"))!)?([_\p{L}][_\p{L}\p{N}\.]*)'; + // Cell reference (with or without a sheet reference) ensuring absolute/relative + const CALCULATION_REGEXP_CELLREF_RELATIVE = '((([^\s\(,!&%^\/\*\+<>=-]*)|(\'[^\']*\')|(\"[^\"]*\"))!)?(\$?\b[a-z]{1,3})(\$?\d{1,7})(?![\w.])'; + // Cell ranges ensuring absolute/relative + const CALCULATION_REGEXP_COLUMNRANGE_RELATIVE = '(\$?[a-z]{1,3}):(\$?[a-z]{1,3})'; + const CALCULATION_REGEXP_ROWRANGE_RELATIVE = '(\$?\d{1,7}):(\$?\d{1,7})'; + // Defined Names: Named Range of cells, or Named Formulae + const CALCULATION_REGEXP_DEFINEDNAME = '((([^\s,!&%^\/\*\+<>=-]*)|(\'[^\']*\')|(\"[^\"]*\"))!)?([_\p{L}][_\p{L}\p{N}\.]*)'; // Error const CALCULATION_REGEXP_ERROR = '\#[A-Z][A-Z0_\/]*[!\?]?'; @@ -128,6 +134,13 @@ class Calculation */ public $formulaError; + /** + * Reference Helper. + * + * @var ReferenceHelper + */ + private static $referenceHelper; + /** * An array of the nested cell references accessed by the calculation engine, used for the debug log. * @@ -2660,6 +2673,7 @@ class Calculation $this->spreadsheet = $spreadsheet; $this->cyclicReferenceStack = new CyclicReferenceStack(); $this->debugLog = new Logger($this->cyclicReferenceStack); + self::$referenceHelper = ReferenceHelper::getInstance(); } private static function loadLocales(): void @@ -3395,6 +3409,7 @@ class Calculation if (($cellID !== null) && ($this->getValueFromCache($wsCellReference, $cellValue))) { return $cellValue; } + $this->debugLog->writeDebugLog('Evaluating formula for cell ', $wsCellReference); if (($wsTitle[0] !== "\x00") && ($this->cyclicReferenceStack->onStack($wsCellReference))) { if ($this->cyclicFormulaCount <= 0) { @@ -3416,6 +3431,7 @@ class Calculation } } + $this->debugLog->writeDebugLog('Formula for cell ', $wsCellReference, ' is ', $formula); // Parse the formula onto the token stack and calculate the value $this->cyclicReferenceStack->push($wsCellReference); $cellValue = $this->processTokenStack($this->internalParseFormula($formula, $pCell), $cellID, $pCell); @@ -3776,13 +3792,13 @@ class Calculation $pCellParent = ($pCell !== null) ? $pCell->getWorksheet() : null; $regexpMatchString = '/^(' . self::CALCULATION_REGEXP_FUNCTION . - '|' . self::CALCULATION_REGEXP_CELLREF . - '|' . self::CALCULATION_REGEXP_NUMBER . - '|' . self::CALCULATION_REGEXP_STRING . - '|' . self::CALCULATION_REGEXP_OPENBRACE . - '|' . self::CALCULATION_REGEXP_NAMEDRANGE . - '|' . self::CALCULATION_REGEXP_ERROR . - ')/sui'; + '|' . self::CALCULATION_REGEXP_CELLREF . + '|' . self::CALCULATION_REGEXP_NUMBER . + '|' . self::CALCULATION_REGEXP_STRING . + '|' . self::CALCULATION_REGEXP_OPENBRACE . + '|' . self::CALCULATION_REGEXP_DEFINEDNAME . + '|' . self::CALCULATION_REGEXP_ERROR . + ')/sui'; // Start with initialisation $index = 0; @@ -3840,6 +3856,7 @@ class Calculation } $opCharacter = $formula[$index]; // Get the first character of the value at the current index position + if ((isset(self::$comparisonOperators[$opCharacter])) && (strlen($formula) > $index) && (isset(self::$comparisonOperators[$formula[$index + 1]]))) { $opCharacter .= $formula[++$index]; } @@ -4131,8 +4148,8 @@ class Calculation } elseif (($localeConstant = array_search(trim(strtoupper($val)), self::$localeBoolean)) !== false) { $stackItemType = 'Constant'; $val = self::$excelConstants[$localeConstant]; - } elseif (preg_match('/^' . self::CALCULATION_REGEXP_NAMEDRANGE . '.*/miu', $val, $match)) { - $stackItemType = 'Named Range'; + } elseif (preg_match('/^' . self::CALCULATION_REGEXP_DEFINEDNAME . '.*/miu', $val, $match)) { + $stackItemType = 'Defined Name'; $stackItemReference = $val; } $details = $stack->getStackItem($stackItemType, $val, $stackItemReference, $currentCondition, $currentOnlyIf, $currentOnlyIfNot); @@ -4171,18 +4188,20 @@ class Calculation while (($formula[$index] == "\n") || ($formula[$index] == "\r")) { ++$index; } + if ($formula[$index] == ' ') { while ($formula[$index] == ' ') { ++$index; } + // If we're expecting an operator, but only have a space between the previous and next operands (and both are // Cell References) then we have an INTERSECTION operator if ( ($expectingOperator) && ((preg_match('/^' . self::CALCULATION_REGEXP_CELLREF . '.*/Ui', substr($formula, $index), $match)) && ($output[count($output) - 1]['type'] == 'Cell Reference') || - (preg_match('/^' . self::CALCULATION_REGEXP_NAMEDRANGE . '.*/miu', substr($formula, $index), $match)) && - ($output[count($output) - 1]['type'] == 'Named Range' || $output[count($output) - 1]['type'] == 'Value') + (preg_match('/^' . self::CALCULATION_REGEXP_DEFINEDNAME . '.*/miu', substr($formula, $index), $match)) && + ($output[count($output) - 1]['type'] == 'Defined Name' || $output[count($output) - 1]['type'] == 'Value') ) ) { while ( @@ -4711,20 +4730,25 @@ class Calculation if (isset($storeKey)) { $branchStore[$storeKey] = $token; } - // if the token is a named range, push the named range name onto the stack - } elseif (preg_match('/^' . self::CALCULATION_REGEXP_NAMEDRANGE . '$/miu', $token, $matches)) { - $namedRange = $matches[6]; - $this->debugLog->writeDebugLog('Evaluating Named Range ', $namedRange); + // if the token is a named range or formula, evaluate it and push the result onto the stack + } elseif (preg_match('/^' . self::CALCULATION_REGEXP_DEFINEDNAME . '$/miu', $token, $matches)) { + $definedName = $matches[6]; + if ($pCell === null || $pCellWorksheet === null) { + return $this->raiseFormulaError("undefined name '$token'"); + } - $cellValue = $this->extractNamedRange($namedRange, ((null !== $pCell) ? $pCellWorksheet : null), false); - $pCell->attach($pCellParent); - $this->debugLog->writeDebugLog('Evaluation Result for named range ', $namedRange, ' is ', $this->showTypeDetails($cellValue)); - $stack->push('Named Range', $cellValue, $namedRange); + $this->debugLog->writeDebugLog('Evaluating Defined Name ', $definedName); + $namedRange = DefinedName::resolveName($definedName, $pCellWorksheet); + if ($namedRange === null) { + return $this->raiseFormulaError("undefined name '$definedName'"); + } + + $result = $this->evaluateDefinedName($pCell, $namedRange, $pCellWorksheet, $stack); if (isset($storeKey)) { - $branchStore[$storeKey] = $cellValue; + $branchStore[$storeKey] = $result; } } else { - return $this->raiseFormulaError("undefined variable '$token'"); + return $this->raiseFormulaError("undefined name '$token'"); } } } @@ -5107,21 +5131,21 @@ class Calculation } // Named range? - $namedRange = NamedRange::resolveRange($pRange, $pSheet); - if ($namedRange !== null) { - $pSheet = $namedRange->getWorksheet(); - $pRange = $namedRange->getRange(); - $splitRange = Coordinate::splitRange($pRange); - // Convert row and column references - if (ctype_alpha($splitRange[0][0])) { - $pRange = $splitRange[0][0] . '1:' . $splitRange[0][1] . $namedRange->getWorksheet()->getHighestRow(); - } elseif (ctype_digit($splitRange[0][0])) { - $pRange = 'A' . $splitRange[0][0] . ':' . $namedRange->getWorksheet()->getHighestColumn() . $splitRange[0][1]; - } - } else { + $namedRange = DefinedName::resolveName($pRange, $pSheet); + if ($namedRange === null) { return Functions::REF(); } + $pSheet = $namedRange->getWorksheet(); + $pRange = $namedRange->getValue(); + $splitRange = Coordinate::splitRange($pRange); + // Convert row and column references + if (ctype_alpha($splitRange[0][0])) { + $pRange = $splitRange[0][0] . '1:' . $splitRange[0][1] . $namedRange->getWorksheet()->getHighestRow(); + } elseif (ctype_digit($splitRange[0][0])) { + $pRange = 'A' . $splitRange[0][0] . ':' . $namedRange->getWorksheet()->getHighestColumn() . $splitRange[0][1]; + } + // Extract range $aReferences = Coordinate::extractAllCellReferencesInRange($pRange); if (!isset($aReferences[1])) { @@ -5240,4 +5264,59 @@ class Calculation return '[ ' . implode(' | ', $tokensStr) . ' ]'; } + + /** + * @return mixed|string + */ + private function evaluateDefinedName(Cell $pCell, DefinedName $namedRange, Worksheet $pCellWorksheet, Stack $stack) + { + $definedNameScope = $namedRange->getScope(); + if ($definedNameScope !== null && $definedNameScope !== $pCellWorksheet) { + // The defined name isn't in our current scope, so #REF + $result = Functions::REF(); + $stack->push('Error', $result, $namedRange->getName()); + + return $result; + } + + $definedNameValue = $namedRange->getValue(); + $definedNameType = $namedRange->isFormula() ? 'Formula' : 'Range'; + $definedNameWorksheet = $namedRange->getWorksheet(); + + if ($definedNameValue[0] !== '=') { + $definedNameValue = '=' . $definedNameValue; + } + + $this->debugLog->writeDebugLog("Defined Name is a {$definedNameType} with a value of {$definedNameValue}"); + + $recursiveCalculationCell = ($definedNameWorksheet !== null && $definedNameWorksheet !== $pCellWorksheet) + ? $definedNameWorksheet->getCell('A1') + : $pCell; + $recursiveCalculationCellAddress = $recursiveCalculationCell !== null + ? $recursiveCalculationCell->getCoordinate() + : null; + + // Adjust relative references in ranges and formulae so that we execute the calculation for the correct rows and columns + $definedNameValue = self::$referenceHelper->updateFormulaReferencesAnyWorksheet( + $definedNameValue, + Coordinate::columnIndexFromString($pCell->getColumn()) - 1, + $pCell->getRow() - 1 + ); + + $this->debugLog->writeDebugLog("Value adjusted for relative references is {$definedNameValue}"); + + $recursiveCalculator = new self($this->spreadsheet); + $recursiveCalculator->getDebugLog()->setWriteDebugLog($this->getDebugLog()->getWriteDebugLog()); + $recursiveCalculator->getDebugLog()->setEchoDebugLog($this->getDebugLog()->getEchoDebugLog()); + $result = $recursiveCalculator->_calculateFormulaValue($definedNameValue, $recursiveCalculationCellAddress, $recursiveCalculationCell); + + if ($this->getDebugLog()->getWriteDebugLog()) { + $this->debugLog->mergeDebugLog(array_slice($recursiveCalculator->getDebugLog()->getLog(), 3)); + $this->debugLog->writeDebugLog("Evaluation Result for Named {$definedNameType} {$namedRange->getName()} is {$this->showTypeDetails($result)}"); + } + + $stack->push('Defined Name', $result, $namedRange->getName()); + + return $result; + } } diff --git a/src/PhpSpreadsheet/Calculation/Engine/Logger.php b/src/PhpSpreadsheet/Calculation/Engine/Logger.php index d69ea56d..3c0f2377 100644 --- a/src/PhpSpreadsheet/Calculation/Engine/Logger.php +++ b/src/PhpSpreadsheet/Calculation/Engine/Logger.php @@ -106,6 +106,20 @@ class Logger } } + /** + * Write a series of entries to the calculation engine debug log. + * + * @param string[] $args + */ + public function mergeDebugLog(array $args): void + { + if ($this->writeDebugLog) { + foreach ($args as $entry) { + $this->writeDebugLog($entry); + } + } + } + /** * Clear the calculation engine debug log. */ diff --git a/src/PhpSpreadsheet/Calculation/Functions.php b/src/PhpSpreadsheet/Calculation/Functions.php index 2a4e6f40..2e8a7ecf 100644 --- a/src/PhpSpreadsheet/Calculation/Functions.php +++ b/src/PhpSpreadsheet/Calculation/Functions.php @@ -647,7 +647,7 @@ class Functions preg_match('/^' . Calculation::CALCULATION_REGEXP_CELLREF . '$/i', $cellReference, $matches); $cellReference = $matches[6] . $matches[7]; - $worksheetName = trim($matches[3], "'"); + $worksheetName = str_replace("''", "'", trim($matches[2], "'")); $worksheet = (!empty($worksheetName)) ? $pCell->getWorksheet()->getParent()->getSheetByName($worksheetName) diff --git a/src/PhpSpreadsheet/Calculation/LookupRef.php b/src/PhpSpreadsheet/Calculation/LookupRef.php index 7c00ca32..0636258b 100644 --- a/src/PhpSpreadsheet/Calculation/LookupRef.php +++ b/src/PhpSpreadsheet/Calculation/LookupRef.php @@ -290,7 +290,7 @@ class LookupRef (!preg_match('/^' . Calculation::CALCULATION_REGEXP_CELLREF . '$/i', $cellAddress1, $matches)) || (($cellAddress2 !== null) && (!preg_match('/^' . Calculation::CALCULATION_REGEXP_CELLREF . '$/i', $cellAddress2, $matches))) ) { - if (!preg_match('/^' . Calculation::CALCULATION_REGEXP_NAMEDRANGE . '$/i', $cellAddress1, $matches)) { + if (!preg_match('/^' . Calculation::CALCULATION_REGEXP_DEFINEDNAME . '$/i', $cellAddress1, $matches)) { return Functions::REF(); } diff --git a/src/PhpSpreadsheet/Cell/AddressHelper.php b/src/PhpSpreadsheet/Cell/AddressHelper.php index 77a521b0..04fa3b8c 100644 --- a/src/PhpSpreadsheet/Cell/AddressHelper.php +++ b/src/PhpSpreadsheet/Cell/AddressHelper.php @@ -35,7 +35,7 @@ class AddressHelper $columnReference = (string) $currentColumnNumber; } // Bracketed C references are relative to the current column - if ($columnReference[0] === '[') { + if (is_string($columnReference) && $columnReference[0] === '[') { $columnReference = $currentColumnNumber + trim($columnReference, '[]'); } @@ -47,6 +47,52 @@ class AddressHelper return $A1CellReference; } + /** + * Converts a formula that uses R1C1 format cell address to an A1 format cell address. + */ + public static function convertFormulaToA1( + string $formula, + int $currentRowNumber = 1, + int $currentColumnNumber = 1 + ): string { + if (substr($formula, 0, 3) == 'of:') { + $formula = substr($formula, 3); + $temp = explode('"', $formula); + $key = false; + foreach ($temp as &$value) { + // Only replace in alternate array entries (i.e. non-quoted blocks) + if ($key = !$key) { + $value = str_replace(['[.', '.', ']'], '', $value); + } + } + } else { + // Convert R1C1 style references to A1 style references (but only when not quoted) + $temp = explode('"', $formula); + $key = false; + foreach ($temp as &$value) { + // Only replace in alternate array entries (i.e. non-quoted blocks) + if ($key = !$key) { + preg_match_all('/(R(\[?-?\d*\]?))(C(\[?-?\d*\]?))/', $value, $cellReferences, PREG_SET_ORDER + PREG_OFFSET_CAPTURE); + // Reverse the matches array, otherwise all our offsets will become incorrect if we modify our way + // through the formula from left to right. Reversing means that we work right to left.through + // the formula + $cellReferences = array_reverse($cellReferences); + // Loop through each R1C1 style reference in turn, converting it to its A1 style equivalent, + // then modify the formula to use that new reference + foreach ($cellReferences as $cellReference) { + $A1CellReference = self::convertToA1($cellReference[0][0], $currentRowNumber, $currentColumnNumber); + $value = substr_replace($value, $A1CellReference, $cellReference[0][1], strlen($cellReference[0][0])); + } + } + } + } + unset($value); + // Then rebuild the formula string + $formula = implode('"', $temp); + + return $formula; + } + /** * Converts an A1 format cell address to an R1C1 format cell address. * If $currentRowNumber or $currentColumnNumber are provided, then the R1C1 address will be formatted as a relative address. diff --git a/src/PhpSpreadsheet/Cell/Cell.php b/src/PhpSpreadsheet/Cell/Cell.php index 74ed9268..5dee411b 100644 --- a/src/PhpSpreadsheet/Cell/Cell.php +++ b/src/PhpSpreadsheet/Cell/Cell.php @@ -265,6 +265,8 @@ class Cell } catch (Exception $ex) { if (($ex->getMessage() === 'Unable to access External Workbook') && ($this->calculatedValue !== null)) { return $this->calculatedValue; // Fallback for calculations referencing external files. + } elseif (strpos($ex->getMessage(), 'undefined name') !== false) { + return \PhpOffice\PhpSpreadsheet\Calculation\Functions::NAME(); } throw new \PhpOffice\PhpSpreadsheet\Calculation\Exception( diff --git a/src/PhpSpreadsheet/DefinedName.php b/src/PhpSpreadsheet/DefinedName.php new file mode 100644 index 00000000..dbadd4ce --- /dev/null +++ b/src/PhpSpreadsheet/DefinedName.php @@ -0,0 +1,263 @@ +worksheet). + * + * @var bool + */ + protected $localOnly; + + /** + * Scope. + * + * @var Worksheet + */ + protected $scope; + + /** + * Whether this is a named range or a named formula. + * + * @var bool + */ + protected $isFormula; + + /** + * Create a new Defined Name. + */ + public function __construct( + string $name, + ?Worksheet $worksheet = null, + ?string $value = null, + bool $localOnly = false, + ?Worksheet $scope = null + ) { + if ($worksheet === null) { + $worksheet = $scope; + } + + // Set local members + $this->name = $name; + $this->worksheet = $worksheet; + $this->value = (string) $value; + $this->localOnly = $localOnly; + // If local only, then the scope will be set to worksheet unless a scope is explicitly set + $this->scope = ($localOnly === true) ? (($scope === null) ? $worksheet : $scope) : null; + // If the range string contains characters that aren't associated with the range definition (A-Z,1-9 + // for cell references, and $, or the range operators (colon comma or space), quotes and ! for + // worksheet names + // then this is treated as a named formula, and not a named range + $this->isFormula = self::testIfFormula($this->value); + } + + /** + * Create a new defined name, either a range or a formula. + */ + public static function createInstance( + string $name, + ?Worksheet $worksheet = null, + ?string $value = null, + bool $localOnly = false, + ?Worksheet $scope = null + ): self { + $value = (string) $value; + $isFormula = self::testIfFormula($value); + if ($isFormula) { + return new NamedFormula($name, $worksheet, $value, $localOnly, $scope); + } + + return new NamedRange($name, $worksheet, $value, $localOnly, $scope); + } + + public static function testIfFormula(string $value): bool + { + if (substr($value, 0, 1) === '=') { + $value = substr($value, 1); + } + + if (is_numeric($value)) { + return true; + } + + $segMatcher = false; + foreach (explode("'", $value) as $subVal) { + // Only test in alternate array entries (the non-quoted blocks) + if ( + ($segMatcher = !$segMatcher) && + (preg_match('/' . self::REGEXP_IDENTIFY_FORMULA . '/miu', $subVal)) + ) { + return true; + } + } + + return false; + } + + /** + * Get name. + */ + public function getName(): string + { + return $this->name; + } + + /** + * Set name. + */ + public function setName(string $name): self + { + if (!empty($name)) { + // Old title + $oldTitle = $this->name; + + // Re-attach + if ($this->worksheet !== null) { + $this->worksheet->getParent()->removeNamedRange($this->name, $this->worksheet); + } + $this->name = $name; + + if ($this->worksheet !== null) { + $this->worksheet->getParent()->addNamedRange($this); + } + + // New title + $newTitle = $this->name; + ReferenceHelper::getInstance()->updateNamedFormulas($this->worksheet->getParent(), $oldTitle, $newTitle); + } + + return $this; + } + + /** + * Get worksheet. + */ + public function getWorksheet(): ?Worksheet + { + return $this->worksheet; + } + + /** + * Set worksheet. + */ + public function setWorksheet(?Worksheet $value): self + { + $this->worksheet = $value; + + return $this; + } + + /** + * Get range or formula value. + */ + public function getValue(): string + { + return $this->value; + } + + /** + * Set range or formula value. + */ + public function setValue(string $value): self + { + $this->value = $value; + + return $this; + } + + /** + * Get localOnly. + */ + public function getLocalOnly(): bool + { + return $this->localOnly; + } + + /** + * Set localOnly. + */ + public function setLocalOnly(bool $value): self + { + $this->localOnly = $value; + $this->scope = $value ? $this->worksheet : null; + + return $this; + } + + /** + * Get scope. + */ + public function getScope(): ?Worksheet + { + return $this->scope; + } + + /** + * Set scope. + */ + public function setScope(?Worksheet $value): self + { + $this->scope = $value; + $this->localOnly = $value !== null; + + return $this; + } + + /** + * Identify whether this is a named range or a named formula. + */ + public function isFormula(): bool + { + return $this->isFormula; + } + + /** + * Resolve a named range to a regular cell range or formula. + */ + public static function resolveName(string $pDefinedName, Worksheet $pSheet): ?self + { + return $pSheet->getParent()->getDefinedName($pDefinedName, $pSheet); + } + + /** + * Implement PHP __clone to create a deep clone, not just a shallow copy. + */ + public function __clone() + { + $vars = get_object_vars($this); + foreach ($vars as $key => $value) { + if (is_object($value)) { + $this->$key = clone $value; + } else { + $this->$key = $value; + } + } + } +} diff --git a/src/PhpSpreadsheet/NamedFormula.php b/src/PhpSpreadsheet/NamedFormula.php new file mode 100644 index 00000000..ffb1c9b4 --- /dev/null +++ b/src/PhpSpreadsheet/NamedFormula.php @@ -0,0 +1,45 @@ +value; + } + + /** + * Set the formula value. + */ + public function setFormula(string $formula): self + { + if (!empty($formula)) { + $this->value = $formula; + } + + return $this; + } +} diff --git a/src/PhpSpreadsheet/NamedRange.php b/src/PhpSpreadsheet/NamedRange.php index 94fe8190..db9c5f12 100644 --- a/src/PhpSpreadsheet/NamedRange.php +++ b/src/PhpSpreadsheet/NamedRange.php @@ -2,234 +2,54 @@ namespace PhpOffice\PhpSpreadsheet; +use PhpOffice\PhpSpreadsheet\Cell\Coordinate; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; -class NamedRange +class NamedRange extends DefinedName { /** - * Range name. - * - * @var string + * Create a new Named Range. */ - private $name; - - /** - * Worksheet on which the named range can be resolved. - * - * @var Worksheet - */ - private $worksheet; - - /** - * Range of the referenced cells. - * - * @var string - */ - private $range; - - /** - * Is the named range local? (i.e. can only be used on $this->worksheet). - * - * @var bool - */ - private $localOnly; - - /** - * Scope. - * - * @var Worksheet - */ - private $scope; - - /** - * Create a new NamedRange. - * - * @param string $pName - * @param string $pRange - * @param bool $pLocalOnly - * @param null|Worksheet $pScope Scope. Only applies when $pLocalOnly = true. Null for global scope. - */ - public function __construct($pName, Worksheet $pWorksheet, $pRange = 'A1', $pLocalOnly = false, $pScope = null) - { - // Validate data - if (($pName === null) || ($pWorksheet === null) || ($pRange === null)) { - throw new Exception('Parameters can not be null.'); + public function __construct( + string $name, + ?Worksheet $worksheet = null, + string $range = 'A1', + bool $localOnly = false, + ?Worksheet $scope = null + ) { + if ($worksheet === null && $scope === null) { + throw new Exception('You must specify a worksheet or a scope for a Named Range'); } - - // Set local members - $this->name = $pName; - $this->worksheet = $pWorksheet; - $this->range = $pRange; - $this->localOnly = $pLocalOnly; - $this->scope = ($pLocalOnly == true) ? (($pScope == null) ? $pWorksheet : $pScope) : null; + parent::__construct($name, $worksheet, $range, $localOnly, $scope); } /** - * Get name. - * - * @return string + * Get the range value. */ - public function getName() + public function getRange(): string { - return $this->name; + return $this->value; } /** - * Set name. - * - * @param string $value - * - * @return $this + * Set the range value. */ - public function setName($value) + public function setRange(string $range): self { - if ($value !== null) { - // Old title - $oldTitle = $this->name; - - // Re-attach - if ($this->worksheet !== null) { - $this->worksheet->getParent()->removeNamedRange($this->name, $this->worksheet); - } - $this->name = $value; - - if ($this->worksheet !== null) { - $this->worksheet->getParent()->addNamedRange($this); - } - - // New title - $newTitle = $this->name; - ReferenceHelper::getInstance()->updateNamedFormulas($this->worksheet->getParent(), $oldTitle, $newTitle); + if (!empty($range)) { + $this->value = $range; } return $this; } - /** - * Get worksheet. - * - * @return Worksheet - */ - public function getWorksheet() + public function getCellsInRange(): array { - return $this->worksheet; - } - - /** - * Set worksheet. - * - * @param Worksheet $value - * - * @return $this - */ - public function setWorksheet(?Worksheet $value = null) - { - if ($value !== null) { - $this->worksheet = $value; + $range = $this->value; + if (substr($range, 0, 1) === '=') { + $range = substr($range, 1); } - return $this; - } - - /** - * Get range. - * - * @return string - */ - public function getRange() - { - return $this->range; - } - - /** - * Set range. - * - * @param string $value - * - * @return $this - */ - public function setRange($value) - { - if ($value !== null) { - $this->range = $value; - } - - return $this; - } - - /** - * Get localOnly. - * - * @return bool - */ - public function getLocalOnly() - { - return $this->localOnly; - } - - /** - * Set localOnly. - * - * @param bool $value - * - * @return $this - */ - public function setLocalOnly($value) - { - $this->localOnly = $value; - $this->scope = $value ? $this->worksheet : null; - - return $this; - } - - /** - * Get scope. - * - * @return null|Worksheet - */ - public function getScope() - { - return $this->scope; - } - - /** - * Set scope. - * - * @return $this - */ - public function setScope(?Worksheet $value = null) - { - $this->scope = $value; - $this->localOnly = $value != null; - - return $this; - } - - /** - * Resolve a named range to a regular cell range. - * - * @param string $pNamedRange Named range - * @param null|Worksheet $pSheet Scope. Use null for global scope - * - * @return NamedRange - */ - public static function resolveRange($pNamedRange, Worksheet $pSheet) - { - return $pSheet->getParent()->getNamedRange($pNamedRange, $pSheet); - } - - /** - * Implement PHP __clone to create a deep clone, not just a shallow copy. - */ - public function __clone() - { - $vars = get_object_vars($this); - foreach ($vars as $key => $value) { - if (is_object($value)) { - $this->$key = clone $value; - } else { - $this->$key = $value; - } - } + return Coordinate::extractAllCellReferencesInRange($range); } } diff --git a/src/PhpSpreadsheet/Reader/Gnumeric.php b/src/PhpSpreadsheet/Reader/Gnumeric.php index 77c9bdf7..f4a44895 100644 --- a/src/PhpSpreadsheet/Reader/Gnumeric.php +++ b/src/PhpSpreadsheet/Reader/Gnumeric.php @@ -4,7 +4,7 @@ namespace PhpOffice\PhpSpreadsheet\Reader; use PhpOffice\PhpSpreadsheet\Cell\Coordinate; use PhpOffice\PhpSpreadsheet\Cell\DataType; -use PhpOffice\PhpSpreadsheet\NamedRange; +use PhpOffice\PhpSpreadsheet\DefinedName; use PhpOffice\PhpSpreadsheet\Reader\Gnumeric\PageSetup; use PhpOffice\PhpSpreadsheet\Reader\Security\XmlScanner; use PhpOffice\PhpSpreadsheet\ReferenceHelper; @@ -736,18 +736,19 @@ class Gnumeric extends BaseReader { // Loop through definedNames (global named ranges) if (isset($gnmXML->Names)) { - foreach ($gnmXML->Names->Name as $namedRange) { - $name = (string) $namedRange->name; - $range = (string) $namedRange->value; - if (stripos($range, '#REF!') !== false) { + foreach ($gnmXML->Names->Name as $definedName) { + $name = (string) $definedName->name; + $value = (string) $definedName->value; + if (stripos($value, '#REF!') !== false) { continue; } - $range = Worksheet::extractSheetTitle($range, true); - $range[0] = trim($range[0], "'"); - if ($worksheet = $this->spreadsheet->getSheetByName($range[0])) { - $extractedRange = str_replace('$', '', $range[1]); - $this->spreadsheet->addNamedRange(new NamedRange($name, $worksheet, $extractedRange)); + [$worksheetName] = Worksheet::extractSheetTitle($value, true); + $worksheetName = trim($worksheetName, "'"); + $worksheet = $this->spreadsheet->getSheetByName($worksheetName); + // Worksheet might still be null if we're only loading selected sheets rather than the full spreadsheet + if ($worksheet !== null) { + $this->spreadsheet->addDefinedName(DefinedName::createInstance($name, $worksheet, $value)); } } } diff --git a/src/PhpSpreadsheet/Reader/Ods.php b/src/PhpSpreadsheet/Reader/Ods.php index 74d65db8..4ceac653 100644 --- a/src/PhpSpreadsheet/Reader/Ods.php +++ b/src/PhpSpreadsheet/Reader/Ods.php @@ -11,6 +11,7 @@ use DOMNode; use PhpOffice\PhpSpreadsheet\Calculation\Calculation; use PhpOffice\PhpSpreadsheet\Cell\Coordinate; use PhpOffice\PhpSpreadsheet\Cell\DataType; +use PhpOffice\PhpSpreadsheet\DefinedName; use PhpOffice\PhpSpreadsheet\Reader\Exception as ReaderException; use PhpOffice\PhpSpreadsheet\Reader\Ods\PageSettings; use PhpOffice\PhpSpreadsheet\Reader\Ods\Properties as DocumentProperties; @@ -21,6 +22,7 @@ use PhpOffice\PhpSpreadsheet\Shared\Date; use PhpOffice\PhpSpreadsheet\Shared\File; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Style\NumberFormat; +use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use XMLReader; use ZipArchive; @@ -547,30 +549,7 @@ class Ods extends BaseReader if ($hasCalculatedValue) { $type = DataType::TYPE_FORMULA; $cellDataFormula = substr($cellDataFormula, strpos($cellDataFormula, ':=') + 1); - $temp = explode('"', $cellDataFormula); - $tKey = false; - foreach ($temp as &$value) { - // Only replace in alternate array entries (i.e. non-quoted blocks) - if ($tKey = !$tKey) { - // Cell range reference in another sheet - $value = preg_replace('/\[([^\.]+)\.([^\.]+):\.([^\.]+)\]/U', '$1!$2:$3', $value); - - // Cell reference in another sheet - $value = preg_replace('/\[([^\.]+)\.([^\.]+)\]/U', '$1!$2', $value); - - // Cell range reference - $value = preg_replace('/\[\.([^\.]+):\.([^\.]+)\]/U', '$1:$2', $value); - - // Simple cell reference - $value = preg_replace('/\[\.([^\.]+)\]/U', '$1', $value); - - $value = Calculation::translateSeparator(';', ',', $value, $inBraces); - } - } - unset($value); - - // Then rebuild the formula string - $cellDataFormula = implode('"', $temp); + $cellDataFormula = $this->convertToExcelFormulaValue($cellDataFormula); } if ($cellData->hasAttributeNS($tableNs, 'number-columns-repeated')) { @@ -662,8 +641,11 @@ class Ods extends BaseReader $pageSettings->setPrintSettingsForWorksheet($spreadsheet->getActiveSheet(), $worksheetStyleName); ++$worksheetID; } - } + $this->readDefinedRanges($spreadsheet, $workbookData, $tableNs); + $this->readDefinedExpressions($spreadsheet, $workbookData, $tableNs); + } + $spreadsheet->setActiveSheetIndex(0); // Return return $spreadsheet; } @@ -715,4 +697,99 @@ class Ods extends BaseReader return $value; } + + private function convertToExcelAddressValue(string $openOfficeAddress): string + { + $excelAddress = $openOfficeAddress; + + // Cell range 3-d reference + // As we don't support 3-d ranges, we're just going to take a quick and dirty approach + // and assume that the second worksheet reference is the same as the first + $excelAddress = preg_replace('/\$?([^\.]+)\.([^\.]+):\$?([^\.]+)\.([^\.]+)/miu', '$1!$2:$4', $excelAddress); + // Cell range reference in another sheet + $excelAddress = preg_replace('/\$?([^\.]+)\.([^\.]+):\.([^\.]+)/miu', '$1!$2:$3', $excelAddress); + // Cell reference in another sheet + $excelAddress = preg_replace('/\$?([^\.]+)\.([^\.]+)/miu', '$1!$2', $excelAddress); + // Cell range reference + $excelAddress = preg_replace('/\.([^\.]+):\.([^\.]+)/miu', '$1:$2', $excelAddress); + // Simple cell reference + $excelAddress = preg_replace('/\.([^\.]+)/miu', '$1', $excelAddress); + + return $excelAddress; + } + + private function convertToExcelFormulaValue(string $openOfficeFormula): string + { + $temp = explode('"', $openOfficeFormula); + $tKey = false; + foreach ($temp as &$value) { + // Only replace in alternate array entries (i.e. non-quoted blocks) + if ($tKey = !$tKey) { + // Cell range reference in another sheet + $value = preg_replace('/\[\$?([^\.]+)\.([^\.]+):\.([^\.]+)\]/miu', '$1!$2:$3', $value); + // Cell reference in another sheet + $value = preg_replace('/\[\$?([^\.]+)\.([^\.]+)\]/miu', '$1!$2', $value); + // Cell range reference + $value = preg_replace('/\[\.([^\.]+):\.([^\.]+)\]/miu', '$1:$2', $value); + // Simple cell reference + $value = preg_replace('/\[\.([^\.]+)\]/miu', '$1', $value); + + $value = Calculation::translateSeparator(';', ',', $value, $inBraces); + } + } + + // Then rebuild the formula string + $excelFormula = implode('"', $temp); + + return $excelFormula; + } + + /** + * Read any Named Ranges that are defined in this spreadsheet. + */ + private function readDefinedRanges(Spreadsheet $spreadsheet, DOMElement $workbookData, string $tableNs): void + { + $namedRanges = $workbookData->getElementsByTagNameNS($tableNs, 'named-range'); + foreach ($namedRanges as $definedNameElement) { + $definedName = $definedNameElement->getAttributeNS($tableNs, 'name'); + $baseAddress = $definedNameElement->getAttributeNS($tableNs, 'base-cell-address'); + $range = $definedNameElement->getAttributeNS($tableNs, 'cell-range-address'); + + $baseAddress = $this->convertToExcelAddressValue($baseAddress); + $range = $this->convertToExcelAddressValue($range); + + $this->addDefinedName($spreadsheet, $baseAddress, $definedName, $range); + } + } + + /** + * Read any Named Formulae that are defined in this spreadsheet. + */ + private function readDefinedExpressions(Spreadsheet $spreadsheet, DOMElement $workbookData, string $tableNs): void + { + $namedExpressions = $workbookData->getElementsByTagNameNS($tableNs, 'named-expression'); + foreach ($namedExpressions as $definedNameElement) { + $definedName = $definedNameElement->getAttributeNS($tableNs, 'name'); + $baseAddress = $definedNameElement->getAttributeNS($tableNs, 'base-cell-address'); + $expression = $definedNameElement->getAttributeNS($tableNs, 'expression'); + + $baseAddress = $this->convertToExcelAddressValue($baseAddress); + $expression = $this->convertToExcelFormulaValue($expression); + + $this->addDefinedName($spreadsheet, $baseAddress, $definedName, $expression); + } + } + + /** + * Assess scope and store the Defined Name. + */ + private function addDefinedName(Spreadsheet $spreadsheet, string $baseAddress, string $definedName, string $value): void + { + [$sheetReference] = Worksheet::extractSheetTitle($baseAddress, true); + $worksheet = $spreadsheet->getSheetByName($sheetReference); + // Worksheet might still be null if we're only loading selected sheets rather than the full spreadsheet + if ($worksheet !== null) { + $spreadsheet->addDefinedName(DefinedName::createInstance((string) $definedName, $worksheet, $value)); + } + } } diff --git a/src/PhpSpreadsheet/Reader/Xlsx.php b/src/PhpSpreadsheet/Reader/Xlsx.php index 381f107e..73f91852 100644 --- a/src/PhpSpreadsheet/Reader/Xlsx.php +++ b/src/PhpSpreadsheet/Reader/Xlsx.php @@ -4,7 +4,7 @@ namespace PhpOffice\PhpSpreadsheet\Reader; use PhpOffice\PhpSpreadsheet\Cell\Coordinate; use PhpOffice\PhpSpreadsheet\Cell\Hyperlink; -use PhpOffice\PhpSpreadsheet\NamedRange; +use PhpOffice\PhpSpreadsheet\DefinedName; use PhpOffice\PhpSpreadsheet\Reader\Security\XmlScanner; use PhpOffice\PhpSpreadsheet\Reader\Xlsx\AutoFilter; use PhpOffice\PhpSpreadsheet\Reader\Xlsx\Chart; @@ -1345,11 +1345,6 @@ class Xlsx extends BaseReader foreach ($xmlWorkbook->definedNames->definedName as $definedName) { // Extract range $extractedRange = (string) $definedName; - if (($spos = strpos($extractedRange, '!')) !== false) { - $extractedRange = substr($extractedRange, 0, $spos) . str_replace('$', '', substr($extractedRange, $spos)); - } else { - $extractedRange = str_replace('$', '', $extractedRange); - } // Valid range? if (stripos((string) $definedName, '#REF!') !== false || $extractedRange == '') { @@ -1367,39 +1362,40 @@ class Xlsx extends BaseReader break; default: if ($mapSheetId[(int) $definedName['localSheetId']] !== null) { + $range = Worksheet::extractSheetTitle((string) $definedName, true); + $scope = $excel->getSheet($mapSheetId[(int) $definedName['localSheetId']]); if (strpos((string) $definedName, '!') !== false) { - $range = Worksheet::extractSheetTitle((string) $definedName, true); $range[0] = str_replace("''", "'", $range[0]); $range[0] = str_replace("'", '', $range[0]); - if ($worksheet = $docSheet->getParent()->getSheetByName($range[0])) { - $extractedRange = str_replace('$', '', $range[1]); - $scope = $docSheet->getParent()->getSheet($mapSheetId[(int) $definedName['localSheetId']]); - $excel->addNamedRange(new NamedRange((string) $definedName['name'], $worksheet, $extractedRange, true, $scope)); + if ($worksheet = $excel->getSheetByName($range[0])) { + $excel->addDefinedName(DefinedName::createInstance((string) $definedName['name'], $worksheet, $extractedRange, true, $scope)); + } else { + $excel->addDefinedName(DefinedName::createInstance((string) $definedName['name'], $scope, $extractedRange, true, $scope)); } + } else { + $excel->addDefinedName(DefinedName::createInstance((string) $definedName['name'], $scope, $extractedRange, true)); } } break; } } elseif (!isset($definedName['localSheetId'])) { + $definedRange = (string) $definedName; // "Global" definedNames $locatedSheet = null; - $extractedSheetName = ''; if (strpos((string) $definedName, '!') !== false) { + // Modify range, and extract the first worksheet reference + // Need to split on a comma or a space if not in quotes, and extract the first part. + $definedNameValueParts = preg_split("/[ ,](?=([^']*'[^']*')*[^']*$)/miuU", $definedRange); // Extract sheet name - $extractedSheetName = Worksheet::extractSheetTitle((string) $definedName, true); - $extractedSheetName = trim($extractedSheetName[0], "'"); + [$extractedSheetName] = Worksheet::extractSheetTitle((string) $definedNameValueParts[0], true); + $extractedSheetName = trim($extractedSheetName, "'"); // Locate sheet $locatedSheet = $excel->getSheetByName($extractedSheetName); - - // Modify range - [$worksheetName, $extractedRange] = Worksheet::extractSheetTitle($extractedRange, true); } - if ($locatedSheet !== null) { - $excel->addNamedRange(new NamedRange((string) $definedName['name'], $locatedSheet, $extractedRange, false)); - } + $excel->addDefinedName(DefinedName::createInstance((string) $definedName['name'], $locatedSheet, $definedRange, false)); } } } diff --git a/src/PhpSpreadsheet/Reader/Xml.php b/src/PhpSpreadsheet/Reader/Xml.php index 5bf18465..6a526d02 100644 --- a/src/PhpSpreadsheet/Reader/Xml.php +++ b/src/PhpSpreadsheet/Reader/Xml.php @@ -2,8 +2,10 @@ namespace PhpOffice\PhpSpreadsheet\Reader; +use PhpOffice\PhpSpreadsheet\Cell\AddressHelper; use PhpOffice\PhpSpreadsheet\Cell\Coordinate; use PhpOffice\PhpSpreadsheet\Cell\DataType; +use PhpOffice\PhpSpreadsheet\DefinedName; use PhpOffice\PhpSpreadsheet\Document\Properties; use PhpOffice\PhpSpreadsheet\Reader\Security\XmlScanner; use PhpOffice\PhpSpreadsheet\Reader\Xml\PageSettings; @@ -418,6 +420,20 @@ class Xml extends BaseReader $spreadsheet->getActiveSheet()->setTitle($worksheetName, false, false); } + // locally scoped defined names + if (isset($worksheet->Names[0])) { + foreach ($worksheet->Names[0] as $definedName) { + $definedName_ss = $definedName->attributes($namespaces['ss']); + $name = (string) $definedName_ss['Name']; + $definedValue = (string) $definedName_ss['RefersTo']; + $convertedValue = AddressHelper::convertFormulaToA1($definedValue); + if ($convertedValue[0] === '=') { + $convertedValue = substr($convertedValue, 1); + } + $spreadsheet->addDefinedName(DefinedName::createInstance($name, $spreadsheet->getActiveSheet(), $convertedValue, true)); + } + } + $columnID = 'A'; if (isset($worksheet->Table->Column)) { foreach ($worksheet->Table->Column as $columnData) { @@ -532,58 +548,7 @@ class Xml extends BaseReader if ($hasCalculatedValue) { $type = DataType::TYPE_FORMULA; $columnNumber = Coordinate::columnIndexFromString($columnID); - if (substr($cellDataFormula, 0, 3) == 'of:') { - $cellDataFormula = substr($cellDataFormula, 3); - $temp = explode('"', $cellDataFormula); - $key = false; - foreach ($temp as &$value) { - // Only replace in alternate array entries (i.e. non-quoted blocks) - if ($key = !$key) { - $value = str_replace(['[.', '.', ']'], '', $value); - } - } - } else { - // Convert R1C1 style references to A1 style references (but only when not quoted) - $temp = explode('"', $cellDataFormula); - $key = false; - foreach ($temp as &$value) { - // Only replace in alternate array entries (i.e. non-quoted blocks) - if ($key = !$key) { - preg_match_all('/(R(\[?-?\d*\]?))(C(\[?-?\d*\]?))/', $value, $cellReferences, PREG_SET_ORDER + PREG_OFFSET_CAPTURE); - // Reverse the matches array, otherwise all our offsets will become incorrect if we modify our way - // through the formula from left to right. Reversing means that we work right to left.through - // the formula - $cellReferences = array_reverse($cellReferences); - // Loop through each R1C1 style reference in turn, converting it to its A1 style equivalent, - // then modify the formula to use that new reference - foreach ($cellReferences as $cellReference) { - $rowReference = $cellReference[2][0]; - // Empty R reference is the current row - if ($rowReference == '') { - $rowReference = $rowID; - } - // Bracketed R references are relative to the current row - if ($rowReference[0] == '[') { - $rowReference = $rowID + trim($rowReference, '[]'); - } - $columnReference = $cellReference[4][0]; - // Empty C reference is the current column - if ($columnReference == '') { - $columnReference = $columnNumber; - } - // Bracketed C references are relative to the current column - if (is_string($columnReference) && $columnReference[0] == '[') { - $columnReference = $columnNumber + trim($columnReference, '[]'); - } - $A1CellReference = Coordinate::stringFromColumnIndex($columnReference) . $rowReference; - $value = substr_replace($value, $A1CellReference, $cellReference[0][1], strlen($cellReference[0][0])); - } - } - } - } - unset($value); - // Then rebuild the formula string - $cellDataFormula = implode('"', $temp); + $cellDataFormula = AddressHelper::convertFormulaToA1($cellDataFormula, $rowID, $columnNumber); } $spreadsheet->getActiveSheet()->getCell($columnID . $rowID)->setValueExplicit((($hasCalculatedValue) ? $cellDataFormula : $cellValue), $type); @@ -638,6 +603,21 @@ class Xml extends BaseReader ++$worksheetID; } + // Globally scoped defined names + $activeWorksheet = $spreadsheet->setActiveSheetIndex(0); + if (isset($xml->Names[0])) { + foreach ($xml->Names[0] as $definedName) { + $definedName_ss = $definedName->attributes($namespaces['ss']); + $name = (string) $definedName_ss['Name']; + $definedValue = (string) $definedName_ss['RefersTo']; + $convertedValue = AddressHelper::convertFormulaToA1($definedValue); + if ($convertedValue[0] === '=') { + $convertedValue = substr($convertedValue, 1); + } + $spreadsheet->addDefinedName(DefinedName::createInstance($name, $activeWorksheet, $convertedValue)); + } + } + // Return return $spreadsheet; } diff --git a/src/PhpSpreadsheet/ReferenceHelper.php b/src/PhpSpreadsheet/ReferenceHelper.php index 5809e3d4..9205b76e 100644 --- a/src/PhpSpreadsheet/ReferenceHelper.php +++ b/src/PhpSpreadsheet/ReferenceHelper.php @@ -2,6 +2,7 @@ namespace PhpOffice\PhpSpreadsheet; +use PhpOffice\PhpSpreadsheet\Calculation\Calculation; use PhpOffice\PhpSpreadsheet\Cell\Coordinate; use PhpOffice\PhpSpreadsheet\Cell\DataType; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; @@ -604,11 +605,11 @@ class ReferenceHelper } } - // Update workbook: named ranges - if (count($pSheet->getParent()->getNamedRanges()) > 0) { - foreach ($pSheet->getParent()->getNamedRanges() as $namedRange) { - if ($namedRange->getWorksheet()->getHashCode() == $pSheet->getHashCode()) { - $namedRange->setRange($this->updateCellReference($namedRange->getRange(), $pBefore, $pNumCols, $pNumRows)); + // Update workbook: define names + if (count($pSheet->getParent()->getDefinedNames()) > 0) { + foreach ($pSheet->getParent()->getDefinedNames() as $definedName) { + if ($definedName->getWorksheet()->getHashCode() === $pSheet->getHashCode()) { + $definedName->setValue($this->updateCellReference($definedName->getValue(), $pBefore, $pNumCols, $pNumRows)); } } } @@ -758,6 +759,141 @@ class ReferenceHelper return implode('"', $formulaBlocks); } + /** + * Update all cell references within a formula, irrespective of worksheet. + */ + public function updateFormulaReferencesAnyWorksheet(string $formula = '', int $insertColumns = 0, int $insertRows = 0): string + { + $formula = $this->updateCellReferencesAllWorksheets($formula, $insertColumns, $insertRows); + + if ($insertColumns !== 0) { + $formula = $this->updateColumnRangesAllWorksheets($formula, $insertColumns); + } + + if ($insertRows !== 0) { + $formula = $this->updateRowRangesAllWorksheets($formula, $insertRows); + } + + return $formula; + } + + private function updateCellReferencesAllWorksheets(string $formula, int $insertColumns, int $insertRows): string + { + $splitCount = preg_match_all( + '/' . Calculation::CALCULATION_REGEXP_CELLREF_RELATIVE . '/mui', + $formula, + $splitRanges, + PREG_OFFSET_CAPTURE + ); + + $columnLengths = array_map('strlen', array_column($splitRanges[6], 0)); + $rowLengths = array_map('strlen', array_column($splitRanges[7], 0)); + $columnOffsets = array_column($splitRanges[6], 1); + $rowOffsets = array_column($splitRanges[7], 1); + + $columns = $splitRanges[6]; + $rows = $splitRanges[7]; + + while ($splitCount > 0) { + --$splitCount; + $columnLength = $columnLengths[$splitCount]; + $rowLength = $rowLengths[$splitCount]; + $columnOffset = $columnOffsets[$splitCount]; + $rowOffset = $rowOffsets[$splitCount]; + $column = $columns[$splitCount][0]; + $row = $rows[$splitCount][0]; + + if (!empty($column) && $column[0] !== '$') { + $column = Coordinate::stringFromColumnIndex(Coordinate::columnIndexFromString($column) + $insertColumns); + $formula = substr($formula, 0, $columnOffset) . $column . substr($formula, $columnOffset + $columnLength); + } + if (!empty($row) && $row[0] !== '$') { + $row += $insertRows; + $formula = substr($formula, 0, $rowOffset) . $row . substr($formula, $rowOffset + $rowLength); + } + } + + return $formula; + } + + private function updateColumnRangesAllWorksheets(string $formula, int $insertColumns): string + { + $splitCount = preg_match_all( + '/' . Calculation::CALCULATION_REGEXP_COLUMNRANGE_RELATIVE . '/mui', + $formula, + $splitRanges, + PREG_OFFSET_CAPTURE + ); + + $fromColumnLengths = array_map('strlen', array_column($splitRanges[1], 0)); + $fromColumnOffsets = array_column($splitRanges[1], 1); + $toColumnLengths = array_map('strlen', array_column($splitRanges[2], 0)); + $toColumnOffsets = array_column($splitRanges[2], 1); + + $fromColumns = $splitRanges[1]; + $toColumns = $splitRanges[2]; + + while ($splitCount > 0) { + --$splitCount; + $fromColumnLength = $fromColumnLengths[$splitCount]; + $toColumnLength = $toColumnLengths[$splitCount]; + $fromColumnOffset = $fromColumnOffsets[$splitCount]; + $toColumnOffset = $toColumnOffsets[$splitCount]; + $fromColumn = $fromColumns[$splitCount][0]; + $toColumn = $toColumns[$splitCount][0]; + + if (!empty($fromColumn) && $fromColumn[0] !== '$') { + $fromColumn = Coordinate::stringFromColumnIndex(Coordinate::columnIndexFromString($fromColumn) + $insertColumns); + $formula = substr($formula, 0, $fromColumnOffset) . $fromColumn . substr($formula, $fromColumnOffset + $fromColumnLength); + } + if (!empty($toColumn) && $toColumn[0] !== '$') { + $toColumn = Coordinate::stringFromColumnIndex(Coordinate::columnIndexFromString($toColumn) + $insertColumns); + $formula = substr($formula, 0, $toColumnOffset) . $toColumn . substr($formula, $toColumnOffset + $toColumnLength); + } + } + + return $formula; + } + + private function updateRowRangesAllWorksheets(string $formula, int $insertRows): string + { + $splitCount = preg_match_all( + '/' . Calculation::CALCULATION_REGEXP_ROWRANGE_RELATIVE . '/mui', + $formula, + $splitRanges, + PREG_OFFSET_CAPTURE + ); + + $fromRowLengths = array_map('strlen', array_column($splitRanges[1], 0)); + $fromRowOffsets = array_column($splitRanges[1], 1); + $toRowLengths = array_map('strlen', array_column($splitRanges[2], 0)); + $toRowOffsets = array_column($splitRanges[2], 1); + + $fromRows = $splitRanges[1]; + $toRows = $splitRanges[2]; + + while ($splitCount > 0) { + --$splitCount; + $fromRowLength = $fromRowLengths[$splitCount]; + $toRowLength = $toRowLengths[$splitCount]; + $fromRowOffset = $fromRowOffsets[$splitCount]; + $toRowOffset = $toRowOffsets[$splitCount]; + $fromRow = $fromRows[$splitCount][0]; + $toRow = $toRows[$splitCount][0]; + + if (!empty($fromRow) && $fromRow[0] !== '$') { + $fromRow += $insertRows; + $formula = substr($formula, 0, $fromRowOffset) . $fromRow . substr($formula, $fromRowOffset + $fromRowLength); + } + if (!empty($toRow) && $toRow[0] !== '$') { + $toRow += $insertRows; + $formula = substr($formula, 0, $toRowOffset) . $toRow . substr($formula, $toRowOffset + $toRowLength); + } + } + + return $formula; + } + /** * Update cell reference. * diff --git a/src/PhpSpreadsheet/Spreadsheet.php b/src/PhpSpreadsheet/Spreadsheet.php index 46e97852..19c11526 100644 --- a/src/PhpSpreadsheet/Spreadsheet.php +++ b/src/PhpSpreadsheet/Spreadsheet.php @@ -3,6 +3,7 @@ namespace PhpOffice\PhpSpreadsheet; use PhpOffice\PhpSpreadsheet\Calculation\Calculation; +use PhpOffice\PhpSpreadsheet\Shared\StringHelper; use PhpOffice\PhpSpreadsheet\Style\Style; use PhpOffice\PhpSpreadsheet\Worksheet\Iterator; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; @@ -14,6 +15,9 @@ class Spreadsheet const VISIBILITY_HIDDEN = 'hidden'; const VISIBILITY_VERY_HIDDEN = 'veryHidden'; + private const DEFINED_NAME_IS_RANGE = false; + private const DEFINED_NAME_IS_FORMULA = true; + private static $workbookViewVisibilityValues = [ self::VISIBILITY_VISIBLE, self::VISIBILITY_HIDDEN, @@ -67,7 +71,7 @@ class Spreadsheet * * @var NamedRange[] */ - private $namedRanges = []; + private $definedNames = []; /** * CellXf supervisor. @@ -482,8 +486,8 @@ class Spreadsheet // Create document security $this->security = new Document\Security(); - // Set named ranges - $this->namedRanges = []; + // Set defined names + $this->definedNames = []; // Create the cellXf supervisor $this->cellXfSupervisor = new Style(true); @@ -724,7 +728,7 @@ class Spreadsheet public function getIndex(Worksheet $pSheet) { foreach ($this->workSheetCollection as $key => $value) { - if ($value->getHashCode() == $pSheet->getHashCode()) { + if ($value->getHashCode() === $pSheet->getHashCode()) { return $key; } } @@ -868,54 +872,159 @@ class Spreadsheet } /** - * Get named ranges. + * Get an array of all Named Ranges. * * @return NamedRange[] */ - public function getNamedRanges() + public function getNamedRanges(): array { - return $this->namedRanges; + return array_filter( + $this->definedNames, + function (DefinedName $definedName) { + return $definedName->isFormula() === self::DEFINED_NAME_IS_RANGE; + } + ); } /** - * Add named range. + * Get an array of all Named Formulae. * - * @return bool + * @return NamedFormula[] */ - public function addNamedRange(NamedRange $namedRange) + public function getNamedFormulae(): array { - if ($namedRange->getScope() == null) { + return array_filter( + $this->definedNames, + function (DefinedName $definedName) { + return $definedName->isFormula() === self::DEFINED_NAME_IS_FORMULA; + } + ); + } + + /** + * Get an array of all Defined Names (both named ranges and named formulae). + * + * @return DefinedName[] + */ + public function getDefinedNames(): array + { + return $this->definedNames; + } + + /** + * Add a named range. + * If a named range with this name already exists, then this will replace the existing value. + */ + public function addNamedRange(NamedRange $namedRange): void + { + $this->addDefinedName($namedRange); + } + + /** + * Add a named formula. + * If a named formula with this name already exists, then this will replace the existing value. + */ + public function addNamedFormula(NamedFormula $namedFormula): void + { + $this->addDefinedName($namedFormula); + } + + /** + * Add a defined name (either a named range or a named formula). + * If a defined named with this name already exists, then this will replace the existing value. + */ + public function addDefinedName(DefinedName $definedName): void + { + $upperCaseName = StringHelper::strToUpper($definedName->getName()); + if ($definedName->getScope() == null) { // global scope - $this->namedRanges[$namedRange->getName()] = $namedRange; + $this->definedNames[$upperCaseName] = $definedName; } else { // local scope - $this->namedRanges[$namedRange->getScope()->getTitle() . '!' . $namedRange->getName()] = $namedRange; + $this->definedNames[$definedName->getScope()->getTitle() . '!' . $upperCaseName] = $definedName; } - - return true; } /** * Get named range. * - * @param string $namedRange * @param null|Worksheet $pSheet Scope. Use null for global scope - * - * @return null|NamedRange */ - public function getNamedRange($namedRange, ?Worksheet $pSheet = null) + public function getNamedRange(string $namedRange, ?Worksheet $pSheet = null): ?NamedRange { $returnValue = null; - if ($namedRange != '' && ($namedRange !== null)) { + if ($namedRange !== '') { + $namedRange = StringHelper::strToUpper($namedRange); + // first look for global named range + $returnValue = $this->getGlobalDefinedNameByType($namedRange, self::DEFINED_NAME_IS_RANGE); + // then look for local named range (has priority over global named range if both names exist) + $returnValue = $this->getLocalDefinedNameByType($namedRange, self::DEFINED_NAME_IS_RANGE, $pSheet) ?: $returnValue; + } + + return $returnValue instanceof NamedRange ? $returnValue : null; + } + + /** + * Get named formula. + * + * @param null|Worksheet $pSheet Scope. Use null for global scope + */ + public function getNamedFormula(string $namedFormula, ?Worksheet $pSheet = null): ?NamedFormula + { + $returnValue = null; + + if ($namedFormula !== '') { + $namedFormula = StringHelper::strToUpper($namedFormula); + // first look for global named formula + $returnValue = $this->getGlobalDefinedNameByType($namedFormula, self::DEFINED_NAME_IS_FORMULA); + // then look for local named formula (has priority over global named formula if both names exist) + $returnValue = $this->getLocalDefinedNameByType($namedFormula, self::DEFINED_NAME_IS_FORMULA, $pSheet) ?: $returnValue; + } + + return $returnValue instanceof NamedFormula ? $returnValue : null; + } + + private function getGlobalDefinedNameByType(string $name, bool $type): ?DefinedName + { + if (isset($this->definedNames[$name]) && $this->definedNames[$name]->isFormula() === $type) { + return $this->definedNames[$name]; + } + + return null; + } + + private function getLocalDefinedNameByType(string $name, bool $type, ?Worksheet $pSheet = null): ?DefinedName + { + if ( + ($pSheet !== null) && isset($this->definedNames[$pSheet->getTitle() . '!' . $name]) + && $this->definedNames[$pSheet->getTitle() . '!' . $name]->isFormula() === $type + ) { + return $this->definedNames[$pSheet->getTitle() . '!' . $name]; + } + + return null; + } + + /** + * Get named range. + * + * @param null|Worksheet $pSheet Scope. Use null for global scope + */ + public function getDefinedName(string $definedName, ?Worksheet $pSheet = null): ?DefinedName + { + $returnValue = null; + + if ($definedName !== '') { + $definedName = StringHelper::strToUpper($definedName); // first look for global defined name - if (isset($this->namedRanges[$namedRange])) { - $returnValue = $this->namedRanges[$namedRange]; + if (isset($this->definedNames[$definedName])) { + $returnValue = $this->definedNames[$definedName]; } // then look for local defined name (has priority over global defined name if both names exist) - if (($pSheet !== null) && isset($this->namedRanges[$pSheet->getTitle() . '!' . $namedRange])) { - $returnValue = $this->namedRanges[$pSheet->getTitle() . '!' . $namedRange]; + if (($pSheet !== null) && isset($this->definedNames[$pSheet->getTitle() . '!' . $definedName])) { + $returnValue = $this->definedNames[$pSheet->getTitle() . '!' . $definedName]; } } @@ -925,20 +1034,55 @@ class Spreadsheet /** * Remove named range. * - * @param string $namedRange * @param null|Worksheet $pSheet scope: use null for global scope * * @return $this */ - public function removeNamedRange($namedRange, ?Worksheet $pSheet = null) + public function removeNamedRange(string $namedRange, ?Worksheet $pSheet = null): self { + if ($this->getNamedRange($namedRange, $pSheet) === null) { + return $this; + } + + return $this->removeDefinedName($namedRange, $pSheet); + } + + /** + * Remove named formula. + * + * @param null|Worksheet $pSheet scope: use null for global scope + * + * @return $this + */ + public function removeNamedFormula(string $namedFormula, ?Worksheet $pSheet = null): self + { + if ($this->getNamedFormula($namedFormula, $pSheet) === null) { + return $this; + } + + return $this->removeDefinedName($namedFormula, $pSheet); + } + + /** + * Remove defined name. + * + * @param null|Worksheet $pSheet scope: use null for global scope + * + * @return $this + */ + public function removeDefinedName(string $definedName, ?Worksheet $pSheet = null): self + { + $definedName = StringHelper::strToUpper($definedName); + if ($pSheet === null) { - if (isset($this->namedRanges[$namedRange])) { - unset($this->namedRanges[$namedRange]); + if (isset($this->definedNames[$definedName])) { + unset($this->definedNames[$definedName]); } } else { - if (isset($this->namedRanges[$pSheet->getTitle() . '!' . $namedRange])) { - unset($this->namedRanges[$pSheet->getTitle() . '!' . $namedRange]); + if (isset($this->definedNames[$pSheet->getTitle() . '!' . $definedName])) { + unset($this->definedNames[$pSheet->getTitle() . '!' . $definedName]); + } elseif (isset($this->definedNames[$definedName])) { + unset($this->definedNames[$definedName]); } } @@ -1017,7 +1161,7 @@ class Spreadsheet public function getCellXfByHashCode($pValue) { foreach ($this->cellXfCollection as $cellXf) { - if ($cellXf->getHashCode() == $pValue) { + if ($cellXf->getHashCode() === $pValue) { return $cellXf; } } @@ -1132,7 +1276,7 @@ class Spreadsheet public function getCellStyleXfByHashCode($pValue) { foreach ($this->cellStyleXfCollection as $cellStyleXf) { - if ($cellStyleXf->getHashCode() == $pValue) { + if ($cellStyleXf->getHashCode() === $pValue) { return $cellStyleXf; } } diff --git a/src/PhpSpreadsheet/Worksheet/BaseDrawing.php b/src/PhpSpreadsheet/Worksheet/BaseDrawing.php index 1b145a81..be2f23df 100644 --- a/src/PhpSpreadsheet/Worksheet/BaseDrawing.php +++ b/src/PhpSpreadsheet/Worksheet/BaseDrawing.php @@ -218,7 +218,7 @@ class BaseDrawing implements IComparable $iterator = $this->worksheet->getDrawingCollection()->getIterator(); while ($iterator->valid()) { - if ($iterator->current()->getHashCode() == $this->getHashCode()) { + if ($iterator->current()->getHashCode() === $this->getHashCode()) { $this->worksheet->getDrawingCollection()->offsetUnset($iterator->key()); $this->worksheet = null; diff --git a/src/PhpSpreadsheet/Worksheet/Worksheet.php b/src/PhpSpreadsheet/Worksheet/Worksheet.php index 3acab637..19833b71 100644 --- a/src/PhpSpreadsheet/Worksheet/Worksheet.php +++ b/src/PhpSpreadsheet/Worksheet/Worksheet.php @@ -13,9 +13,9 @@ use PhpOffice\PhpSpreadsheet\Chart\Chart; use PhpOffice\PhpSpreadsheet\Collection\Cells; use PhpOffice\PhpSpreadsheet\Collection\CellsFactory; use PhpOffice\PhpSpreadsheet\Comment; +use PhpOffice\PhpSpreadsheet\DefinedName; use PhpOffice\PhpSpreadsheet\Exception; use PhpOffice\PhpSpreadsheet\IComparable; -use PhpOffice\PhpSpreadsheet\NamedRange; use PhpOffice\PhpSpreadsheet\ReferenceHelper; use PhpOffice\PhpSpreadsheet\RichText\RichText; use PhpOffice\PhpSpreadsheet\Shared; @@ -797,9 +797,9 @@ class Worksheet implements IComparable public function rebindParent(Spreadsheet $parent) { if ($this->parent !== null) { - $namedRanges = $this->parent->getNamedRanges(); - foreach ($namedRanges as $namedRange) { - $parent->addNamedRange($namedRange); + $definedNames = $this->parent->getDefinedNames(); + foreach ($definedNames as $definedName) { + $parent->addDefinedName($definedName); } $this->parent->removeSheetByIndex( @@ -1192,11 +1192,11 @@ class Worksheet implements IComparable // Named range? if ( (!preg_match('/^' . Calculation::CALCULATION_REGEXP_CELLREF . '$/i', $pCoordinate, $matches)) && - (preg_match('/^' . Calculation::CALCULATION_REGEXP_NAMEDRANGE . '$/i', $pCoordinate, $matches)) + (preg_match('/^' . Calculation::CALCULATION_REGEXP_DEFINEDNAME . '$/i', $pCoordinate, $matches)) ) { - $namedRange = NamedRange::resolveRange($pCoordinate, $this); + $namedRange = DefinedName::resolveName($pCoordinate, $this); if ($namedRange !== null) { - $pCoordinate = $namedRange->getRange(); + $pCoordinate = $namedRange->getValue(); return $namedRange->getWorksheet()->getCell($pCoordinate, $createIfNotExists); } @@ -1292,11 +1292,11 @@ class Worksheet implements IComparable // Named range? if ( (!preg_match('/^' . Calculation::CALCULATION_REGEXP_CELLREF . '$/i', $pCoordinate, $matches)) && - (preg_match('/^' . Calculation::CALCULATION_REGEXP_NAMEDRANGE . '$/i', $pCoordinate, $matches)) + (preg_match('/^' . Calculation::CALCULATION_REGEXP_DEFINEDNAME . '$/i', $pCoordinate, $matches)) ) { - $namedRange = NamedRange::resolveRange($pCoordinate, $this); + $namedRange = DefinedName::resolveName($pCoordinate, $this); if ($namedRange !== null) { - $pCoordinate = $namedRange->getRange(); + $pCoordinate = $namedRange->getValue(); if ($this->getHashCode() != $namedRange->getWorksheet()->getHashCode()) { if (!$namedRange->getLocalOnly()) { return $namedRange->getWorksheet()->cellExists($pCoordinate); @@ -2564,10 +2564,10 @@ class Worksheet implements IComparable */ public function namedRangeToArray($pNamedRange, $nullValue = null, $calculateFormulas = true, $formatData = true, $returnCellRef = false) { - $namedRange = NamedRange::resolveRange($pNamedRange, $this); + $namedRange = DefinedName::resolveName($pNamedRange, $this); if ($namedRange !== null) { $pWorkSheet = $namedRange->getWorksheet(); - $pCellRange = $namedRange->getRange(); + $pCellRange = $namedRange->getValue(); return $pWorkSheet->rangeToArray($pCellRange, $nullValue, $calculateFormulas, $formatData, $returnCellRef); } diff --git a/src/PhpSpreadsheet/Writer/Ods/Content.php b/src/PhpSpreadsheet/Writer/Ods/Content.php index 6588473a..96e66850 100644 --- a/src/PhpSpreadsheet/Writer/Ods/Content.php +++ b/src/PhpSpreadsheet/Writer/Ods/Content.php @@ -12,6 +12,7 @@ use PhpOffice\PhpSpreadsheet\Style\Font; use PhpOffice\PhpSpreadsheet\Worksheet\Row; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use PhpOffice\PhpSpreadsheet\Writer\Exception; +use PhpOffice\PhpSpreadsheet\Writer\Ods; use PhpOffice\PhpSpreadsheet\Writer\Ods\Cell\Comment; /** @@ -23,6 +24,18 @@ class Content extends WriterPart const NUMBER_ROWS_REPEATED_MAX = 1048576; const CELL_STYLE_PREFIX = 'ce'; + private $formulaConvertor; + + /** + * Set parent Ods writer. + */ + public function __construct(Ods $writer) + { + parent::__construct($writer); + + $this->formulaConvertor = new Formula($this->getParentWriter()->getSpreadsheet()->getDefinedNames()); + } + /** * Write content.xml to XML format. * @@ -90,7 +103,9 @@ class Content extends WriterPart $this->writeSheets($objWriter); - $objWriter->writeElement('table:named-expressions'); + // Defined names (ranges and formulae) + (new NamedExpressions($objWriter, $this->getParentWriter()->getSpreadsheet(), $this->formulaConvertor))->write(); + $objWriter->endElement(); $objWriter->endElement(); $objWriter->endElement(); @@ -193,7 +208,7 @@ class Content extends WriterPart // don't do anything } } - $objWriter->writeAttribute('table:formula', 'of:' . $cell->getValue()); + $objWriter->writeAttribute('table:formula', $this->formulaConvertor->convertFormula($cell->getValue())); if (is_numeric($formulaValue)) { $objWriter->writeAttribute('office:value-type', 'float'); } else { diff --git a/src/PhpSpreadsheet/Writer/Ods/Formula.php b/src/PhpSpreadsheet/Writer/Ods/Formula.php new file mode 100644 index 00000000..db766fb4 --- /dev/null +++ b/src/PhpSpreadsheet/Writer/Ods/Formula.php @@ -0,0 +1,119 @@ +definedNames[] = $definedName->getName(); + } + } + + public function convertFormula(string $formula, string $worksheetName = ''): string + { + $formula = $this->convertCellReferences($formula, $worksheetName); + $formula = $this->convertDefinedNames($formula); + + if (substr($formula, 0, 1) !== '=') { + $formula = '=' . $formula; + } + + return 'of:' . $formula; + } + + private function convertDefinedNames(string $formula): string + { + $splitCount = preg_match_all( + '/' . Calculation::CALCULATION_REGEXP_DEFINEDNAME . '/mui', + $formula, + $splitRanges, + PREG_OFFSET_CAPTURE + ); + + $lengths = array_map('strlen', array_column($splitRanges[0], 0)); + $offsets = array_column($splitRanges[0], 1); + $values = array_column($splitRanges[0], 0); + + while ($splitCount > 0) { + --$splitCount; + $length = $lengths[$splitCount]; + $offset = $offsets[$splitCount]; + $value = $values[$splitCount]; + + if (in_array($value, $this->definedNames, true)) { + $formula = substr($formula, 0, $offset) . '$$' . $value . substr($formula, $offset + $length); + } + } + + return $formula; + } + + private function convertCellReferences(string $formula, string $worksheetName): string + { + $splitCount = preg_match_all( + '/' . Calculation::CALCULATION_REGEXP_CELLREF_RELATIVE . '/mui', + $formula, + $splitRanges, + PREG_OFFSET_CAPTURE + ); + + $lengths = array_map('strlen', array_column($splitRanges[0], 0)); + $offsets = array_column($splitRanges[0], 1); + + $worksheets = $splitRanges[2]; + $columns = $splitRanges[6]; + $rows = $splitRanges[7]; + + // Replace any commas in the formula with semi-colons for Ods + // If by chance there are commas in worksheet names, then they will be "fixed" again in the loop + // because we've already extracted worksheet names with our preg_match_all() + $formula = str_replace(',', ';', $formula); + while ($splitCount > 0) { + --$splitCount; + $length = $lengths[$splitCount]; + $offset = $offsets[$splitCount]; + $worksheet = $worksheets[$splitCount][0]; + $column = $columns[$splitCount][0]; + $row = $rows[$splitCount][0]; + + $newRange = ''; + if (empty($worksheet)) { + if (($offset === 0) || ($formula[$offset - 1] !== ':')) { + // We need a worksheet + $worksheet = $worksheetName; + } + } else { + $worksheet = str_replace("''", "'", trim($worksheet, "'")); + } + if (!empty($worksheet)) { + $newRange = "['" . str_replace("'", "''", $worksheet) . "'"; + } elseif (substr($formula, $offset - 1, 1) !== ':') { + $newRange = '['; + } + $newRange .= '.'; + + if (!empty($column)) { + $newRange .= $column; + } + if (!empty($row)) { + $newRange .= $row; + } + // close the wrapping [] unless this is the first part of a range + $newRange .= substr($formula, $offset + $length, 1) !== ':' ? ']' : ''; + + $formula = substr($formula, 0, $offset) . $newRange . substr($formula, $offset + $length); + } + + return $formula; + } +} diff --git a/src/PhpSpreadsheet/Writer/Ods/NamedExpressions.php b/src/PhpSpreadsheet/Writer/Ods/NamedExpressions.php new file mode 100644 index 00000000..9edc5c64 --- /dev/null +++ b/src/PhpSpreadsheet/Writer/Ods/NamedExpressions.php @@ -0,0 +1,126 @@ +objWriter = $objWriter; + $this->spreadsheet = $spreadsheet; + $this->formulaConvertor = $formulaConvertor; + } + + public function write(): void + { + $this->objWriter->startElement('table:named-expressions'); + $this->writeExpressions(); + $this->objWriter->endElement(); + } + + private function writeExpressions(): void + { + $definedNames = $this->spreadsheet->getDefinedNames(); + + foreach ($definedNames as $definedName) { + if ($definedName->isFormula()) { + $this->objWriter->startElement('table:named-expression'); + $this->writeNamedFormula($definedName, $this->spreadsheet->getActiveSheet()); + } else { + $this->objWriter->startElement('table:named-range'); + $this->writeNamedRange($definedName); + } + + $this->objWriter->endElement(); + } + } + + private function writeNamedFormula(DefinedName $definedName, Worksheet $defaultWorksheet): void + { + $this->objWriter->writeAttribute('table:name', $definedName->getName()); + $this->objWriter->writeAttribute( + 'table:expression', + $this->formulaConvertor->convertFormula($definedName->getValue(), $definedName->getWorksheet()->getTitle()) + ); + $this->objWriter->writeAttribute('table:base-cell-address', $this->convertAddress( + $definedName, + "'" . (($definedName->getWorksheet() !== null) ? $definedName->getWorksheet()->getTitle() : $defaultWorksheet->getTitle()) . "'!\$A\$1" + )); + } + + private function writeNamedRange(DefinedName $definedName): void + { + $this->objWriter->writeAttribute('table:name', $definedName->getName()); + $this->objWriter->writeAttribute('table:base-cell-address', $this->convertAddress( + $definedName, + "'" . $definedName->getWorksheet()->getTitle() . "'!\$A\$1" + )); + $this->objWriter->writeAttribute('table:cell-range-address', $this->convertAddress($definedName, $definedName->getValue())); + } + + private function convertAddress(DefinedName $definedName, string $address): string + { + $splitCount = preg_match_all( + '/' . Calculation::CALCULATION_REGEXP_CELLREF_RELATIVE . '/mui', + $address, + $splitRanges, + PREG_OFFSET_CAPTURE + ); + + $lengths = array_map('strlen', array_column($splitRanges[0], 0)); + $offsets = array_column($splitRanges[0], 1); + + $worksheets = $splitRanges[2]; + $columns = $splitRanges[6]; + $rows = $splitRanges[7]; + + while ($splitCount > 0) { + --$splitCount; + $length = $lengths[$splitCount]; + $offset = $offsets[$splitCount]; + $worksheet = $worksheets[$splitCount][0]; + $column = $columns[$splitCount][0]; + $row = $rows[$splitCount][0]; + + $newRange = ''; + if (empty($worksheet)) { + if (($offset === 0) || ($address[$offset - 1] !== ':')) { + // We need a worksheet + $worksheet = $definedName->getWorksheet()->getTitle(); + } + } else { + $worksheet = str_replace("''", "'", trim($worksheet, "'")); + } + if (!empty($worksheet)) { + $newRange = "'" . str_replace("'", "''", $worksheet) . "'."; + } + + if (!empty($column)) { + $newRange .= $column; + } + if (!empty($row)) { + $newRange .= $row; + } + + $address = substr($address, 0, $offset) . $newRange . substr($address, $offset + $length); + } + + if (substr($address, 0, 1) === '=') { + $address = substr($address, 1); + } + + return $address; + } +} diff --git a/src/PhpSpreadsheet/Writer/Xls.php b/src/PhpSpreadsheet/Writer/Xls.php index 4f4b256a..c7c2e7d6 100644 --- a/src/PhpSpreadsheet/Writer/Xls.php +++ b/src/PhpSpreadsheet/Writer/Xls.php @@ -108,7 +108,7 @@ class Xls extends BaseWriter { $this->spreadsheet = $spreadsheet; - $this->parser = new Xls\Parser(); + $this->parser = new Xls\Parser($spreadsheet); } /** diff --git a/src/PhpSpreadsheet/Writer/Xls/Parser.php b/src/PhpSpreadsheet/Writer/Xls/Parser.php index e3b01260..f89957a4 100644 --- a/src/PhpSpreadsheet/Writer/Xls/Parser.php +++ b/src/PhpSpreadsheet/Writer/Xls/Parser.php @@ -2,7 +2,9 @@ namespace PhpOffice\PhpSpreadsheet\Writer\Xls; +use PhpOffice\PhpSpreadsheet\Calculation\Calculation; use PhpOffice\PhpSpreadsheet\Shared\StringHelper; +use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet as PhpspreadsheetWorksheet; use PhpOffice\PhpSpreadsheet\Writer\Exception as WriterException; @@ -78,7 +80,7 @@ class Parser * * @var string */ - private $parseTree; + public $parseTree; /** * Array of external sheets. @@ -467,11 +469,15 @@ class Parser 'BAHTTEXT' => [368, 1, 0, 0], ]; + private $spreadsheet; + /** * The class constructor. */ - public function __construct() + public function __construct(Spreadsheet $spreadsheet) { + $this->spreadsheet = $spreadsheet; + $this->currentCharacter = 0; $this->currentToken = ''; // The token we are working on. $this->formula = ''; // The formula to parse. @@ -518,6 +524,8 @@ class Parser // match error codes } elseif (preg_match('/^#[A-Z0\\/]{3,5}[!?]{1}$/', $token) || $token == '#N/A') { return $this->convertError($token); + } elseif (preg_match('/^' . Calculation::CALCULATION_REGEXP_DEFINEDNAME . '$/mui', $token) && $this->spreadsheet->getDefinedName($token) !== null) { + return $this->convertDefinedName($token); // commented so argument number can be processed correctly. See toReversePolish(). /*elseif (preg_match("/[A-Z0-9\xc0-\xdc\.]+/", $token)) { @@ -739,6 +747,26 @@ class Parser return pack('C', 0xFF); } + private function convertDefinedName(string $name): void + { + if (strlen($name) > 255) { + throw new WriterException('Defined Name is too long'); + } + + $nameReference = 1; + foreach ($this->spreadsheet->getDefinedNames() as $definedName) { + if ($name === $definedName->getName()) { + break; + } + ++$nameReference; + } + + $ptgRef = pack('Cvxx', $this->ptg['ptgName'], $nameReference); + + throw new WriterException('Cannot yet write formulae with defined names to Xls'); +// return $ptgRef; + } + /** * Look up the REF index that corresponds to an external sheet name * (or range). If it doesn't exist yet add it to the workbook's references @@ -1056,6 +1084,8 @@ class Parser } elseif (preg_match("/^[A-Z0-9\xc0-\xdc\\.]+$/i", $token) && ($this->lookAhead === '(')) { // if it's a function call return $token; + } elseif (preg_match('/^' . Calculation::CALCULATION_REGEXP_DEFINEDNAME . '$/miu', $token) && $this->spreadsheet->getDefinedName($token) !== null) { + return $token; } elseif (substr($token, -1) === ')') { // It's an argument of some description (e.g. a named range), // precise nature yet to be determined @@ -1174,8 +1204,8 @@ class Parser $result = $this->term(); while ( ($this->currentToken == '+') || - ($this->currentToken == '-') || - ($this->currentToken == '^') + ($this->currentToken == '-') || + ($this->currentToken == '^') ) { if ($this->currentToken == '+') { $this->advance(); @@ -1219,7 +1249,7 @@ class Parser $result = $this->fact(); while ( ($this->currentToken == '*') || - ($this->currentToken == '/') + ($this->currentToken == '/') ) { if ($this->currentToken == '*') { $this->advance(); @@ -1277,7 +1307,7 @@ class Parser return $result; } elseif ( preg_match('/^(\$)?[A-Ia-i]?[A-Za-z](\$)?\d+:(\$)?[A-Ia-i]?[A-Za-z](\$)?\d+$/', $this->currentToken) || - preg_match('/^(\$)?[A-Ia-i]?[A-Za-z](\$)?\d+\.\.(\$)?[A-Ia-i]?[A-Za-z](\$)?\d+$/', $this->currentToken) + preg_match('/^(\$)?[A-Ia-i]?[A-Za-z](\$)?\d+\.\.(\$)?[A-Ia-i]?[A-Za-z](\$)?\d+$/', $this->currentToken) ) { // if it's a range A1:B2 or $A$1:$B$2 // must be an error? @@ -1310,9 +1340,14 @@ class Parser $this->advance(); return $result; - } elseif (preg_match("/^[A-Z0-9\xc0-\xdc\\.]+$/i", $this->currentToken)) { + } elseif (preg_match("/^[A-Z0-9\xc0-\xdc\\.]+$/i", $this->currentToken) && ($this->lookAhead === '(')) { // if it's a function call return $this->func(); + } elseif (preg_match('/^' . Calculation::CALCULATION_REGEXP_DEFINEDNAME . '$/miu', $this->currentToken) && $this->spreadsheet->getDefinedName($this->currentToken) !== null) { + $result = $this->createTree('ptgName', $this->currentToken, ''); + $this->advance(); + + return $result; } throw new WriterException('Syntax error: ' . $this->currentToken . ', lookahead: ' . $this->lookAhead . ', current char: ' . $this->currentCharacter); diff --git a/src/PhpSpreadsheet/Writer/Xls/Workbook.php b/src/PhpSpreadsheet/Writer/Xls/Workbook.php index d5f61bf2..27d3395f 100644 --- a/src/PhpSpreadsheet/Writer/Xls/Workbook.php +++ b/src/PhpSpreadsheet/Writer/Xls/Workbook.php @@ -2,7 +2,10 @@ namespace PhpOffice\PhpSpreadsheet\Writer\Xls; +use PHP_CodeSniffer\Tokenizers\PHP; +use PhpOffice\PhpSpreadsheet\Calculation\Calculation; use PhpOffice\PhpSpreadsheet\Cell\Coordinate; +use PhpOffice\PhpSpreadsheet\DefinedName; use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException; use PhpOffice\PhpSpreadsheet\Shared\Date; use PhpOffice\PhpSpreadsheet\Shared\StringHelper; @@ -521,6 +524,57 @@ class Workbook extends BIFFwriter $this->writeStyle(); } + private function parseDefinedNameValue(DefinedName $pDefinedName): string + { + $definedRange = $pDefinedName->getValue(); + $splitCount = preg_match_all( + '/' . Calculation::CALCULATION_REGEXP_CELLREF . '/mui', + $definedRange, + $splitRanges, + PREG_OFFSET_CAPTURE + ); + + $lengths = array_map('strlen', array_column($splitRanges[0], 0)); + $offsets = array_column($splitRanges[0], 1); + + $worksheets = $splitRanges[2]; + $columns = $splitRanges[6]; + $rows = $splitRanges[7]; + + while ($splitCount > 0) { + --$splitCount; + $length = $lengths[$splitCount]; + $offset = $offsets[$splitCount]; + $worksheet = $worksheets[$splitCount][0]; + $column = $columns[$splitCount][0]; + $row = $rows[$splitCount][0]; + + $newRange = ''; + if (empty($worksheet)) { + if (($offset === 0) || ($definedRange[$offset - 1] !== ':')) { + // We need a worksheet + $worksheet = $pDefinedName->getWorksheet()->getTitle(); + } + } else { + $worksheet = str_replace("''", "'", trim($worksheet, "'")); + } + if (!empty($worksheet)) { + $newRange = "'" . str_replace("'", "''", $worksheet) . "'!"; + } + + if (!empty($column)) { + $newRange .= "\${$column}"; + } + if (!empty($row)) { + $newRange .= "\${$row}"; + } + + $definedRange = substr($definedRange, 0, $offset) . $newRange . substr($definedRange, $offset + $length); + } + + return $definedRange; + } + /** * Writes all the DEFINEDNAME records (BIFF8). * So far this is only used for repeating rows/columns (print titles) and print areas. @@ -530,20 +584,11 @@ class Workbook extends BIFFwriter $chunk = ''; // Named ranges - if (count($this->spreadsheet->getNamedRanges()) > 0) { + $definedNames = $this->spreadsheet->getDefinedNames(); + if (count($definedNames) > 0) { // Loop named ranges - $namedRanges = $this->spreadsheet->getNamedRanges(); - foreach ($namedRanges as $namedRange) { - // Create absolute coordinate - $range = Coordinate::splitRange($namedRange->getRange()); - $iMax = count($range); - for ($i = 0; $i < $iMax; ++$i) { - $range[$i][0] = '\'' . str_replace("'", "''", $namedRange->getWorksheet()->getTitle()) . '\'!' . Coordinate::absoluteCoordinate($range[$i][0]); - if (isset($range[$i][1])) { - $range[$i][1] = Coordinate::absoluteCoordinate($range[$i][1]); - } - } - $range = Coordinate::buildRange($range); // e.g. Sheet1!$A$1:$B$2 + foreach ($definedNames as $definedName) { + $range = $this->parseDefinedNameValue($definedName); // parse formula try { @@ -555,14 +600,14 @@ class Workbook extends BIFFwriter $formulaData = "\x3A" . substr($formulaData, 1); } - if ($namedRange->getLocalOnly()) { + if ($definedName->getLocalOnly()) { // local scope - $scope = $this->spreadsheet->getIndex($namedRange->getScope()) + 1; + $scope = $this->spreadsheet->getIndex($definedName->getScope()) + 1; } else { // global scope $scope = 0; } - $chunk .= $this->writeData($this->writeDefinedNameBiff8($namedRange->getName(), $formulaData, $scope, false)); + $chunk .= $this->writeData($this->writeDefinedNameBiff8($definedName->getName(), $formulaData, $scope, false)); } catch (PhpSpreadsheetException $e) { // do nothing } diff --git a/src/PhpSpreadsheet/Writer/Xls/Worksheet.php b/src/PhpSpreadsheet/Writer/Xls/Worksheet.php index a1c485c2..a1c258c0 100644 --- a/src/PhpSpreadsheet/Writer/Xls/Worksheet.php +++ b/src/PhpSpreadsheet/Writer/Xls/Worksheet.php @@ -825,7 +825,6 @@ class Worksheet extends BIFFwriter private function writeFormula($row, $col, $formula, $xfIndex, $calculatedValue) { $record = 0x0006; // Record identifier - // Initialize possible additional value for STRING record that should be written after the FORMULA record? $stringValue = null; diff --git a/src/PhpSpreadsheet/Writer/Xlsx/DefinedNames.php b/src/PhpSpreadsheet/Writer/Xlsx/DefinedNames.php new file mode 100644 index 00000000..8c3da827 --- /dev/null +++ b/src/PhpSpreadsheet/Writer/Xlsx/DefinedNames.php @@ -0,0 +1,223 @@ +objWriter = $objWriter; + $this->spreadsheet = $spreadsheet; + } + + public function write(): void + { + // Write defined names + $this->objWriter->startElement('definedNames'); + + // Named ranges + if (count($this->spreadsheet->getDefinedNames()) > 0) { + // Named ranges + $this->writeNamedRangesAndFormulae(); + } + + // Other defined names + $sheetCount = $this->spreadsheet->getSheetCount(); + for ($i = 0; $i < $sheetCount; ++$i) { + // NamedRange for autoFilter + $this->writeNamedRangeForAutofilter($this->spreadsheet->getSheet($i), $i); + + // NamedRange for Print_Titles + $this->writeNamedRangeForPrintTitles($this->spreadsheet->getSheet($i), $i); + + // NamedRange for Print_Area + $this->writeNamedRangeForPrintArea($this->spreadsheet->getSheet($i), $i); + } + + $this->objWriter->endElement(); + } + + /** + * Write defined names. + */ + private function writeNamedRangesAndFormulae(): void + { + // Loop named ranges + $definedNames = $this->spreadsheet->getDefinedNames(); + foreach ($definedNames as $definedName) { + $this->writeDefinedName($definedName); + } + } + + /** + * Write Defined Name for named range. + */ + private function writeDefinedName(DefinedName $pDefinedName): void + { + // definedName for named range + $this->objWriter->startElement('definedName'); + $this->objWriter->writeAttribute('name', $pDefinedName->getName()); + if ($pDefinedName->getLocalOnly() && $pDefinedName->getScope() !== null) { + $this->objWriter->writeAttribute('localSheetId', $pDefinedName->getScope()->getParent()->getIndex($pDefinedName->getScope())); + } + + $definedRange = $pDefinedName->getValue(); + $splitCount = preg_match_all( + '/' . Calculation::CALCULATION_REGEXP_CELLREF_RELATIVE . '/mui', + $definedRange, + $splitRanges, + PREG_OFFSET_CAPTURE + ); + + $lengths = array_map('strlen', array_column($splitRanges[0], 0)); + $offsets = array_column($splitRanges[0], 1); + + $worksheets = $splitRanges[2]; + $columns = $splitRanges[6]; + $rows = $splitRanges[7]; + + while ($splitCount > 0) { + --$splitCount; + $length = $lengths[$splitCount]; + $offset = $offsets[$splitCount]; + $worksheet = $worksheets[$splitCount][0]; + $column = $columns[$splitCount][0]; + $row = $rows[$splitCount][0]; + + $newRange = ''; + if (empty($worksheet)) { + if (($offset === 0) || ($definedRange[$offset - 1] !== ':')) { + // We should have a worksheet + $worksheet = $pDefinedName->getWorksheet()->getTitle(); + } + } else { + $worksheet = str_replace("''", "'", trim($worksheet, "'")); + } + if (!empty($worksheet)) { + $newRange = "'" . str_replace("'", "''", $worksheet) . "'!"; + } + + if (!empty($column)) { + $newRange .= $column; + } + if (!empty($row)) { + $newRange .= $row; + } + + $definedRange = substr($definedRange, 0, $offset) . $newRange . substr($definedRange, $offset + $length); + } + + if (substr($definedRange, 0, 1) === '=') { + $definedRange = substr($definedRange, 1); + } + + $this->objWriter->writeRawData($definedRange); + + $this->objWriter->endElement(); + } + + /** + * Write Defined Name for autoFilter. + */ + private function writeNamedRangeForAutofilter(Worksheet $pSheet, int $pSheetId = 0): void + { + // NamedRange for autoFilter + $autoFilterRange = $pSheet->getAutoFilter()->getRange(); + if (!empty($autoFilterRange)) { + $this->objWriter->startElement('definedName'); + $this->objWriter->writeAttribute('name', '_xlnm._FilterDatabase'); + $this->objWriter->writeAttribute('localSheetId', $pSheetId); + $this->objWriter->writeAttribute('hidden', '1'); + + // Create absolute coordinate and write as raw text + $range = Coordinate::splitRange($autoFilterRange); + $range = $range[0]; + // Strip any worksheet ref so we can make the cell ref absolute + [$ws, $range[0]] = Worksheet::extractSheetTitle($range[0], true); + + $range[0] = Coordinate::absoluteCoordinate($range[0]); + $range[1] = Coordinate::absoluteCoordinate($range[1]); + $range = implode(':', $range); + + $this->objWriter->writeRawData('\'' . str_replace("'", "''", $pSheet->getTitle()) . '\'!' . $range); + + $this->objWriter->endElement(); + } + } + + /** + * Write Defined Name for PrintTitles. + */ + private function writeNamedRangeForPrintTitles(Worksheet $pSheet, int $pSheetId = 0): void + { + // NamedRange for PrintTitles + if ($pSheet->getPageSetup()->isColumnsToRepeatAtLeftSet() || $pSheet->getPageSetup()->isRowsToRepeatAtTopSet()) { + $this->objWriter->startElement('definedName'); + $this->objWriter->writeAttribute('name', '_xlnm.Print_Titles'); + $this->objWriter->writeAttribute('localSheetId', $pSheetId); + + // Setting string + $settingString = ''; + + // Columns to repeat + if ($pSheet->getPageSetup()->isColumnsToRepeatAtLeftSet()) { + $repeat = $pSheet->getPageSetup()->getColumnsToRepeatAtLeft(); + + $settingString .= '\'' . str_replace("'", "''", $pSheet->getTitle()) . '\'!$' . $repeat[0] . ':$' . $repeat[1]; + } + + // Rows to repeat + if ($pSheet->getPageSetup()->isRowsToRepeatAtTopSet()) { + if ($pSheet->getPageSetup()->isColumnsToRepeatAtLeftSet()) { + $settingString .= ','; + } + + $repeat = $pSheet->getPageSetup()->getRowsToRepeatAtTop(); + + $settingString .= '\'' . str_replace("'", "''", $pSheet->getTitle()) . '\'!$' . $repeat[0] . ':$' . $repeat[1]; + } + + $this->objWriter->writeRawData($settingString); + + $this->objWriter->endElement(); + } + } + + /** + * Write Defined Name for PrintTitles. + */ + private function writeNamedRangeForPrintArea(Worksheet $pSheet, int $pSheetId = 0): void + { + // NamedRange for PrintArea + if ($pSheet->getPageSetup()->isPrintAreaSet()) { + $this->objWriter->startElement('definedName'); + $this->objWriter->writeAttribute('name', '_xlnm.Print_Area'); + $this->objWriter->writeAttribute('localSheetId', $pSheetId); + + // Print area + $printArea = Coordinate::splitRange($pSheet->getPageSetup()->getPrintArea()); + + $chunks = []; + foreach ($printArea as $printAreaRect) { + $printAreaRect[0] = Coordinate::absoluteReference($printAreaRect[0]); + $printAreaRect[1] = Coordinate::absoluteReference($printAreaRect[1]); + $chunks[] = '\'' . str_replace("'", "''", $pSheet->getTitle()) . '\'!' . implode(':', $printAreaRect); + } + + $this->objWriter->writeRawData(implode(',', $chunks)); + + $this->objWriter->endElement(); + } + } +} diff --git a/src/PhpSpreadsheet/Writer/Xlsx/Workbook.php b/src/PhpSpreadsheet/Writer/Xlsx/Workbook.php index 88befd02..0a20ea9d 100644 --- a/src/PhpSpreadsheet/Writer/Xlsx/Workbook.php +++ b/src/PhpSpreadsheet/Writer/Xlsx/Workbook.php @@ -2,13 +2,11 @@ namespace PhpOffice\PhpSpreadsheet\Writer\Xlsx; -use PhpOffice\PhpSpreadsheet\Cell\Coordinate; -use PhpOffice\PhpSpreadsheet\NamedRange; use PhpOffice\PhpSpreadsheet\Shared\Date; use PhpOffice\PhpSpreadsheet\Shared\XMLWriter; use PhpOffice\PhpSpreadsheet\Spreadsheet; -use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; use PhpOffice\PhpSpreadsheet\Writer\Exception as WriterException; +use PhpOffice\PhpSpreadsheet\Writer\Xlsx\DefinedNames as DefinedNamesWriter; class Workbook extends WriterPart { @@ -55,7 +53,7 @@ class Workbook extends WriterPart $this->writeSheets($objWriter, $spreadsheet); // definedNames - $this->writeDefinedNames($objWriter, $spreadsheet); + (new DefinedNamesWriter($objWriter, $spreadsheet))->write(); // calcPr $this->writeCalcPr($objWriter, $recalcRequired); @@ -224,183 +222,4 @@ class Workbook extends WriterPart throw new WriterException('Invalid parameters passed.'); } } - - /** - * Write Defined Names. - * - * @param XMLWriter $objWriter XML Writer - */ - private function writeDefinedNames(XMLWriter $objWriter, Spreadsheet $spreadsheet): void - { - // Write defined names - $objWriter->startElement('definedNames'); - - // Named ranges - if (count($spreadsheet->getNamedRanges()) > 0) { - // Named ranges - $this->writeNamedRanges($objWriter, $spreadsheet); - } - - // Other defined names - $sheetCount = $spreadsheet->getSheetCount(); - for ($i = 0; $i < $sheetCount; ++$i) { - // definedName for autoFilter - $this->writeDefinedNameForAutofilter($objWriter, $spreadsheet->getSheet($i), $i); - - // definedName for Print_Titles - $this->writeDefinedNameForPrintTitles($objWriter, $spreadsheet->getSheet($i), $i); - - // definedName for Print_Area - $this->writeDefinedNameForPrintArea($objWriter, $spreadsheet->getSheet($i), $i); - } - - $objWriter->endElement(); - } - - /** - * Write named ranges. - * - * @param XMLWriter $objWriter XML Writer - */ - private function writeNamedRanges(XMLWriter $objWriter, Spreadsheet $spreadsheet): void - { - // Loop named ranges - $namedRanges = $spreadsheet->getNamedRanges(); - foreach ($namedRanges as $namedRange) { - $this->writeDefinedNameForNamedRange($objWriter, $namedRange); - } - } - - /** - * Write Defined Name for named range. - * - * @param XMLWriter $objWriter XML Writer - */ - private function writeDefinedNameForNamedRange(XMLWriter $objWriter, NamedRange $pNamedRange): void - { - // definedName for named range - $objWriter->startElement('definedName'); - $objWriter->writeAttribute('name', $pNamedRange->getName()); - if ($pNamedRange->getLocalOnly()) { - $objWriter->writeAttribute('localSheetId', $pNamedRange->getScope()->getParent()->getIndex($pNamedRange->getScope())); - } - - // Create absolute coordinate and write as raw text - $range = Coordinate::splitRange($pNamedRange->getRange()); - $iMax = count($range); - for ($i = 0; $i < $iMax; ++$i) { - $range[$i][0] = '\'' . str_replace("'", "''", $pNamedRange->getWorksheet()->getTitle()) . '\'!' . Coordinate::absoluteReference($range[$i][0]); - if (isset($range[$i][1])) { - $range[$i][1] = Coordinate::absoluteReference($range[$i][1]); - } - } - $range = Coordinate::buildRange($range); - - $objWriter->writeRawData($range); - - $objWriter->endElement(); - } - - /** - * Write Defined Name for autoFilter. - * - * @param XMLWriter $objWriter XML Writer - * @param int $pSheetId - */ - private function writeDefinedNameForAutofilter(XMLWriter $objWriter, Worksheet $pSheet, $pSheetId = 0): void - { - // definedName for autoFilter - $autoFilterRange = $pSheet->getAutoFilter()->getRange(); - if (!empty($autoFilterRange)) { - $objWriter->startElement('definedName'); - $objWriter->writeAttribute('name', '_xlnm._FilterDatabase'); - $objWriter->writeAttribute('localSheetId', $pSheetId); - $objWriter->writeAttribute('hidden', '1'); - - // Create absolute coordinate and write as raw text - $range = Coordinate::splitRange($autoFilterRange); - $range = $range[0]; - // Strip any worksheet ref so we can make the cell ref absolute - [$ws, $range[0]] = Worksheet::extractSheetTitle($range[0], true); - - $range[0] = Coordinate::absoluteCoordinate($range[0]); - $range[1] = Coordinate::absoluteCoordinate($range[1]); - $range = implode(':', $range); - - $objWriter->writeRawData('\'' . str_replace("'", "''", $pSheet->getTitle()) . '\'!' . $range); - - $objWriter->endElement(); - } - } - - /** - * Write Defined Name for PrintTitles. - * - * @param XMLWriter $objWriter XML Writer - * @param int $pSheetId - */ - private function writeDefinedNameForPrintTitles(XMLWriter $objWriter, Worksheet $pSheet, $pSheetId = 0): void - { - // definedName for PrintTitles - if ($pSheet->getPageSetup()->isColumnsToRepeatAtLeftSet() || $pSheet->getPageSetup()->isRowsToRepeatAtTopSet()) { - $objWriter->startElement('definedName'); - $objWriter->writeAttribute('name', '_xlnm.Print_Titles'); - $objWriter->writeAttribute('localSheetId', $pSheetId); - - // Setting string - $settingString = ''; - - // Columns to repeat - if ($pSheet->getPageSetup()->isColumnsToRepeatAtLeftSet()) { - $repeat = $pSheet->getPageSetup()->getColumnsToRepeatAtLeft(); - - $settingString .= '\'' . str_replace("'", "''", $pSheet->getTitle()) . '\'!$' . $repeat[0] . ':$' . $repeat[1]; - } - - // Rows to repeat - if ($pSheet->getPageSetup()->isRowsToRepeatAtTopSet()) { - if ($pSheet->getPageSetup()->isColumnsToRepeatAtLeftSet()) { - $settingString .= ','; - } - - $repeat = $pSheet->getPageSetup()->getRowsToRepeatAtTop(); - - $settingString .= '\'' . str_replace("'", "''", $pSheet->getTitle()) . '\'!$' . $repeat[0] . ':$' . $repeat[1]; - } - - $objWriter->writeRawData($settingString); - - $objWriter->endElement(); - } - } - - /** - * Write Defined Name for PrintTitles. - * - * @param XMLWriter $objWriter XML Writer - * @param int $pSheetId - */ - private function writeDefinedNameForPrintArea(XMLWriter $objWriter, Worksheet $pSheet, $pSheetId = 0): void - { - // definedName for PrintArea - if ($pSheet->getPageSetup()->isPrintAreaSet()) { - $objWriter->startElement('definedName'); - $objWriter->writeAttribute('name', '_xlnm.Print_Area'); - $objWriter->writeAttribute('localSheetId', $pSheetId); - - // Print area - $printArea = Coordinate::splitRange($pSheet->getPageSetup()->getPrintArea()); - - $chunks = []; - foreach ($printArea as $printAreaRect) { - $printAreaRect[0] = Coordinate::absoluteReference($printAreaRect[0]); - $printAreaRect[1] = Coordinate::absoluteReference($printAreaRect[1]); - $chunks[] = '\'' . str_replace("'", "''", $pSheet->getTitle()) . '\'!' . implode(':', $printAreaRect); - } - - $objWriter->writeRawData(implode(',', $chunks)); - - $objWriter->endElement(); - } - } } diff --git a/tests/PhpSpreadsheetTests/Calculation/DefinedNamesCalculationTest.php b/tests/PhpSpreadsheetTests/Calculation/DefinedNamesCalculationTest.php new file mode 100644 index 00000000..d0a5aacb --- /dev/null +++ b/tests/PhpSpreadsheetTests/Calculation/DefinedNamesCalculationTest.php @@ -0,0 +1,98 @@ +load($inputFileName); + + $calculatedCellValue = $spreadsheet->getActiveSheet()->getCell($cellAddress)->getCalculatedValue(); + self::assertSame($expectedValue, $calculatedCellValue, "Failed calculation for cell {$cellAddress}"); + } + + /** + * @dataProvider namedRangeCalculationTest2 + */ + public function testNamedRangeCalculationsWithAdjustedRateValue(string $cellAddress, float $expectedValue): void + { + $inputFileType = 'Xlsx'; + $inputFileName = __DIR__ . '/../../data/Calculation/DefinedNames/NamedRanges.xlsx'; + + $reader = IOFactory::createReader($inputFileType); + $spreadsheet = $reader->load($inputFileName); + + $spreadsheet->getActiveSheet()->getCell('B1')->setValue(12.5); + + $calculatedCellValue = $spreadsheet->getActiveSheet()->getCell($cellAddress)->getCalculatedValue(); + self::assertSame($expectedValue, $calculatedCellValue, "Failed calculation for cell {$cellAddress}"); + } + + /** + * @dataProvider namedRangeCalculationTest1 + */ + public function testNamedFormulaCalculations1(string $cellAddress, float $expectedValue): void + { + $inputFileType = 'Xlsx'; + $inputFileName = __DIR__ . '/../../data/Calculation/DefinedNames/NamedFormulae.xlsx'; + + $reader = IOFactory::createReader($inputFileType); + $spreadsheet = $reader->load($inputFileName); + + $calculatedCellValue = $spreadsheet->getActiveSheet()->getCell($cellAddress)->getCalculatedValue(); + self::assertSame($expectedValue, $calculatedCellValue, "Failed calculation for cell {$cellAddress}"); + } + + /** + * @dataProvider namedRangeCalculationTest2 + */ + public function testNamedFormulaeCalculationsWithAdjustedRateValue(string $cellAddress, float $expectedValue): void + { + $inputFileType = 'Xlsx'; + $inputFileName = __DIR__ . '/../../data/Calculation/DefinedNames/NamedFormulae.xlsx'; + + $reader = IOFactory::createReader($inputFileType); + $spreadsheet = $reader->load($inputFileName); + + $spreadsheet->getActiveSheet()->getCell('B1')->setValue(12.5); + + $calculatedCellValue = $spreadsheet->getActiveSheet()->getCell($cellAddress)->getCalculatedValue(); + self::assertSame($expectedValue, $calculatedCellValue, "Failed calculation for cell {$cellAddress}"); + } + + public function namedRangeCalculationTest1(): array + { + return [ + ['C4', 56.25], + ['C5', 54.375], + ['C6', 48.75], + ['C7', 52.5], + ['C8', 41.25], + ['B10', 33.75], + ['C10', 253.125], + ]; + } + + public function namedRangeCalculationTest2(): array + { + return [ + ['C4', 93.75], + ['C5', 90.625], + ['C6', 81.25], + ['C7', 87.5], + ['C8', 68.75], + ['C10', 421.875], + ]; + } +} diff --git a/tests/PhpSpreadsheetTests/Calculation/Engine/RangeTest.php b/tests/PhpSpreadsheetTests/Calculation/Engine/RangeTest.php index 9dfa4bf0..4f1ff397 100644 --- a/tests/PhpSpreadsheetTests/Calculation/Engine/RangeTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/Engine/RangeTest.php @@ -29,10 +29,9 @@ class RangeTest extends TestCase /** * @dataProvider providerRangeEvaluation * - * @param string $formula - * @param int $expectedResult + * @param mixed $expectedResult */ - public function testRangeEvaluation($formula, $expectedResult): void + public function testRangeEvaluation(string $formula, $expectedResult): void { $workSheet = $this->spreadSheet->getActiveSheet(); $workSheet->setCellValue('E1', $formula); @@ -76,13 +75,8 @@ class RangeTest extends TestCase /** * @dataProvider providerNamedRangeEvaluation - * - * @param string $group1 - * @param string $group2 - * @param string $formula - * @param int $expectedResult */ - public function testNamedRangeEvaluation($group1, $group2, $formula, $expectedResult): void + public function testNamedRangeEvaluation(string $group1, string $group2, string $formula, int $expectedResult): void { $workSheet = $this->spreadSheet->getActiveSheet(); $this->spreadSheet->addNamedRange(new NamedRange('GROUP1', $workSheet, $group1)); @@ -97,26 +91,26 @@ class RangeTest extends TestCase public function providerNamedRangeEvaluation() { return[ - ['A1:B3', 'A1:C2', '=SUM(GROUP1,GROUP2)', 48], - ['A1:B3', 'A1:C2', '=COUNT(GROUP1,GROUP2)', 12], - ['A1:B3', 'A1:C2', '=SUM(GROUP1 GROUP2)', 12], - ['A1:B3', 'A1:C2', '=COUNT(GROUP1 GROUP2)', 4], - ['A1:B2', 'B2:C3', '=SUM(GROUP1,GROUP2)', 40], - ['A1:B2', 'B2:C3', '=COUNT(GROUP1,GROUP2)', 8], - ['A1:B2', 'B2:C3', '=SUM(GROUP1 GROUP2)', 5], - ['A1:B2', 'B2:C3', '=COUNT(GROUP1 GROUP2)', 1], + ['$A$1:$B$3', '$A$1:$C$2', '=SUM(GROUP1,GROUP2)', 48], + ['$A$1:$B$3', '$A$1:$C$2', '=COUNT(GROUP1,GROUP2)', 12], + ['$A$1:$B$3', '$A$1:$C$2', '=SUM(GROUP1 GROUP2)', 12], + ['$A$1:$B$3', '$A$1:$C$2', '=COUNT(GROUP1 GROUP2)', 4], + ['$A$1:$B$2', '$B$2:$C$3', '=SUM(GROUP1,GROUP2)', 40], + ['$A$1:$B$2', '$B$2:$C$3', '=COUNT(GROUP1,GROUP2)', 8], + ['$A$1:$B$2', '$B$2:$C$3', '=SUM(GROUP1 GROUP2)', 5], + ['$A$1:$B$2', '$B$2:$C$3', '=COUNT(GROUP1 GROUP2)', 1], + ['Worksheet!$A$1:$B$2', 'Worksheet!$B$2:$C$3', '=SUM(GROUP1,GROUP2)', 40], + ['Worksheet!$A$1:Worksheet!$B$2', 'Worksheet!$B$2:Worksheet!$C$3', '=SUM(GROUP1,GROUP2)', 40], ]; } /** * @dataProvider providerUTF8NamedRangeEvaluation * - * @param string $names - * @param string $ranges - * @param string $formula - * @param int $expectedResult + * @param string[] $names + * @param string[] $ranges */ - public function testUTF8NamedRangeEvaluation($names, $ranges, $formula, $expectedResult): void + public function testUTF8NamedRangeEvaluation(array $names, array $ranges, string $formula, int $expectedResult): void { $workSheet = $this->spreadSheet->getActiveSheet(); foreach ($names as $index => $name) { @@ -132,21 +126,19 @@ class RangeTest extends TestCase public function providerUTF8NamedRangeEvaluation() { return[ - [['Γειά', 'σου', 'Κόσμε'], ['A1', 'B1:B2', 'C1:C3'], '=SUM(Γειά,σου,Κόσμε)', 26], - [['Γειά', 'σου', 'Κόσμε'], ['A1', 'B1:B2', 'C1:C3'], '=COUNT(Γειά,σου,Κόσμε)', 6], - [['Здравствуй', 'мир'], ['A1:A3', 'C1:C3'], '=SUM(Здравствуй,мир)', 30], + [['Γειά', 'σου', 'Κόσμε'], ['$A$1', '$B$1:$B$2', '$C$1:$C$3'], '=SUM(Γειά,σου,Κόσμε)', 26], + [['Γειά', 'σου', 'Κόσμε'], ['$A$1', '$B$1:$B$2', '$C$1:$C$3'], '=COUNT(Γειά,σου,Κόσμε)', 6], + [['Здравствуй', 'мир'], ['$A$1:$A$3', '$C$1:$C$3'], '=SUM(Здравствуй,мир)', 30], ]; } /** * @dataProvider providerCompositeNamedRangeEvaluation - * - * @param string $composite - * @param int $expectedSum - * @param int $expectedCount */ - public function testCompositeNamedRangeEvaluation($composite, $expectedSum, $expectedCount): void + public function testCompositeNamedRangeEvaluation(string $composite, int $expectedSum, int $expectedCount): void { + self::markTestSkipped('must be revisited.'); + $workSheet = $this->spreadSheet->getActiveSheet(); $this->spreadSheet->addNamedRange(new NamedRange('COMPOSITE', $workSheet, $composite)); @@ -163,12 +155,12 @@ class RangeTest extends TestCase { return[ // Calculation engine doesn't yet handle union ranges with overlap - // 'Union with overlap' => [ - // 'A1:C1,A3:C3,B1:C3', - // 63, - // 12, - // ], - 'Intersection' => [ + 'Union with overlap' => [ + 'A1:C1,A3:C3,B1:C3', + 63, + 12, + ], + 'Union and Intersection' => [ 'A1:C1,A3:C3 B1:C3', 23, 5, diff --git a/tests/PhpSpreadsheetTests/Calculation/FormulaAsStringTest.php b/tests/PhpSpreadsheetTests/Calculation/FormulaAsStringTest.php index 2cbdc960..9afe5570 100644 --- a/tests/PhpSpreadsheetTests/Calculation/FormulaAsStringTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/FormulaAsStringTest.php @@ -21,7 +21,7 @@ class FormulaAsStringTest extends TestCase $workSheet->setCellValue('A2', 20); $workSheet->setCellValue('A3', 30); $workSheet->setCellValue('A4', 40); - $spreadsheet->addNamedRange(new \PhpOffice\PhpSpreadsheet\NamedRange('namedCell', $workSheet, 'A4')); + $spreadsheet->addNamedRange(new \PhpOffice\PhpSpreadsheet\NamedRange('namedCell', $workSheet, '$A$4')); $workSheet->setCellValue('B1', 'uPPER'); $workSheet->setCellValue('B2', '=TRUE()'); $workSheet->setCellValue('B3', '=FALSE()'); @@ -30,7 +30,7 @@ class FormulaAsStringTest extends TestCase $ws2->setCellValue('A1', 100); $ws2->setCellValue('A2', 200); $ws2->setTitle('Sheet2'); - $spreadsheet->addNamedRange(new \PhpOffice\PhpSpreadsheet\NamedRange('A2B', $ws2, 'A2')); + $spreadsheet->addNamedRange(new \PhpOffice\PhpSpreadsheet\NamedRange('A2B', $ws2, '$A$2')); $spreadsheet->setActiveSheetIndex(0); $cell2 = $workSheet->getCell('D1'); diff --git a/tests/PhpSpreadsheetTests/DefinedNameFormulaTest.php b/tests/PhpSpreadsheetTests/DefinedNameFormulaTest.php new file mode 100644 index 00000000..50304991 --- /dev/null +++ b/tests/PhpSpreadsheetTests/DefinedNameFormulaTest.php @@ -0,0 +1,167 @@ +getActiveSheet(); + + $definedNamesForTest = $this->providerRangeOrFormula(); + foreach ($definedNamesForTest as $key => $definedNameData) { + [$value] = $definedNameData; + $name = str_replace([' ', '-'], '_', $key); + $spreadSheet->addDefinedName(DefinedName::createInstance($name, $workSheet, $value)); + } + + $allDefinedNames = $spreadSheet->getDefinedNames(); + self::assertSame(count($definedNamesForTest), count($allDefinedNames)); + } + + public function testGetNamedRanges(): void + { + $spreadSheet = new Spreadsheet(); + $workSheet = $spreadSheet->getActiveSheet(); + + $rangeOrFormula = []; + $definedNamesForTest = $this->providerRangeOrFormula(); + foreach ($definedNamesForTest as $key => $definedNameData) { + [$value, $isFormula] = $definedNameData; + $rangeOrFormula[] = !$isFormula; + $name = str_replace([' ', '-'], '_', $key); + $spreadSheet->addDefinedName(DefinedName::createInstance($name, $workSheet, $value)); + } + + $allNamedRanges = $spreadSheet->getNamedRanges(); + self::assertSame(count(array_filter($rangeOrFormula)), count($allNamedRanges)); + } + + public function testGetScopedNamedRange(): void + { + $rangeName = 'NAMED_RANGE'; + $globalRangeValue = 'A1'; + $localRangeValue = 'A2'; + + $spreadSheet = new Spreadsheet(); + $workSheet = $spreadSheet->getActiveSheet(); + + $spreadSheet->addDefinedName(DefinedName::createInstance($rangeName, $workSheet, $globalRangeValue)); + $spreadSheet->addDefinedName(DefinedName::createInstance($rangeName, $workSheet, $localRangeValue, true)); + + $localScopedRange = $spreadSheet->getNamedRange($rangeName, $workSheet); + self::assertSame($localRangeValue, $localScopedRange->getValue()); + } + + public function testGetGlobalNamedRange(): void + { + $rangeName = 'NAMED_RANGE'; + $globalRangeValue = 'A1'; + $localRangeValue = 'A2'; + + $spreadSheet = new Spreadsheet(); + $workSheet1 = $spreadSheet->getActiveSheet(); + $spreadSheet->createSheet(1); + $workSheet2 = $spreadSheet->getSheet(1); + + $spreadSheet->addDefinedName(DefinedName::createInstance($rangeName, $workSheet1, $globalRangeValue)); + $spreadSheet->addDefinedName(DefinedName::createInstance($rangeName, $workSheet1, $localRangeValue, true)); + + $localScopedRange = $spreadSheet->getNamedRange($rangeName, $workSheet2); + self::assertSame($globalRangeValue, $localScopedRange->getValue()); + } + + public function testGetNamedFormulae(): void + { + $spreadSheet = new Spreadsheet(); + $workSheet = $spreadSheet->getActiveSheet(); + + $rangeOrFormula = []; + $definedNamesForTest = $this->providerRangeOrFormula(); + foreach ($definedNamesForTest as $key => $definedNameData) { + [$value, $isFormula] = $definedNameData; + $rangeOrFormula[] = $isFormula; + $name = str_replace([' ', '-'], '_', $key); + $spreadSheet->addDefinedName(DefinedName::createInstance($name, $workSheet, $value)); + } + + $allNamedFormulae = $spreadSheet->getNamedFormulae(); + self::assertSame(count(array_filter($rangeOrFormula)), count($allNamedFormulae)); + } + + public function testGetScopedNamedFormula(): void + { + $formulaName = 'GERMAN_VAT_RATE'; + $globalFormulaValue = '=19.0%'; + $localFormulaValue = '=16.0%'; + + $spreadSheet = new Spreadsheet(); + $workSheet = $spreadSheet->getActiveSheet(); + + $spreadSheet->addDefinedName(DefinedName::createInstance($formulaName, $workSheet, $globalFormulaValue)); + $spreadSheet->addDefinedName(DefinedName::createInstance($formulaName, $workSheet, $localFormulaValue, true)); + + $localScopedFormula = $spreadSheet->getNamedFormula($formulaName, $workSheet); + self::assertSame($localFormulaValue, $localScopedFormula->getValue()); + } + + public function testGetGlobalNamedFormula(): void + { + $formulaName = 'GERMAN_VAT_RATE'; + $globalFormulaValue = '=19.0%'; + $localFormulaValue = '=16.0%'; + + $spreadSheet = new Spreadsheet(); + $workSheet1 = $spreadSheet->getActiveSheet(); + $spreadSheet->createSheet(1); + $workSheet2 = $spreadSheet->getSheet(1); + + $spreadSheet->addDefinedName(DefinedName::createInstance($formulaName, $workSheet1, $globalFormulaValue)); + $spreadSheet->addDefinedName(DefinedName::createInstance($formulaName, $workSheet1, $localFormulaValue, true)); + + $localScopedFormula = $spreadSheet->getNamedFormula($formulaName, $workSheet2); + self::assertSame($globalFormulaValue, $localScopedFormula->getValue()); + } + + public function providerRangeOrFormula(): array + { + return [ + 'simple range' => ['A1', false], + 'simple absolute range' => ['$A$1', false], + 'simple integer value' => ['42', true], + 'simple float value' => ['12.5', true], + 'simple string value' => ['"HELLO WORLD"', true], + 'range with a worksheet name' => ['Sheet2!$A$1', false], + 'range with a quoted worksheet name' => ["'Work Sheet #2'!\$A\$1:\$E\$1", false], + 'range with a quoted worksheet name containing quotes' => ["'Mark''s WorkSheet'!\$A\$1:\$E\$1", false], + 'range with a utf-8 worksheet name' => ['Γειά!$A$1', false], + 'range with a quoted utf-8 worksheet name' => ["'Γειά σου Κόσμε'!\$A\$1", false], + 'range with a quoted worksheet name with quotes in a formula' => ["'Mark''s WorkSheet'!\$A\$1+5", true], + 'range with a quoted worksheet name in a formula' => ["5*'Work Sheet #2'!\$A\$1", true], + 'multiple ranges with quoted worksheet names with quotes in a formula' => ["'Mark''s WorkSheet'!\$A\$1+'Mark''s WorkSheet'!\$B\$2", true], + 'named range in a formula' => ['NAMED_RANGE_VALUE+12', true], + 'named range and range' => ['NAMED_RANGE_VALUE_1,Sheet2!$A$1', false], + 'range with quoted utf-8 worksheet name and a named range' => ["NAMED_RANGE_VALUE_1,'Γειά σου Κόσμε'!\$A\$1", false], + 'composite named range' => ['NAMED_RANGE_VALUE_1,NAMED_RANGE_VALUE_2 NAMED_RANGE_VALUE_3', false], + 'named ranges in a formula' => ['NAMED_RANGE_VALUE_1/NAMED_RANGE_VALUE_2', true], + 'utf-8 named range' => ['Γειά', false], + 'utf-8 named range in a formula' => ['2*Γειά', true], + 'utf-8 named ranges' => ['Γειά,σου Κόσμε', false], + 'utf-8 named ranges in a formula' => ['Здравствуй+мир', true], + ]; + } +} diff --git a/tests/PhpSpreadsheetTests/DefinedNameTest.php b/tests/PhpSpreadsheetTests/DefinedNameTest.php new file mode 100644 index 00000000..3f5b75a0 --- /dev/null +++ b/tests/PhpSpreadsheetTests/DefinedNameTest.php @@ -0,0 +1,124 @@ +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() + ); + } +} diff --git a/tests/PhpSpreadsheetTests/NamedFormulaTest.php b/tests/PhpSpreadsheetTests/NamedFormulaTest.php new file mode 100644 index 00000000..44254829 --- /dev/null +++ b/tests/PhpSpreadsheetTests/NamedFormulaTest.php @@ -0,0 +1,126 @@ +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()); + self::assertSame( + '=16%', + $this->spreadsheet->getNamedFormula('foo', $this->spreadsheet->getActiveSheet())->getValue() + ); + } + + public function testAddScopedNamedFormulaWithSameName(): void + { + $this->spreadsheet->addNamedFormula( + new NamedFormula('Foo', $this->spreadsheet->getActiveSheet(), '=19%') + ); + $this->spreadsheet->addNamedFormula( + new NamedFormula('FOO', $this->spreadsheet->getSheetByName('Sheet #2'), '=16%', true) + ); + + self::assertCount(2, $this->spreadsheet->getNamedFormulae()); + self::assertSame( + '=19%', + $this->spreadsheet->getNamedFormula('foo', $this->spreadsheet->getActiveSheet())->getValue() + ); + self::assertSame( + '=16%', + $this->spreadsheet->getNamedFormula('foo', $this->spreadsheet->getSheetByName('Sheet #2'))->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->getSheetByName('Sheet #2'), '=16%', true) + ); + + $this->spreadsheet->removeNamedFormula('Foo', $this->spreadsheet->getActiveSheet()); + + self::assertCount(1, $this->spreadsheet->getNamedFormulae()); + self::assertSame( + '=16%', + $this->spreadsheet->getNamedFormula('foo', $this->spreadsheet->getSheetByName('Sheet #2'))->getValue() + ); + } + + public function testRemoveScopedNamedFormulaWhenDuplicateNames(): void + { + $this->spreadsheet->addNamedFormula( + new NamedFormula('Foo', $this->spreadsheet->getActiveSheet(), '=19%') + ); + $this->spreadsheet->addNamedFormula( + new NamedFormula('FOO', $this->spreadsheet->getSheetByName('Sheet #2'), '=16%', true) + ); + + $this->spreadsheet->removeNamedFormula('Foo', $this->spreadsheet->getSheetByName('Sheet #2')); + + self::assertCount(1, $this->spreadsheet->getNamedFormulae()); + self::assertSame( + '=19%', + $this->spreadsheet->getNamedFormula('foo')->getValue() + ); + } +} diff --git a/tests/PhpSpreadsheetTests/NamedRangeTest.php b/tests/PhpSpreadsheetTests/NamedRangeTest.php new file mode 100644 index 00000000..62cd735b --- /dev/null +++ b/tests/PhpSpreadsheetTests/NamedRangeTest.php @@ -0,0 +1,126 @@ +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->addNamedRange( + new NamedRange('Foo', $this->spreadsheet->getActiveSheet(), '=A1') + ); + + self::assertCount(1, $this->spreadsheet->getDefinedNames()); + self::assertCount(1, $this->spreadsheet->getNamedRanges()); + self::assertCount(0, $this->spreadsheet->getNamedFormulae()); + } + + public function testAddDuplicateNamedRange(): void + { + $this->spreadsheet->addNamedRange( + new NamedRange('Foo', $this->spreadsheet->getActiveSheet(), '=A1') + ); + $this->spreadsheet->addNamedRange( + new NamedRange('FOO', $this->spreadsheet->getActiveSheet(), '=B1') + ); + + self::assertCount(1, $this->spreadsheet->getNamedRanges()); + self::assertSame( + '=B1', + $this->spreadsheet->getNamedRange('foo', $this->spreadsheet->getActiveSheet())->getValue() + ); + } + + public function testAddScopedNamedRangeWithSameName(): void + { + $this->spreadsheet->addNamedRange( + new NamedRange('Foo', $this->spreadsheet->getActiveSheet(), '=A1') + ); + $this->spreadsheet->addNamedRange( + new NamedRange('FOO', $this->spreadsheet->getSheetByName('Sheet #2'), '=B1', true) + ); + + self::assertCount(2, $this->spreadsheet->getNamedRanges()); + self::assertSame( + '=A1', + $this->spreadsheet->getNamedRange('foo', $this->spreadsheet->getActiveSheet())->getValue() + ); + self::assertSame( + '=B1', + $this->spreadsheet->getNamedRange('foo', $this->spreadsheet->getSheetByName('Sheet #2'))->getValue() + ); + } + + public function testRemoveNamedRange(): void + { + $this->spreadsheet->addDefinedName( + new NamedRange('Foo', $this->spreadsheet->getActiveSheet(), '=A1') + ); + $this->spreadsheet->addDefinedName( + new NamedRange('Bar', $this->spreadsheet->getActiveSheet(), '=B1') + ); + + $this->spreadsheet->removeNamedRange('Foo', $this->spreadsheet->getActiveSheet()); + + self::assertCount(1, $this->spreadsheet->getNamedRanges()); + } + + public function testRemoveGlobalNamedRangeWhenDuplicateNames(): void + { + $this->spreadsheet->addNamedRange( + new NamedRange('Foo', $this->spreadsheet->getActiveSheet(), '=A1') + ); + $this->spreadsheet->addNamedRange( + new NamedRange('FOO', $this->spreadsheet->getSheetByName('Sheet #2'), '=B1', true) + ); + + $this->spreadsheet->removeNamedRange('Foo', $this->spreadsheet->getActiveSheet()); + + self::assertCount(1, $this->spreadsheet->getNamedRanges()); + self::assertSame( + '=B1', + $this->spreadsheet->getNamedRange('foo', $this->spreadsheet->getSheetByName('Sheet #2'))->getValue() + ); + } + + public function testRemoveScopedNamedRangeWhenDuplicateNames(): void + { + $this->spreadsheet->addNamedRange( + new NamedRange('Foo', $this->spreadsheet->getActiveSheet(), '=A1') + ); + $this->spreadsheet->addNamedRange( + new NamedRange('FOO', $this->spreadsheet->getSheetByName('Sheet #2'), '=B1', true) + ); + + $this->spreadsheet->removeNamedRange('Foo', $this->spreadsheet->getSheetByName('Sheet #2')); + + self::assertCount(1, $this->spreadsheet->getNamedRanges()); + self::assertSame( + '=A1', + $this->spreadsheet->getNamedRange('foo')->getValue() + ); + } +} diff --git a/tests/PhpSpreadsheetTests/ReferenceHelperTest.php b/tests/PhpSpreadsheetTests/ReferenceHelperTest.php index 08462b5b..bf32f746 100644 --- a/tests/PhpSpreadsheetTests/ReferenceHelperTest.php +++ b/tests/PhpSpreadsheetTests/ReferenceHelperTest.php @@ -112,4 +112,21 @@ class ReferenceHelperTest extends TestCase { return require 'tests/data/ReferenceHelperFormulaUpdates.php'; } + + /** + * @dataProvider providerMultipleWorksheetFormulaUpdates + */ + public function testUpdateFormulaForMultipleWorksheets(string $formula, int $insertRows, int $insertColumns, string $expectedResult): void + { + $referenceHelper = ReferenceHelper::getInstance(); + + $result = $referenceHelper->updateFormulaReferencesAnyWorksheet($formula, $insertRows, $insertColumns); + + self::assertSame($expectedResult, $result); + } + + public function providerMultipleWorksheetFormulaUpdates(): array + { + return require 'tests/data/ReferenceHelperFormulaUpdatesMultipleSheet.php'; + } } diff --git a/tests/PhpSpreadsheetTests/Writer/Xls/FormulaErrTest.php b/tests/PhpSpreadsheetTests/Writer/Xls/FormulaErrTest.php index 21a7c928..bbb00d89 100644 --- a/tests/PhpSpreadsheetTests/Writer/Xls/FormulaErrTest.php +++ b/tests/PhpSpreadsheetTests/Writer/Xls/FormulaErrTest.php @@ -14,7 +14,7 @@ class FormulaErrTest extends TestCase $obj = new \PhpOffice\PhpSpreadsheet\Spreadsheet(); $sheet0 = $obj->setActiveSheetIndex(0); $sheet0->setCellValue('A1', 2); - $obj->addNamedRange(new NamedRange('DEFNAM', $sheet0, 'A1')); + $obj->addNamedRange(new NamedRange('DEFNAM', $sheet0, '$A$1')); $sheet0->setCellValue('B1', '=2*DEFNAM'); $sheet0->setCellValue('C1', '=DEFNAM=2'); $sheet0->setCellValue('D1', '=CONCAT("X",DEFNAM)'); diff --git a/tests/PhpSpreadsheetTests/Writer/Xls/WorkbookTest.php b/tests/PhpSpreadsheetTests/Writer/Xls/WorkbookTest.php index 7a83b697..5ebe645f 100644 --- a/tests/PhpSpreadsheetTests/Writer/Xls/WorkbookTest.php +++ b/tests/PhpSpreadsheetTests/Writer/Xls/WorkbookTest.php @@ -22,7 +22,7 @@ class WorkbookTest extends TestCase $strUnique = 0; $str_table = []; $colors = []; - $parser = new Parser(); + $parser = new Parser($spreadsheet); $this->workbook = new Workbook($spreadsheet, $strTotal, $strUnique, $str_table, $colors, $parser); } diff --git a/tests/data/Calculation/DefinedNames/NamedFormulae.xlsx b/tests/data/Calculation/DefinedNames/NamedFormulae.xlsx new file mode 100644 index 0000000000000000000000000000000000000000..d6b77c06ac49a59407d210755f9c6ccbb076be30 GIT binary patch literal 9216 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 literal 0 HcmV?d00001 diff --git a/tests/data/Calculation/DefinedNames/NamedRanges.xlsx b/tests/data/Calculation/DefinedNames/NamedRanges.xlsx new file mode 100644 index 0000000000000000000000000000000000000000..d4cea6ca3068296056658bb75cffaf252c356f44 GIT binary patch literal 9221 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 - + 1 1