SpreadSheet-Coding.com

Snippets

Merge Cells

July 25, 2022

Are you looking for merge cells snippets?

Below are the different snippets you can use to merge cells with PhpSpreadsheet or Google Sheets API PHP Client.

PhpSpreadsheet (Excel Files)

PHP
// Merge cells from A1 to D5
$sheet->mergeCells('A1:D5');

Google Sheets API PHP Client

PHP
$requests = [
    new Google_Service_Sheets_Request([
        'mergeCells'   => [
            'range'    => ['startRowIndex'=> 1, 'endRowIndex'=> 3, 'startColumnIndex'=> 1, 'endColumnIndex'=> 3],
            'mergeType'=> 'MERGE_ALL',
        ],
    ])
];

$batchUpdateRequest = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest([
    'requests' => $requests
]);

$response = $service->spreadsheets->batchUpdate($spreadsheetId, $batchUpdateRequest);

[you_might_be_interested ids=”55,767″]