【问题标题】:PHP / Zend / Google Spreadsheet not getting all rowsPHP / Zend / Google 电子表格未获取所有行
【发布时间】:2014-06-14 00:31:39
【问题描述】:

我正在尝试通过 PHP/Zend 脚本从 Google 电子表格中获取所有行。这是我正在使用的脚本:

    $service = Zend_Gdata_Spreadsheets::AUTH_SERVICE_NAME;
    $client = Zend_Gdata_ClientLogin::getHttpClient('xxxxxxxxx', 'xxxxxxx', $service);
    $spreadsheetService = new Zend_Gdata_Spreadsheets($client);

    // Get spreadsheet key
    $spreadsheetsKey = 'xxxxxxxxxxxxx';   
    $worksheetId = 'xxx';

    // Get cell feed
    $query = new Zend_Gdata_Spreadsheets_CellQuery();
    $query->setSpreadsheetKey($spreadsheetsKey);
    $query->setWorksheetId($worksheetId);
    $cellFeed = $spreadsheetService->getCellFeed($query);

    // Build an array of entries:
    $ssRows = array();
    $ssRow = array();
    $titleRow = array();

    $tableRow = 1;
    foreach($cellFeed as $cellEntry) {
        $row = $cellEntry->cell->getRow();
        $col = $cellEntry->cell->getColumn();
        $val = $cellEntry->cell->getText();
        // Add each row as a new array:
        if ($row != $tableRow) {
            array_push($ssRows, $ssRow);
            $ssRow = array();
            // Move to the next row / new array
            $tableRow = $row;
        }
        // Build the array of titles:
        if ($row == 1) {
            $titleRow[$col] = $val;
        } 
        // Build the array of results with the title as the key:
        else {
            $key = $titleRow[$col];
            $ssRow[$key] = $val;                
        }
    }

    // Pass the results array:
    return array_reverse($ssRows);

这为我构建了一个包含电子表格中大部分详细信息的数组,但是它总是错过最后一个条目 - 任何人都可以看到我做错了什么,或者有没有更好的方法从电子表格中获取所有数据?

该表格由 3 部分组成,基于不同的答案。在填写一个部分时,我想显示一个返回表单的 URL,其中预先填写了第一个表单中的一些详细信息,以便更快地填写表单的第二部分。这一切都很好,只是缺少最后一个条目才是主要问题!

谢谢!

【问题讨论】:

    标签: php zend-framework google-sheets gdata


    【解决方案1】:

    你的代码是这样工作的:

    if (next_row) {
        data[] = current_row
        current_row = array();
    }
    if (first_row) {
        title_row logic
    } else {
        add cell to current_row
    }
    

    因此,您只需在转到下一行后将行添加到您的收集器。这将错过最后一行,因为您会错过最后一次转换。

    简单的解决方法是在foreach 循环之后添加array_push($ssRows, $ssRow);。您需要为0 行添加检查,然后应该跳过。


    也许更合适的解决方法是逐行迭代,然后逐个单元格,而不仅仅是逐个单元格。

    【讨论】:

    • D'oh 似乎是一个显而易见的答案,看不到福雷斯特的树木!我尝试获取列表提要,但似乎无法得到任何响应,而且文档非常薄!遵循文档中的示例不会返回任何内容!当列表提要接受查询时,这很痛苦,这意味着我可以选择我得到的结果有多早,而不是获取所有内容!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多