forked from PHPOffice/PhpSpreadsheet
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix PHPOffice#3720. Third-party product created a spreadsheet which PhpSpreadsheet could not read because of unexpected namespacing in workbook.xml.rels. The file which demonstrated the problem was attached to PHPOffice#3423, however I do not believe it was related to the original problem. Nevertheless, the original issue specifically called out Protection, so I put some Protection tests in the validation test for the fix. In doing so, I found that Style/Protection is particularly confusing. Its properties will often have the value `inherit`, which isn't all that helpful; and, even when the `locked` value is `protected`, the cell won't actually be locked unless the sheet is protected as well. The `hidden` property is even more obscure - it applies only to formulas, and refers to hiding the property on the formula bar, not in the cell. I have added methods `isLocked` and `isHiddenOnFormulaBar` to `Cell`. I corrected the docs to explain this. And, as long as I was looking at the docs, I corrected some examples to use `getHighestDataRow/Column` rather than `getHighestRow/Column`, a frequent problem for users (e.g. PHPOffice#3721). As a side note, the change to Cell.php is my first use of the nullsafe operator. This is one of many new options available now that we require Php8.0+.
- Loading branch information
Showing
7 changed files
with
228 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx; | ||
|
||
use PhpOffice\PhpSpreadsheet\Reader\Xlsx; | ||
|
||
class Issue3720Test extends \PHPUnit\Framework\TestCase | ||
{ | ||
private static string $testbook = 'tests/data/Reader/XLSX/issue.3720.xlsx'; | ||
|
||
public function testPreliminaries(): void | ||
{ | ||
$file = 'zip://'; | ||
$file .= self::$testbook; | ||
$file .= '#xl/_rels/workbook.xml.rels'; | ||
$data = file_get_contents($file); | ||
// confirm that file contains expected namespaced xml tag | ||
if ($data === false) { | ||
self::fail('Unable to read file'); | ||
} else { | ||
self::assertStringContainsString('<ns3:Relationships ', $data); | ||
} | ||
} | ||
|
||
public function testInfo(): void | ||
{ | ||
$reader = new Xlsx(); | ||
$workSheetInfo = $reader->listWorkSheetInfo(self::$testbook); | ||
$info1 = $workSheetInfo[1]; | ||
self::assertEquals('Welcome', $info1['worksheetName']); | ||
self::assertEquals('H', $info1['lastColumnLetter']); | ||
self::assertEquals(7, $info1['lastColumnIndex']); | ||
self::assertEquals(49, $info1['totalRows']); | ||
self::assertEquals(8, $info1['totalColumns']); | ||
} | ||
|
||
public function testSheetNames(): void | ||
{ | ||
$reader = new Xlsx(); | ||
$worksheetNames = $reader->listWorksheetNames(self::$testbook); | ||
$expected = [ | ||
'Data', | ||
'Welcome', | ||
'Sheet 1', | ||
'Sheet 2', | ||
'Sheet 3', | ||
'Sheet 4', | ||
'Sheet 5', | ||
'Sheet 6', | ||
'Sheet 7', | ||
'Sheet 8', | ||
'Sheet 9', | ||
'Sheet 10', | ||
]; | ||
self::assertEquals($expected, $worksheetNames); | ||
} | ||
|
||
public function testLoadXlsx(): void | ||
{ | ||
$reader = new Xlsx(); | ||
$spreadsheet = $reader->load(self::$testbook); | ||
$sheets = $spreadsheet->getAllSheets(); | ||
self::assertCount(12, $sheets); | ||
$sheet = $spreadsheet->getSheetByNameOrThrow('Sheet 1'); | ||
$sheetProtection = $sheet->getProtection(); | ||
self::assertTrue($sheetProtection->getSheet()); | ||
self::assertSame(' FILL IN WHITE CELLS ONLY', $sheet->getCell('B3')->getValue()); | ||
// inherit because no cell, row, or column style. | ||
// effectively protected because sheet is locked. | ||
self::assertTrue($sheet->getCell('A12')->isLocked()); | ||
// unprotected because column is unprotected (no cell or row style) | ||
self::assertFalse($sheet->getCell('B12')->isLocked()); | ||
// inherit because cell has style with protection omitted. | ||
// effectively protected because sheet is locked. | ||
self::assertTrue($sheet->getCell('B11')->isLocked()); | ||
$sheet = $spreadsheet->getSheetByNameOrThrow('Welcome'); | ||
$drawings = $sheet->getDrawingCollection(); | ||
self::assertCount(1, $drawings); | ||
$failmsg = ''; | ||
if (isset($drawings[0])) { | ||
$draw0 = $drawings[0]; | ||
if (method_exists($draw0, 'getPath')) { | ||
self::assertSame('image1.jpeg', basename($draw0->getPath())); | ||
} else { | ||
$failmsg = 'unexpected missing getPath method'; | ||
} | ||
} else { | ||
$failmsg = 'unexpected missing array item 0'; | ||
} | ||
$spreadsheet->disconnectWorksheets(); | ||
if ($failmsg !== '') { | ||
self::fail($failmsg); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpOffice\PhpSpreadsheetTests\Worksheet; | ||
|
||
use PhpOffice\PhpSpreadsheet\Spreadsheet; | ||
use PhpOffice\PhpSpreadsheet\Style\Protection; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class Protection2Test extends TestCase | ||
{ | ||
public function testisHiddenOnFormulaBar(): void | ||
{ | ||
$spreadsheet = new Spreadsheet(); | ||
$sheet = $spreadsheet->getActiveSheet(); | ||
$sheet->getCell('A1')->setValue('X') | ||
->getStyle()->getProtection() | ||
->setHidden(Protection::PROTECTION_UNPROTECTED); | ||
$sheet->getCell('A2')->setValue('=SUM(1,2)') | ||
->getStyle()->getProtection() | ||
->setHidden(Protection::PROTECTION_UNPROTECTED); | ||
$sheet->getCell('B1')->setValue('X') | ||
->getStyle()->getProtection() | ||
->setHidden(Protection::PROTECTION_PROTECTED); | ||
$sheet->getCell('B2')->setValue('=SUM(1,2)') | ||
->getStyle()->getProtection() | ||
->setHidden(Protection::PROTECTION_PROTECTED); | ||
$sheet->getCell('C1')->setValue('X'); | ||
$sheet->getCell('C2')->setValue('=SUM(1,2)'); | ||
self::assertFalse($sheet->getCell('A1')->isHiddenOnFormulaBar()); | ||
self::assertFalse($sheet->getCell('A2')->isHiddenOnFormulaBar()); | ||
self::assertFalse($sheet->getCell('B1')->isHiddenOnFormulaBar()); | ||
self::assertFalse($sheet->getCell('B2')->isHiddenOnFormulaBar()); | ||
self::assertFalse($sheet->getCell('C1')->isHiddenOnFormulaBar()); | ||
self::assertFalse($sheet->getCell('C2')->isHiddenOnFormulaBar()); | ||
$sheetProtection = $sheet->getProtection(); | ||
$sheetProtection->setSheet(true); | ||
self::assertFalse($sheet->getCell('A1')->isHiddenOnFormulaBar()); | ||
self::assertFalse($sheet->getCell('A2')->isHiddenOnFormulaBar()); | ||
self::assertFalse($sheet->getCell('B1')->isHiddenOnFormulaBar(), 'not a formula1'); | ||
self::assertTrue($sheet->getCell('B2')->isHiddenOnFormulaBar()); | ||
self::assertFalse($sheet->getCell('C1')->isHiddenOnFormulaBar(), 'not a formula2'); | ||
self::assertTrue($sheet->getCell('C2')->isHiddenOnFormulaBar()); | ||
self::assertFalse($sheet->getCell('D1')->isHiddenOnFormulaBar(), 'uninitialized cell is not formula'); | ||
$spreadsheet->disconnectWorksheets(); | ||
} | ||
|
||
public function testisLocked(): void | ||
{ | ||
$spreadsheet = new Spreadsheet(); | ||
$sheet = $spreadsheet->getActiveSheet(); | ||
$sheet->getCell('A1')->setValue('X') | ||
->getStyle()->getProtection() | ||
->setLocked(Protection::PROTECTION_UNPROTECTED); | ||
$sheet->getCell('A2')->setValue('=SUM(1,2)') | ||
->getStyle()->getProtection() | ||
->setLocked(Protection::PROTECTION_UNPROTECTED); | ||
$sheet->getCell('B1')->setValue('X') | ||
->getStyle()->getProtection() | ||
->setLocked(Protection::PROTECTION_PROTECTED); | ||
$sheet->getCell('B2')->setValue('=SUM(1,2)') | ||
->getStyle()->getProtection() | ||
->setLocked(Protection::PROTECTION_PROTECTED); | ||
$sheet->getCell('C1')->setValue('X'); | ||
$sheet->getCell('C2')->setValue('=SUM(1,2)'); | ||
self::assertFalse($sheet->getCell('A1')->isLocked()); | ||
self::assertFalse($sheet->getCell('A2')->isLocked()); | ||
self::assertFalse($sheet->getCell('B1')->isLocked()); | ||
self::assertFalse($sheet->getCell('B2')->isLocked()); | ||
self::assertFalse($sheet->getCell('C1')->isLocked()); | ||
self::assertFalse($sheet->getCell('C2')->isLocked()); | ||
$sheetProtection = $sheet->getProtection(); | ||
$sheetProtection->setSheet(true); | ||
self::assertFalse($sheet->getCell('A1')->isLocked()); | ||
self::assertFalse($sheet->getCell('A2')->isLocked()); | ||
self::assertTrue($sheet->getCell('B1')->isLocked()); | ||
self::assertTrue($sheet->getCell('B2')->isLocked()); | ||
self::assertTrue($sheet->getCell('C1')->isLocked()); | ||
self::assertTrue($sheet->getCell('C2')->isLocked()); | ||
self::assertTrue($sheet->getCell('D1')->isLocked(), 'uninitialized cell'); | ||
$spreadsheet->disconnectWorksheets(); | ||
} | ||
} |
Binary file not shown.