尝试在 PHP 中向 Google 电子表格追加一行 [英] Trying to append a row to a Google Spreadsheet in PHP
问题描述
我通过 Composer 使用 Google 的客户端 API (https://packagist.org/packages/google/apiclient) 并且我已成功通过身份验证并收到访问令牌.
I'm using Google's Client API via Composer (https://packagist.org/packages/google/apiclient) and I have successfully authenticated and received an access token.
我正在尝试向驱动器中的 Google 工作表添加一行,但找不到任何专门针对 PHP 的相关文档.
I am trying to add a row to a Google sheet in my drive, but I can't find any relevant documentation that specifically addresses PHP.
这是我到目前为止所得到的:
Here's what I've got so far:
$service = new Google_Service_Sheets($a4e->google); // my authenticated Google client object
$spreadsheetId = "11I1xNv8cHzBGE7uuZtB9fQzbgrz4z7lIaEADfta60nc";
$range = "Sheet1!A1:E";
$valueRange= new Google_Service_Sheets_ValueRange();
$service->spreadsheets_values->update($spreadsheetId,$range,$valueRange);
这将返回以下错误:
Fatal error: Uncaught exception 'Google_Service_Exception' with message '{ "error": { "code": 400, "message": "Invalid valueInputOption: INPUT_VALUE_OPTION_UNSPECIFIED", "errors": [ { "message": "Invalid valueInputOption: INPUT_VALUE_OPTION_UNSPECIFIED", "domain": "global", "reason": "badRequest" } ], "status": "INVALID_ARGUMENT" } } ' in /usr/share/nginx/vendor/google/apiclient/src/Google/Http/REST.php
我对Google_Service_Sheets_ValueRange()
"对象的格式以及如何将一行附加到工作表的末尾而不必指定特定范围感到困惑.
I'm stuck as to the format of the "Google_Service_Sheets_ValueRange()
" object, and also how to append a row to the end of the sheet, rather than having to specify a particular range.
我将不胜感激任何有关此问题的帮助.
I would greatly appreciate any help with this issue.
推荐答案
我遇到了同样的问题,缺少相关文档.但我找到了解决办法.这是一个工作示例:
I had the same problem, there is a lack of documentation about this. But I found a solution. Here is a working example:
// ...
// Create the value range Object
$valueRange= new Google_Service_Sheets_ValueRange();
// You need to specify the values you insert
$valueRange->setValues(["values" => ["a", "b"]]); // Add two values
// Then you need to add some configuration
$conf = ["valueInputOption" => "RAW"];
// Update the spreadsheet
$service->spreadsheets_values->update($spreadsheetId, $range, $valueRange, $conf);
我认为它的语法很奇怪,我没有找到关于它的明确文档,我只是尝试了一些组合,现在它可以工作了!不确定这是正确的方式,希望能帮到你.
I think it's weird syntax, and I did not found clear documentation about it, I just tried some combination and now it works! Not sure it's the right way, hope it could help.
这篇关于尝试在 PHP 中向 Google 电子表格追加一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!