繁体   English   中英

Google电子表格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-clientlogin

通过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地址 - 使用您的Google帐户的地址。

我也使用-d source=... (如上面的'using curl'文件),但我想这是不必要的。

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

复制身份验证密钥: Auth=...在服务器返回的文档中。

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

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

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

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

在此处说明: 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

查找工作表的listfeed URL: xmllint --format workheets.xml | less 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".../>看起来不错......

最后,添加行。 这是user2029890在他们的回答中描述的部分: 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 -H'Content 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解析器非常挑剔 - 它需要围绕属性值的双引号。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM