0

I've succesfully set up my credentials and access following the Quickstart guide & the API v4 documentation.

Everything works great, I successfully tried to:

  • Update the spreadsheet title
  • Read/write values in each cell
  • etc.

However I keep getting the following error while trying to merge some cells:

Fatal error: Uncaught Google_Service_Exception: { "error": { "code": 400, "message": "Invalid JSON payload received. Unknown name \"merge_type\" at 'requests': Cannot find field.\nInvalid JSON payload received. Unknown name \"range\" at 'requests': Cannot find field.", "errors": [ { "message": "Invalid JSON payload received. Unknown name \"merge_type\" at 'requests': Cannot find field.\nInvalid JSON payload received. Unknown name \"range\" at 'requests': Cannot find field.", "domain": "global", "reason": "badRequest" } ], "status": "INVALID_ARGUMENT" } }

Source code:

<?php

include_once(__DIR__.'/vendor/autoload.php');
include_once(__DIR__.'/client.php');

$id_spreadsheet = '12xymzMe8OLbX9vw84'; // My spreadsheet ID (redacted for Stackoverflow)

/* Retrieve the API access */
$service = new Google_Service_Sheets(getClient());

/* Merge header columns */
$range = new Google_Service_Sheets_GridRange();
$range->setStartRowIndex(0);
$range->setEndRowIndex(1);
$range->setStartColumnIndex(1);
$range->setEndColumnIndex(24);
$range->setSheetId(0);

$request = new Google_Service_Sheets_MergeCellsRequest();
$request->setMergeType('MERGE_COLUMNS');
$request->setRange($range);

$batchUpdateRequest = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest(['requests' => $request]);
$response = $service->spreadsheets->batchUpdate($id_spreadsheet, $batchUpdateRequest);
echo '<pre>', var_export($response, true), '</pre>', "\n";
Bruno Leveque
  • 2,647
  • 2
  • 23
  • 33
  • There is a documentation that explains how you can use the [`MergeCellsRequest`](https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/request#mergecellsrequest) function. It was also explained in [this SO post](https://stackoverflow.com/questions/43498024/how-to-merge-cells-with-google-sheets-api/43501914), although it was written in python. But there it was explained how you can use the function. – MαπμQμαπkγVπ.0 Nov 02 '18 at 09:08
  • Thank you. I've carefully read the documentation and examples as well as the post you mentioned before posting here. Unfortunately, the Python & PHP API libraries provided by Google don't work the same way. – Bruno Leveque Nov 02 '18 at 14:14

1 Answers1

1

If you've not resolved this issue, the problem is in the request you are sending, try to set your request in Google_Service_Sheets_BatchUpdateSpreadsheetRequest setRequests method

$batchUpdateRequest = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest();
$batchUpdateRequest->setRequests($request);
$response = $service->spreadsheets->batchUpdate($id_spreadsheet, $batchUpdateRequest);
echo '<pre>', var_export($response, true), '</pre>', "\n";
Rajender Verma
  • 389
  • 3
  • 9