Skip to content

Novis\Spreadsheet\Sheet::mergeCells

$sheet->mergeCells(string $range): Sheet$sheet->mergeCells(
    string $range,
): Sheet

Joins the cells of $range, such as "A1:C1", into one cell, and returns the sheet.

Excel shows the value of the top left cell over the whole range. The range can use rows you already added and rows you add later, until the sheet is finished.

Good to know: two merged ranges of one sheet cannot overlap, and a second range that overlaps the first throws a LogicError. A text that is not a range of cells, such as "A1" alone or "title", throws a LogicError too. A CSV file has no merged cells, so mergeCells does nothing there.

The examples below put a title over three columns, merge cells of rows that are already added, and show the error for two ranges that overlap.

Parameters

  • $rangestring

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

A title over three columns

<?nvs
// Shows how to put a title over three columns of a report.
use Novis\Spreadsheet\Reader;
use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();
var $sheet = $book->addSheet('Report');
$sheet->addRow(['Sales by shop']);
$sheet->addRows([['Shop', 'Orders', 'Sales'], ['North', 12, 340.5], ['South', 8, 210.0]]);
// The title in A1 now covers the cells A1, B1 and C1.
$sheet->mergeCells('A1:C1');
bytes $file = $book->finish()->bytes ?? Core\Bytes::fill(0, 0);
foreach (Reader::open($file)->merged('Report') as var $range) {
echo $range, "\n";
}
Output
A1:C1

One cell for each group of rows

<?nvs
// Shows how to merge the shop name over all the rows of that shop.
use Novis\Spreadsheet\Reader;
use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();
var $sheet = $book->addSheet('Orders');
$sheet->addRows([
['shop' => 'North', 'order' => 1042],
['shop' => 'North', 'order' => 1043],
['shop' => 'South', 'order' => 1044],
['shop' => 'South', 'order' => 1045],
]);
// The rows are already added. `mergeCells` can still use them.
$sheet->mergeCells('A2:A3')->mergeCells('A4:A5');
bytes $file = $book->finish()->bytes ?? Core\Bytes::fill(0, 0);
// With `merged: 'fill'`, every cell of a merged range has the value of its top left cell.
foreach (Reader::open($file)->rows('Orders', { header: true, merged: 'fill' }) as var $row) {
echo $row->values['order'], ': ', $row->values['shop'], "\n";
}
Output
1042: North
1043: North
1044: South
1045: South

Ranges that overlap

<?nvs
// Shows the error for two merged ranges that overlap.
use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();
var $sheet = $book->addSheet('Report');
$sheet->mergeCells('A1:C1');
// The cell B1 is in both ranges, so the second `mergeCells` throws a `LogicError`.
try {
$sheet->mergeCells('B1:D1');
} catch (LogicError $e) {
echo $e->message, "\n";
}
Output
The merged range `B1:D1` overlaps another merged range of the sheet `Report`.