Skip to content

Commit

Permalink
upd mergeCells()
Browse files Browse the repository at this point in the history
  • Loading branch information
aVadim483 committed Nov 4, 2023
1 parent dded275 commit 259748d
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
5 changes: 0 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,6 @@ Use `composer` to install **FastExcelWriter** into your project:
composer require avadim/fast-excel-writer
```

Also, you can download package and include autoload file of the library:
```php
require 'path/to/fast-excel-writer/src/autoload.php';
```

## Changes In Version 4

* Now the library works even faster
Expand Down
18 changes: 15 additions & 3 deletions src/FastExcelWriter/Sheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -1854,10 +1854,11 @@ public function writeArrayTo($topLeftCell, array $data): Sheet
* mergeCells(['A1:B2', 'C1:D2'])
*
* @param array|string|int $rangeSet
* @param int|null $actionMode 0 - exception, 1 - replace, 2 - keep
*
* @return $this
*/
public function mergeCells($rangeSet): Sheet
public function mergeCells($rangeSet, ?int $actionMode = 0): Sheet
{
foreach((array)$rangeSet as $range) {
if (isset($this->mergeCells[$range]) || empty($range)) {
Expand All @@ -1873,10 +1874,21 @@ public function mergeCells($rangeSet): Sheet
&& ((($dimension['colNum1'] >= $savedDimension['colNum1']) && ($dimension['colNum1'] <= $savedDimension['colNum2']))
|| (($dimension['colNum2'] >= $savedDimension['colNum1']) && ($dimension['colNum2'] <= $savedDimension['colNum2'])))
) {
throw new Exception("Cannot merge cells $range because they are intersecting with $savedRange");
if ($actionMode === 1) {
unset($this->mergeCells[$savedRange]);
}
elseif ($actionMode === 2) {
$dimension = [];
break;
}
else {
throw new Exception("Cannot merge cells $range because they are intersecting with $savedRange");
}
}
}
$this->mergeCells[$dimension['range']] = $dimension;
if ($dimension) {
$this->mergeCells[$dimension['range']] = $dimension;
}
}

return $this;
Expand Down
33 changes: 33 additions & 0 deletions tests/FastExcelWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,39 @@ public function testExcelWriter5()
unlink($testFileName);
}

public function testExcelWriterMergedCells()
{
$testFileName = __DIR__ . '/test_merged.xlsx';
if (file_exists($testFileName)) {
unlink($testFileName);
}

$excel = Excel::create();
$sheet = $excel->sheet();

$area = $sheet->beginArea();
$area->setValue('A1', 'A');
// Write value to automerged cells
$area->setValue('A2:D2', 'A2:D2');
$sheet->writeAreas();

$sheet->writeCell(11);
$sheet->writeCell(12);
$sheet->writeCell(13);
$sheet->nextRow();
$sheet->writeCell(21);
$sheet->writeCell(32);
$sheet->writeCell(43);
$sheet->mergeCells('A4:C4');

$this->excelReader = $this->saveCheckRead($excel, $testFileName);
$mergedCells = $this->excelReader->sheet()->getMergedCells();
$a = ['A2' => 'A2:D2', 'A4' => 'A4:C4'];
$this->assertEquals($a, $mergedCells);

unlink($testFileName);
}

public function testExcelWriterNotesAndImages()
{
$testFileName = __DIR__ . '/test_notes_images.xlsx';
Expand Down

0 comments on commit 259748d

Please sign in to comment.