Question

I have this code which is working, to load a Google Spreadsheet and load some data from it. If the spreadsheet in question is public, how do i modify the code to not require a username/password?

$key="keytothespreadsheet";
$user="test@example.com";
$pass="*****";

$authService = Zend_Gdata_Spreadsheets::AUTH_SERVICE_NAME;
$httpClient = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $authService);
$gdClient = new Zend_Gdata_Spreadsheets($httpClient);
$query = new Zend_Gdata_Spreadsheets_DocumentQuery();
$query->setSpreadsheetKey($key);
$feed = $gdClient->getWorksheetFeed($query);
print_r($feed);
Was it helpful?

Solution

In the following line, the HTTP client is optional:

$gdClient = new Zend_Gdata_Spreadsheets($httpClient);


So, just don't pass it. The following are equivalent:

$gdClient = new Zend_Gdata_Spreadsheets();
// or
$gdClient = new Zend_Gdata_Spreadsheets(null);
// or
$gdClient = new Zend_Gdata_Spreadsheets(new Zend_Http_Client());

OTHER TIPS

Like @Matt, I wanted to access a public spreadsheet without supplying credentials. Thanks to @Derek Illchuk, I got part of the way there. It still wasn't working, however, until I learned the following:

  1. Note that the File > Publish to the Web feature is not the same thing as Sharing Settings > Public On The Web. If you forget to enable "Publish to the Web", you'll get this error: "Expected response code 200, got 400 The spreadsheet at this URL could not be found. Make sure that you have the right URL and that the owner of the spreadsheet hasn't deleted it."

  2. In the "Publish to the Web" settings, be sure to uncheck "Require viewers to sign in with their ___ account.". Otherwise you'll get this error: "Expected response code 200, got 403 You do not have view access to the spreadsheet. Make sure you are properly authenticated."

  3. According to Google's documentation, "The spreadsheets feed only supports the 'private' visibility and the 'full' projection." However, I found that I needed to specify 'public' visibility and 'basic' projection. Otherwise I got this error: "Expected response code 200, got 501 Bad or unsupported projection for this type of operation."

Here is what worked for me:

    $spreadsheetService = new Zend_Gdata_Spreadsheets(null);
    $query = new Zend_Gdata_Spreadsheets_CellQuery();
    $query->setSpreadsheetKey($spreadsheetKey);
    $query->setWorksheetId($worksheetId);
    $query->setVisibility('public'); //options are 'private' or 'public'
    $query->setProjection('basic'); //options are 'full' or 'basic'
    $cellFeed = $spreadsheetService->getCellFeed($query);

    foreach ($cellFeed as $cellEntry) {
        $text = $cellEntry->content->text;
        //Do something
        break; //I only wanted the first cell (R1C1).
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top