Zend_GData Spreadsheet - grid-id的查询参数值无效

时间:2012-02-06 09:15:37

标签: php zend-framework google-sheets zend-gdata

我正在尝试通过Zend_GData库更新Google电子表格。我确定用户ID和密码是正确的。我认为问题在于电子表格密钥或工作表ID。即使我不知道如何获得这个workheetID。请帮助我。这是m工作表的确切URL。

https://docs.google.com/spreadsheet/ccc?key=0AmW1-CVeX7oOdGRkYURWNXVMRi1DTW9NTGNhZ09OTGc&hl=en_US#gid=0

这是我的代码。

$user = 'xxxxxxxxxxxxxxxxxxxxxx';
$pass = 'xxxxxxxxxxxxxxxxxx';
$service = Zend_Gdata_Spreadsheets::AUTH_SERVICE_NAME;
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
$spreadsheetService = new Zend_Gdata_Spreadsheets($client);
$spreadsheetKey = "0AmW1-CVeX7oOdGRkYURWNXVMRi1DTW9NTGNhZ09OTGc";
$worksheetId = "Sheet1";
try
{           
    $spreadsheetService->insertRow(array('Prasad','10000'),$spreadsheetKey,$worksheetId);

}
catch(Exception $error)
{

    echo $error->getMessage() ;

}

这是我在浏览器屏幕上看到的错误消息。

预期的响应代码200,得到400网格标识的查询参数值无效。

2 个答案:

答案 0 :(得分:5)

我解决了这个问题。我可以使用下面的代码找到SpreadsheetKey和WorkSheetID。

$user = 'xxxxxxxxxxxxxxx';
$pass = 'xxxxxxxxxxxxx';
$service            = Zend_Gdata_Spreadsheets::AUTH_SERVICE_NAME;
$client             = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
$spreadsheetService = new Zend_Gdata_Spreadsheets($client);
$feed               = $spreadsheetService->getSpreadsheetFeed();
$sheetName = "Test1";
foreach($feed->entries as $entry)
{               
    if($entry->title->text == $sheetName)
    {
        $spreadsheetURL = $entry->id;
        break;
    }
}

$spreadsheetKey = basename($spreadsheetURL);

$query = new Zend_Gdata_Spreadsheets_DocumentQuery();
$query->setSpreadsheetKey($spreadsheetKey);
$feed = $spreadsheetService->getWorksheetFeed($query); 

echo "Spreadskeet Key : $spreadsheetKey <br/>";

foreach($feed->entries as $entry)
{
    echo "ID of sheet {$entry->title->text} is " . basename($entry->id) . " <br/>";
}

一旦我获得了这些值,我的脚本也无效。然后我发现当我们定义数组时,'Key'应该以小写形式提及,没有空格。

$spreadsheetService->insertRow(array('name'=>'Prasad','value'=>'10000'),$spreadsheetKey,$worksheetId);

答案 1 :(得分:0)

请尝试此代码,测试并为我工作

<?php
// load Zend Gdata libraries
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata_Spreadsheets');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');

// set credentials for ClientLogin authentication
$user = "xxxxxxxxxxxxxxx";
$pass = "xxxxxxxxxxxxxxx";
try {  
  // connect to API
  $service = Zend_Gdata_Spreadsheets::AUTH_SERVICE_NAME;
  $client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
  $service = new Zend_Gdata_Spreadsheets($client);

  // get list of available spreadsheets
  $feed = $service->getSpreadsheetFeed();
} catch (Exception $e) {
  die('ERROR: ' . $e->getMessage());
}
foreach($feed->entries as $entry) {
    $spreadsheetURL = $entry->id;
}
$spreadsheetKey = basename($spreadsheetURL);
$query = new Zend_Gdata_Spreadsheets_DocumentQuery();
$query->setSpreadsheetKey($spreadsheetKey);
$feed = $service->getWorksheetFeed($query);
echo "Spreadsheet Key: <strong>$spreadsheetKey</strong> <br>";
foreach($feed as $entry) {
    $sName = $entry->title->text;
    echo "ID of sheet $sName is: <strong>".basename($entry->id)."</strong><br>";
}
?>