Novis\Spreadsheet\Reader::merged
$reader->merged(tainted string|int $sheet): array<tainted string>$reader->merged( tainted string|int $sheet, ): array<tainted string>
Returns the merged cells of one sheet.
$sheet is the sheet name, or its position from 0. Each item in the list is a range, such as "A1:C1", from the top-left cell to the bottom-right cell of one merged area. A sheet without merged cells gives an empty list, and so does a CSV file.
Good to know: in a file, only the top-left cell of a merged area has a value. When you read rows, the other cells of the area are null. Read with merged: 'fill' to give every cell of the area the value of the top-left cell. Each range is a tainted string, because it comes from the file. A sheet that does not exist throws a LogicError.
The examples below list the merged cells, fill every cell of a merged area, and check each sheet before an import.
Parameters
$sheettainted string|int
Return value
Returns array<tainted string>.
Examples
List the merged cells
<?nvs// Shows how to list the merged cells of a sheet.use Novis\Spreadsheet\Reader;use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();var $sheet = $book->addSheet('Report');$sheet->mergeCells('A1:C1')->mergeCells('A4:A5');$sheet->addRows([['Sales report'], ['Shop', 'Month', 'Sales'], ['North', 'May', 120], ['South', 'May', 80], [null, 'June', 95]]);var $reader = Reader::open($book->finish()->bytes ?? Core\Bytes::fill(0, 0));
// Each merged area is a range from its top-left cell to its bottom-right cell.foreach ($reader->merged('Report') as tainted string $range) { echo $range, "\n";}A1:C1A4:A5Fill every cell of a merged area
<?nvs// Shows how the option `merged: 'fill'` gives the value of a merged area to each of its cells.use Novis\Spreadsheet\Reader;use Novis\Spreadsheet\Workbook;
// The shop name `South` is in A3, and A3:A4 is merged. So in Excel it is next to both months.var $book = Workbook::createInMemory();var $sheet = $book->addSheet('Sales');$sheet->mergeCells('A3:A4');$sheet->addRows([['Shop', 'Month'], ['North', 'May'], ['South', 'May'], [null, 'June']]);var $reader = Reader::open($book->finish()->bytes ?? Core\Bytes::fill(0, 0));
// By default, only the top-left cell has the value. The shop of row 4 is `null`.foreach ($reader->rows('Sales', { header: true }) as var $row) { echo $row->number, ': ', $row->values['Shop'] ?? 'null', ' ', $row->values['Month'], "\n";}
// With `merged: 'fill'`, row 4 also has the shop `South`.foreach ($reader->rows('Sales', { header: true, merged: 'fill' }) as var $row) { echo $row->number, ': ', $row->values['Shop'] ?? 'null', ' ', $row->values['Month'], "\n";}2: North May3: South May4: null June2: North May3: South May4: South JuneCheck a sheet before an import
<?nvs// Shows how to check that a sheet has no merged cells before a row by row import.use Novis\Spreadsheet\Reader;use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();$book->addSheet('Plain')->addRows([['Item', 'Price'], ['Widget', 2.5]]);$book->addSheet('Layout')->mergeCells('A1:B1')->addRows([['Price list'], ['Widget', 2.5]]);var $reader = Reader::open($book->finish()->bytes ?? Core\Bytes::fill(0, 0));
// `merged` takes the position of the sheet. An empty list means the sheet has no merged cells.foreach ($reader->sheets() as var $info) { uint $count = Core\Arr::count($reader->merged($info->position as int)); echo $info->name, ': ', $count == 0 ? 'ready to import' : "merged areas: {$count}", "\n";}Plain: ready to importLayout: merged areas: 1