dongqianchi0512 2014-06-13 16:26
浏览 42
已采纳

PHP / Zend / Google Spreadsheet没有获得所有行

I am trying to get all the rows from a Google spreadsheet via a PHP/Zend script. This is the script I am using:

    $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);

This builds me an array with MOST of the details from the spreadsheet, however it always misses off the last entry - can anyone see what I am doing wrong, or is there a better way to get all the data from the spreadsheet?

The form is a 3 part form, based on different answers. On filling out one part, I want to display a URL back to the form, with some details from the first form pre-filled to make the second part of the form faster to fill out. This is all fine, it is simply the missing last entry that is the major problem!

Thanks!

  • 写回答

1条回答 默认 最新

  • dpxpz37157 2014-06-13 16:31
    关注

    Your code works like this:

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

    So you only add the rows to your collector once you go to the next row. This will miss the last row because you'll miss that last transition.

    The easy fix is to add array_push($ssRows, $ssRow); right after the foreach loop. You will need to add a check for 0 rows, this should be skipped then.


    Perhaps a more proper fix is to iterate by row, then by cell, rather than just by cell.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 用51单片机控制急停。
  • ¥15 孟德尔随机化结果不一致
  • ¥15 在使用pyecharts时出现问题
  • ¥15 深度学习残差模块模型
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用