Commit Graph

388 Commits

Author SHA1 Message Date
oleibman 7ddf6ccf41
PrintArea Causes Exception
I wanted to investigate #1523, but I couldn't duplicate its results
because the sample code in the issue caused an Exception to be thrown.
The exception happened because, when the Print Range Set method is
set to Insert (as oppposed to Overwrite), and the Print Range is
empty, it is created with a leading or trailing comma, and Writer/Xlsx
objects. This is, in a sense, a user error, but the software should
be more resilient. This can be accomplished by ensuring that no
leading or trailing comma is generated when Print Range is empty.

This code change fixes that problem. Since I couldn't reproduce the
original, I can't say for sure that it fixes it. However, with the
sample code provided, I can write a spreadsheet which Excel reads
without any problems, so it probably fixes the original.

Closes  #1544
Fixes #1523
2020-07-26 12:15:57 +09:00
Adrien Crivelli 0489e785d2
Merge branch 'master' into Page-Setup-Page-Order 2020-07-26 10:50:41 +09:00
MarkBaker 16a9ff14d4 Experiment 2020-07-25 23:17:26 +02:00
Mark Baker fe121e8f7a
Additional statistical unit tests for non-happy path (#1594)
* Additional statistical unit tests for non-happy path
2020-07-25 21:58:08 +02:00
Mark Baker 57213deb64
Implementation of MS Excel's LOGNORM.DIST(), NORM.S.DIST(), F.DIST(), GAUSS() and GAMMA() functions (#1588)
* `GAUSS()` and `GAMMA()`, `NORM.S.DIST()`, `LOGNORM.DIST()` and `F.DIST()` function implementations, and further unit tests for a number of the statistical functions

Co-authored-by: Adrien Crivelli <adrien.crivelli@gmail.com>
2020-07-25 12:44:51 +02:00
Mark Baker 5233e9caaf
Merge branch 'master' into Page-Setup-Page-Order 2020-07-19 12:57:48 +02:00
Dhaval Purohit 7e12575d86
Borders are complete even on rowspanned columns using HTML reader
Fixed #1455
Closes #1473
2020-07-19 14:04:53 +09:00
Adrien Crivelli 395b750030
Stricter visibility 2020-07-19 12:30:31 +09:00
Adrien Crivelli c3fa31de13
Missing typing 2020-07-19 12:21:40 +09:00
Adrien Crivelli 7cb4884b96
WEBSERVICE is HTTP client agnostic
HTTP client must be configured via `Settings::setHttpClient()`. This is
a small breaking change, but only for the very few people who started using
WEBSERVICE from last version.

Fixes #1562
Closes #1568
2020-07-19 11:33:01 +09:00
oleibman 165034ad70
Restoring State After Static Changes in Tests (#1571)
This request does not change any source code, only tests.

For a change on which I was working, a test passed when run on its own,
but failed when run as part of the full test suite. It turned out that
an existing test had changed a static value,
thousands separator in this case, and failed to restore it.
The test turned out to be AdvancedBinderTest.

The search for the offending test was more difficult than it should have
been because 26 test scripts which had nothing to do with thousands
separator nevertheless changed that value. They all changed
decimal separator, currency code, and compatibility mode as well,
again for no reason. I changed all of those to eliminate those operations.

I changed the following tests, which actually do change the static
properties identified above for a reason, to restore them as part of teardown.
- CalculationTest sets compatibilityMode and locale
- DayTest sets compatibilityMode, returnDateType, and excelCalendar
- CountTest sets compatibilityMode
- FunctionsTest sets compatibilityMode and returnDateType
- AdvancedValueBinderTest sets currencyCode, decimalSeparator, thousandsSeparator
- StringHelperTest sets currencyCode, decimalSeparator, thousandsSeparator
- NumberFormatTest sets currencyCode, decimalSeparator, thousandsSeparator
- HtmlNumberFormatTest sets currencyCode, decimalSeparator, thousandsSeparator
2020-07-15 13:23:00 +02:00
Mark Baker b89968d206
Additional Unit Tests (#1582) 2020-07-14 10:58:50 +02:00
MarkBaker 6cbb622a9e Minor refactoring 2020-07-05 18:25:39 +02:00
MarkBaker cf6769eab1 Hopefully a final phpcs fix before I start looking at how to run a pre-commit hook on Windows 10 2020-07-05 17:17:56 +02:00
MarkBaker e89196f65b phpcs fixes, although I thought I'd successfully added the pre-commit hook to pick those up before the commit, guess the problem is running from Windoze, so I'll have to address that 2020-07-05 17:05:44 +02:00
MarkBaker d009347e25 Forgot to check in the test files for the unit tests 2020-07-05 16:28:46 +02:00
MarkBaker 8629337101 Retrieving print/page setup for the Xml Reader 2020-07-05 16:22:35 +02:00
paulkned 7f23ccb69d
Added support for the WEBSERVICE function (#1409)
Co-authored-by: Paul Kievits <kievits@rsm.nl>
2020-06-29 10:17:58 +09:00
Adrien Crivelli f1fb8dcf1f
Don't ouput row and columns without any cells in HTML writer
If row or column dimensions are accessed, then HTML writer would
still generate lots of empty cells, to show nothing at all. This
now ignore row and column dimensions to only output cell that
actually exists (even if those cells are empty).

Fixes #1235
Close #1537
2020-06-28 22:03:37 +09:00
Adrien Crivelli a90bf863ab
Merge pull request #1499 from oleibman/htmledit
Add ability to save edited Html/Pdf
2020-06-28 17:46:56 +09:00
Adrien Crivelli 2896e6ceb9
Consistent regexp escaping 2020-06-28 17:34:32 +09:00
Mark Baker a264cafe4c
Helper class for the conversion of cell addresses between A1 and R1C1 formats, and vice-versa (#1558)
* Helper class for the conversion of cell addresses between A1 and R1C1 formats, and vice-versa
2020-06-27 23:03:25 +02:00
Mark Baker 10a4a95d67
Handle Ranges formatted as 3-d ranges, as long as the references are both to the same worksheet (#1540) 2020-06-21 14:41:51 +02:00
Dawid Warmuz 859bef1901
Add support for IFS() logical function (#1442)
* Add support for IFS() logical function

* Use Exception as false value in IFS logical function, so it never collides with string in spreadsheet
2020-06-20 18:21:19 +02:00
Christoph Ziegenberg ca506ba87f
Corrected date time detection (#1492)
* Corrected date time detection

German and Swiss ZIP codes (special formats provided in German Excel versions) were detected as date time value, because the regular expression for date time formats falsely matched their formats ("\C\H\-00000" and "\D-00000").
2020-06-20 17:15:38 +02:00
oleibman b3d30f4cbc
Xls Writer - Correct Timestamp Bug, Improve Coverage (#1493)
* Xls Writer - Correct Timestamp Bug, Improve Coverage

I believe that Xls Writer is 100% covered now.

The Xls Writer sets its timestamp incorrectly. The problem is actually
in Shared/Ole::localDateToOLE, which converts its timestamp using
gmmktime; mktime is correct. If I save a file at 3:00 p.m. in San Francisco,
this bug means the time is actually recorded as 3:00 p.m. UTC.
A consequence of this is that if you use Phpspreadsheet to read the
file and save it as a new Xls, the creation timestamp goes further
and further back in time with each generation (or further forward
if east of Greenwich). One of the tests added confirms that
the creation timestamp is consistent with the start and end times
of the test.

The major change in coverage is adding tests to save GIF and BMP
images, which aren't supported in Xls, but are converted to PNG
in the PhpSpreadsheet code.
2020-06-19 21:08:36 +02:00
Arne Jørgensen a5a0268050
Fix HLOOKUP on single row (#1512)
Fixes a bug when doing a HLOOKUP on a single row.

```php
<?php

require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();

/**
 * Single row.
 */
$singleRow = "=HLOOKUP(10, {5, 10, 15}, 1, 0)";
$sheet->getCell('A1')->setValue($singleRow);

// Should echo 10, but echos '#N/A' and some PHP notices and warnings.
echo $sheet->getCell('A1')->getCalculatedValue() . PHP_EOL;

/**
 * Multiple rows.
 */
$multipleRows = "=HLOOKUP(10, {5, 10, 15; 20, 25, 30}, 1, 0)";
$sheet->getCell('A2')->setValue($multipleRows);

// Should echo: 10 and also does.
echo $sheet->getCell('A2')->getCalculatedValue() . PHP_EOL;
```

Co-authored-by: Mark Baker <mark@lange.demon.co.uk>
2020-06-19 21:06:41 +02:00
oleibman 38fab4e632
Fix for #1505 (#1525)
This problem is the same as #1238, which was resolved by #1239.
For that issue, the fix was to check in one place whether
$this->mapCellXfIndex[$xfIndex] was set before using it.
The sample spreadsheet supplied as a description for this
problem had exactly the same problem in 2 other places in the code.
In addition, there were 7 other places in the code where that
particular item was used unchecked. This fix corrects all 9 locations.
The spreadsheet supplied with the problem is used as the basis
for some new tests, which particularly test column dimensions
and styles, the problems involved in this case.
2020-06-19 21:01:18 +02:00
oleibman 3844186397
Fix for Issue 1495 (#1500)
#1495 reports that ActiveSheet can change when calculation
involves jumping around between sheets.
Save index before calculation, restore after, add test.
2020-06-19 20:57:20 +02:00
Arne Jørgensen 1a44ef9109
Fix MATCH when comparing different numeric types (#1521)
Let MATCH compare numerics of different type (e.g. integers and floats).

```php
<?php

require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();

// Row: 1, 2, 3, 4, 5. MATCH for 4.6.
$sheet->getCell('A1')->setValue(1);
$sheet->getCell('A2')->setValue(2);
$sheet->getCell('A3')->setValue(3);
$sheet->getCell('A4')->setValue(4);
$sheet->getCell('A5')->setValue(5);

$sheet->getCell('B1')->setValue('=MATCH(4.6, A1:A5, 1)');

// Should echo 4, but echos '#N/A'.
echo $sheet->getCell('B1')->getCalculatedValue() . PHP_EOL;

// Row: 1, 2, 3, 3.8, 5. MATCH for 4.
$sheet->getCell('C1')->setValue(1);
$sheet->getCell('C2')->setValue(2);
$sheet->getCell('C3')->setValue(3);
$sheet->getCell('C4')->setValue(3.8);
$sheet->getCell('C5')->setValue(5);

$sheet->getCell('D1')->setValue('=MATCH(4, C1:C5, 1)');

// Should echo 4, but echos 3.
echo $sheet->getCell('D1')->getCalculatedValue() . PHP_EOL;
```

Co-authored-by: Mark Baker <mark@lange.demon.co.uk>
2020-06-19 20:54:04 +02:00
Arne Jørgensen 73c336ac96
Fix exact MATCH on ranges with empty cells (#1520)
Fixes a bug when doing exact match on ranges with empty cells.

```php
<?php

require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();

// Row: 1, null, 4, null, 8.
$sheet->getCell('A1')->setValue(1);
$sheet->getCell('A3')->setValue(4);
$sheet->getCell('A5')->setValue(8);

$sheet->getCell('B1')->setValue('=MATCH(4, A1:A5, 1)');

// Should echo 3, but echos '#N/A'.
echo $sheet->getCell('B1')->getCalculatedValue() . PHP_EOL;

// Row: 1, null, 4, null, null.
$sheet->getCell('C1')->setValue(1);
$sheet->getCell('C3')->setValue(4);

$sheet->getCell('D1')->setValue('=MATCH(5, C1:C5, 1)');

// Should echo 3, but echos '#N/A'.
echo $sheet->getCell('D1')->getCalculatedValue() . PHP_EOL;
```
2020-06-19 20:51:46 +02:00
oleibman d8b4c3b26e
Fix for #1533 (#1534)
Code assumes that formula whose result starts with # indicates error.
Change to check entire result against error list in Functions.
2020-06-19 20:40:28 +02:00
oleibman 262896086a
Improve Coverage for Sylk (#1514)
* Improve Coverage for Sylk

I believe that both BaseReader and Sylk Reader are now 100% covered.

Documentation available for this format is sparse.
It was always incomplete, and in some cases inaccurate.
My goal was to use PhpSpreadsheet to load the test file,
save it as Xlsx, and visually compare the two, then add a test
loaded with assertions. Cell values and calculated values,
and border styles were generally handled pretty well without changes.
Other types of styling were not handled so well. I added a few cells
to exercise some previously uncovered code.

Sylk files must be ASCII. I have deprecated the use of the
setEncoding and getEncoding functions, which had no test cases.
2020-06-19 20:35:44 +02:00
oleibman 73379cdfb1
Improve Coverage for Gnumeric (#1517)
* Improve Coverage for Gnumeric

I believe that both BaseReader and Gnumeric Reader are now 100% covered.

My goal was to use PhpSpreadsheet to load the test file,
save it as Xlsx, and visually compare the two, then add a test
loaded with assertions. Results were generally pretty good,
but there were no tests with assertions. I added a few cells
to exercise some previously uncovered code. Code was extensively
refactored; logic changes are noted below.

Code allowed for specifying document properties in an old format.
I considered removing that, but I found the original spec at
http://www.jfree.org/jworkbook/download/gnumeric-xml.pdf
This allowed me to create an old file, which was not handled
correctly because of namespace differences. The code was corrected
to allow for this difference.

Added support for textRotation.

Mapping of fill types was not correct.

* PHP7.2 Error

One assertion failed under PHP7.2. Apparently there was some change in
the handling of SimpleXMLElement between 7.2 and 7.3. Casting to string
before use eliminates the problem.

* Scrutinizer Recommendations

All minor, solved (hopefully) mostly by casts.

* One Last Scrutinizer Fix

... I hope.
2020-06-19 20:34:02 +02:00
oleibman ce6ac1f040
Fix For #1509 (#1518)
* Fix For #1509

User expected no CSV enclosures after $writer->setEnclosure(''),
which had been changed to be consistent with $reader->setEnclosure('').
Writer will now omit enclosures after code above; no change to Reader.
Tests have been added for this condition.

* Add Option to Write CSV Enclosure Only When Required

Allowing the user to specify no enclosure when writing a CSV can lead to
a situation where PhpSpreadsheet (likewise Excel) will not read the
resulting file as intended, e.g. if any cell contains a delimiter character.
This is demonstrated in new test TestBadReread.
No existing setting will rectify this situation.

A better choice would be to add an option to write the enclosure
only when it is needed, which is what Excel does. The RFC4180 spec at
https://tools.ietf.org/html/rfc4180
states when it is needed - when the cell contains the delimiter,
or the enclosure, or a newline.
New test TestGoodReread demonstrates that the file is read as intended.

The documentation has been updated to describe the new function,
and to change the write example where the enclosure is set to null.

* Scrutinizer Suggestions

3 minor changes, all in tests.
2020-06-19 20:28:57 +02:00
oleibman 82ea1d5596
Fix for #1516 (#1530)
This problem is that ZipStream, in contrast to ZipArchive,
is saving 2 files with the same path. I have opened an issue with
ZipStream, who agree that this appears to be a bug.

For the case in question, PhpSpreadsheet is attempting to save
a file with the same path twice (and unexpectedly succeeding)
because of a clone operation. This fix attempts to rectify the problem
by keeping track of all the paths being saved in the zip file,
and not attempting to save any duplicate paths.

The problem case attempted to save printersettings1.bin twice,
but there are other possible exposures, e.g. by cloning a sheet
with a drawing.The new test cases clone an existing sample which
has both printer settings and drawings.
2020-06-19 20:26:02 +02:00
Mark Baker 12dd92bafe
Resolve utf-8 named ranges in calculation engine (#1522)
* Resolve use of UTF-8 in defined names in the calculation engine
2020-06-13 17:35:29 +02:00
oleibman 360c8d8284
Merge branch 'master' into htmledit 2020-06-09 00:39:52 -07:00
Owen Leibman c47b407e39 Different Example for Callback
Replace default gridlines with different style. Usable in PDF
as well as HTML.

Documentation mentioned use of setUseBOM with Html, but that method
does not exist, and there is no real reason to support it.
Removed it from documentation.
2020-06-09 00:22:22 -07:00
Mark Baker 5c18bb5798
Range operator tests (#1501)
* Improved handling of named ranges, although there are still some issues (names ranges using a union type with an overlap don't handle the overlap twice, which as the MS Excel approach to set overlaps as opposed to the mathematical approach which only applies overlap values once)

* Fix tests that misused space and comma as simple separators in cell ranges
2020-06-02 07:38:35 +02:00
Adrien Crivelli b9a59660d0
Password and hash are exclusive
As specified in https://docs.microsoft.com/en-us/openspecs/office_standards/ms-xlsx/85f5567f-2599-41ad-ae26-8cfab23ce754
password and hashValue are exlusive and thus should be treated
transparently with a single API in our model.
2020-05-31 22:36:37 +09:00
Reijn dfa6f77178
Add support protection of worksheet by a specific hash algorithm 2020-05-31 20:29:20 +09:00
Owen Leibman edc411e6dd Add ability to save edited Html/Pdf
We give users the ability to edit Html/Pdf, but it's a little cumbersome
to use the edited Html for an Html file, and difficult to use it
for a Pdf. I believe we could make it fairly painless in both cases
by allowing the user to set a callback to edit the generated Html.
This can be accomplished with fewer than a dozen lines of very simple code.
I think this would be easier than grabbing the Html in pieces,
editing it, and reassembling it. I think it would also be simpler
than an alternative I considered, namely the addition of a new method
(e.g. saveEditedHtml) to each of the Html and Pdf writers.

One edit that users might like to make when editing html is to add
fallback fonts, something that is not currently available in
PhpSpreadsheet, and might be difficult to add. A natural extension to
that idea would be the use of webfonts, something which is guaranteed
difficult to add. See samples/Basic/17b_Html for an example of this.

None of the PDF writers support webfonts yet. That doesn't mean they
won't do so in future, but, for now, samples/Pdf/21a_Pdf is a prosaic
example of something you could do with this callback. In fact, this
opens the door to letting the user replace the entire body with data
of their choosing, effectively allowing PhpSpreadsheet (where you can
set things like paper size and orientation) to be used as a front-end to
the Pdf processor without the user having to be be overly familiar with
the vagaries of the PDF processor. I think this is actually a pretty
nice idea. YMMV. See samples/Basic/21b_Pdf for an example.
2020-05-30 21:27:35 -07:00
Mark Baker 8b2bba9bdb
Range operator tests (#1498)
* Fix intersection operator when working with named ranges
2020-05-29 21:53:28 +02:00
Alban Duval 7ed96e0be1
Calcualtion - DATEDIF - fix result for Y & YM units (#1466)
Bugfix for negative results and too small results

2000-02-02 => 2001-02-01
 > DATEDIF with Y unit: 0 year (returned -1 before fix)
 > DATEDIF with YM unit: 11 months (returned -1 before fix)
2020-05-25 21:33:48 +02:00
oleibman 5dd7e883c6
Fix Issue 1441 (isDateTime and Formulas) (#1480)
* Fix Issue 1441 (isDateTime and Formulas)

When you have a date-field which is a formula, isDateTime returns false.
https://github.com/PHPOffice/PhpSpreadsheet/issues/1441

Report makes sense; fixed as suggested. Also fixed a few minor
related issues, and added tests so that Shared/Date and Shared/TimeZone
are now completely covered.

Date/setDefaultTimeZone and TimeZone/setTimeZone were not consistent
about what to do in event of failure - return false or throw.
They will now both return false, which is what Date's function
said it would do in its doc block anyhow. Date/validateTimeZone will
continue to throw; it was protected, but was never called outside
Date, so I changed it to private.

TimeZone/getTimeZoneAdjustment checked for 'UST' when it probably
meant 'UTC', and, as it turns out, the check is not even needed.

The most serious problem was that TimeZone/validateTimeZone does not
check the backwards-compatible time zones. The timezone project
aggressively, and very controversially, "demotes" timezones;
such timezones eventually wind up in the PHP backwards-compatible list.
We want to make sure to check that list so that our applications do not
break when this happens.
2020-05-24 20:02:39 +02:00
oleibman 585409a949
Testing - Delete Temp Files When No Longer Needed (#1488)
No code changes. The tests in all of these scripts write to at least
one temporary file, which is then read and not used again. The file
should be deleted to avoid filling up the disk system.
2020-05-24 20:03:07 +09:00
oleibman 41b95c1542
CSV Sample File Was Miscoded (#1489)
File author erroneously assumed that backslash was used to escape
quotes in CSV; in fact, doubling the quote is used for escape.
The test still worked, but mainly because the content of the cell
with the escape wasn't tested. The file is now fixed, and
a new test added.
2020-05-24 19:57:39 +09:00
oleibman 9947de3b89
Restore working directory if test fails (#1490)
This test changes directory then performs an assertion.
No problem if the assertion succeeds. I was a little concerned about
what would happen if the assertion fails, leaving us in the
new directory. So I have changed test to use setUp/tearDown
to ensure that we end up where we started.
2020-05-24 19:54:59 +09:00
oleibman 84e03da5c7
Code Coverage for Shared\CodePage (#1491)
While investigating something else in Shared, I noticed that CodePage
had poor test coverage and a high complexity rating. This change
addresses both; Scrutinizer would love it, although its interface on
GitHub seems broken at the moment (all PRs show "Waiting for External
Code Coverage").
2020-05-24 19:51:28 +09:00