Skip to content

Commit

Permalink
The order of writing entries to the file has been changed so that the…
Browse files Browse the repository at this point in the history
… mimetype is determined correctly. Bug fixes and some improvements
  • Loading branch information
aVadim483 committed Sep 19, 2023
1 parent a23b863 commit 22c7165
Show file tree
Hide file tree
Showing 7 changed files with 309 additions and 189 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## v.4.7

The order of writing entries to the file has been changed so that the mimetype is determined correctly

Bug fixes and some improvements

New method
* Sheet::setActiveCell($cellAddress);

## v.4.6

New methods
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ Jump To:
* [Helpers methods](/docs/01-workbook.md#helpers-methods)
* [Sheets](/docs/02-sheets.md)
* [Create, select and remove sheet](/docs/02-sheets.md#create-select-and-remove-sheet)
* [Sheet settings](/docs/02-sheets.md#create-select-and-remove-sheet)
* [Page settings](/docs/02-sheets.md#create-select-and-remove-sheet)
* [Sheet settings](/docs/02-sheets.md#sheet-settings)
* [Page settings](/docs/02-sheets.md#page-settings)
* [Freeze Panes and Autofilter](/docs/02-sheets.md#freeze-panes-and-autofilter)
* [Setting Active Cells](/docs/02-sheets.md#setting-active-cells)
* [Writing](/docs/03-writing.md)
* [Writing Row by Row vs Direct](/docs/03-writing.md#writing-row-by-row-vs-direct)
* [Direct Writing To Cells](/docs/03-writing.md#direct-writing-to-cells)
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
},
"require-dev": {
"phpunit/phpunit": "^9.0",
"ext-fileinfo": "*",
"avadim/fast-excel-reader": "dev-main"
},
"funding": [
Expand Down
10 changes: 10 additions & 0 deletions docs/02-sheets.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,14 @@ $sheet->setFreeze('B2');
$sheet->setAutofilter(1);
```

### Setting Active Cells

```php
// Selecting one active cell
$sheet->setActiveCell('B2');

// Selecting a range of cells
$sheet->setActiveCell('B2:C3');
```

Returns to [README.md](/README.md)
122 changes: 113 additions & 9 deletions src/FastExcelWriter/Sheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ class Sheet
'footer' => '0.5',
];

protected ?string $activeCell = null;
protected array $sheetViews = [];


Expand Down Expand Up @@ -503,6 +504,28 @@ public function setFreezeColumns(int $freezeColumns): Sheet
return $this;
}

/**
* @param $cellAddress
*
* @return $this
*/
public function setActiveCell($cellAddress): Sheet
{
$address = $this->_parseAddress($cellAddress);
if (!isset($address['row'], $address['col'])) {
ExceptionAddress::throwNew('Wrong cell address %s', print_r($address, 1));
}
if ($address['cell1'] === $address['cell2']) {
$this->activeCell = $address['cell1'];
}
else {
$this->activeCell = $address['cell1'] . ':' . $address['cell2'];
}

return $this;
}


/**
* @param int|null $row
* @param int|null $col
Expand Down Expand Up @@ -977,6 +1000,13 @@ public function setRowHeight($rowNum, $height): Sheet
elseif (is_numeric($rowNum)) {
$this->_setRowSettings($rowNum, 'height', str_replace(',', '.', (float)$height));
}
else {
$address = $this->_parseAddress($rowNum);
for ($row = $address['rowNum1']; $row <= $address['rowNum2']; $row++) {
$this->setRowHeight($row, $height);
}
}

return $this;
}

Expand Down Expand Up @@ -1013,6 +1043,13 @@ public function setRowVisible($rowNum, bool $visible): Sheet
elseif (is_numeric($rowNum)) {
$this->_setRowSettings($rowNum, 'hidden', $visible ? 0 : 1);
}
else {
$address = $this->_parseAddress($rowNum);
for ($row = $address['rowNum1']; $row <= $address['rowNum2']; $row++) {
$this->setRowVisible($row, $visible);
}
}

return $this;
}

Expand Down Expand Up @@ -1109,7 +1146,7 @@ protected function _writeRow(?Writer $writer, array $row = [], array $rowOptions
if ($this->rowCountWritten === 0) {
$_styleCache = [];
}
$nnn = $this->rowCountWritten;

if (isset($this->rowSettings[$this->rowCountWritten])) {
$rowOptions = array_replace($this->rowSettings[$this->rowCountWritten], $rowOptions);
}
Expand Down Expand Up @@ -1774,7 +1811,7 @@ public function getMergedCells(): array
protected function _writeCurrentRow(): int
{
$savedRow = $this->currentRowIdx;
if (!empty($this->cells['values']) || !empty($this->cells['styles'])) {
if (!empty($this->cells['values']) || !empty($this->cells['styles']) || $this->rowSettings) {
$writer = $this->excel->getWriter();
if (!$this->open) {
$writer->writeSheetDataBegin($this);
Expand Down Expand Up @@ -2415,7 +2452,7 @@ public function writeDataBegin($writer)
return;
}

$sheetFileName = $writer->tempFilename('xl/worksheets/' . $this->xmlName);
$sheetFileName = $writer->tempFilename();
$this->setFileWriter($writer::makeWriteBuffer($sheetFileName));

$this->fileWriter->write('<sheetData>');
Expand Down Expand Up @@ -2898,6 +2935,9 @@ public function addImage(string $cell, string $imageFile, ?array $imageStyle = [
$colIdx = $dimension['colIndex'];
}
if ($cell) {
if ($rowIdx >= $this->currentRowIdx && !isset($this->cells['values'][$rowIdx][$colIdx])) {
$this->cells['values'][$rowIdx][$colIdx] = null;
}
$imageData = $this->excel->loadImageFile($imageFile);
if ($imageData) {
$imageData['cell'] = $cell;
Expand Down Expand Up @@ -3383,12 +3423,76 @@ public function pagePaperWidth($paperWidth): Sheet
*/
public function getSheetViews(): array
{
$result = $this->sheetViews;
if ($this->isRightToLeft()) {
$result[0]['rightToLeft'] = 'true';
}
if ($this->active) {
$result[0]['tabSelected'] = 'true';
$result = [];
$paneRow = ($this->freezeRows ? $this->freezeRows + 1 : 0);
$paneCol = ($this->freezeColumns ? $this->freezeColumns + 1 : 0);
foreach ($this->sheetViews as $n => $sheetView) {
$result[$n] = [
'_attr' => $sheetView,
];
if ($this->isRightToLeft()) {
$result[$n]['_attr']['rightToLeft'] = 'true';
}
if ($this->active) {
$result[$n]['_attr']['tabSelected'] = 'true';
}
if ($this->freezeRows && $this->freezeColumns) {
// frozen rows and cols
$result[$n]['_items'] = [
[
'_tag' => 'pane',
'_attr' => ['ySplit' => $this->freezeRows, 'xSplit' => $this->freezeColumns, 'topLeftCell' => Excel::cellAddress($paneRow, $paneCol), 'activePane' => 'bottomRight', 'state' => 'frozen'],
],
[
'_tag' => 'selection',
'_attr' => ['activeCell' => Excel::cellAddress($paneRow, 1), 'pane' => 'topRight', 'sqref' => Excel::cellAddress($paneRow, 1)],
],
[
'_tag' => 'selection',
'_attr' => ['activeCell' => Excel::cellAddress(1, $paneCol), 'pane' => 'bottomLeft', 'sqref' => Excel::cellAddress(1, $paneCol)],
],
[
'_tag' => 'selection',
'_attr' => ['activeCell' => Excel::cellAddress($paneRow, $paneCol), 'pane' => 'bottomRight', 'sqref' => Excel::cellAddress($paneRow, $paneCol)],
],
];
}
elseif ($this->freezeRows) {
// frozen rows only
$result[$n]['_items'] = [
[
'_tag' => 'pane',
'_attr' => ['ySplit' => $this->freezeRows, 'topLeftCell' => Excel::cellAddress($paneRow, 1), 'activePane' => 'bottomRight', 'state' => 'frozen'],
],
[
'_tag' => 'selection',
'_attr' => ['activeCell' => Excel::cellAddress($paneRow, 1), 'pane' => 'bottomLeft', 'sqref' => Excel::cellAddress($paneRow, 1)],
],
];
}
elseif ($this->freezeColumns) {
// frozen cols only
$result[$n]['_items'] = [
[
'_tag' => 'pane',
'_attr' => ['xSplit' => $this->freezeColumns, 'topLeftCell' => Excel::cellAddress(1, $paneCol), 'activePane' => 'topRight', 'state' => 'frozen'],
],
[
'_tag' => 'selection',
'_attr' => ['activeCell' => Excel::cellAddress(1, $paneCol), 'pane' => 'topRight', 'sqref' => Excel::cellAddress(1, $paneCol)],
],
];
}
else {
// not frozen
$activeCell = $this->activeCell ?? $this->minCell();
$result[$n]['_items'] = [
[
'_tag' => 'selection',
'_attr' => ['activeCell' => $activeCell, 'pane' => 'topLeft', 'sqref' => $activeCell],
],
];
}
}

return $result;
Expand Down
Loading

0 comments on commit 22c7165

Please sign in to comment.