Commit Graph

2535 Commits

Author SHA1 Message Date
Mark Baker 2259de578b
Lookup ref further tests and examples (#1918)
* Extract LookupRef\INDEX() into index() method of LookupRef\Matrix class
Additional tests
* Bugfix for returning a column using INDEX()
* Some improvements to ROW() and COLUMN()
* Simplify some of the INDEX() logic, eliminating redundant code
2021-03-11 22:34:47 +01:00
Mark Baker 499ce61cf7
Unhappy path tests for FORMULATEXT() Function (#1915)
* Unhappy path tests
2021-03-10 22:38:41 +01:00
MarkBaker 4717741dc2 Update ChangeLog 2021-03-10 21:30:39 +01:00
oleibman 13b62becdd
Fix for Issue #1887 - Lose Track of Selected Cells After Save (#1908)
* Fix for Issue #1887 - Lose Track of Selected Cells After Save

Issue #1887 reports that selected cells are lost after saving Xlsx. Testing indicates that this applies to the object in memory, though not to the saved spreadsheet.

Xlsx writer tries to save calculated values for cells which contain formulas. Calculation::_calculateFormulaValue issues a getStyle call merely to retrieve the quotePrefix property, which, if set, indicates that the cell does not contain a formula even though it looks like one. A side-effect of calls to getStyle is that selectedCell is updated. That is clearly accidental, and highly undesirable, in this case. Code is changed to save selectedCell before getStyle call and restore it afterwards.

The problem was reported only for Xlsx save. To be on the safe side, test is made for output formats of Xlsx, Xls, Ods, Html (which basically includes Pdf), and Csv. For all of those, the object in memory is tested after the save. For Xlsx and Xls, the saved file is also tested. It does not make sense to test the saved file for Csv and Html. It does make sense to test it for Ods, but the necessary support is not yet present in either the Ods Reader or Ods Writer - a project for another day.

* Move Logic Out of Calculation, Add Support for Ods ActiveSheet and SelectedCells

Mark Baker thought logic belonged in Worksheet, not Calculation.
I couldn't get it to work in Worksheet, but doing it in Cell works,
and that has already been used to preserve ActiveSheet over call to
getCalculatedValue, so this just extends that idea to SelectedCells.

Original tests could not completely support Ods because of a lack of support
for ActiveSheet and SelectedCells in Ods Reader and Writer.
There's a lot missing in Ods support, but a journey of 1000 miles ...
Those two particular concepts are now supported for Ods.
2021-03-10 21:23:08 +01:00
Mark Baker 70f372d88c
Start refactoring the Lookup and Reference functions (#1912)
* Start refactoring the Lookup and Reference functions
 - COLUMN(), COLUMNS(), ROW() and ROWS()
 - LOOKUP(), VLOOKUP() and HLOOKUP()
 - Refactor TRANSPOSE() and ADDRESS() functions into their own classes

* Additional unit tests
 - LOOKUP()
 - TRANSPOSE()
 - ADDRESS()
2021-03-10 21:18:33 +01:00
Mark Baker f81ffd9a4f
Additional argument validation for LEFT(), MID() and RIGHT() text functions (#1909)
* Additional argument validation for LEFT(), MID() and RIGHT() text functions
2021-03-08 12:54:06 +01:00
Mark Baker c4ed0ee7b0
Minor scrutinizer improvements (#1906)
* Minor scrutinizer improvements
* Minor typing improvements
2021-03-07 14:22:03 +01:00
Mark Baker 2d8c8c8ecf
Trend unit tests (#1899)
- Move TREND() functions into the Statistical Trends class
- Unit tests for TREND()
- Create Confidence class for Statistical Confidence functions
2021-03-06 22:50:19 +01:00
Mark Baker a79a4ddbab
Statistical refactoring - Confidence() and Trend() (#1898)
- Move TREND() functions into the Statistical Trends class
- Unit tests for TREND()
- Create Confidence class for Statistical Confidence functions, and the CONFIDENCE() method
2021-03-04 21:45:56 +01:00
Mark Baker d2a83b404a
Statistical trends additional functions and unit tests (#1896)
* PEARSON() and CORREL() are identical functions
* Unit tests for GROWTH() function
* Move GROWTH() function into Statistical\Trends Class
2021-03-03 23:18:56 +01:00
Patrick Brouwers 000e6088c9
Reverted Scrutinzer fix in Xslx Reader listWorksheetInfo (#1895) 2021-03-03 21:34:45 +01:00
Mark Baker 3e672710cd
Writing defined names without a worksheet reference in Xlsx (#1892) 2021-03-03 18:16:37 +01:00
Mark Baker 70e371189c
Move the trend functions from Statistical and into their own group class (#1890)
* Move the trend functions from Statistical and into their own group class
* Additional LINEST()/LOGEST() tests, and fix for the returned array
2021-03-03 12:51:50 +01:00
oleibman 04e7c30758
Fix Two 32-bit Timestamp Problems, and Minor getFormattedValue Bug (#1891)
I ran the test suite using 32-bit PHP. There were 2 places where changes
were needed due to 32-bit timestamps.

Reader\\Xml.php was using strtotime as an intermediate step in converting
a string timestamp to an Excel timestamp. The XML file type stores pure timestamps
(i.e. no date portion) as, e.g., 1899-12-31T02:30:00.000, and that value
causes an error using strtotime on a 32-bit system. However, it is sufficient
to use that value in a DateTime constructor, and that will work for 32- and 64-bit.

There was no test for that particular cell, so I added one to the XML read test.
And that's when I discovered the getFormattedValue bug. The cell's format
is `hh":"mm":"ss`. The quotes around the colons are disrupting the formatting.
PhpSpreadsheet formats the cell by converting the Excel format
to a Php Date format, in this case `H\:m\:s`.
That's a problem,
since Excel thinks 'm' means *minutes*, but PHP thinks it means *months*.
This is not a problem when the colon is not quoted; there are ample tests for that.
I added my best guess as to how to recognize this situation,
changing `\:m` to `:i`. The XML read test
now succeeds, and no other tests were broken by this change.

Test Shared\\DateTest had one test where the expected result of converting to a
Unix timestamp exceeds 2**32. Since a Unix timestamp is strictly an int,
that test fails on a 32-bit system. In the discussion regarding recently merged
PR #1870, it was felt that the user base might still be using the functions
that convert to and from a timestamp. So, we should not drop this test, but,
since it cannot succeed on a 32-bit system, I changed it to be skipped
whenever the expected result exceeded PHP_INT_MAX. There are 3 "toTimestamp"
functions within that test. Only one of these had been affected, but I thought
it was a good idea to add additional tests to the others to demonstrate this
condition.

In the course of testing, I also discovered some 32-bit problems with
bitwise and base-conversion functions. I am preparing separate PRs to
deal with those.
2021-03-03 10:52:11 +01:00
MarkBaker c55269cb06 Changelog 2021-03-02 18:54:11 +01:00
MarkBaker f8c58fbb7e Changelog 2021-03-02 18:51:13 +01:00
Mark Baker 42e8680fc0
Statistics more unit tests (#1889)
* Additional unit tests
2021-03-02 18:01:39 +01:00
Mark Baker 2eaf9b53aa
Start splitting some of the basic Statistical functions out into separate classes (#1888)
* Start splitting some of the basic Statistical functions out into separate classes containing just a few similar functions

* Splitting some of the basic Statistical functions out into separate classes containing just a few similar functions - MAX(), MAXA(), MIN() and MINA()

* Splitting some more of the basic Statistical functions out into separate classes containing just a few similar functions - StandardDeviations and Variances
2021-03-02 09:07:28 +01:00
Ivan Stanojevic 8721f795fc
Update ReferenceHelper.php (#1873)
* Update ReferenceHelper for Defined Names
2021-03-01 12:33:35 +01:00
adancabanas 0715b63f11
Pdf Writer strtoupper() fix (#1629)
* _setPageSize's strtoupper()  on array argument

PhpSpreadsheet/Writer/Pdf.php Class defines a protected static mixed array called $paperSizes, this array contains string values along with array values. 'strtoupper() expects parameter 1 to be string, array given' error happens due to array passed to $paperSize variable from that $paperSizes mixed array on the Mpdf Class where Pdf extends

Examples of cases, when a 'Letter' paper size is chosen, then no problem occurs since the index in that value for the array is a string value, but when 'Tabloid' paper size is chosen the value in the index for that paper size is an array, that's when the strtoupper() error happens

* _setPageSize's strtoupper() on array argument

PhpSpreadsheet/Writer/Pdf.php Class defines a protected static mixed array called $paperSizes, this array contains string values along with array values. 'strtoupper() expects parameter 1 to be string, array given' error happens due to array passed to $paperSize variable from that $paperSizes mixed array on the Dompdf Class where Pdf extends

Examples of cases; when a 'Letter' paper size is chosen, then no problem occurs since the index in the array for that value a string, but when 'Tabloid' paper size is chosen the value in the index for that paper size is an array, that's when the strtoupper() error happens.
2021-02-28 17:58:57 +01:00
Mark Baker 1d6f36d8df
Initial Formula Translation tests (#1886)
* Initial Formula Translation tests
2021-02-28 13:18:51 +01:00
Mark Baker ee969fdcfe
Additional conditionals from math trig (#1885)
* Use our new Conditional logic to implement the SUMIF() and SUMIFS() Mathematical functions
2021-02-28 10:24:33 +01:00
Mark Baker 761c84a946
Avoid the performance/memory overheads of "clone on modify" of $args (#1884)
* Avoid the performance/memory overheads of "clone on modify" of $args when building the condition set/database for AVERAGEIFS(), MAXIFS() and MINIFS()
* Avoid the performance/memory overheads of "clone on modify" of $args when building the condition set/database for COUNTIFS()
2021-02-27 23:10:33 +01:00
oleibman 80a20fc991
100% Coverage for Calculation/DateTime (#1870)
* 100% Coverage for Calculation/DateTime

The code in DateTime is now completely covered.
Along the way, some errors were discovered and corrected.
- The tests which have had to be changed at the start of every year are
replaced by more robust equivalents which do not require annual changes.
- Several places in the code where Gnumeric and OpenOffice were thought to differ
from Excel do not appear to have had any justification.
I have left a comment where such code has been removed.
- Use DateTime when possible rather than date, time, or strftime functions to avoid
potential Y2038 problems.
- Some impossible code has been removed, replaced by an explanatory comment.
- NETWORKDAYS had a bug when the start date was Sunday. There had been no tests
of this condition.
- Some functions allow boolean and null arguments where a number is expected.
This is more complicated than the equivalent situations in MathTrig because
the initial date for these calculations can be Day 1 rather than Day 0.
- More testing for dates from 1900-01-01 through the fictitious
everywhere-but-Excel 1900-01-29.
    - This showed that there is an additional Excel bug - Excel evaluates
WEEKNUM(emptycell) as 0, which is not a valid result for
WEEKNUM without a second argument.
PhpSpreadsheet now duplicates this bug.
    - There is a similar and even worse bug for 1904-01-01 in 1904 calculations.
Weeknum returns 0 for this,
but returns the correct value for arguments of 0 or null.
    - DATEVALUE should accept 1900-02-29 (sigh) and relatives.
PhpSpreadsheet now duplicates this bug.
- Testing bootstrap sets default timezone. This appears to be a relic from
the releases of PHP where the unwise decision, subsequenly reversed,
was made to issue messages for
"no default timezone is set" rather than just use a sensible default.
This was a disruptive setting for some of the tests I added.
There is only one test in the entire suite which is default-timezone-dependent.
Setting and resetting of default timezone is moved to that test
(Reader/ODS/ODSTest), and out of bootstrap.
- There had been no testing of NOW() function.
- DATEVALUE test had no tests for 1904 calendar and needs some.
- DATE test changed 1900/1904 calendar in use without restoring it.
- WEEKDAY test had no tests for 1904 calendar and needs some.
    - Which revealed a bug in Shared/Date (excelToDateTimeObject was not
recognizing 1904-01-01 as valid when 1904 calendar is in use).
    - And an additional bug in that legal 1904-calendar values in the 0.0-1.0
range yielded the same "wrong" answers as 1900-calendar (see "One note" below).
Also the comment for one of the calendar-1904 tests was wrong in attempting
to identify what time of day the fraction represented.

I had wanted to break this up into a set of smaller modules, a process already
started for Engineering and MathTrig.
However the number of source code changes was sufficient that I wanted
a clean delta for this request.
If it is merged, I will work on breaking it up afterwards.

One note - Shared/Date/excelToDateTimeObject, when calendar-1900 is in use,
returns an unexpected result if its argument is between 0 and 1,
which is nominally invalid for that calendar.
It uses a base-1970 calendar in that instance. That check is not justifiable
for calendar-1904, where values in that range are legal,
so I made the check specific to calendar-1900,
and adjusted 3 1904 unit test results accordingly. However, I have to admit that
I don't understand why that check should be made even for calendar-1900.
It certainly doesn't match anything that Excel does.
I would recommend scrapping that code altogether.
If agreed, I would do this as part of the break-up into smaller modules.

Another note -
more controversially, it is clear that PhpSpreadsheet needs to support
the Excel and PHP date formats. Although it requires further study,
I am not convinced that it needs to support Unix timestamp format.
Since that is a potential source of Y2038 problems on 32-bit systems,
I would like to open a PR to deprecate the use of that format.
Please let me know if you are aware of a valid reason to continue to support it.
2021-02-27 20:43:22 +01:00
Mark Baker 08673b5820
Initial experiments using the new Database query logic with Conditional Statistical Functions (#1880)
- Refactoring of the Statistical Conditional functions (`AVERAGEIF()`, `AVERAGEIFS()`, `COUNTIF()`, `COUNTIFS()`, `MAXIFS()` and `MINIFS()` to use the new Database functions codebase.
- Extended unit testing
- Fix handling for null values
- Fixes to wildcard text searches

There's still scope for further improvements to memory usage and performance; but for now the code is stable with all unit tests passing
2021-02-27 18:26:12 +01:00
MarkBaker 8dcdf58131 Update change log 2021-02-27 15:22:25 +01:00
oleibman cb23cca3ec
Avoid Duplicate Titles When Reading Multiple HTML Files (#1829)
This issue arose while researching issue #1823. The issue was not a bug;
it just required clarification to the author of how to use the software.
But, while researching, I discovered that loading html into 2
sheets of a spreadsheet has a problem if the html title tag is the same
for the 2 sheets. PhpSpreadsheet would be able to save the resulting file,
but Excel would not be able to read it properly because of the duplicate title.
The worksheet setTitle method allows for disambiguation is such a circumstance.
The html reader passed a parameter indicating "don't disambiguate", but I can't
see any harm in changing that to "disambiguate". An extremely simple fix,
with tests to back it up.
2021-02-27 15:10:04 +01:00
Mark Baker 25f7dcb9fd
Enable support for wildcard text searches in Excel Database functions (#1876)
* Enable support for wildcard text searches in Excel Database functions
2021-02-23 19:26:29 +01:00
Mark Baker 40a6dee0a4
Enable support for dates and percentages in Excel Database functions (#1875)
* Enable support for dates and percentages in Excel Database functions, and CountIf/AverageIf/etc
* Enable support for booleans in Excel Database functions
2021-02-22 20:40:40 +01:00
Mark Baker 3764f30354
Refactor the Excel Database functions; and rewrite the query building (#1871)
* Refactor the Excel Database functions; and rewrite the query building to fix a bug with complex multi-criteria queries that involve both AND and OR conditions
* Fix handling for empty cells and NULL values in searches
* Expand unit tests; and add TODOs for dates, percentages, and wildcard text comparisons
2021-02-22 12:46:57 +01:00
Mark Baker 1318b90330
Bugfix #1858; Apply stricter scoping rules to named range/cell access (#1866)
* Apply stricter scoping rules to named range/cell access via Worksheet object
* Additional unit tests
2021-02-19 22:03:50 +01:00
Mark Baker b0eb272ce1
Bugfix #1858; `getCell()` method should support named cells correctly (#1865)
Resolution for Issue [#1858](https://github.com/PHPOffice/PhpSpreadsheet/issues/1858)
2021-02-19 12:28:07 +01:00
Mark Baker 409c05b542
Additional Unit Test Cases for Convert UoM (#1864)
* Additional Unit Test Cases
2021-02-19 08:41:26 +01:00
Mark Baker b269c26f6e
Advanced Value Binder improvements (#1863)
* Refactor times, and add unit tests
2021-02-18 23:14:14 +01:00
Mark Baker 5afda811c9
Advanced Value Binder Improvements (#1862)
Advanced Value Binder
 - Improved format checking/setting for fractions;
 - Better percentage checking;
 - Some minor refactoring;
 - Improved unit testing
2021-02-18 19:17:47 +01:00
Mark Baker 7c7b229041
Let's see what Scrutinizer makes of these changes (#1859)
* Let's see what Scrutinizer makes of these changes
2021-02-18 12:39:24 +01:00
oleibman 10c9c4cf23
Make Documentation Updates Easier and More Accurate (#1573)
I made a documentation change and noticed that the result when browsed
locally did not quite match what is seen when browsing from the web.
After some research, I found https://github.com/mkdocs/mkdocs/issues/2028
That described my situation well and suggested adding an extra
javascript script to the configuration. This worked exactly as desired
on my local machine. This accounts for the presence of extrajs.js
and mkdocs.yml in this request.

In addition to the display problem, "mkdocs build" generates the
documentation into a directory which is not ignored by git. I added
that directory to .gitignore as part of this request.

Finally, since I don't know how exactly the documentation makes it
to production, I made an insignificant change to one doc file as
a sanity check.
2021-02-15 20:50:20 +01:00
oleibman ef3f8f3070
Use DateTime Rather than gmmktime in Sample Template (#1827)
This avoids a potential Y2038 problem on 32-bit systems - see issue #1826.
2021-02-14 18:54:49 +01:00
MarkBaker 4e1f89d04c Fix coding standards issue 2021-02-14 17:12:01 +01:00
MarkBaker e29600116e Merge branch 'reader-array-formula-bugfix' 2021-02-14 17:10:01 +01:00
MarkBaker 9909af6ccb Fix coding standards issue 2021-02-14 16:47:49 +01:00
Mark Baker f277817955
Update deprecation notices to reflect deprecated from the next release (#1856)
* Update deprecation notices to reflect deprecated from the next release
2021-02-14 16:09:09 +01:00
Mark Baker c407ff6fe7
Start extracting Logical Excel functions into separate groups in dedicated classes (#1855)
* Start extracting Logical Excel functions into separate groups in dedicated classes

* Extracting remaining Logical Excel functions into separate groups in dedicated classes
2021-02-14 11:55:21 +01:00
MarkBaker 8d0692397b Additional Office 365 functions for Excel added in 2020 2021-02-13 22:08:22 +01:00
oleibman a24ca09bd4
Support 'Forms' for ROMAN Function (#1828)
* Support 'Forms' for ROMAN Function

This seems like an exceptionally silly thing for MS to have implemented
(Wikipedia on Roman Numerals: "There is no indication this is anything
other than an invention by the programmer").
Nevertheless, we can, and therefore probably should, implement it.

Not that I can implement it by an algorithm - Excel describes the various extra
styles as "more concise", "more concise", "more concise", and "simplified".
Nevertheless, since the universe of potential calls is relatively small,
it can be implemented as a table of values where the new forms would return
a different value than "classic". This table is relatively large, so I have
put it its own member to avoid overhead when the function is needed.

* Move ROMAN To Its Own Class

See discussion in PR #1837

* PHP 8.1 Deprecations

PHP8.1 Unit tests failed. 1 line fixes are available for
- Shared/Font
- Shared/XMLWriter
- Style/Color
- Writer/HTML

The problem is that an error is also reported for a strcmp at
line 272 of Cell/Cell. Not only does that line not invoke strcmp,
there is no strcmp in all of Cell/Cell, so I don't know what to make
of the error message. Oh well, let's fix what can be fixed.

Still dealing with the mysterious PHP8.1 unit test failure in Cell\Cell,
which seems to have something to do with strcmp. The only uses of
strcmp that I can find in src/ are in Calculation. I can't find any
use of it in test/ or samples/. So, if this doesn't fix the problem,
I may have to give up.
2021-02-13 21:23:58 +01:00
oleibman cabcfaa522
ROUND Accepts null, false, and true as First Parameter (#1837)
* ROUND Accepts null, false, and true as First Parameter

Issue #1789 was addressed by PR #1799. In a follow-up discussion,
it came to light that ROUND was not handling the unexpected case where the
first parameter is an empty cell in the same manner that Excel does.
Subsequent investigation showed that a boolean first parameter is permitted.
I broadened my investigation to include the following related functions.
- ROUNDUP
- ROUNDDOWN
- MROUND
- TRUNC
- INT
- FLOOR
- FLOOR.MATH
- FLOOR.PRECISE
- CEILING
- CEILING.MATH
- CEILING.PRECISE

All of these allow a NULL first parameter, and all except MROUND allow boolean.
For completeness, I will note that all treat null string as invalid.
I suspect there are other functions which permit
similarly unexpected parameters, but I consider them out of scope for this PR.

CEILING.MATH and CEILING.PRECISE were unimplemented, and are now supported
as part of this PR.

The tests for each of these functions have been re-coded, though all the original
test data is still included in the test cases, plus several new cases for each.
The new tests now take place as a user would invoke the functions,
through a spreadsheet cell rather than a
direct call to the appropriate function within Calculation/MathTrig.
Aside from being more realistic, the new tests are also more complete.
For example, FLOOR.MATH can take from 1-3 arguments, and the existing tests
confirmed that the function in Calculation could handle a single argument.
However, the function list in Calculation.php erroneously set the number of
arguments for FLOOR.MATH to exactly 3, so, if a user tried to get the calculated
result of a cell containing FLOOR.MATH(1.2), the result would be an Exception.

Aside from the parameter support, there are a few minor code changes.
Ods, as well as Gnumeric, allows the omission of the second parameter for
FLOAT and CEILING; Excel does not. A potential divide-by-zero error is
avoided in CEILING, FLOOR, and FLOORMATH.

I will note that it would probably be beneficial in terms of maintainability
to break MathTrig up into many individual modules. The same would hold for the
other Calculation modules. I would be willing to look into this if you agree
that it would be worthwhile.
2021-02-13 21:00:08 +01:00
Mark Baker c54e3e9979
Extract DELTA() and GESTEP() functions from the Engineering class into a dedicated Comparison classes (#1853)
* Extract DELTA() and GESTEP() functions from the Engineering class into a dedicated Comparison classes

Retain the original methods in the Engineering class as stubs for BC, but deprecate them. They will be removed for PHPSpreadsheet v2

Note that unit tests still point to the Engineering class stubs; these should be modified to use the Erf and ErfC classes directly when the stubs are removed
2021-02-13 20:52:20 +01:00
MarkBaker 15a5d13aba Update docs 2021-02-13 15:58:06 +01:00
Mark Baker 42ecc270ec
Extract Permutation functions from the Statistical class into a dedicated Permutations class (#1851)
* Extract Permutation functions from the Statistical class into a dedicated Permutations class

Retain the original methods in the Statistical class as stubs for BC, but deprecate them. They will be removed for PHPSpreadsheet v2

Note that unit tests still point to the Statistical class stubs; these should be modified to use the Permutations class directly when the stubs are removed

Also provided a basic implementationof the PERMUTATIONA() Function
2021-02-13 15:35:07 +01:00
MarkBaker e824caf857 Update Changelog 2021-02-13 13:09:49 +01:00