【问题标题】:Google spreadsheet API how to insert new rowGoogle 电子表格 API 如何插入新行
【发布时间】:2014-04-25 03:33:19
【问题描述】:

到目前为止,我只使用 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

【问题讨论】:

    标签: php google-api google-sheets


    【解决方案1】:

    好吧,没有答案,但更多研究将我指向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);
    

    希望这对其他人有帮助

    【讨论】:

    • 我试图通过 curl 插入行。我的电子表格链接是这样的:docs.google.com/a/nascenia.com/spreadsheets/d/… 那么这个电子表格的关键是什么?我收到无效的请求 URI!
    • 如果我没记错 $api => 'acces token' or credential.你能清除一次吗。
    • 谢谢!当然帮了大忙。我猜你应该在最后加上curl_close($curl);
    • @AwladLiton 您需要找到工作表的列表提要 URL。看我的回答。
    • 我收到400 Bad Request 响应:无法写入空白行;使用 delete 代替`请帮助
    【解决方案2】:

    这是我在获得授权时做的一些笔记。我认为谷歌的文档相当薄。希望这可以帮助某人。

    自 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=...(如上面的“使用 curl”文档),但我想这是不必要的。

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

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

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

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

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

    查找电子表格的名称,从&lt;link rel="http://schemas.google.com/spreadsheets/2006#worksheetsfeed".../&gt;复制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 &gt;worksheets.xml

    为您的工作表查找列表提要 URL:xmllint --format workheets.xml | less 应在此处描述:https://developers.google.com/google-apps/spreadsheets/#adding_a_list_row,但同样,URI 与我看到的不匹配...&lt;link rel="http://schemas.google.com/spreadsheets/2006#listfeed".../&gt; 看起来不错...

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

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

    XML 解析器非常挑剔 - 它需要在属性值两边加上双引号。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-12
      • 1970-01-01
      相关资源
      最近更新 更多