Skip to content

Commit

Permalink
upd: group/outline rows and columns
Browse files Browse the repository at this point in the history
  • Loading branch information
aVadim483 committed Jun 27, 2024
1 parent ce9a77d commit 19394f8
Show file tree
Hide file tree
Showing 7 changed files with 297 additions and 55 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## v.5.5

* Support rich text in cells and notes
* Support rich text in cells and notes\
* Group/outline columns and rows

## v.5.3

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ Jump To:
* [Page settings](/docs/02-sheets.md#page-settings)
* [Row's settings](/docs/02-sheets.md#rows-settings)
* [Column's settings](/docs/02-sheets.md#columns-settings)
* [Group/outline rows and columns](/docs/02-sheets.md#groupoutline-rows-and-columns)
* [Define Named Ranges](/docs/02-sheets.md#define-named-ranges)
* [Freeze Panes and Autofilter](/docs/02-sheets.md#freeze-panes-and-autofilter)
* [Setting Active Cells](/docs/02-sheets.md#setting-active-cells)
Expand Down
83 changes: 82 additions & 1 deletion docs/02-sheets.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ $sheet1->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)
Therefore, the following code will throw an error "Row number must be greater then written rows"
Therefore, the following code will throw an error "Row number must be greater than written rows"

```php
$sheet = $excel->sheet();
Expand Down Expand Up @@ -173,6 +173,87 @@ $this->setColWidth('D', 10);
$this->setColWidth('D', 30);
// The column width will be set to the width of the content, but not less than 20
$this->setColWidthAuto('D');
```
### Group/outline rows and columns

Set group level for the specified rows

```php
$sheet = $excel->sheet();

// the first level
$sheet->writeRow($rowData1)->applyRowOutlineLevel(1);
$sheet->writeRow($rowData2)->applyRowOutlineLevel(1);

// the second level
$sheet->writeRow($rowData3)->applyRowOutlineLevel(2);
$sheet->writeRow($rowData4)->applyRowOutlineLevel(2);

// back to the first level
$sheet->writeRow($rowData5)->applyRowOutlineLevel(1);

// write rows without grouping
$sheet->writeRow($rowData6);
$sheet->writeRow($rowData7);
```

You can set up grouping for future rows.

```php
// set level 1 for row 4
$sheet->setRowOutlineLevel(4, 1);

// set level 1 for rows 5, 6, 7
$sheet->setRowOutlineLevel([5, 6, 7], 1);

// set level 1 for rows from 9 to 15
$sheet->setRowOutlineLevel('9:15', 1);
// set level 2 for rows from 11 to 13
$sheet->setRowOutlineLevel('11:13', 2);
```

You can set up grouping for a sequence of rows.

```php
$sheet = $excel->sheet();

// Writing rows without grouping
$sheet->writeRow([...]);
$sheet->writeRow([...]);

// Increase group level (set level to 1)
$sheet->beginOutlineLevel();
$sheet->writeRow([...]);
$sheet->writeRow([...]);

// Increase group level again (set level to 2) with collapsing
$sheet->beginOutlineLevel(true);
$sheet->writeRow([...]);
$sheet
->writeCell('...')
->writeCell('...')
->writeCell('...')
->nextRow();
$sheet->writeRow([...]);

// Decrease group level (back to 1)
$sheet->endOutlineLevel();
$sheet->writeRow([...]);

// Set zero level
$sheet->endOutlineLevel();
```

Set group level for the specified columns

```php
$sheet->setColOutlineLevel('B', 1);
$sheet->setColOutlineLevel('C', 1);
$sheet->setColOutlineLevel('D', 1);

$sheet->setColOutlineLevel(['F', 'g', 'h', 'i', 'J'], 1);
$sheet->setColOutlineLevel('g:i', 2);

```

### Define Named Ranges
Expand Down
2 changes: 1 addition & 1 deletion src/FastExcelWriter/Area.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function __construct(Sheet $sheet, $range)
$dimension = Excel::rangeDimension($range, true);
}
if ($dimension['rowNum1'] <= $sheet->rowCountWritten) {
throw new Exception("Cannot make area range $range (row number must be greater then written rows)");
throw new Exception("Cannot make area range $range (row number must be greater than written rows)");
}

$this->sheet = $sheet;
Expand Down
4 changes: 2 additions & 2 deletions src/FastExcelWriter/Excel.php
Original file line number Diff line number Diff line change
Expand Up @@ -760,8 +760,8 @@ public static function colLetterRange($colKeys, ?int $baseNum = 0): array

return array_merge(...$columns);
}
elseif (strpos($colKeys, '-')) {
[$num1, $num2] = explode('-', $colKeys);
elseif (($p1 = strpos($colKeys, '-')) || ($p2 = strpos($colKeys, ':'))) {
[$num1, $num2] = $p1 ? explode('-', $colKeys) : explode(':', $colKeys);
$columns = [];
for ($colNum = self::colNumber($num1); $colNum <= self::colNumber($num2); $colNum++) {
$columns[] = self::colLetter($colNum);
Expand Down
Loading

0 comments on commit 19394f8

Please sign in to comment.