试图通过Google Sheets API和BatchUpdate方法将数据添加到Google工作表,但是我得到了这个错误(不知道它指的是什么)
> PHP Fatal error: Uncaught Google\Service\Exception: { "error": {
> "code": 400,
> "message": "Invalid JSON payload received. Unknown name \"\": Root element must be a message.",
> "errors": [
> {
> "message": "Invalid JSON payload received. Unknown name \"\": Root element must be a message.",
> "reason": "invalid"
> }
> ],
> "status": "INVALID_ARGUMENT" } }
这是我的新代码:
$spreadsheetId = 'myspreadsheetid';
$client = new Google_Client();
$client->setAuthConfig('mycredntials.json');
$client->setApplicationName('Sheet Automation');
$client->setScopes(Google_Service_Sheets::SPREADSHEETS);
$service = new Google_Service_Sheets($client);
$range = 'A2:L';
$valueInputOption = 'USER_ENTERED';
$body = new Google_Service_Sheets_ValueRange([
'values' => $row1
]);
$params = [
'valueInputOption' => $valueInputOption
];
$result = $service->spreadsheets_values->append($spreadsheetId, $range, $body, $params);
编辑:
我从数据库中获得的数据被放入一个数组($row1)中,回显时该数组如下所示:
(
[cdr_id] => myid
[calldate] => 2021-05-27
[src] => mysource
[accountcode] => myaccountcode
[userfield] => disposition
[ivr_std_txn_full] =>
)
然后,我获取该信息并使用内爆将所有内容放入一行(这可能是问题所在)
echo $line1 = implode(',', $row1)
我尝试设置要追加到$row1和$line1的值,但仍然遇到负载问题。
发布于 2021-05-28 02:59:47
一旦我最终弄清楚了,解决方案就相当简单了。如果您不打算手动输入值,而是使用一个填充了数据的数组,那么您只需要引用数组中的每个键,而不仅仅是数组。
请求正文最终如下所示:
$body = new Google_Service_Sheets_ValueRange([
//'values' => $row1
'values' =>
[
[
$row1['cdr_id'], $row1['calldate'], $row1['src'], $row1['accountcode'], $row1['userfield'], $row1['ivr_std_txn_full']
]
]
]);
$params = [
'valueInputOption' => $valueInputOption
];
$result = $service->spreadsheets_values->append($spreadsheetId, $range, $body, $params);
https://stackoverflow.com/questions/67712879
复制相似问题