Search code examples
phpzend-gdata

Get a list of Worksheets using Zend Gdata API for PHP


I want to list all the worksheets with the name of the sheet and the "worksheetid" which is used in the API to reference the sheet.


Solution

  • Here is a script you can use to output all of the worksheets and their ids for a particular spreadsheet. It takes a URL query string of 'name' which is the name of the spreadsheet whose worksheets you want the ids of (e.g. name-of-file.php?name=myspreadsheet)

    <?php
        set_include_path($_SERVER["DOCUMENT_ROOT"] . "/library/");
    
        require_once 'Zend/Loader/Autoloader.php';
        $autoloader = Zend_Loader_Autoloader::getInstance();
        $autoloader->setFallbackAutoloader(true);
    
    /**
     * Username, password and the name of the spreadsheet we would like to use
     * Note that the spreadsheet is case sensitive and white space sensitive
     */
    
        $user = "your-gmail-account-name-at-gmail-dot-com";
        $pass = "your-gmail-account-password";
        if($_POST['name'])
        {
            $spreadsheetToFind = $_POST['name'];
        }
        elseif($_GET['name'])
        {
            $spreadsheetToFind = $_GET['name'];
        }
    
    /**
     * Establish a connection to our spreadsheets and get a complete list of them
     */
    
        $service            = Zend_Gdata_Spreadsheets::AUTH_SERVICE_NAME;
        $client             = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
        $spreadsheetService = new Zend_Gdata_Spreadsheets($client);
        $feed               = $spreadsheetService->getSpreadsheetFeed();
    
    /**
     * We loop through all of the spreadsheets until we have found the one
     * declared earlier. The id of the spreadsheet is then extracted using
     * basename and we store the id for use in our next query, to
     * obtain all of the worksheets in the spreadsheet
     */
    
        foreach($feed->entries as $entry)
        {               
            $spreadsheetTitle = $entry->title->text;
    
            if($spreadsheetTitle == $spreadsheetToFind)
            {
                $spreadsheetURL = $entry->id;
            }
        }
    
        $spreadsheetKey = basename($spreadsheetURL);
    
        $query = new Zend_Gdata_Spreadsheets_DocumentQuery();
        $query->setSpreadsheetKey($spreadsheetKey);
        $feed = $spreadsheetService->getWorksheetFeed($query); // now that we have the desired spreadsheet, we need the worksheets
    
    /**
     * Loop through all of our worksheets and echo
     * its name as well as its id
     */
    
        echo("<table><tr><td><strong>Spreadsheet Name:</strong></td><td>" . $spreadsheetToFind . "</td></tr><tr><td><strong>Spreadsheet ID:</strong></td><td>" . $spreadsheetKey . "</td></tr>");
    
        foreach($feed->entries as $entry)
        {
            echo("<tr><td><strong>".$entry->title->text . ": </strong></td><td>".basename($entry->id)."</td></tr>");
    
        }
    
        echo("</table>");
    ?>
    

    Note that this script depends on the Zend library being at your document root in a folder named 'library'.