Google Spreadsheet API: memory exceeded

吃可爱长大的小学妹 提交于 2019-12-05 22:41:53

I just ran into this today. When calling the insertRow() method, my script used upwards of 130MB of memory inserting into a worksheet of ~600 records. I tested this on framework version 1.11.

As a work-around, I use the existing Zend HTTP client object to send a POST with the Atom entry containing the data for the row to be inserted. I followed Google's protocol for adding a list row.

Below is the code I came up with. The $values parameter is an associative array that has keys matching the column names for the row. Of course, you already know your $spreadsheetKey and $worksheetId (if the worksheet you are inserting into is the first worksheet in the spreadsheet, I'm not sure its ID is necessary).

$authService = Zend_Gdata_Spreadsheets::AUTH_SERVICE_NAME;
$httpClient = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $authService);

function insertRow($httpClient, $spreadsheetKey, $worksheetId, $values) {
    $entry = createEntry($values);
    $httpClient->setUri("https://spreadsheets.google.com/feeds/list/".$spreadsheetKey."/".$worksheetId."/private/full");
    $response = $httpClient->setRawData($entry, 'application/atom+xml')->request('POST');
    return $response->getStatus() == 201;
}

function createEntry($values) { 
    $entry = "<entry xmlns=\"http://www.w3.org/2005/Atom\"";
    $entry .= " xmlns:gsx=\"http://schemas.google.com/spreadsheets/2006/extended\">";
    foreach($values as $key => $value) {
        $entry .= "<gsx:".$key.">".$value."</gsx:".$key.">";
    }
    $entry .= "</entry>";
    return $entry;
}

Hope this helps.

Sorry but I can't comment Jonathan Freeland's post, so I post it like this.

I added this inside of insertRow() to get it to work.

$token = $httpClient->getClientLoginToken();
$httpClient->setHeaders('Authorization','GoogleLogin auth='.$token);
$httpClient->setHeaders('Content-Type', 'application/atom+xml');

This is a serious Zend_Gdata bug. It is downloading the entire spreadsheet (by doing an empty query) and loading it into memory just to build a request url for the insert. I reported the issue as well as a fix but the issue has been ignored by the Zend maintainers and may never get fixed.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!