diff --git a/docs/02-sheets.md b/docs/02-sheets.md index 3b96556..f933b30 100644 --- a/docs/02-sheets.md +++ b/docs/02-sheets.md @@ -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); @@ -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) @@ -151,9 +151,9 @@ $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], @@ -161,19 +161,34 @@ $colOptions = [ '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 diff --git a/src/FastExcelWriter/Sheet.php b/src/FastExcelWriter/Sheet.php index ee05e07..4a53c3f 100644 --- a/src/FastExcelWriter/Sheet.php +++ b/src/FastExcelWriter/Sheet.php @@ -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]); + } } } @@ -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; @@ -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) *