试图在PHP中向Google Spreadsheet附加一行 [英] Trying to append a row to a Google Spreadsheet in PHP

查看:664
本文介绍了试图在PHP中向Google Spreadsheet附加一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过Composer使用Google的客户端API( https://packagist.org/packages/google/ apiclient ),并且我已成功验证身份并收到访问令牌。



我试图向我的驱动器中的Google工作表添加一行,但我可以找不到任何专门针对PHP的相关文档。



以下是我到目前为止的内容:

  $ service = new Google_Service_Sheets($ a4e-> google); //我的经过身份验证的Google客户端对象
$ spreadsheetId =11I1xNv8cHzBGE7uuZtB9fQzbgrz4z7lIaEADfta60nc;
$ range =Sheet1!A1:E;
$ valueRange = new Google_Service_Sheets_ValueRange();
$ service-> spreadsheets_values->更新($ spreadsheetId,$ range,$ valueRange);

这会返回以下错误:

<$ p $ {p> 致命错误:带有消息'{错误的未捕获异常'Google_Service_Exception':{code:400,message:无效的valueInputOption:INPUT_VALUE_OPTION_UNSPECIFIED,errors:[{在/ usr / share / nginx / vendor / google /中输入message:Invalid valueInputOption:INPUT_VALUE_OPTION_UNSPECIFIED,domain:global,reason:badRequest}],status:INVALID_ARGUMENT apiclient / src / Google / Http / REST.php

我坚持使用 Google_Service_Sheets_ValueRange()对象,以及如何在表格末尾附加一行,而不必指定特定的范围。



我非常感谢这个问题的任何帮助。

解决方案

我有同样的问题,是缺乏这方面的文件。但我找到了一个解决方案。这是一个工作示例:

  // ... 

//创建值范围Object
$ valueRange = new Google_Service_Sheets_ValueRange();

//你需要指定你插入的值
$ valueRange-> setValues([values=> [a,b]]); //添加两个值

//然后你需要添加一些配置
$ conf = [valueInputOption=> 生的];

//更新电子表格
$ service-> spreadsheets_values->更新($ spreadsheetId,$ range,$ valueRange,$ conf);

我认为这是很奇怪的语法,我没有找到明确的文档,我只是尝试了一些组合现在它工作!不确定它是否正确 ,希望它能帮上忙。


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.

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);

This returns the following error:

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

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 Spreadsheet附加一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆