Retrieve only specific columns of spreadsheet with zend gdata from gdocs

喜夏-厌秋 提交于 2019-12-13 06:06:25

问题


I am working with relatively large spreadsheets from gdocs and access them via the zend library. I only need certain columns at any given time so in order to save memory it would be helpful to use the list-based feed to only retrieve these certain columns and not the whole spreadsheet. The basic query I am using is according to the zend documentation

$query = new Zend_Gdata_Spreadsheets_ListQuery();
$query->setSpreadsheetKey($spreadsheetKey);
$query->setWorksheetId($worksheetId); 
$listFeed = $spreadsheetService->getListFeed($query);

I have the opportunity to send a structured query via $query->setSpreadsheetQuery('name=someName'); but as far as I can see this only works for limiting the number of rows returned and not the number of columns. Is there a way to somehow use this to get specific columns? Alternatively it would be helpful to get only certain, previously specified rows of the spreadsheet so as to retrieve only blocks of the spreadsheet and thin out one block at the time. Either way, I need to avoid having the whole spreadsheet in the memory at any given time.

Thanks for any help.


回答1:


I managed to work around this problem using a cell-based feed. Basically what I did was to first get the column number for a specific column header like so

$query = new Zend_Gdata_Spreadsheets_CellQuery();
$query->setSpreadsheetKey($spreadsheetkey);
$query->setWorksheetId($wworksheetkey);
$query->setMinRow(1);
$query->setMaxRow(1);
$headerFeed = $spreadsheetService->getCellFeed($query);

foreach($headerFeed as $cellEntry){
    if(strcasecmp($cellEntry->cell->getText(), $column) ==  0){
        $colNr = $cellEntry->cell->getColumn();
        break;
    }
}

which will give me the column number for the $column column. I would then proceed to get a specific range of rows for the given column number so that I can read in the whole column block by block without ever having the whole spreadsheet(or column) in memory. This can be done as

$query->setMinCol($colNr);
$query->setMaxCol($colNr);
$query->setMinRow($startingRow);
$query->setMaxRow($endingRow);

$columnFeed = $spreadsheetService->getCellFeed($query);
foreach($columnFeed as $cellEntry){
    $result[$cellEntry->cell->getRow()] = $cellEntry->cell->getText();
}

by changing $startingRow and $endingRow I can iterate through the column. One has to be careful with the choice of these though, since if they are 'out of bounds' the google api will return a http 400 code. The cells don't have to have value in them, but they have to exist(scroll down your spreadsheet and the last row number you see should be your maximum for both values). I think per default a spreadsheet with 100(empty) rows is created.

This is probably not the most elegant or fastest solution, but it works for me.



来源:https://stackoverflow.com/questions/11531777/retrieve-only-specific-columns-of-spreadsheet-with-zend-gdata-from-gdocs

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