批量更新单元格不适用于Google电子表格API

时间:2015-07-16 09:59:14

标签: php google-spreadsheet-api

我设法更新单个单元格,现在我正在尝试批量更新多个单元格,但它无法正常工作。请告诉我这段代码有什么问题。 这是我正在使用的代码:

$feed = "<feed xmlns='http://www.w3.org/2005/Atom' xmlns:batch='http://schemas.google.com/gdata/batch' xmlns:gs='http://schemas.google.com/spreadsheets/2006'>\n";
        $feed .= "<id>https://spreadsheets.google.com/feeds/cells/$gID/$workSheetID/private/full</id>\n";

        foreach($postData as $post)
        {
            $row = $post["row"];
            $col = $post["col"];
            $data = $post["data"];
            $rc = "R$row" . "C$col";
            $requestURL = "https://spreadsheets.google.com/feeds/cells/$gID/$workSheetID/private/full/batch";
            $url = "https://spreadsheets.google.com/feeds/cells/$gID/$workSheetID/private/full/$rc";

            $feed .= "<entry>\n";
            $feed .= "<batch:id>A$row</batch:id>\n";
            $feed .= "<batch:operation type = 'update'/>\n";
            $feed .= "<id>$url</id>\n";
            $feed .= "<link rel='edit' type='application/atom+xml' href=$url />\n";
            $feed .= "<gs:cell row=$row col=$col inputValue='newData' />\n";
            $feed .= "</entry>\n";
        }
        $feed .= "</feed>";

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $requestURL);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $feed);
        curl_setopt($curl, CURLOPT_HEADER, false);
        $headers = array(
            "Authorization: Bearer " . $this->accessTokenArray->access_token,
            "GData-Version: 3.0",
            "Content-Type: application/atom+xml",
            "If-Match: *",
        );
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_VERBOSE, true);
        $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        return curl_exec($curl);

这是我得到的输出:

<?xml version='1.0' encoding='UTF-8'?>
<feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearch/1.1/' xmlns:batch='http://schemas.google.com/gdata/batch' xmlns:gs='http://schemas.google.com/spreadsheets/2006'>
<id>https://spreadsheets.google.com/feeds/cells/1/13IBClQO0ZUdDXrPhEkpesKE6FKCxWDtPqLQXoK_JWXg/batch/1437040636408</id>
<updated>2015-07-16T09:57:16.413Z</updated>
<title>Batch Feed</title>
<entry>
<id>https://spreadsheets.google.com/feeds/cells/1/13IBClQO0ZUdDXrPhEkpesKE6FKCxWDtPqLQXoK_JWXg/batch/1437040636408/fatal</id>
<updated>2015-07-16T09:57:16.413Z</updated>
<title>Fatal Error</title>
<content>Feed processing was interrupted.</content>
<batch:interrupted reason='[Line 3, Column 8, element feed] The spreadsheet at this URL could not be found. Make sure that you have the right URL and that the owner of the spreadsheet hasn&apos;t deleted it.' parsed='0' success='0' error='0' unprocessed='0'/>
</entry>
</feed>

2 个答案:

答案 0 :(得分:0)

尝试在批量更新调用的所有网址中交换key和workSheetID的值,包括XML Feed中的网址,并且有效。

答案 1 :(得分:0)

通过编辑以下代码行来更改版本

GData-Version: 1.0". 

另外

$url = "https://spreadsheets.google.com/feeds/cells/$gID/$workSheetID/private/full/$rc";

应该包含$ rc之后的版本,它必须像

https://spreadsheets.google.com/feeds/cells/key/worksheetId/private/full/R2C5/version, 

检查文件。
希望这会有所帮助。