我已经按照快速入门指南和API v4文档成功设置了凭据并可以访问。
一切正常,我成功尝试:
但是,在尝试合并某些单元格时,我始终收到以下错误消息:
致命错误:未捕获的Google_Service_Exception:{“错误”:{“代码”: 400,“消息”:“接收到无效的JSON有效载荷。未知名称 \'merge_type \'在'requests':找不到字段。\ n无效的JSON有效负载 收到。 'requests'中的未知名称\“ range \”:找不到字段。“, “错误”:[{“消息”:“接收到无效的JSON有效负载。未知名称 \'merge_type \'在'requests':找不到字段。\ n无效的JSON有效负载 收到。 'requests'中的未知名称\“ range \”:找不到字段。“, “ domain”:“ global”,“ reason”:“ badRequest”}],“ status”: “ INVALID_ARGUMENT”}}
源代码:
<?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";
答案 0 :(得分:1)
如果您尚未解决此问题,则问题出在您发送的请求中,
尝试使用Google_Service_Sheets_BatchUpdateSpreadsheetRequest
setRequests
方法设置您的请求
$batchUpdateRequest = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest();
$batchUpdateRequest->setRequests($request);
$response = $service->spreadsheets->batchUpdate($id_spreadsheet, $batchUpdateRequest);
echo '<pre>', var_export($response, true), '</pre>', "\n";