谷歌电子表格API如何插入新行


Google spreadsheet API how to insert new row

到目前为止,我只使用API从Google电子表格中检索数据,但我不知道如何插入数据。我看了一遍,但没有一个例子足够清楚。

为了检索数据,我所要做的就是构建一个URL,并在PHP中使用CURL进行检索,如下所示:

    //Get spreadsheet data
    $headers = array(
        "Authorization: GoogleLogin auth=" . $auth,
        "GData-Version: 3.0",
        );
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, "https://spreadsheets.google.com/tq?tqx=out:html&tq=select%20D%2C%20E%20where%20B%3D&key=1c1xxxxxxxxxxxxx                                                                                                                                           
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1
    $response= curl_exec($curl);

,我如何构造一个类似的URL来插入?我认为会有一个类似的URL可以使用。有人能给我指正确的方向吗?我在这里找不到有关它的信息https://developers.google.com/chart/interactive/docs/querylanguage

没有答案,但更多的研究表明https://developers.google.com/google-apps/spreadsheets/#working_with_list-based_feeds,点击"添加列表行"的"协议"链接可以给我足够的线索来构建以下内容:

//create xml with each element representing a column header (note: for some reason the headers must only contain alphanumeric characters)
$xml = "
<entry xmlns='http://www.w3.org/2005/Atom'
    xmlns:gsx='http://schemas.google.com/spreadsheets/2006/extended'>
  <gsx:id>123</gsx:id>
  <gsx:status>123</gsx:status>
  <gsx:date>4/26/2014</gsx:date>
  <gsx:user>bob</gsx:user>
</entry>";
//set headers which includes auth from elsewhere in the code
$headers = array(
    "Authorization: GoogleLogin auth=" . $auth,
    "GData-Version: 3.0",
    "Content-Type: application/atom+xml",
    );
//post the xml. The key in the url is taken from the actual url when you view the sheet 
$curl = curl_init();
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_URL, "https://spreadsheets.google.com/feeds/list/xxxxxxxxxxxxxxxkeyxxxxxxxxx/0/private/full");
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, "$xml");
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($curl);

希望这能帮助其他人

以下是我在进行授权时所做的一些注意事项。我认为谷歌的文档相当单薄。希望这能帮助到别人。

截至2017年,身份验证不再如此处所述工作

查看此帖子-比"官方"文档更容易访问:https://developers.google.com/gdata/articles/using_cURL#authenticating-客户端登录

通过创建新的电子表格http://drive.google.com

通过创建新的应用程序特定密码https://security.google.com/settings/security/apppasswords

使用应用程序特定密码创建身份验证令牌:curl -v https://www.google.com/accounts/ClientLogin --data-urlencode Email=yourname@gmail.com --data-urlencode Passwd=... -d accountType=GOOGLE -d service=wise

不必是GMail地址-使用您的谷歌帐户地址。

我也使用了-d source=...(就像上面的"使用curl"文档中一样),但我想这是不必要的。

关于wise字符串,请参阅https://developers.google.com/gdata/faq#clientlogin以获取服务名称列表。

复制服务器返回的文档中的auth-key:Auth=...

如果你能以某种方式使身份验证工作(我没有),其余的可能仍然有效:

获取电子表格列表:curl -v -H 'Authorization: GoogleLogin auth=...' https://spreadsheets.google.com/feeds/spreadsheets/private/full > spreadsheets.xml

查找电子表格的工作表URL:xmllint --format spreadsheets.xml | less

查找电子表格的名称,从<link rel="http://schemas.google.com/spreadsheets/2006#worksheetsfeed".../> 复制href

此处解释:https://developers.google.com/google-apps/spreadsheets/#retrieving_information_about_worksheets,但具有不同的rel URI。:-(

获取电子表格中的工作表列表:curl -v -H 'Authorization: GoogleLogin auth=...' https://spreadsheets.google.com/feeds/worksheets/.../private/full >worksheets.xml

查找工作表的列表提要URL:xmllint --format workheets.xml | less应在此处进行描述:https://developers.google.com/google-apps/spreadsheets/#adding_a_list_row,但是,URI与我看到的不匹配。。。<link rel="http://schemas.google.com/spreadsheets/2006#listfeed".../>看起来不错。。。

最后,添加行。这是用户2029890在他们的答案中描述的部分:curl -v -H 'Content-Type: application/atom+xml' -H 'Authorization: GoogleLogin auth=...' -d '<entry xmlns="http://www.w3.org/2005/Atom" xmlns:gsx="http://schemas.google.com/spreadsheets/2006/extended"><gsx:id>123</gsx:id><gsx:user>bob</gsx:user></entry>' https://spreadsheets.google.com/feeds/list/.../.../private/full

XML元素名称,例如gsx:idgsx:user,必须与列标题相匹配。

XML解析器非常挑剔——它需要在属性值周围使用双引号。