2014-06-13 21 views
0

我想通過PHP/Zend腳本從Google電子表格中獲取所有行。這是我使用的腳本:PHP/Zend/Google Spreadsheet沒有得到所有行

$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到表單中,並從第一個表單中預先填充一些細節,以便更快地填寫表單的第二部分。這一切都很好,它只是缺少最後一項是主要問題!

謝謝!

回答

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行添加一個檢查,然後這應該被跳過。


也許更合適的解決方法是按行,然後按單元格而不是按單元格進行迭代。

+0

D'oh似乎是一個明顯的答案,無法看到Forrest的樹!我試着得到一個列表提要,但似乎無法得到任何迴應,而且文檔非常薄!以下文檔中的示例不返回任何內容!當列表提要接受一個查詢時,這是一種痛苦,這意味着我可以選擇我得到的結果的年齡大小,而不是一切! – Scoobler