Google Spreadsheet API:超出了内存 [英] Google Spreadsheet API: memory exceeded
问题描述
不知道是否有人已与谷歌电子表格API或Zend_GData类的经验,但它是值得一去的:
当我尝试插入一个值750行电子表格,它需要很长时间,然后抛出一个错误,我的内存限制(这是128 MB!)被超过。查询此电子表格的所有记录时,我也得到了这一点,但是我可以进行成像,因为它有相当多的数据。但为什么插入一行时会发生这种情况?这并不复杂,是吗?下面是我使用的代码:
公共函数insertIntoSpreadsheet($用户名,$密码,$ spreadSheetId,$数据=阵列()) {
$ service = Zend_Gdata_Spreadsheets :: AUTH_SERVICE_NAME;
$ client = Zend_Gdata_ClientLogin :: getHttpClient($ username,$ password,$ service);
$ client-> setConfig(array('timeout'=> 240));
$ service = new Zend_Gdata_Spreadsheets($ client);
if(count($ data)== 0){
die(No valid data);
}
尝试{
$ newEntry = $ service-> insertRow($ data,$ spreadSheetId);
返回true;
} catch(Exception $ e){
return false;
}
}
I今天刚刚遇到了这个。当调用 insertRow()
方法时,我的脚本使用大于130MB的内存插入到约600条记录的工作表中。我在框架版本1.11 上对此进行了测试。
<作为解决方案,我使用现有的Zend HTTP客户端对象来发送包含要插入的行的数据的Atom条目的POST。我遵循Google的协议,以便添加列表行。
以下是我想出的代码。 $ values
参数是一个关联数组,其关键字与该行的列名称相匹配。当然,你已经知道你的 $ spreadsheetKey
和 $ worksheetId
(如果您插入工作表是在第一个工作表在电子表格中,我不确定它的ID是否必要)。
$ authService = Zend_Gdata_Spreadsheets :: AUTH_SERVICE_NAME;
$ httpClient = Zend_Gdata_ClientLogin :: getHttpClient($ user,$ pass,$ authService);
函数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;
}
函数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: 。$关键。 > 中;
}
$ entry。=< / entry>;
返回$ entry;
}
希望这有助于您。
Don't know if anyone has experience with the Google Spreadsheets API or the Zend_GData classes but it's worth a go:
When I try to insert a value in a 750 row spreadsheet, it takes ages and then throws an error that my memory limit (which is 128 MB!) was exceeded. I also got this when querying all records of this spreadsheet but this I can imaging because it's quite a lot of data. But why does this happen when inserting a row? That's not too complex, is it? Here's the code I used:
public function insertIntoSpreadsheet($username, $password, $spreadSheetId, $data = array()) {
$service = Zend_Gdata_Spreadsheets::AUTH_SERVICE_NAME;
$client = Zend_Gdata_ClientLogin::getHttpClient($username, $password, $service);
$client->setConfig(array( 'timeout' => 240 ));
$service = new Zend_Gdata_Spreadsheets($client);
if (count($data) == 0) {
die("No valid data");
}
try {
$newEntry = $service->insertRow($data, $spreadSheetId);
return true;
} catch (Exception $e) {
return false;
}
}
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.
这篇关于Google Spreadsheet API:超出了内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!