Skip to content

Novis\Spreadsheet\Sheet::addOutlineGroup

$sheet->addOutlineGroup(
    string $rows,
    ?{ collapsed?: bool } $options = null,
): Sheet

Puts rows, such as "5:9", or columns, such as "B:D", in an outline group, and returns the sheet. Excel shows a button beside the group, and a person can hide and show the group with one click.

collapsed: true hides the rows or columns of the group when the file opens. A person can still show them in Excel.

Good to know: add a group of rows before you add the rows in it. Add a group of columns before the first row of the sheet. A group added too late throws a LogicError. A row can also get its group from the level option of addRow. A CSV file has no groups, so addOutlineGroup does nothing there.

The examples below group the order lines under each order, and hide a group of columns. The last one shows the error for a group added after its rows.

Parameters

  • $rowsstring
  • $options?{ collapsed?: bool }default null

Return value

Returns Sheet.

Changelog

VersionChange
0.0.6Each Novis\Spreadsheet\Sheet and Workbook method that adds to the file says what it adds, for example addRow(), addChart() and addSheet(). setPrintOptions() replaces print(). Chart->withSeries() and combinedWith() return a new chart.

Examples

Group the lines of an order

<?nvs
// Shows how to group the lines of an order under the row of the order.
use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();
var $sheet = $book->addSheet('Orders');
// The group is added before rows 3 and 4 are added.
$sheet->addOutlineGroup('3:4');
$sheet->addRow(['what' => 'Order 1042', 'total' => 9.5]);
$sheet->addRow(['what' => 'Widget', 'total' => 5.0]);
$sheet->addRow(['what' => 'Gadget', 'total' => 4.5]);
bytes $file = $book->finish()->bytes ?? Core\Bytes::fill(0, 0);
// `Core\Zip::read` returns the XML of the sheet. These lines check that rows 3 and 4 are in the group.
var $xml = Core\Zip::read($file, 'xl/worksheets/sheet1.xml') as string;
echo Core\Str::contains($xml, '<row r="3" outlineLevel="1">') ? 'row 3 is in the group' : 'row 3 is not', "\n";
echo Core\Str::contains($xml, '<row r="4" outlineLevel="1">') ? 'row 4 is in the group' : 'row 4 is not', "\n";
Output
row 3 is in the group
row 4 is in the group

Hide a group of columns

<?nvs
// Shows how to hide the detail columns of a report in a group a person can open.
use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();
var $sheet = $book->addSheet('Sales');
// A group of columns is added before the first row. `collapsed: true` hides columns B and C.
$sheet->addOutlineGroup('B:C', { collapsed: true });
$sheet->addRow(['shop' => 'North', 'january' => 120, 'february' => 140, 'total' => 260]);
$sheet->addRow(['shop' => 'South', 'january' => 80, 'february' => 95, 'total' => 175]);
bytes $file = $book->finish()->bytes ?? Core\Bytes::fill(0, 0);
// `Core\Zip::read` returns the XML of the sheet. This line checks that column B is hidden.
var $xml = Core\Zip::read($file, 'xl/worksheets/sheet1.xml') as string;
echo Core\Str::contains($xml, '<col min="2" max="2" width="8" customWidth="1" hidden="1" outlineLevel="1"/>') ? 'column B is hidden' : 'column B is shown', "\n";
Output
column B is hidden

A group after its rows

<?nvs
// Shows the error for a group of rows that are already added.
use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();
var $sheet = $book->addSheet('Orders');
$sheet->addRows([['order' => 1042], ['order' => 1043], ['order' => 1044]]);
// Rows 2 and 3 are already in the file, so `addOutlineGroup` throws a `LogicError`.
try {
$sheet->addOutlineGroup('2:3');
} catch (LogicError $e) {
echo $e->message, "\n";
}
// The `level` option of `addRow` puts a new row in a group.
$sheet->addRow(['order' => 1045], { level: 1 });
echo "row 5 is in a group\n";
Output
Rows 2:3 of the sheet `Orders` are already written. Group rows before you add them, or give each row its `level`.
row 5 is in a group