Skip to content

Commit

Permalink
fix setColVisible()
Browse files Browse the repository at this point in the history
  • Loading branch information
aVadim483 committed Sep 18, 2024
1 parent 8fc9d09 commit 5e289fa
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 17 deletions.
47 changes: 31 additions & 16 deletions docs/02-sheets.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ $rowOptions = [
'border' => 'thin',
'height' => 28,
];
$sheet1->writeRow(['aaa', 'bbb', 'ccc'], $rowOptions);
$sheet->writeRow(['aaa', 'bbb', 'ccc'], $rowOptions);
```
Other way with the same result

```php
$sheet1->writeRow(['aaa', 'bbb', 'ccc', null, 'eee'])
$sheet->writeRow(['aaa', 'bbb', 'ccc', null, 'eee'])
->applyFillColor('#fffeee')
->applyBorder('thin')
->applyRowHeight(28);
Expand All @@ -102,25 +102,25 @@ You can set row's height or visibility

```php
// Set height of row 2 to 33
$sheet1->setRowHeight(2, 33);
$sheet->setRowHeight(2, 33);

// Set height of rows 3,5 and 7 to 33
$sheet1->setRowHeight([3, 5, 7], 33);
$sheet->setRowHeight([3, 5, 7], 33);

// Set heights of several rows
$sheet1->setRowHeights([1 => 20, 2 => 33, 3 => 40]);
$sheet->setRowHeights([1 => 20, 2 => 33, 3 => 40]);

// Hide row 8
$sheet1->setRowVisible(8, false);
$sheet->setRowVisible(8, false);

// Other way
$sheet1->setRowHidden(8);
$sheet->setRowHidden(8);

// Hide rows 9, 10, 11
$sheet1->setRowVisible([9, 10, 11], false);
$sheet->setRowVisible([9, 10, 11], false);

// Show row 10
$sheet1->setRowVisible(10, true);
$sheet->setRowVisible(10, true);
```
IMPORTANT: You can only use the setRowXX() functions on rows numbered at least as high as the current one.
See [Writing Row by Row vs Direct](/docs/03-writing.md#writing-row-by-row-vs-direct)
Expand Down Expand Up @@ -151,29 +151,44 @@ $sheet->setColWidthAuto('D');
$sheet->setColOptions('D', ['width' => 'auto']);

// Set width of specific columns
$sheet1->setColWidths(['B' => 10, 'C' => 'auto', 'E' => 30, 'F' => 40]);
$sheet->setColWidths(['B' => 10, 'C' => 'auto', 'E' => 30, 'F' => 40]);
// Set width of columns from 'A'
$sheet1->setColWidths([10, 20, 30, 40], 24);
$sheet->setColWidths([10, 20, 30, 40], 24);

$colOptions = [
'B' => ['width' => 10],
'C' => ['width' => 'auto'],
'E' => ['width' => 30],
'F' => ['width' => 40],
];
$sheet1->setColOptions($colOptions);
$sheet->setColOptions($colOptions);

```
You can define a minimal width of columns. Note that the minimum value has higher priority
```php
// Set minimum width to 20
$this->setColMinWidth('D', 20);
$sheet->setColMinWidth('D', 20);

// The value 10 will not be set because it is less than the minimum value
$this->setColWidth('D', 10);
$sheet->setColWidth('D', 10);

// But width 30 will be set
$this->setColWidth('D', 30);
$sheet->setColWidth('D', 30);

// The column width will be set to the width of the content, but not less than 20
$this->setColWidthAuto('D');
$sheet->setColWidthAuto('D');

// Hide column B
$sheet->setColVisible('B', false);

// Other way
$sheet->setColHidden('B');

// Hide columns B, E, H
$sheet->setColVisible(['B', 'E', 'H'], false);

// Show column E
$sheet->setColVisible('E', true);
```
### Group/outline rows and columns

Expand Down
26 changes: 25 additions & 1 deletion src/FastExcelWriter/Sheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -841,7 +841,12 @@ public function setColVisible($col, bool $val): Sheet
$colIndexes = Excel::colIndexRange($col);
foreach($colIndexes as $colIdx) {
if ($colIdx >= 0) {
$this->_setColAttributes($colIdx, ['hidden' => (int)$val]);
if ($val) {
$this->_delColAttributes($colIdx, ['hidden']);
}
else {
$this->_setColAttributes($colIdx, ['hidden' => 1]);
}
}
}

Expand Down Expand Up @@ -998,6 +1003,10 @@ public function getColAttributes(): array
if ($this->colAttributes) {
foreach ($this->colAttributes as $colIdx => $attributes) {
if ($attributes) {
if (isset($this->colAttributes[$colIdx]['min'], $this->colAttributes[$colIdx]['min']) && count($this->colAttributes[$colIdx]) === 2) {
// only 'min' & 'max'
continue;
}
$result[$colIdx] = $attributes;
if (!isset($result[$colIdx]['min'])) {
$result[$colIdx]['min'] = $colIdx + 1;
Expand Down Expand Up @@ -1036,6 +1045,21 @@ public function _setColAttributes(int $colIdx, array $settings)
}
}

/**
* @param int $colIdx
* @param array $settings
*
* @return void
*/
public function _delColAttributes(int $colIdx, array $settings)
{
foreach ($settings as $key) {
if ($this->colAttributes[$colIdx][$key]) {
unset($this->colAttributes[$colIdx][$key]);
}
}
}

/**
* Set style of single or multiple column(s)
*
Expand Down

0 comments on commit 5e289fa

Please sign in to comment.