使用Zend Gdata API for PHP获取工作表列表

时间:2010-09-29 23:43:35

标签: php zend-gdata

我想列出所有工作表,其中包含工作表的名称和API中用于引用工作表的“worksheetid”。

1 个答案:

答案 0 :(得分:4)

这是一个脚本,可用于输出特定电子表格的所有工作表及其ID。它需要一个URL名称'name'的字符串查询字符串,它是您想要其工具表的电子表格的名称(例如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>");
?>

请注意,此脚本依赖于位于名为“library”的文件夹中的文档根目录的Zend库。