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.

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

报告相同问题?

悬赏问题

  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?