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.
Addsheet May Leave Active Sheet Uninitialized
Fix PHPOffice#4112. Direct cause is that `applyStylesFromArray` tries to save and restore `activeSheetIndex`. However, if activeSheetIndex is -1, indicating no active sheet, the restore should not be attempted. Code is changed to test before attempting to restore. The actual problem, however, is that user specified a sheet number for `addSheet`. That method will set activeSheetIndex most of the time, but this was a gap - when the supplied sheet number (0 in this case) is greater than activeSheetIndex (-1 in this case), it was leaving activeSheetIndex as -1. It is changed to set activeSheetIndex to 0 when activeSheetIndex is negative.
- Loading branch information
Showing
4 changed files
with
50 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpOffice\PhpSpreadsheetTests\Worksheet; | ||
|
||
use PhpOffice\PhpSpreadsheet\Spreadsheet; | ||
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; | ||
use PhpOffice\PhpSpreadsheetTests\Functional\AbstractFunctional; | ||
|
||
class Issue4112Test extends AbstractFunctional | ||
{ | ||
/** | ||
* Problem deleting all sheets then adding one. | ||
* | ||
* @dataProvider providerSheetNumber | ||
*/ | ||
public function testIssue4112(?int $sheetNumber): void | ||
{ | ||
$mySpreadsheet = new Spreadsheet(); | ||
$mySpreadsheet->removeSheetByIndex(0); | ||
$worksheet = new Worksheet($mySpreadsheet, 'addedsheet'); | ||
self::assertSame(-1, $mySpreadsheet->getActiveSheetIndex()); | ||
$mySpreadsheet->addSheet($worksheet, 0); | ||
self::assertSame('addedsheet', $mySpreadsheet->getActiveSheet()->getTitle()); | ||
$row = 1; | ||
$col = 1; | ||
$worksheet->getCell([$col, $row])->setValue('id_uti'); | ||
self::assertSame('id_uti', $worksheet->getCell([$col, $row])->getValue()); | ||
$mySpreadsheet->disconnectWorksheets(); | ||
} | ||
|
||
public static function providerSheetNumber(): array | ||
{ | ||
return [ | ||
'problem case' => [0], | ||
'normal case' => [null], | ||
'negative 1 (as if there were no sheets)' => [-1], | ||
'diffeent negative number' => [-4], | ||
'positive number' => [4], | ||
]; | ||
} | ||
} |