Search code examples
phpzend-frameworkgoogle-sheetszend-gdata

Zend_GData Spreadsheet - Invalid query parameter value for grid-id


I am trying to update a google spreadsheet through Zend_GData library. I am sure the user ID and password is correct. I think the issue is with spreadsheet key or worksheet ID. Even I don't know exactly how to get this worksheetID. Kindly help me on this. This is the exact URL to m worksheet.

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

Here is my code.

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

}

And this is the error message I see on my browser screen.

Expected response code 200, got 400 Invalid query parameter value for grid-id.


Solution

  • I fixed the issue. I could find the SpreadsheetKey and WorkSheetID by using bellow code.

    $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/>";
    }
    

    Once I got those values also my script didn't work. Then I found that when we define the array the 'Key's should mentioned in lower case with no spaces.

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