mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-09-30 08:57:27 +00:00
40f7cd00db
We have identical constants defined in several places, and use literals in others. We aren't consistent in checking limits. This PR makes the use of the constants in Cell/AddressRange the "official" source, deprecates all other constants, and substitutes the constants wherever literals are used. A number of different edge case tests are added. During testing, I discovered that `columnIndexFromString` correctly throws an exception for 4-character string, but allows `XFE` through `ZZZ`, all of which are also invalid. There are similar inconsistencies with related routines, and this PR attempts to make them operate consistently. One suprise is that throwing for `row=0` causes serious regression problems, so it continues to be permitted (but the high row limit is enforced). Further, Reference Helper sometimes dips into negative numbers, resulting in totally unexpected results (-1 affects column Z, -2 column Y, etc.). It is changed to ignore rows and columns outside the limits.
104 lines
3.9 KiB
PHP
104 lines
3.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Worksheet;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class InsertTest extends TestCase
|
|
{
|
|
public function testInsertRow(): void
|
|
{
|
|
$spreadsheet = new Spreadsheet();
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
$sheet->fromArray([
|
|
[1, 2, 3, 4],
|
|
[5, 6, 7, 8],
|
|
[9, 10, 11, 12],
|
|
[13, 14, 15, 16],
|
|
[17, 18, 19, 20],
|
|
]);
|
|
$sheet->getRowDimension(1000)->setVisible(false);
|
|
$sheet->getStyle('C3')->getFont()->setBold(true);
|
|
self::assertSame(1000, $sheet->getHighestRow());
|
|
self::assertSame(5, $sheet->getHighestDataRow());
|
|
$currentRow = 4;
|
|
$sheet->insertNewRowBefore($currentRow, 1);
|
|
self::assertSame(1001, $sheet->getHighestRow());
|
|
self::assertSame(6, $sheet->getHighestDataRow());
|
|
self::assertTrue($sheet->getStyle('C3')->getFont()->getBold());
|
|
self::assertSame(11, $sheet->getCell('C3')->getValue());
|
|
self::assertTrue($sheet->getStyle('C4')->getFont()->getBold());
|
|
self::assertNull($sheet->getCell('C4')->getValue());
|
|
self::assertFalse($sheet->getRowDimension(1001)->getVisible());
|
|
self::assertTrue($sheet->getRowDimension(1000)->getVisible());
|
|
$spreadsheet->disconnectWorksheets();
|
|
}
|
|
|
|
public function testInsertColumn(): void
|
|
{
|
|
$spreadsheet = new Spreadsheet();
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
$sheet->fromArray([
|
|
[1, 2, 3, 4],
|
|
[5, 6, 7, 8],
|
|
[9, 10, 11, 12],
|
|
[13, 14, 15, 16],
|
|
[17, 18, 19, 20],
|
|
]);
|
|
$sheet->getColumnDimension('ZY')->setVisible(false);
|
|
$sheet->getStyle('C3')->getFont()->setBold(true);
|
|
self::assertSame('ZY', $sheet->getHighestColumn());
|
|
self::assertSame('D', $sheet->getHighestDataColumn());
|
|
$currentColumn = 'D';
|
|
$sheet->insertNewColumnBefore($currentColumn, 1);
|
|
self::assertSame('ZZ', $sheet->getHighestColumn());
|
|
self::assertSame('E', $sheet->getHighestDataColumn());
|
|
self::assertTrue($sheet->getStyle('C3')->getFont()->getBold());
|
|
self::assertSame(11, $sheet->getCell('C3')->getValue());
|
|
self::assertTrue($sheet->getStyle('D3')->getFont()->getBold());
|
|
self::assertNull($sheet->getCell('D3')->getValue());
|
|
self::assertFalse($sheet->getColumnDimension('ZZ')->getVisible());
|
|
self::assertTrue($sheet->getColumnDimension('ZY')->getVisible());
|
|
$spreadsheet->disconnectWorksheets();
|
|
}
|
|
|
|
/**
|
|
* A very strange case.
|
|
* Excel does not allow you to insert a column when
|
|
* the highest possible column has non-empty cells.
|
|
* PhpSpreadsheet does, but ...
|
|
*
|
|
* When neither XEY1 nor XEZ1 is set, current code creates a
|
|
* spreadsheet with cell XFH1, which is nominally illegal.
|
|
* Excel does not consider the result corrupt,
|
|
* but XFD1 has moved to I1! Xml has entries up to H1,
|
|
* but then skips to XFH1, which Excel seems to
|
|
* tolerate as "next available column".
|
|
*
|
|
* When XEZ1 is set, current code creates a spreadsheet
|
|
* which Excel opens with an error pop-up.
|
|
* with cells XFD1 and XFH1 both defined. Here Excel
|
|
* can't tolerate XFH1 because last possible column
|
|
* is in use.
|
|
*
|
|
* When XEY1 is set but not XEZ1, Xml defines cells
|
|
* XFC1 (result of shifting XEY1 4 columns) and XFH1,
|
|
* which Excel moves back to next available column (XFD1).
|
|
*/
|
|
public function testOverEdge(): void
|
|
{
|
|
$spreadsheet = new Spreadsheet();
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
$sheet->fromArray([
|
|
[1, 2, 3, 4],
|
|
]);
|
|
$sheet->getCell('XFD1')->setValue('lastcol');
|
|
$sheet->insertNewColumnBefore('D', 4);
|
|
self::assertFalse($sheet->getCellCollection()->has('XFH1'));
|
|
$spreadsheet->disconnectWorksheets();
|
|
}
|
|
}
|